MCPcopy Index your code
hub / github.com/adobe/react-spectrum / useToggleState

Function useToggleState

packages/react-stately/src/toggle/useToggleState.ts:61–91  ·  view source on GitHub ↗
(props: ToggleStateOptions = {})

Source from the content-addressed store, hash-verified

59 * Provides state management for toggle components like checkboxes and switches.
60 */
61export function useToggleState(props: ToggleStateOptions = {}): ToggleState {
62 let {isReadOnly} = props;
63
64 // have to provide an empty function so useControlledState doesn't throw a fit
65 // can't use useControlledState's prop calling because we need the event object from the change
66 let [isSelected, setSelected] = useControlledState(
67 props.isSelected,
68 props.defaultSelected || false,
69 props.onChange
70 );
71 let [initialValue] = useState(isSelected);
72
73 function updateSelected(value) {
74 if (!isReadOnly) {
75 setSelected(value);
76 }
77 }
78
79 function toggleState() {
80 if (!isReadOnly) {
81 setSelected(!isSelected);
82 }
83 }
84
85 return {
86 isSelected,
87 defaultSelected: props.defaultSelected ?? initialValue,
88 setSelected: updateSelected,
89 toggle: toggleState
90 };
91}

Callers 14

useCheckboxGroupItemFunction · 0.90
CheckboxFunction · 0.90
TableCheckboxCellFunction · 0.90
TableSelectAllCellFunction · 0.90
TableCheckboxCellFunction · 0.90
TableSelectAllCellFunction · 0.90
TableCheckboxCellFunction · 0.90
TableSelectAllCellFunction · 0.90
ToggleButton.tsxFile · 0.90
Switch.tsxFile · 0.90
useCheckboxAriaFunction · 0.90
Checkbox.tsxFile · 0.90

Calls 1

useControlledStateFunction · 0.90

Tested by

no test coverage detected