UNPKG

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