UNPKG

1.09 kBPlain TextView Raw
1import * as React from "react";
2import {
3 useSealedState,
4 SealedInitialState,
5} from "reakit-utils/useSealedState";
6import {
7 CompositeState,
8 CompositeActions,
9 CompositeInitialState,
10 useCompositeState,
11} from "../Composite";
12
13export type RadioState = CompositeState & {
14 /**
15 * The `value` attribute of the current checked radio.
16 */
17 state: string | number | undefined;
18};
19
20export type RadioActions = CompositeActions & {
21 /**
22 * Sets `state`.
23 */
24 setState: React.Dispatch<React.SetStateAction<string | number | undefined>>;
25};
26
27export type RadioInitialState = CompositeInitialState &
28 Partial<Pick<RadioState, "state">>;
29
30export type RadioStateReturn = RadioState & RadioActions;
31
32export function useRadioState(
33 initialState: SealedInitialState<RadioInitialState> = {}
34): RadioStateReturn {
35 const { state: initialValue, loop = true, ...sealed } = useSealedState(
36 initialState
37 );
38 const [state, setState] = React.useState(initialValue);
39 const composite = useCompositeState({ ...sealed, loop });
40 return {
41 ...composite,
42 state,
43 setState,
44 };
45}