Skip to content

Commit

Permalink
refactor: use NLDS checkbox group in dynamic form
Browse files Browse the repository at this point in the history
  • Loading branch information
justiandevs committed Nov 7, 2024
1 parent cd951cc commit 4bb08b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/react": "18",
"@types/react-dom": "18",
"@types/validator": "13.12.2",
"@utrecht/checkbox-group-react": "1.0.0",
"@utrecht/component-library-css": "6.1.0",
"@utrecht/component-library-react": "7.2.0",
"@utrecht/design-tokens": "2.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslations } from 'next-intl'
import { getValidators } from '@/lib/utils/form-validator'
import React from 'react'
import { useFormContext } from 'react-hook-form'
import { Paragraph } from '@/components/index'
import { CheckboxGroup } from '@/components/index'

interface CheckboxInputProps extends QuestionField {}

Expand All @@ -17,35 +17,24 @@ export const CheckboxInput = ({ field }: CheckboxInputProps) => {
const errorMessage = errors[field.key]?.message as string

return (
<fieldset aria-invalid={!!errorMessage} data-testid="checkbox-group">
<legend>
{field.meta.label}{' '}
<span> {field.required ? '' : `(${t('not_required_short')})`}</span>
{field.meta.subtitle && <span>{field.meta.subtitle}</span>}
</legend>
{errorMessage && (
<Paragraph
id={`${field.key}-error`}
aria-live="assertive"
style={{ color: 'red' }}
>
{errorMessage}
</Paragraph>
)}
{Object.keys(field.meta.values).map((key: string) => (
<div key={key}>
<input
{...register(`${field.key}.${key}`, getValidators(field, t))}
type="checkbox"
id={`${field.key}-${key}`}
value={key}
aria-describedby={errorMessage ? `${field.key}-error` : undefined}
/>
<label htmlFor={`${field.key}-${key}`}>
{field.meta.values[key]}
</label>
</div>
))}
</fieldset>
<CheckboxGroup
label={field.meta.label}
required={field.required}
id={`${field.key}`}
// @ts-ignore
options={Object.keys(field.meta.values).map((key: string) => {
return {
...register(field.key, {
...getValidators(field, t),
}),
label: field.meta.values[key],
value: key,
id: `${field.key}-${key}`,
}
})}
invalid={Boolean(errorMessage)}
errorMessage={errorMessage}
description={field.meta.subtitle}
></CheckboxGroup>
)
}
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {
} from '@utrecht/component-library-react/dist/css-module'

export { RadioGroup } from '@utrecht/radio-group-react'
export { CheckboxGroup } from '@utrecht/checkbox-group-react'

0 comments on commit 4bb08b8

Please sign in to comment.