2023-03-06 15:52:01 +01:00
|
|
|
import React from "react";
|
2023-07-26 16:52:49 +02:00
|
|
|
import { yupResolver } from "@hookform/resolvers/yup";
|
|
|
|
|
import { Meta, StoryFn } from "@storybook/react";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import * as Yup from "yup";
|
2023-05-04 15:04:47 +02:00
|
|
|
import { Checkbox, CheckboxGroup } from ":/components/Forms/Checkbox/index";
|
2023-07-26 16:52:49 +02:00
|
|
|
import { Button } from ":/components/Button";
|
|
|
|
|
import {
|
|
|
|
|
getFieldState,
|
|
|
|
|
getFieldErrorMessage,
|
|
|
|
|
onSubmit,
|
|
|
|
|
} from ":/tests/reactHookFormUtils";
|
2023-03-06 15:52:01 +01:00
|
|
|
|
|
|
|
|
export default {
|
2023-04-20 14:57:22 +02:00
|
|
|
title: "Components/Forms/Checkbox",
|
2023-03-06 15:52:01 +01:00
|
|
|
component: Checkbox,
|
2023-05-12 16:03:16 +02:00
|
|
|
} as Meta<typeof Checkbox>;
|
2023-03-06 15:52:01 +01:00
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
const Template: StoryFn<typeof Checkbox> = (args) => (
|
2023-03-06 15:52:01 +01:00
|
|
|
<Checkbox {...args} aria-label="Checkbox" />
|
|
|
|
|
);
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const Default = {
|
|
|
|
|
render: Template,
|
|
|
|
|
args: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Checked = {
|
|
|
|
|
render: Template,
|
2023-03-06 15:52:01 +01:00
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const Indeterminate = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
indeterminate: true,
|
|
|
|
|
},
|
2023-03-06 15:52:01 +01:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const WithLabel = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
label: "Label",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const LabelChecked = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const WithTexts = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
text: "This is an optional text",
|
|
|
|
|
},
|
2023-03-06 15:52:01 +01:00
|
|
|
};
|
2023-04-20 14:57:22 +02:00
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const Disabled = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
disabled: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const DisabledChecked = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
disabled: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const Error = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
text: "This is an optional text",
|
|
|
|
|
state: "error",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-12 16:03:16 +02:00
|
|
|
export const Success = {
|
|
|
|
|
render: Template,
|
|
|
|
|
|
|
|
|
|
args: {
|
|
|
|
|
checked: true,
|
|
|
|
|
label: "Label",
|
|
|
|
|
text: "This is an optional text",
|
|
|
|
|
state: "success",
|
|
|
|
|
},
|
2023-04-20 14:57:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Group = () => (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="fs-l fw-bold mb-t">Your offices</div>
|
|
|
|
|
<CheckboxGroup>
|
|
|
|
|
<Checkbox label="Paris" />
|
|
|
|
|
<Checkbox label="New York" text="United States" checked={true} />
|
|
|
|
|
<Checkbox label="Hong Kong" text="Really long text to write something" />
|
|
|
|
|
<Checkbox label="Singapour" checked={true} />
|
|
|
|
|
<Checkbox label="London" text="Offices closed" disabled={true} />
|
|
|
|
|
</CheckboxGroup>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const GroupError = () => (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="fs-l fw-bold mb-t">Your offices</div>
|
|
|
|
|
<CheckboxGroup state="error" text="An important error message">
|
|
|
|
|
<Checkbox label="Paris" />
|
|
|
|
|
<Checkbox label="New York" text="United States" checked={true} />
|
|
|
|
|
<Checkbox label="Hong Kong" text="Really long text to write something" />
|
|
|
|
|
<Checkbox label="Singapour" checked={true} />
|
|
|
|
|
<Checkbox label="London" text="Offices closed" disabled={true} />
|
|
|
|
|
</CheckboxGroup>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const GroupSuccess = () => (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="fs-l fw-bold mb-t">Your offices</div>
|
|
|
|
|
<CheckboxGroup state="success" text="Success message !">
|
|
|
|
|
<Checkbox label="Paris" />
|
|
|
|
|
<Checkbox label="New York" text="United States" checked={true} />
|
|
|
|
|
<Checkbox label="Hong Kong" text="Really long text to write something" />
|
|
|
|
|
<Checkbox label="Singapour" checked={true} />
|
|
|
|
|
<Checkbox label="London" text="Offices closed" disabled={true} />
|
|
|
|
|
</CheckboxGroup>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-07-26 16:52:49 +02:00
|
|
|
|
|
|
|
|
export const ReactHookForm = () => {
|
|
|
|
|
interface CheckboxExampleFormValues {
|
|
|
|
|
terms: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const checkboxExampleSchema = Yup.object().shape({
|
|
|
|
|
terms: Yup.boolean()
|
|
|
|
|
.required()
|
|
|
|
|
.oneOf([true], "You have to accept the terms of use"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { register, handleSubmit, formState } =
|
|
|
|
|
useForm<CheckboxExampleFormValues>({
|
|
|
|
|
defaultValues: {
|
|
|
|
|
terms: false,
|
|
|
|
|
},
|
|
|
|
|
mode: "onChange",
|
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
|
resolver: yupResolver(checkboxExampleSchema),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "1rem",
|
|
|
|
|
width: "400px",
|
|
|
|
|
}}
|
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<Checkbox
|
|
|
|
|
label="I accept the terms of use"
|
|
|
|
|
fullWidth
|
|
|
|
|
state={getFieldState("terms", formState)}
|
|
|
|
|
text={getFieldErrorMessage("terms", formState)}
|
|
|
|
|
{...register("terms")}
|
|
|
|
|
/>
|
|
|
|
|
<Button fullWidth={true}>Log-in</Button>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
};
|