Admin: gedeelde, themagebonden Checkbox en Radio (en RadioGroup); de drie native checkboxen bij Aanbiedingen vervangen
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 15s
Continuous Integration / backend-build (pull_request) Skipped
Continuous Integration / compile-sites (pull_request) Skipped
Continuous Integration / backend-test (pull_request) Skipped
Continuous Integration / vulnerability-scan (pull_request) Skipped
Continuous Integration / config-asf (pull_request) Successful in 6s
Continuous Integration / frontend-prepare (pull_request) Successful in 31s
Continuous Integration / frontend-build (pull_request) Successful in 38s
Continuous Integration / frontend-lint (pull_request) Successful in 33s
Continuous Integration / frontend-test (pull_request) Successful in 1m14s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 09:02:54 +01:00
co-authored by Claude Sonnet 5.5
parent fe095884be
commit 17553e2f5c
5 changed files with 263 additions and 30 deletions
@@ -0,0 +1,105 @@
import { describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useForm } from 'react-hook-form';
import { Checkbox, CheckboxField } from './checkbox';
import { Radio, RadioField, RadioGroup } from './radio';
describe('Checkbox', () => {
it('is a real checkbox: Space toggles it and the label is clickable', async () => {
render(<CheckboxField label="Uitgelicht" data-testid="box" />);
const box = screen.getByRole('checkbox', { name: 'Uitgelicht' });
box.focus();
await userEvent.keyboard(' ');
expect(box).toBeChecked();
await userEvent.click(screen.getByText('Uitgelicht'));
expect(box).not.toBeChecked();
});
it('shows the in-between state and does not change its value', () => {
render(<Checkbox aria-label="alles" indeterminate />);
const box = screen.getByRole('checkbox', { name: 'alles' }) as HTMLInputElement;
expect(box.indeterminate).toBe(true);
expect(box).not.toBeChecked();
});
it('can be disabled and marked invalid', async () => {
render(<Checkbox aria-label="x" disabled invalid />);
const box = screen.getByRole('checkbox', { name: 'x' });
expect(box).toBeDisabled();
expect(box).toHaveAttribute('aria-invalid', 'true');
await userEvent.click(box);
expect(box).not.toBeChecked();
});
it('draws with the colours of the theme (accent when checked, a visible border, a focus ring)', () => {
render(<Checkbox aria-label="x" />);
const cls = screen.getByRole('checkbox').className;
expect(cls).toContain('checked:bg-primary');
expect(cls).toContain('border-muted-foreground');
expect(cls).toContain('focus-visible:ring-ring');
expect(cls).toContain('forced-colors:checked:bg-[Highlight]');
});
it('works with react-hook-form register, like the native input it replaces', async () => {
function Form({ onSubmit }: { onSubmit: (v: { on: boolean }) => void }) {
const { register, handleSubmit } = useForm<{ on: boolean }>({ defaultValues: { on: false } });
return (
<form onSubmit={handleSubmit(onSubmit)}>
<CheckboxField label="Aan" {...register('on')} />
<button type="submit">go</button>
</form>
);
}
let saved: { on: boolean } | null = null;
render(
<Form
onSubmit={(v) => {
saved = v;
}}
/>,
);
await userEvent.click(screen.getByLabelText('Aan'));
await userEvent.click(screen.getByText('go'));
await screen.findByText('go');
expect(saved).toEqual({ on: true });
});
});
describe('Radio', () => {
it('is a real radio group: the arrow keys move and select, with a name for the group', async () => {
render(
<RadioGroup label="Weergave">
<RadioField name="d" value="single" label="Enkel" defaultChecked />
<RadioField name="d" value="slideshow" label="Diavoorstelling" />
</RadioGroup>,
);
expect(screen.getByRole('radiogroup', { name: 'Weergave' })).toBeInTheDocument();
const first = screen.getByRole('radio', { name: 'Enkel' });
const second = screen.getByRole('radio', { name: 'Diavoorstelling' });
first.focus();
await userEvent.keyboard('{ArrowDown}');
expect(second).toBeChecked();
expect(first).not.toBeChecked();
expect(second).toHaveFocus();
});
it('can be disabled and marked invalid, and uses theme colours', () => {
render(<Radio aria-label="r" name="g" value="a" disabled invalid />);
const radio = screen.getByRole('radio');
expect(radio).toBeDisabled();
expect(radio).toHaveAttribute('aria-invalid', 'true');
expect(radio.className).toContain('checked:border-primary');
expect(radio.className).toContain('forced-colors:checked:border-[Highlight]');
});
});
+77
View File
@@ -0,0 +1,77 @@
import * as React from 'react';
import { Check, Minus } from 'lucide-react';
import { cn } from '@/lib/utils';
export interface CheckboxProps extends Omit<React.ComponentProps<'input'>, 'type' | 'size'> {
/** The "some of them" state of a parent checkbox. It is shown with a minus; the value of the box itself does not change. */
indeterminate?: boolean;
/** The field has an error: a red border (and aria-invalid for screen readers). */
invalid?: boolean;
}
/**
* A real checkbox (a native input: Space toggles it, it works with a label, with react-hook-form's register and in forms),
* drawn in the colours of the theme: a clear border, the accent colour with a check mark when checked, a minus for the
* in-between state, hover, a focus ring, disabled and error states. The check mark is an icon over the input in the
* "primary foreground" colour; in forced-colors (high contrast) mode the system colours take over.
*/
export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
({ className, indeterminate = false, invalid = false, ...props }, forwardedRef) => {
const inner = React.useRef<HTMLInputElement | null>(null);
const setRef = (element: HTMLInputElement | null) => {
inner.current = element;
if (typeof forwardedRef === 'function') forwardedRef(element);
else if (forwardedRef) forwardedRef.current = element;
};
React.useEffect(() => {
if (inner.current) inner.current.indeterminate = indeterminate;
}, [indeterminate]);
return (
<span className="relative inline-flex size-5 shrink-0 items-center justify-center">
<input
ref={setRef}
type="checkbox"
aria-invalid={invalid || undefined}
className={cn(
'peer size-5 shrink-0 cursor-pointer appearance-none rounded border-2 border-muted-foreground bg-background transition-colors',
'hover:border-primary checked:border-primary checked:bg-primary indeterminate:border-primary indeterminate:bg-primary',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ring-offset-background',
'disabled:cursor-not-allowed disabled:opacity-50 aria-[invalid=true]:border-destructive',
'forced-colors:checked:bg-[Highlight] forced-colors:checked:border-[Highlight]',
className,
)}
{...props}
/>
<Check
className="pointer-events-none absolute size-3.5 text-primary-foreground opacity-0 peer-checked:opacity-100 peer-indeterminate:opacity-0 peer-disabled:opacity-50 forced-colors:text-[HighlightText]"
strokeWidth={3}
aria-hidden="true"
/>
<Minus
className="pointer-events-none absolute size-3.5 text-primary-foreground opacity-0 peer-indeterminate:opacity-100 peer-disabled:opacity-50 forced-colors:text-[HighlightText]"
strokeWidth={3}
aria-hidden="true"
/>
</span>
);
},
);
Checkbox.displayName = 'Checkbox';
interface CheckboxFieldProps extends CheckboxProps {
label: React.ReactNode;
}
/** A checkbox with its label: the whole line is clickable and at least 24 px high. */
export const CheckboxField = React.forwardRef<HTMLInputElement, CheckboxFieldProps>(({ label, id, disabled, ...props }, ref) => {
const generated = React.useId();
const inputId = id ?? generated;
return (
<label htmlFor={inputId} className={cn('inline-flex min-h-6 cursor-pointer items-center gap-2 py-0.5 text-sm', disabled && 'cursor-not-allowed opacity-70')}>
<Checkbox ref={ref} id={inputId} disabled={disabled} {...props} />
<span>{label}</span>
</label>
);
});
CheckboxField.displayName = 'CheckboxField';
+61
View File
@@ -0,0 +1,61 @@
import * as React from 'react';
import { cn } from '@/lib/utils';
export interface RadioProps extends Omit<React.ComponentProps<'input'>, 'type' | 'size'> {
invalid?: boolean;
}
/**
* A real radio button (a native input: with the same name the arrow keys move and select inside the group, Space selects),
* drawn in the colours of the theme: a clear round border, the accent colour with a dot when selected, hover, a focus
* ring, disabled and error states, and system colours in forced-colors (high contrast) mode.
*/
export const Radio = React.forwardRef<HTMLInputElement, RadioProps>(({ className, invalid = false, ...props }, ref) => (
<span className="relative inline-flex size-5 shrink-0 items-center justify-center">
<input
ref={ref}
type="radio"
aria-invalid={invalid || undefined}
className={cn(
'peer size-5 shrink-0 cursor-pointer appearance-none rounded-full border-2 border-muted-foreground bg-background transition-colors',
'hover:border-primary checked:border-primary',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ring-offset-background',
'disabled:cursor-not-allowed disabled:opacity-50 aria-[invalid=true]:border-destructive',
'forced-colors:checked:border-[Highlight]',
className,
)}
{...props}
/>
<span
className="pointer-events-none absolute size-2.5 rounded-full bg-primary opacity-0 peer-checked:opacity-100 peer-disabled:opacity-50 forced-colors:bg-[Highlight]"
aria-hidden="true"
/>
</span>
));
Radio.displayName = 'Radio';
interface RadioFieldProps extends RadioProps {
label: React.ReactNode;
}
/** A radio button with its label: the whole line is clickable and at least 24 px high. */
export const RadioField = React.forwardRef<HTMLInputElement, RadioFieldProps>(({ label, id, disabled, ...props }, ref) => {
const generated = React.useId();
const inputId = id ?? generated;
return (
<label htmlFor={inputId} className={cn('inline-flex min-h-6 cursor-pointer items-center gap-2 py-0.5 text-sm', disabled && 'cursor-not-allowed opacity-70')}>
<Radio ref={ref} id={inputId} disabled={disabled} {...props} />
<span>{label}</span>
</label>
);
});
RadioField.displayName = 'RadioField';
/** The group around related radio buttons: role radiogroup with a name, so a screen reader announces what is chosen. */
export function RadioGroup({ label, className, children, ...props }: { label: string } & Omit<React.ComponentProps<'div'>, 'aria-label'>) {
return (
<div role="radiogroup" aria-label={label} className={cn('flex flex-wrap gap-x-6 gap-y-1', className)} {...props}>
{children}
</div>
);
}