import { yupResolver } from "@hookform/resolvers/yup"; import { Meta } from "@storybook/react"; import { useForm } from "react-hook-form"; import * as Yup from "yup"; import React from "react"; import { getFieldState, getFieldErrorMessage, onSubmit, } from ":/components/Forms/Examples/ReactHookForm/reactHookFormUtils"; import { Switch } from ":/components/Forms/Switch/index"; import { Button } from ":/components/Button"; export default { title: "Components/Forms/Switch", component: Switch, } as Meta; export const Default = { args: {}, }; export const Checked = { args: { checked: true, }, }; export const WithLabel = { args: { label: "Label", }, }; export const WithLabelChecked = { args: { label: "Label", checked: true, }, }; export const WithText = { args: { label: "Label", text: "This is an optional text", checked: true, }, }; export const FullWidth = { args: { label: "Label", text: "This is an optional text", fullWidth: true, }, }; export const WithLabelRight = { args: { label: "Label", labelSide: "right", }, }; export const WithLabelRightAndText = { args: { label: "Label", labelSide: "right", text: "This is an optional text", }, }; export const WithLabelRightAndFullWidth = { args: { label: "Label", text: "This is an optional text", fullWidth: true, labelSide: "right", }, }; export const Disabled = { args: { label: "Label", text: "This is an optional text", disabled: true, }, }; export const DisabledChecked = { args: { label: "Label", text: "This is an optional text", disabled: true, defaultChecked: true, }, }; export const Error = { args: { label: "Label", text: "This is an optional text", state: "error", defaultChecked: true, }, }; export const Success = { args: { label: "Label", text: "This is an optional text", state: "success", defaultChecked: true, }, }; export const Controlled = { render: () => { const [checked, setChecked] = React.useState(false); return (
Value: {JSON.stringify(checked)}
setChecked(e.target.checked)} />
); }, }; export const FormExample = { render: () => { return (
); }, }; export const FormExampleRight = { render: () => { return (
); }, }; export const ReactHookForm = () => { interface SwitchExampleFormValues { terms: boolean; } const switchExampleSchema = Yup.object().shape({ terms: Yup.boolean() .required() .oneOf([true], "You have to accept the terms of use"), }); const { register, handleSubmit, formState } = useForm({ defaultValues: { terms: false, }, mode: "onChange", reValidateMode: "onChange", resolver: yupResolver(switchExampleSchema), }); return (
); };