2023-05-14 02:26:11 +00:00
|
|
|
import clsx from 'clsx';
|
2022-01-04 12:16:09 +00:00
|
|
|
import {
|
|
|
|
forwardRef,
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
MutableRefObject,
|
|
|
|
ChangeEventHandler,
|
|
|
|
HTMLProps,
|
|
|
|
} from 'react';
|
|
|
|
|
|
|
|
interface Props extends HTMLProps<HTMLInputElement> {
|
|
|
|
checked?: boolean;
|
|
|
|
indeterminate?: boolean;
|
|
|
|
title?: string;
|
|
|
|
label?: string;
|
|
|
|
id: string;
|
|
|
|
className?: string;
|
|
|
|
role?: string;
|
|
|
|
onChange?: ChangeEventHandler<HTMLInputElement>;
|
2023-05-14 02:26:11 +00:00
|
|
|
bold?: boolean;
|
2022-01-04 12:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Checkbox = forwardRef<HTMLInputElement, Props>(
|
|
|
|
(
|
2023-05-14 02:26:11 +00:00
|
|
|
{
|
|
|
|
indeterminate,
|
|
|
|
title,
|
|
|
|
label,
|
|
|
|
id,
|
|
|
|
checked,
|
|
|
|
onChange,
|
|
|
|
bold = true,
|
|
|
|
...props
|
|
|
|
}: Props,
|
2022-01-04 12:16:09 +00:00
|
|
|
ref
|
|
|
|
) => {
|
|
|
|
const defaultRef = useRef<HTMLInputElement>(null);
|
|
|
|
let resolvedRef = ref as MutableRefObject<HTMLInputElement | null>;
|
|
|
|
if (!ref) {
|
|
|
|
resolvedRef = defaultRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (resolvedRef === null || resolvedRef.current === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof indeterminate !== 'undefined') {
|
|
|
|
resolvedRef.current.indeterminate = indeterminate;
|
|
|
|
}
|
|
|
|
}, [resolvedRef, indeterminate]);
|
|
|
|
|
|
|
|
return (
|
2022-09-18 11:42:18 +00:00
|
|
|
<div className="md-checkbox flex" title={title || label}>
|
2022-01-04 12:16:09 +00:00
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
type="checkbox"
|
|
|
|
ref={resolvedRef}
|
|
|
|
onChange={onChange}
|
|
|
|
checked={checked}
|
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...props}
|
|
|
|
/>
|
2023-05-14 02:26:11 +00:00
|
|
|
<label htmlFor={id} className={clsx({ '!font-normal': !bold })}>
|
|
|
|
{label}
|
|
|
|
</label>
|
2022-01-04 12:16:09 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|