UNPKG

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