UNPKG

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