stash/ui/v2.5/src/components/Shared/ThreeStateCheckbox.tsx
WithoutPants 0f64954e5b
Identify task (#1839)
* Add identify task
* Change type naming
* Debounce folder select text input
* Add generic slice comparison function
2021-10-28 14:25:17 +11:00

47 lines
1.1 KiB
TypeScript

import React from "react";
import { Button } from "react-bootstrap";
import { Icon } from ".";
interface IThreeStateCheckbox {
value: boolean | undefined;
setValue: (v: boolean | undefined) => void;
allowUndefined?: boolean;
label?: React.ReactNode;
disabled?: boolean;
}
export const ThreeStateCheckbox: React.FC<IThreeStateCheckbox> = ({
value,
setValue,
allowUndefined,
label,
disabled = false,
}) => {
function cycleState() {
const undefAllowed = allowUndefined ?? true;
if (undefAllowed && value) {
return undefined;
}
if ((!undefAllowed && value) || value === undefined) {
return false;
}
return true;
}
const icon = value === undefined ? "minus" : value ? "check" : "times";
const labelClassName =
value === undefined ? "unset" : value ? "checked" : "not-checked";
return (
<span className={`three-state-checkbox ${labelClassName}`}>
<Button
onClick={() => setValue(cycleState())}
className="minimal"
disabled={disabled}
>
<Icon icon={icon} className="fa-fw" />
</Button>
<span className="label">{label}</span>
</span>
);
};