1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import * as React from 'react';
|
5 | import { unstable_useControlled as useControlled, unstable_useForkRef as useForkRef, unstable_useIsFocusVisible as useIsFocusVisible } from '@mui/utils';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function useSwitch(props) {
|
18 | const {
|
19 | checked: checkedProp,
|
20 | defaultChecked,
|
21 | disabled,
|
22 | onBlur,
|
23 | onChange,
|
24 | onFocus,
|
25 | onFocusVisible,
|
26 | readOnly,
|
27 | required
|
28 | } = props;
|
29 | const [checked, setCheckedState] = useControlled({
|
30 | controlled: checkedProp,
|
31 | default: Boolean(defaultChecked),
|
32 | name: 'Switch',
|
33 | state: 'checked'
|
34 | });
|
35 | const createHandleInputChange = otherProps => event => {
|
36 | var _otherProps$onChange;
|
37 |
|
38 | if (event.nativeEvent.defaultPrevented) {
|
39 | return;
|
40 | }
|
41 | setCheckedState(event.target.checked);
|
42 | onChange == null || onChange(event);
|
43 | (_otherProps$onChange = otherProps.onChange) == null || _otherProps$onChange.call(otherProps, event);
|
44 | };
|
45 | const {
|
46 | isFocusVisibleRef,
|
47 | onBlur: handleBlurVisible,
|
48 | onFocus: handleFocusVisible,
|
49 | ref: focusVisibleRef
|
50 | } = useIsFocusVisible();
|
51 | const [focusVisible, setFocusVisible] = React.useState(false);
|
52 | if (disabled && focusVisible) {
|
53 | setFocusVisible(false);
|
54 | }
|
55 | React.useEffect(() => {
|
56 | isFocusVisibleRef.current = focusVisible;
|
57 | }, [focusVisible, isFocusVisibleRef]);
|
58 | const inputRef = React.useRef(null);
|
59 | const createHandleFocus = otherProps => event => {
|
60 | var _otherProps$onFocus;
|
61 |
|
62 | if (!inputRef.current) {
|
63 | inputRef.current = event.currentTarget;
|
64 | }
|
65 | handleFocusVisible(event);
|
66 | if (isFocusVisibleRef.current === true) {
|
67 | setFocusVisible(true);
|
68 | onFocusVisible == null || onFocusVisible(event);
|
69 | }
|
70 | onFocus == null || onFocus(event);
|
71 | (_otherProps$onFocus = otherProps.onFocus) == null || _otherProps$onFocus.call(otherProps, event);
|
72 | };
|
73 | const createHandleBlur = otherProps => event => {
|
74 | var _otherProps$onBlur;
|
75 | handleBlurVisible(event);
|
76 | if (isFocusVisibleRef.current === false) {
|
77 | setFocusVisible(false);
|
78 | }
|
79 | onBlur == null || onBlur(event);
|
80 | (_otherProps$onBlur = otherProps.onBlur) == null || _otherProps$onBlur.call(otherProps, event);
|
81 | };
|
82 | const handleInputRef = useForkRef(focusVisibleRef, inputRef);
|
83 | const getInputProps = (otherProps = {}) => _extends({
|
84 | checked: checkedProp,
|
85 | defaultChecked,
|
86 | disabled,
|
87 | readOnly,
|
88 | ref: handleInputRef,
|
89 | required,
|
90 | type: 'checkbox',
|
91 | role: 'switch',
|
92 | 'aria-checked': checkedProp
|
93 | }, otherProps, {
|
94 | onChange: createHandleInputChange(otherProps),
|
95 | onFocus: createHandleFocus(otherProps),
|
96 | onBlur: createHandleBlur(otherProps)
|
97 | });
|
98 | return {
|
99 | checked,
|
100 | disabled: Boolean(disabled),
|
101 | focusVisible,
|
102 | getInputProps,
|
103 | inputRef: handleInputRef,
|
104 | readOnly: Boolean(readOnly)
|
105 | };
|
106 | } |
\ | No newline at end of file |