Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use NL Design System form components #76

Merged
merged 15 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 48 additions & 66 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"@hookform/resolvers": "3.9.0",
"@radix-ui/react-checkbox": "1.1.2",
"@radix-ui/react-dialog": "1.1.2",
"@radix-ui/react-form": "0.1.0",
"@radix-ui/react-label": "2.1.0",
"@radix-ui/react-select": "2.1.2",
"@radix-ui/react-slot": "1.1.0",
"@radix-ui/react-visually-hidden": "1.1.0",
Expand Down Expand Up @@ -50,9 +48,11 @@
"@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",
"@utrecht/radio-group-react": "1.0.1",
"@vitejs/plugin-react": "4.3.3",
"autoprefixer": "10.4.20",
"axios": "1.7.7",
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} ${field.required ? `(${t('required_short')})` : `(${t('not_required_short')})`}`}
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>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { useFormStore } from '@/store/form_store'
import { getNearestAddressByCoordinate } from '@/services/location/address'
import { useConfig } from '@/hooks/useConfig'
import { isCoordinates } from '@/lib/utils/map'
import { Alert } from '@utrecht/component-library-react/dist/css-module'
import { LinkButton } from '@utrecht/component-library-react'
import { Fieldset, FieldsetLegend, LinkButton } from '@/components/index'
import { useTranslations } from 'next-intl'
import { FormFieldErrorMessage } from '@/components'

export interface LocationSelectProps {
field?: PublicQuestion
Expand All @@ -24,6 +25,7 @@ export const LocationSelect = ({ field }: LocationSelectProps) => {
const { formState: formStoreState } = useFormStore()
const [address, setAddress] = useState<string | null>(null)
const { config } = useConfig()
const t = useTranslations('describe-add.map')

useEffect(() => {
const getAddress = async () => {
Expand All @@ -44,11 +46,17 @@ export const LocationSelect = ({ field }: LocationSelectProps) => {
}, [formStoreState.coordinates])

return (
<div className="w-full">
{errorMessage && <Alert type="error">{errorMessage}</Alert>}
<label htmlFor="location-button">
{field ? field.meta.label : 'Waar is het?'}
</label>
<Fieldset invalid={Boolean(errorMessage)} className="w-full">
<FieldsetLegend>
{field
? `${field.meta.label} (${t('required_short')})`
: `${t('map_label')} (${t('required_short')})`}
</FieldsetLegend>

{Boolean(errorMessage) && errorMessage && (
<FormFieldErrorMessage>{errorMessage}</FormFieldErrorMessage>
)}

<div className="relative w-full">
<LocationMap />
<Paragraph>{address}</Paragraph>
Expand All @@ -74,7 +82,6 @@ export const LocationSelect = ({ field }: LocationSelectProps) => {
/>
</MapProvider>
</div>
{/* TODO: toon locatie, straatnaam bijv. */}
</div>
</Fieldset>
)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { QuestionField } from '@/types/form'
import Markdown from 'react-markdown'
import { Alert } from '@/components'

interface PlainTextProps extends QuestionField {}

export const PlainText = ({ field }: PlainTextProps) => {
// TODO: Discuss if alert is the only used PlainText type in Signalen, style Markdown
return field.meta.value ? (
<div
className="bg-red-100 rounded-lg p-4"
data-testid="plain-text-hard-stop"
>
<Alert type="error" data-testid="plain-text-hard-stop">
<Markdown>{field.meta.value}</Markdown>
</div>
</Alert>
) : (
<></>
)
Expand Down
55 changes: 20 additions & 35 deletions src/app/[locale]/incident/add/components/questions/RadioInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslations } from 'next-intl'
import { QuestionField } from '@/types/form'
import { getValidators } from '@/lib/utils/form-validator'
import { useFormContext } from 'react-hook-form'
import { Paragraph } from '@/components/index'
import { RadioGroup } from '@/components/index'

interface RadioGroupProps extends QuestionField {}

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

return (
<fieldset aria-invalid={!!errorMessage} role="radiogroup">
<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, {
...getValidators(field, t),
})}
type="radio"
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>
<RadioGroup
label={`${field.meta.label} ${field.required ? `(${t('required_short')})` : `(${t('not_required_short')})`}`}
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}
></RadioGroup>
)
}
Loading
Loading