diff --git a/packages/design/src/Form/components/GenderId/GenderId.stories.tsx b/packages/design/src/Form/components/GenderId/GenderId.stories.tsx index 28d2e1a4..c7b92bb9 100644 --- a/packages/design/src/Form/components/GenderId/GenderId.stories.tsx +++ b/packages/design/src/Form/components/GenderId/GenderId.stories.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { FormProvider, useForm } from 'react-hook-form'; import { type Meta, type StoryObj } from '@storybook/react'; -import { GenderIdPattern } from './GenderId.js'; +import GenderIdPattern from './index.js'; const meta: Meta = { title: 'patterns/GenderIdPattern', @@ -24,67 +24,41 @@ const meta: Meta = { export default meta; +const defaultArgs = { + genderId: 'gender-identity', + label: 'Gender identity', + hint: 'For example, man, woman, non-binary', + required: true, + preferNotToAnswerText: 'Prefer not to share my gender identity', +}; + export const Default: StoryObj = { - args: { - genderId: 'gender-identity', - label: 'Gender identity', - hint: 'For example, man, woman, non-binary', - required: true, - preferNotToAnswerText: 'Prefer not to share my gender identity', - }, + args: { ...defaultArgs }, }; export const Optional: StoryObj = { - args: { - genderId: 'gender-identity', - label: 'Gender identity', - hint: 'For example, man, woman, non-binary', - required: false, - preferNotToAnswerText: 'Prefer not to share my gender identity', - }, + args: { ...defaultArgs, required: false }, }; export const WithError: StoryObj = { args: { - genderId: 'gender-identity', + ...defaultArgs, label: 'Gender identity with error', - hint: 'For example, man, woman, non-binary', - required: true, error: { type: 'custom', message: 'This field has an error', }, - preferNotToAnswerText: 'Prefer not to share my gender identity', }, }; export const WithHint: StoryObj = { - args: { - genderId: 'gender-identity', - label: 'Gender identity', - hint: 'For example, man, woman, non-binary', - required: true, - preferNotToAnswerText: 'Prefer not to share my gender identity', - }, + args: { ...defaultArgs }, }; export const WithCheckboxChecked: StoryObj = { - args: { - genderId: 'gender-identity', - label: 'Gender identity', - hint: 'For example, man, woman, non-binary', - required: true, - preferNotToAnswerText: 'Prefer not to share my gender identity', - preferNotToAnswerChecked: true, - }, + args: { ...defaultArgs, preferNotToAnswerChecked: true }, }; export const WithoutCheckbox: StoryObj = { - args: { - genderId: 'gender-identity', - label: 'Gender identity', - hint: 'For example, man, woman, non-binary', - required: true, - preferNotToAnswerText: undefined, - }, + args: { ...defaultArgs, preferNotToAnswerText: undefined }, }; diff --git a/packages/design/src/Form/components/GenderId/GenderId.tsx b/packages/design/src/Form/components/GenderId/GenderId.tsx deleted file mode 100644 index 6755e4da..00000000 --- a/packages/design/src/Form/components/GenderId/GenderId.tsx +++ /dev/null @@ -1,112 +0,0 @@ -import React, { useEffect, useState } from 'react'; -import classNames from 'classnames'; -import { useFormContext } from 'react-hook-form'; -import { type GenderIdProps } from '@atj/forms'; - -import { type PatternComponent } from '../../index.js'; - -export const GenderIdPattern: PatternComponent = ({ - genderId, - hint, - label, - required, - error, - value, - preferNotToAnswerText, - preferNotToAnswerChecked: initialPreferNotToAnswerChecked = false, -}) => { - const { register, setValue } = useFormContext(); - const [preferNotToAnswerChecked, setPreferNotToAnswerChecked] = useState( - initialPreferNotToAnswerChecked - ); - - const errorId = `input-error-message-${genderId}`; - const hintId = `hint-${genderId}`; - const preferNotToAnswerId = `prefer-not-to-answer-${genderId}`; - - useEffect(() => { - if (preferNotToAnswerChecked) { - setValue(genderId, preferNotToAnswerText, { shouldValidate: true }); - } else { - setValue(genderId, value, { shouldValidate: true }); - } - }, [ - preferNotToAnswerChecked, - setValue, - genderId, - preferNotToAnswerText, - value, - ]); - - const handleCheckboxChange = (event: React.ChangeEvent) => { - setPreferNotToAnswerChecked(event.target.checked); - setValue(preferNotToAnswerId, event.target.checked); - }; - - return ( -
-
- - {hint && ( -
- {hint} -
- )} - {error && ( - - )} - - {preferNotToAnswerText && ( -
- - -
- )} -
-
- ); -}; diff --git a/packages/design/src/Form/components/GenderId/index.tsx b/packages/design/src/Form/components/GenderId/index.tsx index 0ed40bec..0709523b 100644 --- a/packages/design/src/Form/components/GenderId/index.tsx +++ b/packages/design/src/Form/components/GenderId/index.tsx @@ -1,3 +1,114 @@ -import { GenderIdPattern } from './GenderId.js'; +import React, { useEffect, useState } from 'react'; +import classNames from 'classnames'; +import { useFormContext } from 'react-hook-form'; +import { type GenderIdProps } from '@atj/forms'; + +import { type PatternComponent } from '../../index.js'; + +const GenderIdPattern: PatternComponent = ({ + genderId, + hint, + label, + required, + error, + value, + preferNotToAnswerText, + preferNotToAnswerChecked: initialPreferNotToAnswerChecked = false, +}) => { + const { register, setValue } = useFormContext(); + const [preferNotToAnswerChecked, setPreferNotToAnswerChecked] = useState( + initialPreferNotToAnswerChecked + ); + + const errorId = `input-error-message-${genderId}`; + const hintId = `hint-${genderId}`; + const preferNotToAnswerId = `prefer-not-to-answer-${genderId}`; + + useEffect(() => { + if (preferNotToAnswerChecked) { + setValue(genderId, preferNotToAnswerText, { shouldValidate: true }); + } else { + setValue(genderId, value, { shouldValidate: true }); + } + }, [ + preferNotToAnswerChecked, + setValue, + genderId, + preferNotToAnswerText, + value, + ]); + + const handleCheckboxChange = (event: React.ChangeEvent) => { + setPreferNotToAnswerChecked(event.target.checked); + setValue(preferNotToAnswerId, event.target.checked); + }; + + return ( +
+
+ + {hint && ( +
+ {hint} +
+ )} + {error && ( + + )} + + {preferNotToAnswerText && ( +
+ + +
+ )} +
+
+ ); +}; export default GenderIdPattern;