diff --git a/src/components/form/form/form.stories.tsx b/src/components/form/form/form.stories.tsx index 56c396e4..ba3db40b 100644 --- a/src/components/form/form/form.stories.tsx +++ b/src/components/form/form/form.stories.tsx @@ -26,8 +26,14 @@ export const FormComponent: Story = { fields: [ { label: "First name", name: "first_name", required: true }, { label: "Last name", name: "last_name", required: true }, + { label: "Age", name: "age", type: "number", required: true }, { label: "Address", name: "address", required: true }, - { label: "Address (addition)", name: "address", required: true }, + { + label: "Address (addition)", + name: "address", + type: "number", + required: true, + }, { label: "Date of birth", name: "date_of_birth", type: "date" }, { label: "Select school year", @@ -62,6 +68,12 @@ export const FormComponent: Story = { { label: "No", value: "no" }, ], }, + { + label: "I have read and accept the terms and conditions", + name: "accept_tos", + type: "checkbox", + required: true, + }, ], validate: validateForm, validateOnChange: true, @@ -70,12 +82,18 @@ export const FormComponent: Story = { const canvas = within(canvasElement); const firstName = canvas.getByLabelText("First name"); const lastName = canvas.getByLabelText("Last name"); + const age = canvas.getByLabelText("Age"); const schoolYear = canvas.getByLabelText("Select school year"); const address = canvas.getByLabelText("Address"); const address_addition = canvas.getByLabelText("Address (addition)"); const dateOfBirth = canvas.getByLabelText("Date of birth"); const english = canvas.getByLabelText("English"); const math = canvas.getByLabelText("Math"); + const yes = canvas.getByLabelText("Yes"); + const no = canvas.getByLabelText("No"); + const acceptTos = canvas.getByLabelText( + "I have read and accept the terms and conditions", + ); await userEvent.clear(firstName); await userEvent.type(firstName, "John", { delay: 10 }); @@ -83,6 +101,9 @@ export const FormComponent: Story = { await userEvent.clear(lastName); await userEvent.type(lastName, "Doe", { delay: 10 }); + await userEvent.clear(age); + await userEvent.type(age, "33", { delay: 10 }); + await userEvent.click(schoolYear, { delay: 10 }); const junior = await canvas.findByText("Junior"); await userEvent.click(junior, { delay: 10 }); @@ -97,86 +118,44 @@ export const FormComponent: Story = { await userEvent.type(dateOfBirth, "2023-09-15", { delay: 10 }); await userEvent.type(dateOfBirth, "{enter}"); + await userEvent.click(schoolYear); + const senior = await canvas.findByText("Senior"); + await userEvent.click(senior); + await userEvent.click(english, { delay: 10 }); await userEvent.click(math, { delay: 10 }); + + await userEvent.click(yes, { delay: 10 }); + await userEvent.click(no, { delay: 10 }); + + await userEvent.click(acceptTos, { delay: 10 }); }, }; export const TypedResults: Story = { + ...FormComponent, args: { - debug: true, - fields: [ - { label: "Name", name: "name" }, - { label: "Age", name: "age", type: "number" }, - { - label: "Select school year", - name: "school_year", - options: [ - { label: "Freshman" }, - { label: "Sophomore" }, - { label: "Junior" }, - { label: "Senior" }, - { label: "Graduate" }, - ], - }, - { - label: "Select courses", - name: "courses", - type: "checkbox", - required: true, - options: [ - { label: "English", value: "english" }, - { label: "Math", value: "math" }, - { label: "Science", value: "science" }, - ], - }, - { - label: "Receive newsletter", - name: "subscribe_newsletter", - type: "radio", - required: true, - options: [ - { label: "Yes", value: "yes" }, - { label: "No", value: "no" }, - ], - }, - ], + ...FormComponent.args, useTypedResults: true, - validate: validateForm, - validateOnChange: true, - }, - play: async ({ canvasElement }) => { - const canvas = within(canvasElement); - const name = canvas.getByLabelText("Name"); - const age = canvas.getByLabelText("Age"); - const schoolYear = canvas.getByLabelText("Select school year"); - const english = canvas.getByLabelText("English"); - const math = canvas.getByLabelText("Math"); - const newsletter = canvas.getByLabelText("Receive newsletter"); - - await userEvent.clear(name); - await userEvent.type(name, "John", { delay: 10 }); - - await userEvent.clear(age); - await userEvent.type(age, "33", { delay: 10 }); - - await userEvent.click(schoolYear, { delay: 10 }); - const junior = await canvas.findByText("Junior"); - await userEvent.click(junior, { delay: 10 }); - - await userEvent.click(english, { delay: 10 }); - await userEvent.click(math, { delay: 10 }); - await userEvent.click(newsletter, { delay: 10 }); }, }; export const UsageWithFormik: Story = { ...FormComponent, args: { - ...FormComponent.args, + debug: true, fields: [ { label: "First name", name: "first_name", required: true }, { label: "Last name", name: "last_name", required: true }, + { label: "Age", name: "age", type: "number", required: true }, + { label: "Address", name: "address[0]", required: true }, + { + label: "Address (addition)", + name: "address[1]", + type: "number", + required: true, + }, + { label: "Date of birth", name: "date_of_birth", type: "date" }, { label: "Select school year", name: "school_year", @@ -189,10 +168,6 @@ export const UsageWithFormik: Story = { { label: "Graduate" }, ], }, - { label: "Address", name: "address[0]", required: true }, - { label: "Address (addition)", name: "address[1]", required: true }, - { label: "Date of birth", name: "date_of_birth", type: "date" }, - { label: "Select courses", name: "courses", @@ -214,7 +189,15 @@ export const UsageWithFormik: Story = { { label: "No", value: "no" }, ], }, + { + label: "I have read and accept the terms and conditions", + name: "accept_tos", + type: "checkbox", + required: true, + }, ], + validate: validateForm, + validateOnChange: true, }, render: (args) => { return ( diff --git a/src/components/form/input/input.tsx b/src/components/form/input/input.tsx index 7363e759..bd58160b 100644 --- a/src/components/form/input/input.tsx +++ b/src/components/form/input/input.tsx @@ -75,9 +75,14 @@ export const Input: React.FC = ({ // This conditionalizes the presence of the "value" props. const valueProps = type.toLowerCase() === "checkbox" || type.toLowerCase() === "radio" - ? { checked: props.checked, value: valueState } - : { value: valueState }; - + ? typeof value === "undefined" + ? {} + : { + value: value, + } + : { + value: valueState, + }; const input = ( { const canvas = within(canvasElement); const pre = await canvas.findByRole("log"); - const input = canvas.getByLabelText(args.children); + const input = canvas.getByLabelText(args.children as string); // On await userEvent.click(input); diff --git a/src/components/form/radio/radio.tsx b/src/components/form/radio/radio.tsx index 7286b3a0..87c21b31 100644 --- a/src/components/form/radio/radio.tsx +++ b/src/components/form/radio/radio.tsx @@ -16,17 +16,13 @@ export type RadioProps = InputProps & { * @param props * @constructor */ -export const Radio: React.FC = ({ - children, - value, - checked, - ...props -}) => { +export const Radio: React.FC = ({ children, value, ...props }) => { const id = useId(); const _id = props.id || id; + const _props = value ? { ...props, value } : props; return (
- + {children && (