UNPKG

9.33 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["className", "defaultValue", "disabled", "endAdornment", "error", "id", "max", "min", "onBlur", "onInputChange", "onFocus", "onChange", "placeholder", "required", "readOnly", "shiftMultiplier", "startAdornment", "step", "value", "slotProps", "slots"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { getNumberInputUtilityClass } from './numberInputClasses';
9import { unstable_useNumberInput as useNumberInput } from '../unstable_useNumberInput';
10import { unstable_composeClasses as composeClasses } from '../composeClasses';
11import { useSlotProps } from '../utils';
12import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
13import { jsx as _jsx } from "react/jsx-runtime";
14import { jsxs as _jsxs } from "react/jsx-runtime";
15const useUtilityClasses = ownerState => {
16 const {
17 disabled,
18 error,
19 focused,
20 readOnly,
21 formControlContext,
22 isIncrementDisabled,
23 isDecrementDisabled,
24 startAdornment,
25 endAdornment
26 } = ownerState;
27 const slots = {
28 root: ['root', disabled && 'disabled', error && 'error', focused && 'focused', readOnly && 'readOnly', Boolean(formControlContext) && 'formControl', Boolean(startAdornment) && 'adornedStart', Boolean(endAdornment) && 'adornedEnd'],
29 input: ['input', disabled && 'disabled', readOnly && 'readOnly'],
30 incrementButton: ['incrementButton', isIncrementDisabled && 'disabled'],
31 decrementButton: ['decrementButton', isDecrementDisabled && 'disabled']
32 };
33 return composeClasses(slots, useClassNamesOverride(getNumberInputUtilityClass));
34};
35
36/**
37 *
38 * Demos:
39 *
40 * - [Number Input](https://mui.com/base-ui/react-number-input/)
41 *
42 * API:
43 *
44 * - [NumberInput API](https://mui.com/base-ui/react-number-input/components-api/#number-input)
45 */
46const NumberInput = /*#__PURE__*/React.forwardRef(function NumberInput(props, forwardedRef) {
47 var _slots$root, _slots$input, _slots$incrementButto, _slots$decrementButto;
48 const {
49 className,
50 defaultValue,
51 disabled,
52 endAdornment,
53 error,
54 id,
55 max,
56 min,
57 onBlur,
58 onInputChange,
59 onFocus,
60 onChange,
61 placeholder,
62 required,
63 readOnly = false,
64 shiftMultiplier,
65 startAdornment,
66 step,
67 value,
68 slotProps = {},
69 slots = {}
70 } = props,
71 rest = _objectWithoutPropertiesLoose(props, _excluded);
72 const {
73 getRootProps,
74 getInputProps,
75 getIncrementButtonProps,
76 getDecrementButtonProps,
77 focused,
78 error: errorState,
79 disabled: disabledState,
80 formControlContext,
81 isIncrementDisabled,
82 isDecrementDisabled
83 } = useNumberInput({
84 min,
85 max,
86 step,
87 shiftMultiplier,
88 defaultValue,
89 disabled,
90 error,
91 onFocus,
92 onInputChange,
93 onBlur,
94 onChange,
95 required,
96 readOnly,
97 value,
98 inputId: id,
99 componentName: 'NumberInput'
100 });
101 const ownerState = _extends({}, props, {
102 disabled: disabledState,
103 error: errorState,
104 focused,
105 readOnly,
106 formControlContext,
107 isIncrementDisabled,
108 isDecrementDisabled
109 });
110 const classes = useUtilityClasses(ownerState);
111 const propsForwardedToInputSlot = {
112 placeholder
113 };
114 const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
115 const rootProps = useSlotProps({
116 elementType: Root,
117 getSlotProps: getRootProps,
118 externalSlotProps: slotProps.root,
119 externalForwardedProps: rest,
120 additionalProps: {
121 ref: forwardedRef
122 },
123 ownerState,
124 className: [classes.root, className]
125 });
126 const Input = (_slots$input = slots.input) != null ? _slots$input : 'input';
127 const inputProps = useSlotProps({
128 elementType: Input,
129 getSlotProps: otherHandlers => getInputProps(_extends({}, propsForwardedToInputSlot, otherHandlers)),
130 externalSlotProps: slotProps.input,
131 ownerState,
132 className: classes.input
133 });
134 const IncrementButton = (_slots$incrementButto = slots.incrementButton) != null ? _slots$incrementButto : 'button';
135 const incrementButtonProps = useSlotProps({
136 elementType: IncrementButton,
137 getSlotProps: getIncrementButtonProps,
138 externalSlotProps: slotProps.incrementButton,
139 ownerState,
140 className: classes.incrementButton
141 });
142 const DecrementButton = (_slots$decrementButto = slots.decrementButton) != null ? _slots$decrementButto : 'button';
143 const decrementButtonProps = useSlotProps({
144 elementType: DecrementButton,
145 getSlotProps: getDecrementButtonProps,
146 externalSlotProps: slotProps.decrementButton,
147 ownerState,
148 className: classes.decrementButton
149 });
150 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
151 children: [/*#__PURE__*/_jsx(DecrementButton, _extends({}, decrementButtonProps)), /*#__PURE__*/_jsx(IncrementButton, _extends({}, incrementButtonProps)), startAdornment, /*#__PURE__*/_jsx(Input, _extends({}, inputProps)), endAdornment]
152 }));
153});
154process.env.NODE_ENV !== "production" ? NumberInput.propTypes /* remove-proptypes */ = {
155 // ┌────────────────────────────── Warning ──────────────────────────────┐
156 // │ These PropTypes are generated from the TypeScript type definitions. │
157 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
158 // └─────────────────────────────────────────────────────────────────────┘
159 /**
160 * @ignore
161 */
162 children: PropTypes.node,
163 /**
164 * @ignore
165 */
166 className: PropTypes.string,
167 /**
168 * The default value. Use when the component is not controlled.
169 */
170 defaultValue: PropTypes.number,
171 /**
172 * If `true`, the component is disabled.
173 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
174 */
175 disabled: PropTypes.bool,
176 /**
177 * Trailing adornment for this input.
178 */
179 endAdornment: PropTypes.node,
180 /**
181 * 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.
182 */
183 error: PropTypes.bool,
184 /**
185 * The id of the `input` element.
186 */
187 id: PropTypes.string,
188 /**
189 * The maximum value.
190 */
191 max: PropTypes.number,
192 /**
193 * The minimum value.
194 */
195 min: PropTypes.number,
196 /**
197 * @ignore
198 */
199 onBlur: PropTypes.func,
200 /**
201 * Callback fired after the value is clamped and changes - when the `input` is blurred or when
202 * the stepper buttons are triggered.
203 * Called with `undefined` when the value is unset.
204 *
205 * @param {React.FocusEvent<HTMLInputElement>|React.PointerEvent|React.KeyboardEvent} event The event source of the callback
206 * @param {number|undefined} value The new value of the component
207 */
208 onChange: PropTypes.func,
209 /**
210 * @ignore
211 */
212 onFocus: PropTypes.func,
213 /**
214 * Callback fired when the `input` value changes after each keypress, before clamping is applied.
215 * Note that `event.target.value` may contain values that fall outside of `min` and `max` or
216 * are otherwise "invalid".
217 *
218 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
219 */
220 onInputChange: PropTypes.func,
221 /**
222 * The short hint displayed in the `input` before the user enters a value.
223 */
224 placeholder: PropTypes.string,
225 /**
226 * If `true`, the `input` element becomes read-only. The stepper buttons remain active,
227 * with the addition that they are now keyboard focusable.
228 * @default false
229 */
230 readOnly: PropTypes.bool,
231 /**
232 * If `true`, the `input` element is required.
233 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
234 */
235 required: PropTypes.bool,
236 /**
237 * Multiplier applied to `step` if the shift key is held while incrementing
238 * or decrementing the value. Defaults to `10`.
239 */
240 shiftMultiplier: PropTypes.number,
241 /**
242 * The props used for each slot inside the NumberInput.
243 * @default {}
244 */
245 slotProps: PropTypes.shape({
246 decrementButton: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
247 incrementButton: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
248 input: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
249 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
250 }),
251 /**
252 * The components used for each slot inside the InputBase.
253 * Either a string to use a HTML element or a component.
254 * @default {}
255 */
256 slots: PropTypes.shape({
257 decrementButton: PropTypes.elementType,
258 incrementButton: PropTypes.elementType,
259 input: PropTypes.elementType,
260 root: PropTypes.elementType
261 }),
262 /**
263 * Leading adornment for this input.
264 */
265 startAdornment: PropTypes.node,
266 /**
267 * The amount that the value changes on each increment or decrement.
268 */
269 step: PropTypes.number,
270 /**
271 * The current value. Use when the component is controlled.
272 * @default null
273 */
274 value: PropTypes.number
275} : void 0;
276export { NumberInput };
\No newline at end of file