1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage";
|
5 | import * as React from 'react';
|
6 | import { unstable_useForkRef as useForkRef } from '@mui/utils';
|
7 | import { useFormControlContext } from '../FormControl';
|
8 | import { extractEventHandlers } from '../utils/extractEventHandlers';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export function useInput(parameters = {}) {
|
20 | const {
|
21 | defaultValue: defaultValueProp,
|
22 | disabled: disabledProp = false,
|
23 | error: errorProp = false,
|
24 | onBlur,
|
25 | onChange,
|
26 | onFocus,
|
27 | required: requiredProp = false,
|
28 | value: valueProp,
|
29 | inputRef: inputRefProp
|
30 | } = parameters;
|
31 | const formControlContext = useFormControlContext();
|
32 | let defaultValue;
|
33 | let disabled;
|
34 | let error;
|
35 | let required;
|
36 | let value;
|
37 | if (formControlContext) {
|
38 | defaultValue = undefined;
|
39 | disabled = formControlContext.disabled ?? false;
|
40 | error = formControlContext.error ?? false;
|
41 | required = formControlContext.required ?? false;
|
42 | value = formControlContext.value;
|
43 | if (process.env.NODE_ENV !== 'production') {
|
44 | const definedLocalProps = ['defaultValue', 'disabled', 'error', 'required', 'value'].filter(prop => parameters[prop] !== undefined);
|
45 | if (definedLocalProps.length > 0) {
|
46 | 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'));
|
47 | }
|
48 | }
|
49 | } else {
|
50 | defaultValue = defaultValueProp;
|
51 | disabled = disabledProp;
|
52 | error = errorProp;
|
53 | required = requiredProp;
|
54 | value = valueProp;
|
55 | }
|
56 | const {
|
57 | current: isControlled
|
58 | } = React.useRef(value != null);
|
59 | const handleInputRefWarning = React.useCallback(instance => {
|
60 | if (process.env.NODE_ENV !== 'production') {
|
61 | if (instance && instance.nodeName !== 'INPUT' && !instance.focus) {
|
62 | 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'));
|
63 | }
|
64 | }
|
65 | }, []);
|
66 | const inputRef = React.useRef(null);
|
67 | const handleInputRef = useForkRef(inputRef, inputRefProp, handleInputRefWarning);
|
68 | const [focused, setFocused] = React.useState(false);
|
69 |
|
70 |
|
71 |
|
72 | React.useEffect(() => {
|
73 | if (!formControlContext && disabled && focused) {
|
74 | setFocused(false);
|
75 |
|
76 |
|
77 | onBlur?.();
|
78 | }
|
79 | }, [formControlContext, disabled, focused, onBlur]);
|
80 | const handleFocus = otherHandlers => event => {
|
81 |
|
82 |
|
83 | if (formControlContext?.disabled) {
|
84 | event.stopPropagation();
|
85 | return;
|
86 | }
|
87 | otherHandlers.onFocus?.(event);
|
88 | if (formControlContext && formControlContext.onFocus) {
|
89 | formControlContext?.onFocus?.();
|
90 | } else {
|
91 | setFocused(true);
|
92 | }
|
93 | };
|
94 | const handleBlur = otherHandlers => event => {
|
95 | otherHandlers.onBlur?.(event);
|
96 | if (formControlContext && formControlContext.onBlur) {
|
97 | formControlContext.onBlur();
|
98 | } else {
|
99 | setFocused(false);
|
100 | }
|
101 | };
|
102 | const handleChange = otherHandlers => (event, ...args) => {
|
103 | if (!isControlled) {
|
104 | const element = event.target || inputRef.current;
|
105 | if (element == null) {
|
106 | 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));
|
107 | }
|
108 | }
|
109 | formControlContext?.onChange?.(event);
|
110 |
|
111 |
|
112 | otherHandlers.onChange?.(event, ...args);
|
113 | };
|
114 | const handleClick = otherHandlers => event => {
|
115 | if (inputRef.current && event.currentTarget === event.target) {
|
116 | inputRef.current.focus();
|
117 | }
|
118 | otherHandlers.onClick?.(event);
|
119 | };
|
120 | const getRootProps = (externalProps = {}) => {
|
121 |
|
122 | const propsEventHandlers = extractEventHandlers(parameters, ['onBlur', 'onChange', 'onFocus']);
|
123 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
124 | return _extends({}, externalProps, externalEventHandlers, {
|
125 | onClick: handleClick(externalEventHandlers)
|
126 | });
|
127 | };
|
128 | const getInputProps = (externalProps = {}) => {
|
129 | const propsEventHandlers = {
|
130 | onBlur,
|
131 | onChange,
|
132 | onFocus
|
133 | };
|
134 | const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
135 | const mergedEventHandlers = _extends({}, externalEventHandlers, {
|
136 | onBlur: handleBlur(externalEventHandlers),
|
137 | onChange: handleChange(externalEventHandlers),
|
138 | onFocus: handleFocus(externalEventHandlers)
|
139 | });
|
140 | return _extends({}, mergedEventHandlers, {
|
141 | 'aria-invalid': error || undefined,
|
142 | defaultValue: defaultValue,
|
143 | value: value,
|
144 | required,
|
145 | disabled
|
146 | }, externalProps, {
|
147 | ref: handleInputRef
|
148 | }, mergedEventHandlers);
|
149 | };
|
150 | return {
|
151 | disabled,
|
152 | error,
|
153 | focused,
|
154 | formControlContext,
|
155 | getInputProps,
|
156 | getRootProps,
|
157 | inputRef: handleInputRef,
|
158 | required,
|
159 | value
|
160 | };
|
161 | } |
\ | No newline at end of file |