@amsterdam/design-system-react
Version:
All React components from the Amsterdam Design System. Use it to compose pages in your website or application.
25 lines (24 loc) • 1.5 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
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 = CheckboxIcon, id, indeterminate, invalid, ...restProps }, ref) => {
const inputId = 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: inputId, ref: innerRef, type: "checkbox" }), _jsxs("label", { className: "ams-checkbox__label", htmlFor: inputId, children: [_jsx("span", { className: "ams-checkbox__icon-container", hidden: true, children: typeof icon === 'function' ? icon() : icon }), children] })] }));
});
Checkbox.displayName = 'Checkbox';