@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
205 lines (204 loc) • 7.92 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.NumberFieldRoot = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _NumberFieldRootContext = require("./NumberFieldRootContext");
var _useNumberFieldRoot = require("./useNumberFieldRoot");
var _FieldRootContext = require("../../field/root/FieldRootContext");
var _useComponentRenderer = require("../../utils/useComponentRenderer");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* Groups all parts of the number field and manages its state.
* Renders a `<div>` element.
*
* Documentation: [Base UI Number Field](https://base-ui.com/react/components/number-field)
*/
const NumberFieldRoot = exports.NumberFieldRoot = /*#__PURE__*/React.forwardRef(function NumberFieldRoot(props, forwardedRef) {
const {
id,
min,
max,
smallStep,
step,
largeStep,
autoFocus,
required = false,
disabled: disabledProp = false,
invalid = false,
readOnly = false,
name,
defaultValue,
value,
onValueChange,
allowWheelScrub,
format,
render,
className,
...otherProps
} = props;
const numberField = (0, _useNumberFieldRoot.useNumberFieldRoot)(props);
const {
state: fieldState,
disabled: fieldDisabled
} = (0, _FieldRootContext.useFieldRootContext)();
const disabled = fieldDisabled || disabledProp;
const state = React.useMemo(() => ({
...fieldState,
disabled,
invalid,
readOnly,
required,
value: numberField.value,
inputValue: numberField.inputValue,
scrubbing: numberField.isScrubbing
}), [fieldState, disabled, invalid, readOnly, required, numberField.value, numberField.inputValue, numberField.isScrubbing]);
const contextValue = React.useMemo(() => ({
...numberField,
state
}), [numberField, state]);
const {
renderElement
} = (0, _useComponentRenderer.useComponentRenderer)({
ref: forwardedRef,
render: render ?? 'div',
state,
className,
extraProps: otherProps
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_NumberFieldRootContext.NumberFieldRootContext.Provider, {
value: contextValue,
children: renderElement()
});
});
process.env.NODE_ENV !== "production" ? NumberFieldRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Whether to allow the user to scrub the input value with the mouse wheel while focused and
* hovering over the input.
* @default false
*/
allowWheelScrub: _propTypes.default.bool,
/**
* Whether to focus the element on page load.
* @default false
*/
autoFocus: _propTypes.default.bool,
/**
* @ignore
*/
children: _propTypes.default.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
/**
* The uncontrolled value of the field when it’s initially rendered.
*
* To render a controlled number field, use the `value` prop instead.
*/
defaultValue: _propTypes.default.number,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: _propTypes.default.bool,
/**
* Options to format the input value.
*/
format: _propTypes.default.shape({
compactDisplay: _propTypes.default.oneOf(['long', 'short']),
currency: _propTypes.default.string,
currencyDisplay: _propTypes.default.oneOf(['code', 'name', 'narrowSymbol', 'symbol']),
currencySign: _propTypes.default.oneOf(['accounting', 'standard']),
localeMatcher: _propTypes.default.oneOf(['best fit', 'lookup']),
maximumFractionDigits: _propTypes.default.number,
maximumSignificantDigits: _propTypes.default.number,
minimumFractionDigits: _propTypes.default.number,
minimumIntegerDigits: _propTypes.default.number,
minimumSignificantDigits: _propTypes.default.number,
notation: _propTypes.default.oneOf(['compact', 'engineering', 'scientific', 'standard']),
numberingSystem: _propTypes.default.string,
signDisplay: _propTypes.default.oneOf(['always', 'auto', 'exceptZero', 'never']),
style: _propTypes.default.oneOf(['currency', 'decimal', 'percent', 'unit']),
unit: _propTypes.default.string,
unitDisplay: _propTypes.default.oneOf(['long', 'narrow', 'short']),
useGrouping: _propTypes.default.bool
}),
/**
* The id of the input element.
*/
id: _propTypes.default.string,
/**
* Whether the field is forcefully marked as invalid.
* @default false
*/
invalid: _propTypes.default.bool,
/**
* The large step value of the input element when incrementing while the shift key is held. Snaps
* to multiples of this value.
* @default 10
*/
largeStep: _propTypes.default.number,
/**
* The maximum value of the input element.
*/
max: _propTypes.default.number,
/**
* The minimum value of the input element.
*/
min: _propTypes.default.number,
/**
* Identifies the field when a form is submitted.
*/
name: _propTypes.default.string,
/**
* Callback fired when the number value changes.
* @param {number | null} value The new value.
* @param {Event} event The event that triggered the change.
*/
onValueChange: _propTypes.default.func,
/**
* Whether the user should be unable to change the field value.
* @default false
*/
readOnly: _propTypes.default.bool,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: _propTypes.default.oneOfType([_propTypes.default.element, _propTypes.default.func]),
/**
* Whether the user must enter a value before submitting a form.
* @default false
*/
required: _propTypes.default.bool,
/**
* The small step value of the input element when incrementing while the meta key is held. Snaps
* to multiples of this value.
* @default 0.1
*/
smallStep: _propTypes.default.number,
/**
* Amount to increment and decrement with the buttons and arrow keys,
* or to scrub with pointer movement in the scrub area.
* @default 1;
*/
step: _propTypes.default.number,
/**
* The raw numeric value of the field.
*/
value: _propTypes.default.number
} : void 0;