UNPKG

10.4 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import isHostComponent from '../utils/isHostComponent';
6import { getInputUtilityClass } from './inputClasses';
7import useInput from '../useInput';
8import { useSlotProps } from '../utils';
9import composeClasses from '../composeClasses';
10import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
11import { jsx as _jsx } from "react/jsx-runtime";
12import { jsxs as _jsxs } from "react/jsx-runtime";
13var useUtilityClasses = function useUtilityClasses(ownerState) {
14 var disabled = ownerState.disabled,
15 error = ownerState.error,
16 focused = ownerState.focused,
17 formControlContext = ownerState.formControlContext,
18 multiline = ownerState.multiline,
19 startAdornment = ownerState.startAdornment,
20 endAdornment = ownerState.endAdornment;
21 var slots = {
22 root: ['root', disabled && 'disabled', error && 'error', focused && 'focused', Boolean(formControlContext) && 'formControl', multiline && 'multiline', Boolean(startAdornment) && 'adornedStart', Boolean(endAdornment) && 'adornedEnd'],
23 input: ['input', disabled && 'disabled', multiline && 'multiline']
24 };
25 return composeClasses(slots, useClassNamesOverride(getInputUtilityClass));
26};
27
28/**
29 *
30 * Demos:
31 *
32 * - [Input](https://mui.com/base/react-input/)
33 *
34 * API:
35 *
36 * - [Input API](https://mui.com/base/react-input/components-api/#input)
37 */
38var Input = /*#__PURE__*/React.forwardRef(function Input(props, forwardedRef) {
39 var _slots$root, _slots$textarea, _slots$input;
40 var ariaDescribedby = props['aria-describedby'],
41 ariaLabel = props['aria-label'],
42 ariaLabelledby = props['aria-labelledby'],
43 autoComplete = props.autoComplete,
44 autoFocus = props.autoFocus,
45 className = props.className,
46 defaultValue = props.defaultValue,
47 disabled = props.disabled,
48 endAdornment = props.endAdornment,
49 error = props.error,
50 id = props.id,
51 _props$multiline = props.multiline,
52 multiline = _props$multiline === void 0 ? false : _props$multiline,
53 name = props.name,
54 onClick = props.onClick,
55 onChange = props.onChange,
56 onKeyDown = props.onKeyDown,
57 onKeyUp = props.onKeyUp,
58 onFocus = props.onFocus,
59 onBlur = props.onBlur,
60 placeholder = props.placeholder,
61 readOnly = props.readOnly,
62 required = props.required,
63 startAdornment = props.startAdornment,
64 value = props.value,
65 typeProp = props.type,
66 rows = props.rows,
67 _props$slotProps = props.slotProps,
68 slotProps = _props$slotProps === void 0 ? {} : _props$slotProps,
69 _props$slots = props.slots,
70 slots = _props$slots === void 0 ? {} : _props$slots,
71 minRows = props.minRows,
72 maxRows = props.maxRows,
73 other = _objectWithoutProperties(props, ["aria-describedby", "aria-label", "aria-labelledby", "autoComplete", "autoFocus", "className", "defaultValue", "disabled", "endAdornment", "error", "id", "multiline", "name", "onClick", "onChange", "onKeyDown", "onKeyUp", "onFocus", "onBlur", "placeholder", "readOnly", "required", "startAdornment", "value", "type", "rows", "slotProps", "slots", "minRows", "maxRows"]);
74 var _useInput = useInput({
75 disabled: disabled,
76 defaultValue: defaultValue,
77 error: error,
78 onBlur: onBlur,
79 onClick: onClick,
80 onChange: onChange,
81 onFocus: onFocus,
82 required: required,
83 value: value
84 }),
85 getRootProps = _useInput.getRootProps,
86 getInputProps = _useInput.getInputProps,
87 focused = _useInput.focused,
88 formControlContext = _useInput.formControlContext,
89 errorState = _useInput.error,
90 disabledState = _useInput.disabled;
91 var type = !multiline ? typeProp != null ? typeProp : 'text' : undefined;
92 var ownerState = _extends({}, props, {
93 disabled: disabledState,
94 error: errorState,
95 focused: focused,
96 formControlContext: formControlContext,
97 multiline: multiline,
98 type: type
99 });
100 var classes = useUtilityClasses(ownerState);
101 var propsToForward = {
102 'aria-describedby': ariaDescribedby,
103 'aria-label': ariaLabel,
104 'aria-labelledby': ariaLabelledby,
105 autoComplete: autoComplete,
106 autoFocus: autoFocus,
107 id: id,
108 onKeyDown: onKeyDown,
109 onKeyUp: onKeyUp,
110 name: name,
111 placeholder: placeholder,
112 readOnly: readOnly,
113 type: type
114 };
115 var Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
116 var rootProps = useSlotProps({
117 elementType: Root,
118 getSlotProps: getRootProps,
119 externalSlotProps: slotProps.root,
120 externalForwardedProps: other,
121 additionalProps: {
122 ref: forwardedRef
123 },
124 ownerState: ownerState,
125 className: [classes.root, className]
126 });
127 var InputComponent = multiline ? (_slots$textarea = slots.textarea) != null ? _slots$textarea : 'textarea' : (_slots$input = slots.input) != null ? _slots$input : 'input';
128 var inputProps = useSlotProps({
129 elementType: InputComponent,
130 getSlotProps: function getSlotProps(otherHandlers) {
131 return getInputProps(_extends({}, propsToForward, otherHandlers));
132 },
133 externalSlotProps: slotProps.input,
134 additionalProps: _extends({
135 rows: multiline ? rows : undefined
136 }, multiline && !isHostComponent(InputComponent) && {
137 minRows: rows || minRows,
138 maxRows: rows || maxRows
139 }),
140 ownerState: ownerState,
141 className: classes.input
142 });
143 if (process.env.NODE_ENV !== 'production') {
144 if (multiline) {
145 if (rows) {
146 if (minRows || maxRows) {
147 console.warn('MUI: You can not use the `minRows` or `maxRows` props when the input `rows` prop is set.');
148 }
149 }
150 }
151 }
152 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
153 children: [startAdornment, /*#__PURE__*/_jsx(InputComponent, _extends({}, inputProps)), endAdornment]
154 }));
155});
156process.env.NODE_ENV !== "production" ? Input.propTypes /* remove-proptypes */ = {
157 // ----------------------------- Warning --------------------------------
158 // | These PropTypes are generated from the TypeScript type definitions |
159 // | To update them edit TypeScript types and run "yarn proptypes" |
160 // ----------------------------------------------------------------------
161 /**
162 * @ignore
163 */
164 'aria-describedby': PropTypes.string,
165 /**
166 * @ignore
167 */
168 'aria-label': PropTypes.string,
169 /**
170 * @ignore
171 */
172 'aria-labelledby': PropTypes.string,
173 /**
174 * This prop helps users to fill forms faster, especially on mobile devices.
175 * The name can be confusing, as it's more like an autofill.
176 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
177 */
178 autoComplete: PropTypes.string,
179 /**
180 * If `true`, the `input` element is focused during the first mount.
181 */
182 autoFocus: PropTypes.bool,
183 /**
184 * Class name applied to the root element.
185 */
186 className: PropTypes.string,
187 /**
188 * The default value. Use when the component is not controlled.
189 */
190 defaultValue: PropTypes.any,
191 /**
192 * If `true`, the component is disabled.
193 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
194 */
195 disabled: PropTypes.bool,
196 /**
197 * Trailing adornment for this input.
198 */
199 endAdornment: PropTypes.node,
200 /**
201 * If `true`, the `input` will indicate an error by setting the `aria-invalid` attribute on the input and the `Mui-error` class on the root element.
202 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
203 */
204 error: PropTypes.bool,
205 /**
206 * The id of the `input` element.
207 */
208 id: PropTypes.string,
209 /**
210 * @ignore
211 */
212 inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
213 current: PropTypes.object
214 })]),
215 /**
216 * Maximum number of rows to display when multiline option is set to true.
217 */
218 maxRows: PropTypes.number,
219 /**
220 * Minimum number of rows to display when multiline option is set to true.
221 */
222 minRows: PropTypes.number,
223 /**
224 * If `true`, a `textarea` element is rendered.
225 * @default false
226 */
227 multiline: PropTypes.bool,
228 /**
229 * Name attribute of the `input` element.
230 */
231 name: PropTypes.string,
232 /**
233 * @ignore
234 */
235 onBlur: PropTypes.func,
236 /**
237 * @ignore
238 */
239 onChange: PropTypes.func,
240 /**
241 * @ignore
242 */
243 onClick: PropTypes.func,
244 /**
245 * @ignore
246 */
247 onFocus: PropTypes.func,
248 /**
249 * @ignore
250 */
251 onKeyDown: PropTypes.func,
252 /**
253 * @ignore
254 */
255 onKeyUp: PropTypes.func,
256 /**
257 * The short hint displayed in the `input` before the user enters a value.
258 */
259 placeholder: PropTypes.string,
260 /**
261 * It prevents the user from changing the value of the field
262 * (not from interacting with the field).
263 */
264 readOnly: PropTypes.bool,
265 /**
266 * If `true`, the `input` element is required.
267 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
268 */
269 required: PropTypes.bool,
270 /**
271 * Number of rows to display when multiline option is set to true.
272 */
273 rows: PropTypes.number,
274 /**
275 * The props used for each slot inside the Input.
276 * @default {}
277 */
278 slotProps: PropTypes.shape({
279 input: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
280 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
281 }),
282 /**
283 * The components used for each slot inside the InputBase.
284 * Either a string to use a HTML element or a component.
285 * @default {}
286 */
287 slots: PropTypes.shape({
288 input: PropTypes.elementType,
289 root: PropTypes.elementType,
290 textarea: PropTypes.elementType
291 }),
292 /**
293 * Leading adornment for this input.
294 */
295 startAdornment: PropTypes.node,
296 /**
297 * Type of the `input` element. It should be [a valid HTML5 input type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types).
298 * @default 'text'
299 */
300 type: PropTypes /* @typescript-to-proptypes-ignore */.oneOf(['button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', 'url', 'week']),
301 /**
302 * The value of the `input` element, required for a controlled component.
303 */
304 value: PropTypes.any
305} : void 0;
306export default Input;
\No newline at end of file