/
CheckboxState.ts
43 lines (36 loc) · 1.05 KB
/
CheckboxState.ts
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
37
38
39
40
41
42
43
import * as React from "react";
import {
useSealedState,
SealedInitialState
} from "reakit-utils/useSealedState";
export type CheckboxState = {
/**
* Stores the state of the checkbox.
* If checkboxes that share this state have defined a `value` prop, it's
* going to be an array.
*/
state: boolean | "indeterminate" | any[];
};
export type CheckboxActions = {
/**
* Sets `state`.
*/
setState: React.Dispatch<React.SetStateAction<CheckboxState["state"]>>;
};
export type CheckboxInitialState = Partial<Pick<CheckboxState, "state">>;
export type CheckboxStateReturn = CheckboxState & CheckboxActions;
/**
* As simple as `React.useState(false)`
*/
export function useCheckboxState(
initialState: SealedInitialState<CheckboxInitialState> = {}
): CheckboxStateReturn {
const { state: initialValue = false } = useSealedState(initialState);
const [state, setState] = React.useState(initialValue);
return {
state,
setState
};
}
const keys: Array<keyof CheckboxStateReturn> = ["state", "setState"];
useCheckboxState.__keys = keys;