UNPKG

983 BPlain TextView Raw
1import * as React from "react";
2import {
3 useSealedState,
4 SealedInitialState,
5} from "reakit-utils/useSealedState";
6
7export type CheckboxState = {
8 /**
9 * Stores the state of the checkbox.
10 * If checkboxes that share this state have defined a `value` prop, it's
11 * going to be an array.
12 */
13 state: boolean | "indeterminate" | Array<number | string>;
14};
15
16export type CheckboxActions = {
17 /**
18 * Sets `state`.
19 */
20 setState: React.Dispatch<React.SetStateAction<CheckboxState["state"]>>;
21};
22
23export type CheckboxInitialState = Partial<Pick<CheckboxState, "state">>;
24
25export type CheckboxStateReturn = CheckboxState & CheckboxActions;
26
27/**
28 * As simple as `React.useState(false)`
29 */
30export function useCheckboxState(
31 initialState: SealedInitialState<CheckboxInitialState> = {}
32): CheckboxStateReturn {
33 const { state: initialValue = false } = useSealedState(initialState);
34 const [state, setState] = React.useState(initialValue);
35 return { state, setState };
36}