2022-11-24 09:41:03 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-14 15:50:12 +02:00
|
|
|
import DisabledByDefaultRounded from '@mui/icons-material/DisabledByDefaultRounded';
|
|
|
|
|
import Checkbox, { CheckboxProps } from '@mui/material/Checkbox';
|
2022-11-24 09:41:03 +01:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
|
|
|
|
|
|
type CheckState = boolean | undefined | null;
|
|
|
|
|
|
|
|
|
|
function nextState(state: CheckState): CheckState {
|
|
|
|
|
if (state === true) return false;
|
|
|
|
|
if (state === false) return undefined;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ThreeStateCheckboxProps extends Omit<CheckboxProps, 'checked' | 'onChange'> {
|
2023-02-06 10:06:33 +01:00
|
|
|
checked?: boolean | undefined | null;
|
|
|
|
|
onChange?: (checked: boolean | undefined | null) => void;
|
2022-11-24 09:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When checked is true, checkbox contains checkmark
|
|
|
|
|
* When checked is false, checkbox contains cross
|
|
|
|
|
* When checked is null or undefined, checkbox is empty
|
|
|
|
|
*/
|
2023-10-28 00:32:02 +02:00
|
|
|
export const ThreeStateCheckbox: React.FC<ThreeStateCheckboxProps> = ({ checked, onChange, ...rest }) => {
|
2022-11-24 09:41:03 +01:00
|
|
|
const handleChange = useCallback(() => {
|
|
|
|
|
if (onChange) {
|
|
|
|
|
const newState = nextState(checked);
|
|
|
|
|
onChange(newState);
|
|
|
|
|
}
|
|
|
|
|
}, [onChange]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Checkbox
|
|
|
|
|
indeterminateIcon={<DisabledByDefaultRounded />}
|
|
|
|
|
checked={checked === true}
|
|
|
|
|
indeterminate={checked === false}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
{...rest}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|