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 | defaultValue = undefined;
|
37 | disabled = formControlContext.disabled ?? false;
|
38 | error = formControlContext.error ?? false;
|
39 | required = formControlContext.required ?? false;
|
40 | value = formControlContext.value;
|
41 | if (process.env.NODE_ENV !== 'production') {
|
42 | const definedLocalProps = ['defaultValue', 'disabled', 'error', 'required', 'value'].filter(prop => parameters[prop] !== undefined);
|
43 | if (definedLocalProps.length > 0) {
|
44 | 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'));
|
45 | }
|
46 | }
|
47 | } else {
|
48 | defaultValue = defaultValueProp;
|
49 | disabled = disabledProp;
|
50 | error = errorProp;
|
51 | required = requiredProp;
|
52 | value = valueProp;
|
53 | }
|
54 | const {
|
55 | current: isControlled
|
56 | } = React.useRef(value != null);
|
57 | const handleInputRefWarning = React.useCallback(instance => {
|
58 | if (process.env.NODE_ENV !== 'production') {
|
59 | if (instance && instance.nodeName !== 'INPUT' && !instance.focus) {
|
60 | 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'));
|
61 | }
|
62 | }
|
63 | }, []);
|
64 | const inputRef = React.useRef(null);
|
65 | const handleInputRef = useForkRef(inputRef, inputRefProp, handleInputRefWarning);
|
66 | const [focused, setFocused] = React.useState(false);
|
67 |
|
68 |
|
69 |
|
70 | React.useEffect(() => {
|
71 | if (!formControlContext && disabled && focused) {
|
72 | setFocused(false);
|
73 |
|
74 |
|
75 | onBlur?.();
|
76 | }
|
77 | }, [formControlContext, disabled, focused, onBlur]);
|
78 | const handleFocus = otherHandlers => event => {
|
79 |
|
80 |
|
81 | if (formControlContext?.disabled) {
|
82 | event.stopPropagation();
|
83 | return;
|
84 | }
|
85 | otherHandlers.onFocus?.(event);
|
86 | if (formControlContext && formControlContext.onFocus) {
|
87 | formControlContext?.onFocus?.();
|
88 | } else {
|
89 | setFocused(true);
|
90 | }
|
91 | };
|
92 | const handleBlur = otherHandlers => event => {
|
93 | otherHandlers.onBlur?.(event);
|
94 | if (formControlContext && formControlContext.onBlur) {
|
95 | formControlContext.onBlur();
|
96 | } else {
|
97 | setFocused(false);
|
98 | }
|
99 | };
|
100 | const handleChange = otherHandlers => (event, ...args) => {
|
101 | if (!isControlled) {
|
102 | const element = event.target || inputRef.current;
|
103 | if (element == null) {
|
104 | 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));
|
105 | }
|
106 | }
|
107 | formControlContext?.onChange?.(event);
|
108 |
|
109 |
|
110 | otherHandlers.onChange?.(event, ...args);
|
111 | };
|
112 | const handleClick = otherHandlers => event => {
|
113 | if (inputRef.current && event.currentTarget === event.target) {
|
114 | inputRef.current.focus();
|
115 | }
|
116 | otherHandlers.onClick?.(event);
|
117 | };
|
118 | const getRootProps = (externalProps = {}) => {
|
119 |
|
120 | const propsEventHandlers = extractEventHandlers(parameters, ['onBlur', 'onChange', 'onFocus']);
|
121 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
122 | return _extends({}, externalProps, externalEventHandlers, {
|
123 | onClick: handleClick(externalEventHandlers)
|
124 | });
|
125 | };
|
126 | const getInputProps = (externalProps = {}) => {
|
127 | const propsEventHandlers = {
|
128 | onBlur,
|
129 | onChange,
|
130 | onFocus
|
131 | };
|
132 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
133 | const mergedEventHandlers = _extends({}, externalProps, externalEventHandlers, {
|
134 | onBlur: handleBlur(externalEventHandlers),
|
135 | onChange: handleChange(externalEventHandlers),
|
136 | onFocus: handleFocus(externalEventHandlers)
|
137 | });
|
138 | return _extends({}, mergedEventHandlers, {
|
139 | 'aria-invalid': error || undefined,
|
140 | defaultValue: defaultValue,
|
141 | ref: handleInputRef,
|
142 | value: value,
|
143 | required,
|
144 | disabled
|
145 | });
|
146 | };
|
147 | return {
|
148 | disabled,
|
149 | error,
|
150 | focused,
|
151 | formControlContext,
|
152 | getInputProps,
|
153 | getRootProps,
|
154 | inputRef: handleInputRef,
|
155 | required,
|
156 | value
|
157 | };
|
158 | } |
\ | No newline at end of file |