import type { ChangeEvent, ForwardRefRenderFunction, JSX } from 'react'; import React, { forwardRef, useImperativeHandle, useRef } from 'react'; import { Input } from 'reactstrap'; import type { ISelectableAndIndeterminatable } from '~/client/interfaces/selectable-all'; import type { IndeterminateInputElement } from '~/interfaces/indeterminate-input-elm'; type Props = { inputId?: string, inputClassName?: string, isCheckboxDisabled?: boolean, onCheckboxChanged?: (isChecked: boolean) => void, children?: React.ReactNode, } const OperateAllControlSubstance: ForwardRefRenderFunction = (props: Props, ref): JSX.Element => { const { inputId, inputClassName = '', isCheckboxDisabled, onCheckboxChanged, children, } = props; const selectAllCheckboxElm = useRef(null); // publish ISelectable methods useImperativeHandle(ref, () => ({ select: () => { const input = selectAllCheckboxElm.current; if (input != null) { input.checked = true; input.indeterminate = false; } }, deselect: () => { const input = selectAllCheckboxElm.current; if (input != null) { input.checked = false; input.indeterminate = false; } }, setIndeterminate: () => { const input = selectAllCheckboxElm.current; if (input != null) { input.indeterminate = true; } }, })); const checkboxChangedHandler = (e: ChangeEvent) => { if (onCheckboxChanged != null) { onCheckboxChanged(e.target.checked); } }; return (
{children}
); }; export const OperateAllControl = React.memo(forwardRef(OperateAllControlSubstance));