@amsterdam/design-system-react
Version:
All React components from the Amsterdam Design System. Use it to compose pages in your website or application.
29 lines (28 loc) • 1.49 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
* @license EUPL-1.2+
* Copyright Gemeente Amsterdam
*/
import clsx from 'clsx';
import { forwardRef, useEffect, useId, useImperativeHandle, useRef } from 'react';
import CheckboxIcon from './CheckboxIcon';
/**
* @see {@link https://designsystem.amsterdam/?path=/docs/components-forms-checkbox--docs Checkbox docs at Amsterdam Design System}
*/
export const Checkbox = forwardRef(({ children, className, icon, indeterminate, invalid, ...restProps }, ref) => {
const id = useId();
const innerRef = useRef(null);
// use a passed ref if it's there, otherwise use innerRef
useImperativeHandle(ref, () => innerRef.current);
// set input to indeterminate
useEffect(() => {
if (innerRef.current) {
innerRef.current.indeterminate = Boolean(indeterminate);
}
}, [innerRef, indeterminate]);
return (
// This div is here because NVDA doesn't match the input to the label
// without a containing element
_jsxs("div", { className: clsx('ams-checkbox', className), children: [_jsx("input", { ...restProps, "aria-invalid": invalid || undefined, className: "ams-checkbox__input", id: id, ref: innerRef, type: "checkbox" }), _jsxs("label", { className: "ams-checkbox__label", htmlFor: id, children: [_jsx("span", { className: "ams-checkbox__icon-container", children: icon ?? _jsx(CheckboxIcon, {}) }), children] })] }));
});
Checkbox.displayName = 'Checkbox';