Skip to content

Commit

Permalink
fix: update FieldCheckbox and FieldSwitch API
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-dalmet committed Jul 22, 2024
1 parent 0b07555 commit 2eccbc8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 71 deletions.
14 changes: 4 additions & 10 deletions src/components/Form/FieldCheckbox/FieldCheckbox.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ test('update value', async () => {
type="checkbox"
control={form.control}
name="doit"
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -57,9 +55,7 @@ test('double click', async () => {
type="checkbox"
control={form.control}
name="doit"
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -90,9 +86,7 @@ test('default value', async () => {
type="checkbox"
control={form.control}
name="doit"
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -123,7 +117,7 @@ test('disabled', async () => {
control={form.control}
name="doit"
isDisabled
checkboxProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down
30 changes: 15 additions & 15 deletions src/components/Form/FieldCheckbox/docs.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box, Button, Stack } from '@chakra-ui/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';

Expand All @@ -17,11 +18,16 @@ export default {
type FormSchema = z.infer<ReturnType<typeof zFormSchema>>;
const zFormSchema = () =>
z.object({
doit: z.boolean().default(false),
doit: z.literal(true),
});

const formOptions = {
mode: 'onBlur',
resolver: zodResolver(zFormSchema()),
} as const;

export const Default = () => {
const form = useForm<FormSchema>();
const form = useForm<FormSchema>(formOptions);

return (
<Form {...form} onSubmit={(values) => console.log(values)}>
Expand All @@ -32,9 +38,7 @@ export const Default = () => {
control={form.control}
type="checkbox"
name="doit"
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -50,6 +54,7 @@ export const Default = () => {

export const DefaultValues = () => {
const form = useForm<FormSchema>({
...formOptions,
defaultValues: {
doit: true,
},
Expand All @@ -64,9 +69,7 @@ export const DefaultValues = () => {
control={form.control}
type="checkbox"
name="doit"
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -81,7 +84,7 @@ export const DefaultValues = () => {
};

export const Disabled = () => {
const form = useForm<FormSchema>();
const form = useForm<FormSchema>(formOptions);

return (
<Form {...form} onSubmit={(values) => console.log(values)}>
Expand All @@ -93,9 +96,7 @@ export const Disabled = () => {
type="checkbox"
name="doit"
isDisabled
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -111,6 +112,7 @@ export const Disabled = () => {

export const DisabledDefaultValues = () => {
const form = useForm<FormSchema>({
...formOptions,
defaultValues: {
doit: true,
},
Expand All @@ -126,9 +128,7 @@ export const DisabledDefaultValues = () => {
type="checkbox"
name="doit"
isDisabled
checkboxProps={{
children: 'Yes, do it!',
}}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand Down
15 changes: 11 additions & 4 deletions src/components/Form/FieldCheckbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Checkbox, CheckboxProps, Flex } from '@chakra-ui/react';
import { ReactNode } from 'react';

import { Checkbox, CheckboxProps, Flex, FlexProps } from '@chakra-ui/react';
import {
Controller,
ControllerRenderProps,
Expand All @@ -16,13 +18,15 @@ export type FieldCheckboxProps<
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
> = {
type: 'checkbox';
label?: ReactNode;
checkboxProps?: RemoveFromType<
RemoveFromType<
Omit<CheckboxProps, 'isChecked' | 'isDisabled'>,
Omit<CheckboxProps, 'isChecked' | 'isDisabled' | 'children'>,
CheckboxRootProps
>,
ControllerRenderProps
>;
containerProps?: FlexProps;
} & CheckboxRootProps &
FieldCommonProps<TFieldValues, TName>;

Expand All @@ -36,13 +40,16 @@ export const FieldCheckbox = <
<Controller
{...props}
render={({ field: { value, ...field } }) => (
<Flex direction="column" gap={1.5}>
<Flex flexDirection="column" gap={1} flex={1} {...props.containerProps}>
<Checkbox
size={props.size}
isChecked={!!value}
isDisabled={props.isDisabled}
{...props.checkboxProps}
{...field}
/>
>
{props.label}
</Checkbox>
<FormFieldError />
</Flex>
)}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Form/FieldSwitch/FieldSwitch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('update value', async () => {
type="switch"
control={form.control}
name="doit"
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -55,7 +55,7 @@ test('double click', async () => {
type="switch"
control={form.control}
name="doit"
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -86,7 +86,7 @@ test('default value', async () => {
type="switch"
control={form.control}
name="doit"
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down Expand Up @@ -117,7 +117,7 @@ test('disabled', async () => {
control={form.control}
name="doit"
isDisabled
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
</FormField>
)}
Expand Down
50 changes: 16 additions & 34 deletions src/components/Form/FieldSwitch/docs.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box, Button, Stack } from '@chakra-ui/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';

Expand All @@ -8,7 +9,7 @@ import {
FormFieldController,
FormFieldHelper,
FormFieldLabel,
} from '..';
} from '../';

export default {
title: 'Form/FieldSwitch',
Expand All @@ -17,11 +18,16 @@ export default {
type FormSchema = z.infer<ReturnType<typeof zFormSchema>>;
const zFormSchema = () =>
z.object({
doit: z.boolean().default(false),
doit: z.literal(true),
});

const formOptions = {
mode: 'onBlur',
resolver: zodResolver(zFormSchema()),
} as const;

export const Default = () => {
const form = useForm<FormSchema>();
const form = useForm<FormSchema>(formOptions);

return (
<Form {...form} onSubmit={(values) => console.log(values)}>
Expand All @@ -32,7 +38,7 @@ export const Default = () => {
control={form.control}
type="switch"
name="doit"
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -48,6 +54,7 @@ export const Default = () => {

export const DefaultValues = () => {
const form = useForm<FormSchema>({
...formOptions,
defaultValues: {
doit: true,
},
Expand All @@ -62,7 +69,7 @@ export const DefaultValues = () => {
control={form.control}
type="switch"
name="doit"
switchProps={{ children: 'Yes, do it!' }}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -77,7 +84,7 @@ export const DefaultValues = () => {
};

export const Disabled = () => {
const form = useForm<FormSchema>();
const form = useForm<FormSchema>(formOptions);

return (
<Form {...form} onSubmit={(values) => console.log(values)}>
Expand All @@ -88,8 +95,8 @@ export const Disabled = () => {
control={form.control}
type="switch"
name="doit"
switchProps={{ children: 'Yes, do it!' }}
isDisabled
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand All @@ -105,6 +112,7 @@ export const Disabled = () => {

export const DisabledDefaultValues = () => {
const form = useForm<FormSchema>({
...formOptions,
defaultValues: {
doit: true,
},
Expand All @@ -119,34 +127,8 @@ export const DisabledDefaultValues = () => {
control={form.control}
type="switch"
name="doit"
switchProps={{ children: 'Yes, do it!' }}
isDisabled
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
<Box>
<Button type="submit" variant="@primary">
Submit
</Button>
</Box>
</Stack>
</Form>
);
};

export const SwitchProps = () => {
const form = useForm<FormSchema>();

return (
<Form {...form} onSubmit={(values) => console.log(values)}>
<Stack spacing={4}>
<FormField>
<FormFieldLabel>Should I do something?</FormFieldLabel>
<FormFieldController
control={form.control}
type="switch"
name="doit"
switchProps={{ children: 'Yes, do it!', colorScheme: 'purple' }}
label="Yes, do it!"
/>
<FormFieldHelper>Helper</FormFieldHelper>
</FormField>
Expand Down
14 changes: 10 additions & 4 deletions src/components/Form/FieldSwitch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Flex, Switch, SwitchProps } from '@chakra-ui/react';
import { ReactNode } from 'react';

import { Flex, FlexProps, Switch, SwitchProps } from '@chakra-ui/react';
import {
Controller,
ControllerRenderProps,
Expand All @@ -16,13 +18,15 @@ export type FieldSwitchProps<
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
> = {
type: 'switch';
label?: ReactNode;
switchProps?: RemoveFromType<
RemoveFromType<
Omit<SwitchProps, 'isChecked' | 'isDisabled'>,
Omit<SwitchProps, 'isChecked' | 'isDisabled' | 'children'>,
SwitchRootProps
>,
ControllerRenderProps
>;
containerProps?: FlexProps;
} & SwitchRootProps &
FieldCommonProps<TFieldValues, TName>;

Expand All @@ -36,7 +40,7 @@ export const FieldSwitch = <
<Controller
{...props}
render={({ field: { value, ...field } }) => (
<Flex direction="column" gap={1.5}>
<Flex flexDirection="column" gap={1} flex={1} {...props.containerProps}>
<Switch
display="flex"
alignItems="center"
Expand All @@ -45,7 +49,9 @@ export const FieldSwitch = <
isDisabled={props.isDisabled}
{...props.switchProps}
{...field}
/>
>
{props.label}
</Switch>
<FormFieldError />
</Flex>
)}
Expand Down

0 comments on commit 2eccbc8

Please sign in to comment.