-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathThreeStateCheckBox.tsx
36 lines (32 loc) · 1.02 KB
/
ThreeStateCheckBox.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createElement, ReactElement, useEffect, useRef } from "react";
export type ThreeStateCheckBoxEnum = "all" | "some" | "none";
export interface ThreeStateCheckBoxProps {
id?: string;
disabled?: boolean;
value: ThreeStateCheckBoxEnum;
onChange?: () => void;
}
export function ThreeStateCheckBox({ id, disabled, value, onChange }: ThreeStateCheckBoxProps): ReactElement {
const checkboxRef = useRef<HTMLInputElement | null>(null);
useEffect(() => {
if (!checkboxRef.current) {
return;
}
if (value === "all" || value === "none") {
checkboxRef.current.indeterminate = false;
} else if (value === "some") {
checkboxRef.current.indeterminate = true;
}
}, [value]);
return (
<input
id={id}
type="checkbox"
className="three-state-checkbox"
ref={checkboxRef}
checked={value !== "none"}
disabled={disabled}
onChange={onChange}
/>
);
}