UNPKG

6.63 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage";
5import * as React from 'react';
6import { unstable_useForkRef as useForkRef } from '@mui/utils';
7import { useFormControlContext } from '../FormControl';
8import { extractEventHandlers } from '../utils/extractEventHandlers';
9/**
10 *
11 * Demos:
12 *
13 * - [Input](https://mui.com/base-ui/react-input/#hook)
14 *
15 * API:
16 *
17 * - [useInput API](https://mui.com/base-ui/react-input/hooks-api/#use-input)
18 */
19export 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 var _formControlContext$d, _formControlContext$e, _formControlContext$r;
39 defaultValue = undefined;
40 disabled = (_formControlContext$d = formControlContext.disabled) != null ? _formControlContext$d : false;
41 error = (_formControlContext$e = formControlContext.error) != null ? _formControlContext$e : false;
42 required = (_formControlContext$r = formControlContext.required) != null ? _formControlContext$r : false;
43 value = formControlContext.value;
44 if (process.env.NODE_ENV !== 'production') {
45 const definedLocalProps = ['defaultValue', 'disabled', 'error', 'required', 'value'].filter(prop => parameters[prop] !== undefined);
46 if (definedLocalProps.length > 0) {
47 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'));
48 }
49 }
50 } else {
51 defaultValue = defaultValueProp;
52 disabled = disabledProp;
53 error = errorProp;
54 required = requiredProp;
55 value = valueProp;
56 }
57 const {
58 current: isControlled
59 } = React.useRef(value != null);
60 const handleInputRefWarning = React.useCallback(instance => {
61 if (process.env.NODE_ENV !== 'production') {
62 if (instance && instance.nodeName !== 'INPUT' && !instance.focus) {
63 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'));
64 }
65 }
66 }, []);
67 const inputRef = React.useRef(null);
68 const handleInputRef = useForkRef(inputRef, inputRefProp, handleInputRefWarning);
69 const [focused, setFocused] = React.useState(false);
70
71 // The blur won't fire when the disabled state is set on a focused input.
72 // We need to book keep the focused state manually.
73 React.useEffect(() => {
74 if (!formControlContext && disabled && focused) {
75 setFocused(false);
76
77 // @ts-ignore
78 onBlur == null || onBlur();
79 }
80 }, [formControlContext, disabled, focused, onBlur]);
81 const handleFocus = otherHandlers => event => {
82 var _otherHandlers$onFocu;
83 // Fix a bug with IE11 where the focus/blur events are triggered
84 // while the component is disabled.
85 if (formControlContext != null && formControlContext.disabled) {
86 event.stopPropagation();
87 return;
88 }
89 (_otherHandlers$onFocu = otherHandlers.onFocus) == null || _otherHandlers$onFocu.call(otherHandlers, event);
90 if (formControlContext && formControlContext.onFocus) {
91 var _formControlContext$o;
92 formControlContext == null || (_formControlContext$o = formControlContext.onFocus) == null || _formControlContext$o.call(formControlContext);
93 } else {
94 setFocused(true);
95 }
96 };
97 const handleBlur = otherHandlers => event => {
98 var _otherHandlers$onBlur;
99 (_otherHandlers$onBlur = otherHandlers.onBlur) == null || _otherHandlers$onBlur.call(otherHandlers, event);
100 if (formControlContext && formControlContext.onBlur) {
101 formControlContext.onBlur();
102 } else {
103 setFocused(false);
104 }
105 };
106 const handleChange = otherHandlers => (event, ...args) => {
107 var _formControlContext$o2, _otherHandlers$onChan;
108 if (!isControlled) {
109 const element = event.target || inputRef.current;
110 if (element == null) {
111 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));
112 }
113 }
114 formControlContext == null || (_formControlContext$o2 = formControlContext.onChange) == null || _formControlContext$o2.call(formControlContext, event);
115
116 // @ts-ignore
117 (_otherHandlers$onChan = otherHandlers.onChange) == null || _otherHandlers$onChan.call(otherHandlers, event, ...args);
118 };
119 const handleClick = otherHandlers => event => {
120 var _otherHandlers$onClic;
121 if (inputRef.current && event.currentTarget === event.target) {
122 inputRef.current.focus();
123 }
124 (_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
125 };
126 const getRootProps = (externalProps = {}) => {
127 // onBlur, onChange and onFocus are forwarded to the input slot.
128 const propsEventHandlers = extractEventHandlers(parameters, ['onBlur', 'onChange', 'onFocus']);
129 const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
130 return _extends({}, externalProps, externalEventHandlers, {
131 onClick: handleClick(externalEventHandlers)
132 });
133 };
134 const getInputProps = (externalProps = {}) => {
135 const propsEventHandlers = {
136 onBlur,
137 onChange,
138 onFocus
139 };
140 const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
141 const mergedEventHandlers = _extends({}, externalEventHandlers, {
142 onBlur: handleBlur(externalEventHandlers),
143 onChange: handleChange(externalEventHandlers),
144 onFocus: handleFocus(externalEventHandlers)
145 });
146 return _extends({}, mergedEventHandlers, {
147 'aria-invalid': error || undefined,
148 defaultValue: defaultValue,
149 value: value,
150 required,
151 disabled
152 }, externalProps, {
153 ref: handleInputRef
154 }, mergedEventHandlers);
155 };
156 return {
157 disabled,
158 error,
159 focused,
160 formControlContext,
161 getInputProps,
162 getRootProps,
163 inputRef: handleInputRef,
164 required,
165 value
166 };
167}
\No newline at end of file