diff --git a/src/components/forms/fields/CheckboxField/CheckboxField.tsx b/src/components/forms/fields/CheckboxField/CheckboxField.tsx index 1046974..3b8b49c 100644 --- a/src/components/forms/fields/CheckboxField/CheckboxField.tsx +++ b/src/components/forms/fields/CheckboxField/CheckboxField.tsx @@ -69,6 +69,9 @@ export type CheckboxFieldProps = ComponentProps<'div'> & { /** Whether the checkbox is disabled. Passed down to Checkbox component. */ disabled?: undefined | boolean, + + /** The onChange event for the checkbox. Passed down to Checkbox component. */ + onChange?: (e: React.FormEvent) => void, }; /** @@ -105,6 +108,7 @@ export const CheckboxField = (props: CheckboxFieldProps) => { checked={props.checked} defaultChecked={props.defaultChecked} disabled={props.disabled} + onChange={props.onChange} /> {label} diff --git a/src/components/forms/fields/CheckboxGroup/CheckboxGroup.stories.tsx b/src/components/forms/fields/CheckboxGroup/CheckboxGroup.stories.tsx index b332db5..78b8014 100644 --- a/src/components/forms/fields/CheckboxGroup/CheckboxGroup.stories.tsx +++ b/src/components/forms/fields/CheckboxGroup/CheckboxGroup.stories.tsx @@ -12,6 +12,9 @@ import { CheckboxGroup } from './CheckboxGroup.tsx'; type CheckboxGroupArgs = React.ComponentProps; type Story = StoryObj; +const Color = ['Red', 'Green', 'Blue'] as const; +type Color = (typeof Color)[number]; + export default { component: CheckboxGroup, parameters: { @@ -20,11 +23,28 @@ export default { tags: ['autodocs'], argTypes: { }, - render: (args) => - - - - , + render: (args) => { + const [selectedColors, setSelectedColors] = React.useState>([]); + const handleCheckboxChange = (color: Color) => { + if (selectedColors.includes(color)) { + setSelectedColors(selectedColors.filter(c => c !== color)); + } else { + setSelectedColors([...selectedColors, color]); + } + }; + return ( + + {Color.map(color => + { handleCheckboxChange(color as Color); }} + /> + )} + + ); + }, } satisfies Meta; export const CheckboxGroupVertical: Story = {};