diff --git a/src/components/checkbox-group/CheckboxGroup.stories.tsx b/src/components/checkbox-group/CheckboxGroup.stories.tsx index e8f5b2a..7bda94a 100644 --- a/src/components/checkbox-group/CheckboxGroup.stories.tsx +++ b/src/components/checkbox-group/CheckboxGroup.stories.tsx @@ -1,9 +1,8 @@ import { StoryObj } from "@storybook/react"; -import { CheckboxGroup } from "./CheckboxGroup"; +import { CheckboxGroup, type CheckboxGroupProps } from "./CheckboxGroup"; import { CheckboxGroupField } from "./CheckboxGroupField.mock"; import { ZodArrayField, stringArrayField } from "../../fields"; -import { UseCheckboxGroupProps } from "../../hooks"; import { StoryForm } from "../../scenarios/StoryForm"; const languagesOptions = [ @@ -32,8 +31,8 @@ export default meta; const checkboxGroupStory = ( storyObj: { - args: Pick, "field"> & - Omit>, "field">; + args: Pick, "field"> & + Omit>, "field">; } & Omit, "args">, ) => ({ ...storyObj, @@ -62,6 +61,13 @@ export const Optional = checkboxGroupStory({ }, }); +export const Initialized = checkboxGroupStory({ + args: { + field: stringArrayField().optional(), + initialValue: ["ts", "hc"], + }, +}); + export const ComposedField = checkboxGroupStory({ args: { field: stringArrayField(), diff --git a/src/components/checkbox-group/CheckboxGroup.tsx b/src/components/checkbox-group/CheckboxGroup.tsx index 9bf6897..0014ec8 100644 --- a/src/components/checkbox-group/CheckboxGroup.tsx +++ b/src/components/checkbox-group/CheckboxGroup.tsx @@ -1,18 +1,30 @@ -import { ZodArrayField } from "../../fields"; +import { UseFieldOptions } from "form-atoms"; + +import { ZodArrayField, ZodFieldValue } from "../../fields"; import { UseCheckboxGroupProps, useCheckboxGroup } from "../../hooks"; +export type CheckboxGroupProps< + Option, + Field extends ZodArrayField, +> = UseCheckboxGroupProps & + Pick>, "initialValue">; + export const CheckboxGroup = ({ field, options, getValue, getLabel, -}: UseCheckboxGroupProps) => { - const checkboxGroup = useCheckboxGroup({ - field, - options, - getValue, - getLabel, - }); + initialValue, +}: CheckboxGroupProps) => { + const checkboxGroup = useCheckboxGroup( + { + field, + options, + getValue, + getLabel, + }, + { initialValue }, + ); return ( <> diff --git a/src/components/checkbox-group/CheckboxGroupField.mock.tsx b/src/components/checkbox-group/CheckboxGroupField.mock.tsx index 872db69..41257e8 100644 --- a/src/components/checkbox-group/CheckboxGroupField.mock.tsx +++ b/src/components/checkbox-group/CheckboxGroupField.mock.tsx @@ -1,8 +1,6 @@ -import { ReactNode } from "react"; - import { CheckboxGroup } from "./CheckboxGroup"; import { ZodArrayField } from "../../fields"; -import { UseCheckboxGroupProps } from "../../hooks"; +import { CheckboxGroupFieldProps } from "../../hooks"; import { PicoFieldErrors } from "../../scenarios/PicoFieldErrors"; import { FieldLabel } from "../field-label"; @@ -12,7 +10,7 @@ export const CheckboxGroupField = ({ getValue, getLabel, options, -}: { label: ReactNode } & UseCheckboxGroupProps) => ( +}: CheckboxGroupFieldProps) => ( <>

diff --git a/src/hooks/use-checkbox-group/useCheckboxGroup.test.tsx b/src/hooks/use-checkbox-group/useCheckboxGroup.test.tsx index 4ab1285..fa26552 100644 --- a/src/hooks/use-checkbox-group/useCheckboxGroup.test.tsx +++ b/src/hooks/use-checkbox-group/useCheckboxGroup.test.tsx @@ -9,6 +9,25 @@ describe("useCheckboxGroup()", () => { const options = ["electric", "gas", "manual"] as const; const getValue = (opt: string) => opt; + test("initialize the field via options", () => { + // NOTE: test got stuck when the options are inlined + const fieldOptions = { initialValue: ["gas", "manual"] }; + + const field = stringArrayField(); + + const { result } = renderHook(() => + useCheckboxGroup( + { field, options, getValue, getLabel: getValue }, + fieldOptions, + ), + ); + + expect(result.current).toHaveLength(options.length); + expect(result.current[0]).toHaveProperty("checked", false); + expect(result.current[1]).toHaveProperty("checked", true); + expect(result.current[2]).toHaveProperty("checked", true); + }); + describe("with required field", () => { test("it has required checkboxes", () => { const field = stringArrayField(); diff --git a/src/hooks/use-checkbox-group/useCheckboxGroup.tsx b/src/hooks/use-checkbox-group/useCheckboxGroup.tsx index db58f3a..c747146 100644 --- a/src/hooks/use-checkbox-group/useCheckboxGroup.tsx +++ b/src/hooks/use-checkbox-group/useCheckboxGroup.tsx @@ -1,20 +1,29 @@ +import { UseFieldOptions } from "form-atoms"; + import { useCheckboxGroupFieldProps } from "./useCheckboxGroupProps"; -import { ZodArrayField } from "../../fields"; +import { ZodArrayField, ZodFieldValue } from "../../fields"; +import { FieldProps } from "../use-field-props"; import { type UseMultiSelectFieldProps } from "../use-multiselect-field-props"; import { type UseOptionsProps, useOptions } from "../use-options"; +export type CheckboxGroupFieldProps< + Option, + Field extends ZodArrayField, +> = FieldProps & UseCheckboxGroupProps; + export type UseCheckboxGroupProps< Option, Field extends ZodArrayField, > = UseMultiSelectFieldProps & UseOptionsProps