Files
slp-modular-cms/frontend/src/components/ui/radio.tsx
T
SluijsensandClaude Sonnet 5.5 17553e2f5c
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
Admin: gedeelde, themagebonden Checkbox en Radio (en RadioGroup); de drie native checkboxen bij Aanbiedingen vervangen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 09:02:54 +01:00

62 lines
2.8 KiB
TypeScript

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>
);
}