Skip to content

Commit

Permalink
fix(checkboxFieldProps): make checkboxField initializable via options
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Jan 18, 2024
1 parent 7c982a6 commit 83e0dcd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
22 changes: 9 additions & 13 deletions src/fields/checkbox-field/CheckboxInput.mock.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { ReactNode } from "react";

import { CheckboxField } from "./checkboxField";
import { FieldLabel } from "../../components";
import { FieldErrors } from "../../components/field-errors";
import { useCheckboxFieldProps, useRequiredProps } from "../../hooks";
import { BooleanField } from "../boolean-field";
import { FieldErrors, FieldLabel } from "../../components";
import {
type CheckboxFieldProps,
useCheckboxFieldProps,
useRequiredProps,
} from "../../hooks";

export const CheckboxInput = ({
field,
label,
required,
}: {
field: BooleanField | CheckboxField;
label: ReactNode;
required?: boolean;
}) => {
const props = useCheckboxFieldProps(field);
initialValue,
}: CheckboxFieldProps) => {
const props = useCheckboxFieldProps(field, { initialValue });
const requiredProps = useRequiredProps({ field, required });

return (
Expand Down
23 changes: 23 additions & 0 deletions src/fields/checkbox-field/checkboxField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,26 @@ export const Optional = formStory({
},
},
});

export const Initialized = formStory({
args: {
fields: {
visible: checkboxField({ name: "visible" }).optional(),
},
children: ({ fields }) => (
<CheckboxInput
field={fields.visible}
label="Product is visible"
initialValue={true}
/>
),
},
parameters: {
docs: {
description: {
story:
"This example uses the `initialValue` option passed to the `useCheckboxFieldProps(field, options)`",
},
},
},
});
10 changes: 10 additions & 0 deletions src/hooks/use-checkbox-field-props/useCheckboxFieldProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ describe("useCheckboxFieldProps()", () => {

expect(checkboxProps.result.current.checked).toBe(true);
});

test("can be initialized", async () => {
const field = checkboxField();

const checkboxProps = renderHook(() =>
useCheckboxFieldProps(field, { initialValue: true }),
);

expect(checkboxProps.result.current.checked).toBe(true);
});
});
14 changes: 11 additions & 3 deletions src/hooks/use-checkbox-field-props/useCheckboxFieldProps.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { UseFieldOptions } from "form-atoms";
import { ChangeEvent, useMemo } from "react";

import { FieldProps, useFieldProps } from "../";
import { BooleanField, type CheckboxField } from "../../fields";
import type {
BooleanField,
BooleanFieldValue,
CheckboxField,
} from "../../fields";

export type CheckboxFieldProps = FieldProps<CheckboxField | BooleanField>;

const getChecked = (event: ChangeEvent<HTMLInputElement>) =>
event.currentTarget.checked;

export function useCheckboxFieldProps(field: CheckboxField | BooleanField) {
export function useCheckboxFieldProps(
field: CheckboxField | BooleanField,
options?: UseFieldOptions<BooleanFieldValue>,
) {
// undefined (empty checkbox) is rendered as unchecked input
const { value: checked = false, ...props } = useFieldProps<
CheckboxField | BooleanField
>(field, getChecked);
>(field, getChecked, options);

return useMemo(
() => ({
Expand Down

0 comments on commit 83e0dcd

Please sign in to comment.