1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
3 | import * as React from 'react';
|
4 | import { unstable_useForkRef as useForkRef } from '@mui/utils';
|
5 | import { useFormControlContext } from '../FormControl';
|
6 | import extractEventHandlers from '../utils/extractEventHandlers';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export default function useInput(parameters) {
|
18 | const {
|
19 | defaultValue: defaultValueProp,
|
20 | disabled: disabledProp = false,
|
21 | error: errorProp = false,
|
22 | onBlur,
|
23 | onChange,
|
24 | onFocus,
|
25 | required: requiredProp = false,
|
26 | value: valueProp,
|
27 | inputRef: inputRefProp
|
28 | } = parameters;
|
29 | const formControlContext = useFormControlContext();
|
30 | let defaultValue;
|
31 | let disabled;
|
32 | let error;
|
33 | let required;
|
34 | let value;
|
35 | if (formControlContext) {
|
36 | var _formControlContext$d, _formControlContext$e, _formControlContext$r;
|
37 | defaultValue = undefined;
|
38 | disabled = (_formControlContext$d = formControlContext.disabled) != null ? _formControlContext$d : false;
|
39 | error = (_formControlContext$e = formControlContext.error) != null ? _formControlContext$e : false;
|
40 | required = (_formControlContext$r = formControlContext.required) != null ? _formControlContext$r : false;
|
41 | value = formControlContext.value;
|
42 | if (process.env.NODE_ENV !== 'production') {
|
43 | const definedLocalProps = ['defaultValue', 'disabled', 'error', 'required', 'value'].filter(prop => parameters[prop] !== undefined);
|
44 | if (definedLocalProps.length > 0) {
|
45 | console.warn(['MUI: You have set props on an input that is inside a FormControl.', 'Set these props on a FormControl instead. Otherwise they will be ignored.', `Ignored props: ${definedLocalProps.join(', ')}`].join('\n'));
|
46 | }
|
47 | }
|
48 | } else {
|
49 | defaultValue = defaultValueProp;
|
50 | disabled = disabledProp;
|
51 | error = errorProp;
|
52 | required = requiredProp;
|
53 | value = valueProp;
|
54 | }
|
55 | const {
|
56 | current: isControlled
|
57 | } = React.useRef(value != null);
|
58 | const handleInputRefWarning = React.useCallback(instance => {
|
59 | if (process.env.NODE_ENV !== 'production') {
|
60 | if (instance && instance.nodeName !== 'INPUT' && !instance.focus) {
|
61 | console.error(['MUI: You have provided a `slots.input` to the input component', 'that does not correctly handle the `ref` prop.', 'Make sure the `ref` prop is called with a HTMLInputElement.'].join('\n'));
|
62 | }
|
63 | }
|
64 | }, []);
|
65 | const inputRef = React.useRef(null);
|
66 | const handleInputRef = useForkRef(inputRef, inputRefProp, handleInputRefWarning);
|
67 | const [focused, setFocused] = React.useState(false);
|
68 |
|
69 |
|
70 |
|
71 | React.useEffect(() => {
|
72 | if (!formControlContext && disabled && focused) {
|
73 | setFocused(false);
|
74 |
|
75 |
|
76 | onBlur == null ? void 0 : onBlur();
|
77 | }
|
78 | }, [formControlContext, disabled, focused, onBlur]);
|
79 | const handleFocus = otherHandlers => event => {
|
80 | var _otherHandlers$onFocu;
|
81 |
|
82 |
|
83 | if (formControlContext != null && formControlContext.disabled) {
|
84 | event.stopPropagation();
|
85 | return;
|
86 | }
|
87 | (_otherHandlers$onFocu = otherHandlers.onFocus) == null ? void 0 : _otherHandlers$onFocu.call(otherHandlers, event);
|
88 | if (formControlContext && formControlContext.onFocus) {
|
89 | var _formControlContext$o;
|
90 | formControlContext == null ? void 0 : (_formControlContext$o = formControlContext.onFocus) == null ? void 0 : _formControlContext$o.call(formControlContext);
|
91 | } else {
|
92 | setFocused(true);
|
93 | }
|
94 | };
|
95 | const handleBlur = otherHandlers => event => {
|
96 | var _otherHandlers$onBlur;
|
97 | (_otherHandlers$onBlur = otherHandlers.onBlur) == null ? void 0 : _otherHandlers$onBlur.call(otherHandlers, event);
|
98 | if (formControlContext && formControlContext.onBlur) {
|
99 | formControlContext.onBlur();
|
100 | } else {
|
101 | setFocused(false);
|
102 | }
|
103 | };
|
104 | const handleChange = otherHandlers => (event, ...args) => {
|
105 | var _formControlContext$o2, _otherHandlers$onChan;
|
106 | if (!isControlled) {
|
107 | const element = event.target || inputRef.current;
|
108 | if (element == null) {
|
109 | throw new Error(process.env.NODE_ENV !== "production" ? `MUI: Expected valid input target. Did you use a custom \`slots.input\` and forget to forward refs? See https://mui.com/r/input-component-ref-interface for more info.` : _formatMuiErrorMessage(17));
|
110 | }
|
111 | }
|
112 | formControlContext == null ? void 0 : (_formControlContext$o2 = formControlContext.onChange) == null ? void 0 : _formControlContext$o2.call(formControlContext, event);
|
113 |
|
114 |
|
115 | (_otherHandlers$onChan = otherHandlers.onChange) == null ? void 0 : _otherHandlers$onChan.call(otherHandlers, event, ...args);
|
116 | };
|
117 | const handleClick = otherHandlers => event => {
|
118 | var _otherHandlers$onClic;
|
119 | if (inputRef.current && event.currentTarget === event.target) {
|
120 | inputRef.current.focus();
|
121 | }
|
122 | (_otherHandlers$onClic = otherHandlers.onClick) == null ? void 0 : _otherHandlers$onClic.call(otherHandlers, event);
|
123 | };
|
124 | const getRootProps = (externalProps = {}) => {
|
125 |
|
126 | const propsEventHandlers = extractEventHandlers(parameters, ['onBlur', 'onChange', 'onFocus']);
|
127 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
128 | return _extends({}, externalProps, externalEventHandlers, {
|
129 | onClick: handleClick(externalEventHandlers)
|
130 | });
|
131 | };
|
132 | const getInputProps = (externalProps = {}) => {
|
133 | const propsEventHandlers = {
|
134 | onBlur,
|
135 | onChange,
|
136 | onFocus
|
137 | };
|
138 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
139 | const mergedEventHandlers = _extends({}, externalProps, externalEventHandlers, {
|
140 | onBlur: handleBlur(externalEventHandlers),
|
141 | onChange: handleChange(externalEventHandlers),
|
142 | onFocus: handleFocus(externalEventHandlers)
|
143 | });
|
144 | return _extends({}, mergedEventHandlers, {
|
145 | 'aria-invalid': error || undefined,
|
146 | defaultValue: defaultValue,
|
147 | ref: handleInputRef,
|
148 | value: value,
|
149 | required,
|
150 | disabled
|
151 | });
|
152 | };
|
153 | return {
|
154 | disabled,
|
155 | error,
|
156 | focused,
|
157 | formControlContext,
|
158 | getInputProps,
|
159 | getRootProps,
|
160 | inputRef: handleInputRef,
|
161 | required,
|
162 | value
|
163 | };
|
164 | } |
\ | No newline at end of file |