pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { useNumberInput as unstable_useNumberInput } from './useNumberInput';
|
||||
export * from './useNumberInput.types';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export { useNumberInput as unstable_useNumberInput } from './useNumberInput';
|
||||
export * from './useNumberInput.types';
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/// <reference types="react" />
|
||||
export declare const NumberInputActionTypes: {
|
||||
readonly clamp: "numberInput:clamp";
|
||||
readonly inputChange: "numberInput:inputChange";
|
||||
readonly increment: "numberInput:increment";
|
||||
readonly decrement: "numberInput:decrement";
|
||||
readonly decrementToMin: "numberInput:decrementToMin";
|
||||
readonly incrementToMax: "numberInput:incrementToMax";
|
||||
readonly resetInputValue: "numberInput:resetInputValue";
|
||||
};
|
||||
interface NumberInputClampAction {
|
||||
type: typeof NumberInputActionTypes.clamp;
|
||||
event: React.FocusEvent<HTMLInputElement>;
|
||||
inputValue: string;
|
||||
}
|
||||
interface NumberInputInputChangeAction {
|
||||
type: typeof NumberInputActionTypes.inputChange;
|
||||
event: React.ChangeEvent<HTMLInputElement>;
|
||||
inputValue: string;
|
||||
}
|
||||
interface NumberInputIncrementAction {
|
||||
type: typeof NumberInputActionTypes.increment;
|
||||
event: React.PointerEvent | React.KeyboardEvent;
|
||||
applyMultiplier: boolean;
|
||||
}
|
||||
interface NumberInputDecrementAction {
|
||||
type: typeof NumberInputActionTypes.decrement;
|
||||
event: React.PointerEvent | React.KeyboardEvent;
|
||||
applyMultiplier: boolean;
|
||||
}
|
||||
interface NumberInputIncrementToMaxAction {
|
||||
type: typeof NumberInputActionTypes.incrementToMax;
|
||||
event: React.KeyboardEvent;
|
||||
}
|
||||
interface NumberInputDecrementToMinAction {
|
||||
type: typeof NumberInputActionTypes.decrementToMin;
|
||||
event: React.KeyboardEvent;
|
||||
}
|
||||
interface NumberInputResetInputValueAction {
|
||||
type: typeof NumberInputActionTypes.resetInputValue;
|
||||
}
|
||||
export type NumberInputAction = NumberInputClampAction | NumberInputInputChangeAction | NumberInputIncrementAction | NumberInputDecrementAction | NumberInputIncrementToMaxAction | NumberInputDecrementToMinAction | NumberInputResetInputValueAction;
|
||||
export {};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
export const NumberInputActionTypes = {
|
||||
clamp: 'numberInput:clamp',
|
||||
inputChange: 'numberInput:inputChange',
|
||||
increment: 'numberInput:increment',
|
||||
decrement: 'numberInput:decrement',
|
||||
decrementToMin: 'numberInput:decrementToMin',
|
||||
incrementToMax: 'numberInput:incrementToMax',
|
||||
resetInputValue: 'numberInput:resetInputValue'
|
||||
};
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import { NumberInputReducerAction, NumberInputState } from './useNumberInput.types';
|
||||
export declare function numberInputReducer(state: NumberInputState, action: NumberInputReducerAction): NumberInputState;
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import { NumberInputActionTypes } from './numberInputAction.types';
|
||||
import { clampStepwise, isNumber } from './utils';
|
||||
function getClampedValues(rawValue, context) {
|
||||
const {
|
||||
min,
|
||||
max,
|
||||
step
|
||||
} = context;
|
||||
const clampedValue = rawValue === null ? null : clampStepwise(rawValue, min, max, step);
|
||||
const newInputValue = clampedValue === null ? '' : String(clampedValue);
|
||||
return {
|
||||
value: clampedValue,
|
||||
inputValue: newInputValue
|
||||
};
|
||||
}
|
||||
function stepValue(state, context, direction, multiplier) {
|
||||
const {
|
||||
value
|
||||
} = state;
|
||||
const {
|
||||
step = 1,
|
||||
min,
|
||||
max
|
||||
} = context;
|
||||
if (isNumber(value)) {
|
||||
return {
|
||||
up: value + (step != null ? step : 1) * multiplier,
|
||||
down: value - (step != null ? step : 1) * multiplier
|
||||
}[direction];
|
||||
}
|
||||
return {
|
||||
up: min != null ? min : 1,
|
||||
down: max != null ? max : -1
|
||||
}[direction];
|
||||
}
|
||||
function handleClamp(state, context, inputValue) {
|
||||
const {
|
||||
getInputValueAsString
|
||||
} = context;
|
||||
const numberValueAsString = getInputValueAsString(inputValue);
|
||||
const intermediateValue = numberValueAsString === '' || numberValueAsString === '-' ? null : parseInt(numberValueAsString, 10);
|
||||
const clampedValues = getClampedValues(intermediateValue, context);
|
||||
return _extends({}, state, clampedValues);
|
||||
}
|
||||
function handleInputChange(state, context, inputValue) {
|
||||
const {
|
||||
getInputValueAsString
|
||||
} = context;
|
||||
const numberValueAsString = getInputValueAsString(inputValue);
|
||||
if (numberValueAsString.match(/^-?\d+?$/) || numberValueAsString === '' || numberValueAsString === '-') {
|
||||
return _extends({}, state, {
|
||||
inputValue: numberValueAsString
|
||||
});
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
// use this for ArrowUp, ArrowDown, button clicks
|
||||
// use this with applyMultiplier: true for PageUp, PageDown, button shift-clicks
|
||||
function handleStep(state, context, applyMultiplier, direction) {
|
||||
const multiplier = applyMultiplier ? context.shiftMultiplier : 1;
|
||||
const newValue = stepValue(state, context, direction, multiplier);
|
||||
const clampedValues = getClampedValues(newValue, context);
|
||||
return _extends({}, state, clampedValues);
|
||||
}
|
||||
function handleToMinOrMax(state, context, to) {
|
||||
const newValue = context[to];
|
||||
if (!isNumber(newValue)) {
|
||||
return state;
|
||||
}
|
||||
return _extends({}, state, {
|
||||
value: newValue,
|
||||
inputValue: String(newValue)
|
||||
});
|
||||
}
|
||||
export function numberInputReducer(state, action) {
|
||||
const {
|
||||
type,
|
||||
context
|
||||
} = action;
|
||||
switch (type) {
|
||||
case NumberInputActionTypes.clamp:
|
||||
return handleClamp(state, context, action.inputValue);
|
||||
case NumberInputActionTypes.inputChange:
|
||||
return handleInputChange(state, context, action.inputValue);
|
||||
case NumberInputActionTypes.increment:
|
||||
return handleStep(state, context, action.applyMultiplier, 'up');
|
||||
case NumberInputActionTypes.decrement:
|
||||
return handleStep(state, context, action.applyMultiplier, 'down');
|
||||
case NumberInputActionTypes.incrementToMax:
|
||||
return handleToMinOrMax(state, context, 'max');
|
||||
case NumberInputActionTypes.decrementToMin:
|
||||
return handleToMinOrMax(state, context, 'min');
|
||||
case NumberInputActionTypes.resetInputValue:
|
||||
return _extends({}, state, {
|
||||
inputValue: String(state.value)
|
||||
});
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/unstable_useNumberInput/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import { UseNumberInputParameters, UseNumberInputReturnValue } from './useNumberInput.types';
|
||||
export declare function getInputValueAsString(v: string): string;
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Number Input](https://mui.com/base-ui/react-number-input/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useNumberInput API](https://mui.com/base-ui/react-number-input/hooks-api/#use-number-input)
|
||||
*/
|
||||
export declare function useNumberInput(parameters: UseNumberInputParameters): UseNumberInputReturnValue;
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage";
|
||||
import * as React from 'react';
|
||||
import { unstable_useForkRef as useForkRef, unstable_useId as useId } from '@mui/utils';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
import { useControllableReducer } from '../utils/useControllableReducer';
|
||||
import { useFormControlContext } from '../FormControl';
|
||||
import { NumberInputActionTypes } from './numberInputAction.types';
|
||||
import { numberInputReducer } from './numberInputReducer';
|
||||
import { isNumber } from './utils';
|
||||
const STEP_KEYS = ['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown'];
|
||||
const SUPPORTED_KEYS = [...STEP_KEYS, 'Home', 'End'];
|
||||
export function getInputValueAsString(v) {
|
||||
return v ? String(v.trim()) : String(v);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Number Input](https://mui.com/base-ui/react-number-input/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useNumberInput API](https://mui.com/base-ui/react-number-input/hooks-api/#use-number-input)
|
||||
*/
|
||||
export function useNumberInput(parameters) {
|
||||
var _ref;
|
||||
const {
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
shiftMultiplier = 10,
|
||||
defaultValue: defaultValueProp,
|
||||
disabled: disabledProp = false,
|
||||
error: errorProp = false,
|
||||
onBlur,
|
||||
onInputChange,
|
||||
onFocus,
|
||||
onChange,
|
||||
required: requiredProp = false,
|
||||
readOnly: readOnlyProp = false,
|
||||
value: valueProp,
|
||||
inputRef: inputRefProp,
|
||||
inputId: inputIdProp,
|
||||
componentName = 'useNumberInput'
|
||||
} = parameters;
|
||||
|
||||
// TODO: make it work with FormControl
|
||||
const formControlContext = useFormControlContext();
|
||||
const {
|
||||
current: isControlled
|
||||
} = React.useRef(valueProp != null);
|
||||
const handleInputRefWarning = React.useCallback(instance => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (instance && instance.nodeName !== 'INPUT' && !instance.focus) {
|
||||
console.error(['MUI: You have provided a `slots.input` to the input component', 'that does not correctly handle the `ref` prop.', 'Make sure the `ref` prop is called with a HTMLInputElement.'].join('\n'));
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
const inputRef = React.useRef(null);
|
||||
const handleInputRef = useForkRef(inputRef, inputRefProp, handleInputRefWarning);
|
||||
const inputId = useId(inputIdProp);
|
||||
const [focused, setFocused] = React.useState(false);
|
||||
const handleStateChange = React.useCallback((event, field, fieldValue, reason) => {
|
||||
if (field === 'value' && typeof fieldValue !== 'string') {
|
||||
switch (reason) {
|
||||
// only a blur event will dispatch `numberInput:clamp`
|
||||
case 'numberInput:clamp':
|
||||
onChange == null || onChange(event, fieldValue);
|
||||
break;
|
||||
case 'numberInput:increment':
|
||||
case 'numberInput:decrement':
|
||||
case 'numberInput:incrementToMax':
|
||||
case 'numberInput:decrementToMin':
|
||||
onChange == null || onChange(event, fieldValue);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}, [onChange]);
|
||||
const numberInputActionContext = React.useMemo(() => {
|
||||
return {
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
shiftMultiplier,
|
||||
getInputValueAsString
|
||||
};
|
||||
}, [min, max, step, shiftMultiplier]);
|
||||
const initialValue = (_ref = valueProp != null ? valueProp : defaultValueProp) != null ? _ref : null;
|
||||
const initialState = {
|
||||
value: initialValue,
|
||||
inputValue: initialValue ? String(initialValue) : ''
|
||||
};
|
||||
const controlledState = React.useMemo(() => ({
|
||||
value: valueProp
|
||||
}), [valueProp]);
|
||||
const [state, dispatch] = useControllableReducer({
|
||||
reducer: numberInputReducer,
|
||||
controlledProps: controlledState,
|
||||
initialState,
|
||||
onStateChange: handleStateChange,
|
||||
actionContext: React.useMemo(() => numberInputActionContext, [numberInputActionContext]),
|
||||
componentName
|
||||
});
|
||||
const {
|
||||
value,
|
||||
inputValue
|
||||
} = state;
|
||||
React.useEffect(() => {
|
||||
if (!formControlContext && disabledProp && focused) {
|
||||
setFocused(false);
|
||||
onBlur == null || onBlur();
|
||||
}
|
||||
}, [formControlContext, disabledProp, focused, onBlur]);
|
||||
React.useEffect(() => {
|
||||
if (isControlled && isNumber(value)) {
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.resetInputValue
|
||||
});
|
||||
}
|
||||
}, [value, dispatch, isControlled]);
|
||||
const createHandleFocus = otherHandlers => event => {
|
||||
var _otherHandlers$onFocu;
|
||||
(_otherHandlers$onFocu = otherHandlers.onFocus) == null || _otherHandlers$onFocu.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented || event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
if (formControlContext && formControlContext.onFocus) {
|
||||
var _formControlContext$o;
|
||||
formControlContext == null || (_formControlContext$o = formControlContext.onFocus) == null || _formControlContext$o.call(formControlContext);
|
||||
}
|
||||
setFocused(true);
|
||||
};
|
||||
const createHandleInputChange = otherHandlers => event => {
|
||||
var _formControlContext$o2, _otherHandlers$onInpu;
|
||||
if (!isControlled && event.target === null) {
|
||||
throw new Error(process.env.NODE_ENV !== "production" ? `MUI: Expected valid input target. Did you use a custom \`slots.input\` and forget to forward refs? See https://mui.com/r/input-component-ref-interface for more info.` : _formatMuiErrorMessage(17));
|
||||
}
|
||||
formControlContext == null || (_formControlContext$o2 = formControlContext.onChange) == null || _formControlContext$o2.call(formControlContext, event);
|
||||
(_otherHandlers$onInpu = otherHandlers.onInputChange) == null || _otherHandlers$onInpu.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented || event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.inputChange,
|
||||
event,
|
||||
inputValue: event.currentTarget.value
|
||||
});
|
||||
};
|
||||
const createHandleBlur = otherHandlers => event => {
|
||||
var _otherHandlers$onBlur;
|
||||
formControlContext == null || formControlContext.onBlur();
|
||||
(_otherHandlers$onBlur = otherHandlers.onBlur) == null || _otherHandlers$onBlur.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented || event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.clamp,
|
||||
event,
|
||||
inputValue: event.currentTarget.value
|
||||
});
|
||||
setFocused(false);
|
||||
};
|
||||
const createHandleClick = otherHandlers => event => {
|
||||
var _otherHandlers$onClic;
|
||||
(_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented || event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
if (inputRef.current && event.currentTarget === event.target) {
|
||||
inputRef.current.focus();
|
||||
}
|
||||
};
|
||||
const handleStep = direction => event => {
|
||||
const applyMultiplier = Boolean(event.shiftKey);
|
||||
const actionType = {
|
||||
up: NumberInputActionTypes.increment,
|
||||
down: NumberInputActionTypes.decrement
|
||||
}[direction];
|
||||
dispatch({
|
||||
type: actionType,
|
||||
event,
|
||||
applyMultiplier
|
||||
});
|
||||
};
|
||||
const createHandleKeyDown = otherHandlers => event => {
|
||||
var _otherHandlers$onKeyD;
|
||||
(_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented || event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
// this prevents unintended page scrolling
|
||||
if (SUPPORTED_KEYS.includes(event.key)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
switch (event.key) {
|
||||
case 'ArrowUp':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.increment,
|
||||
event,
|
||||
applyMultiplier: !!event.shiftKey
|
||||
});
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.decrement,
|
||||
event,
|
||||
applyMultiplier: !!event.shiftKey
|
||||
});
|
||||
break;
|
||||
case 'PageUp':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.increment,
|
||||
event,
|
||||
applyMultiplier: true
|
||||
});
|
||||
break;
|
||||
case 'PageDown':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.decrement,
|
||||
event,
|
||||
applyMultiplier: true
|
||||
});
|
||||
break;
|
||||
case 'Home':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.incrementToMax,
|
||||
event
|
||||
});
|
||||
break;
|
||||
case 'End':
|
||||
dispatch({
|
||||
type: NumberInputActionTypes.decrementToMin,
|
||||
event
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
const getRootProps = (externalProps = {}) => {
|
||||
const propsEventHandlers = extractEventHandlers(parameters, [
|
||||
// these are handled by the input slot
|
||||
'onBlur', 'onInputChange', 'onFocus', 'onChange']);
|
||||
const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps));
|
||||
return _extends({}, externalProps, externalEventHandlers, {
|
||||
onClick: createHandleClick(externalEventHandlers)
|
||||
});
|
||||
};
|
||||
const getInputProps = (externalProps = {}) => {
|
||||
var _ref2;
|
||||
const propsEventHandlers = {
|
||||
onBlur,
|
||||
onFocus,
|
||||
// onChange from normal props is the custom onChange so we ignore it here
|
||||
onChange: onInputChange
|
||||
};
|
||||
const externalEventHandlers = _extends({}, propsEventHandlers, extractEventHandlers(externalProps, [
|
||||
// onClick is handled by the root slot
|
||||
'onClick'
|
||||
// do not ignore 'onInputChange', we want slotProps.input.onInputChange to enter the DOM and throw
|
||||
]));
|
||||
const mergedEventHandlers = _extends({}, externalEventHandlers, {
|
||||
onFocus: createHandleFocus(externalEventHandlers),
|
||||
// slotProps.onChange is renamed to onInputChange and passed to createHandleInputChange
|
||||
onChange: createHandleInputChange(_extends({}, externalEventHandlers, {
|
||||
onInputChange: externalEventHandlers.onChange
|
||||
})),
|
||||
onBlur: createHandleBlur(externalEventHandlers),
|
||||
onKeyDown: createHandleKeyDown(externalEventHandlers)
|
||||
});
|
||||
const displayValue = (_ref2 = focused ? inputValue : value) != null ? _ref2 : '';
|
||||
|
||||
// get rid of slotProps.input.onInputChange before returning to prevent it from entering the DOM
|
||||
// if it was passed, it will be in mergedEventHandlers and throw
|
||||
delete externalProps.onInputChange;
|
||||
return _extends({
|
||||
type: 'text',
|
||||
id: inputId,
|
||||
'aria-invalid': errorProp || undefined,
|
||||
defaultValue: undefined,
|
||||
value: displayValue,
|
||||
'aria-valuenow': displayValue,
|
||||
'aria-valuetext': String(displayValue),
|
||||
'aria-valuemin': min,
|
||||
'aria-valuemax': max,
|
||||
autoComplete: 'off',
|
||||
autoCorrect: 'off',
|
||||
spellCheck: 'false',
|
||||
required: requiredProp,
|
||||
readOnly: readOnlyProp,
|
||||
'aria-disabled': disabledProp,
|
||||
disabled: disabledProp
|
||||
}, externalProps, {
|
||||
ref: handleInputRef
|
||||
}, mergedEventHandlers);
|
||||
};
|
||||
const handleStepperButtonMouseDown = event => {
|
||||
event.preventDefault();
|
||||
if (inputRef.current) {
|
||||
inputRef.current.focus();
|
||||
}
|
||||
};
|
||||
const stepperButtonCommonProps = {
|
||||
'aria-controls': inputId,
|
||||
tabIndex: -1
|
||||
};
|
||||
const isIncrementDisabled = disabledProp || (isNumber(value) ? value >= (max != null ? max : Number.MAX_SAFE_INTEGER) : false);
|
||||
const getIncrementButtonProps = (externalProps = {}) => {
|
||||
return _extends({}, externalProps, stepperButtonCommonProps, {
|
||||
disabled: isIncrementDisabled,
|
||||
'aria-disabled': isIncrementDisabled,
|
||||
onMouseDown: handleStepperButtonMouseDown,
|
||||
onClick: handleStep('up')
|
||||
});
|
||||
};
|
||||
const isDecrementDisabled = disabledProp || (isNumber(value) ? value <= (min != null ? min : Number.MIN_SAFE_INTEGER) : false);
|
||||
const getDecrementButtonProps = (externalProps = {}) => {
|
||||
return _extends({}, externalProps, stepperButtonCommonProps, {
|
||||
disabled: isDecrementDisabled,
|
||||
'aria-disabled': isDecrementDisabled,
|
||||
onMouseDown: handleStepperButtonMouseDown,
|
||||
onClick: handleStep('down')
|
||||
});
|
||||
};
|
||||
return {
|
||||
disabled: disabledProp,
|
||||
error: errorProp,
|
||||
focused,
|
||||
formControlContext,
|
||||
getInputProps,
|
||||
getIncrementButtonProps,
|
||||
getDecrementButtonProps,
|
||||
getRootProps,
|
||||
required: requiredProp,
|
||||
value,
|
||||
inputValue,
|
||||
isIncrementDisabled,
|
||||
isDecrementDisabled
|
||||
};
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
import * as React from 'react';
|
||||
import { FormControlState } from '../FormControl';
|
||||
import { NumberInputAction } from './numberInputAction.types';
|
||||
import { ActionWithContext } from '../utils/useControllableReducer.types';
|
||||
export type StepDirection = 'up' | 'down';
|
||||
/**
|
||||
* The internal state of the NumberInput.
|
||||
* Modify via the reducer only.
|
||||
*/
|
||||
export interface NumberInputState {
|
||||
/**
|
||||
* The clamped `value` of the `input` element.
|
||||
*/
|
||||
value: number | null;
|
||||
/**
|
||||
* The dirty `value` of the `input` element when it is in focus.
|
||||
*/
|
||||
inputValue: string;
|
||||
}
|
||||
/**
|
||||
* Additional props passed to the number input reducer actions.
|
||||
*/
|
||||
export type NumberInputActionContext = {
|
||||
min?: number;
|
||||
max?: number;
|
||||
step?: number;
|
||||
shiftMultiplier: number;
|
||||
/**
|
||||
* A function that parses the raw input value
|
||||
*/
|
||||
getInputValueAsString: (val: string) => string;
|
||||
};
|
||||
export type NumberInputReducerAction = ActionWithContext<NumberInputAction, NumberInputActionContext>;
|
||||
export interface UseNumberInputParameters {
|
||||
/**
|
||||
* The minimum value.
|
||||
*/
|
||||
min?: number;
|
||||
/**
|
||||
* The maximum value.
|
||||
*/
|
||||
max?: number;
|
||||
/**
|
||||
* The amount that the value changes on each increment or decrement.
|
||||
*/
|
||||
step?: number;
|
||||
/**
|
||||
* Multiplier applied to `step` if the shift key is held while incrementing
|
||||
* or decrementing the value. Defaults to `10`.
|
||||
*/
|
||||
shiftMultiplier?: number;
|
||||
/**
|
||||
* The default value. Use when the component is not controlled.
|
||||
*/
|
||||
defaultValue?: number | null;
|
||||
/**
|
||||
* If `true`, the component is disabled.
|
||||
* The prop defaults to the value (`false`) inherited from the parent FormControl component.
|
||||
*/
|
||||
disabled?: boolean;
|
||||
/**
|
||||
* If `true`, the `input` will indicate an error by setting the `aria-invalid` attribute.
|
||||
* The prop defaults to the value (`false`) inherited from the parent FormControl component.
|
||||
*/
|
||||
error?: boolean;
|
||||
onBlur?: (event?: React.FocusEvent<HTMLInputElement>) => void;
|
||||
onClick?: React.MouseEventHandler;
|
||||
/**
|
||||
* Callback fired when the `input` value changes after each keypress, before clamping is applied.
|
||||
* Note that `event.target.value` may contain values that fall outside of `min` and `max` or
|
||||
* are otherwise "invalid".
|
||||
*
|
||||
* @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
|
||||
*/
|
||||
onInputChange?: React.ChangeEventHandler<HTMLInputElement>;
|
||||
onFocus?: React.FocusEventHandler;
|
||||
/**
|
||||
* Callback fired after the value is clamped and changes - when the `input` is blurred or when
|
||||
* the stepper buttons are triggered.
|
||||
* Called with `undefined` when the value is unset.
|
||||
*
|
||||
* @param {React.FocusEvent<HTMLInputElement>|React.PointerEvent|React.KeyboardEvent} event The event source of the callback
|
||||
* @param {number|undefined} value The new value of the component
|
||||
*/
|
||||
onChange?: (event: React.FocusEvent<HTMLInputElement> | React.PointerEvent | React.KeyboardEvent, value: number | null) => void;
|
||||
/**
|
||||
* The `id` attribute of the input element.
|
||||
*/
|
||||
inputId?: string;
|
||||
/**
|
||||
* The ref of the input element.
|
||||
*/
|
||||
inputRef?: React.Ref<HTMLInputElement>;
|
||||
/**
|
||||
* If `true`, the `input` element is required.
|
||||
* The prop defaults to the value (`false`) inherited from the parent FormControl component.
|
||||
*/
|
||||
required?: boolean;
|
||||
/**
|
||||
* If `true`, the `input` element becomes read-only. The stepper buttons remain active,
|
||||
* with the addition that they are now keyboard focusable.
|
||||
* @default false
|
||||
*/
|
||||
readOnly?: boolean;
|
||||
/**
|
||||
* The current value. Use when the component is controlled.
|
||||
* @default null
|
||||
*/
|
||||
value?: number | null;
|
||||
/**
|
||||
* The name of the component using useNumberInput.
|
||||
* For debugging purposes.
|
||||
* @default 'useNumberInput'
|
||||
*/
|
||||
componentName?: string;
|
||||
}
|
||||
export interface UseNumberInputRootSlotOwnProps {
|
||||
onClick: React.MouseEventHandler | undefined;
|
||||
}
|
||||
export type UseNumberInputRootSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseNumberInputRootSlotOwnProps | 'onBlur' | 'onInputChange' | 'onFocus'> & UseNumberInputRootSlotOwnProps;
|
||||
export interface UseNumberInputInputSlotOwnProps {
|
||||
defaultValue: number | undefined;
|
||||
id: string | undefined;
|
||||
ref: React.RefCallback<HTMLInputElement> | null;
|
||||
value: number | undefined;
|
||||
role?: React.AriaRole;
|
||||
'aria-disabled': React.AriaAttributes['aria-disabled'];
|
||||
'aria-valuemax': React.AriaAttributes['aria-valuemax'];
|
||||
'aria-valuemin': React.AriaAttributes['aria-valuemin'];
|
||||
'aria-valuenow': React.AriaAttributes['aria-valuenow'];
|
||||
'aria-valuetext': React.AriaAttributes['aria-valuetext'];
|
||||
tabIndex?: number;
|
||||
onBlur: React.FocusEventHandler;
|
||||
onChange: React.ChangeEventHandler<HTMLInputElement>;
|
||||
onFocus: React.FocusEventHandler;
|
||||
required: boolean;
|
||||
disabled: boolean;
|
||||
}
|
||||
export type UseNumberInputInputSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseNumberInputInputSlotOwnProps> & UseNumberInputInputSlotOwnProps;
|
||||
export interface UseNumberInputIncrementButtonSlotOwnProps {
|
||||
'aria-controls': React.AriaAttributes['aria-controls'];
|
||||
'aria-disabled': React.AriaAttributes['aria-disabled'];
|
||||
disabled: boolean;
|
||||
tabIndex?: number;
|
||||
}
|
||||
export type UseNumberInputIncrementButtonSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseNumberInputIncrementButtonSlotOwnProps> & UseNumberInputIncrementButtonSlotOwnProps;
|
||||
export interface UseNumberInputDecrementButtonSlotOwnProps {
|
||||
'aria-controls': React.AriaAttributes['aria-controls'];
|
||||
'aria-disabled': React.AriaAttributes['aria-disabled'];
|
||||
disabled: boolean;
|
||||
tabIndex?: number;
|
||||
}
|
||||
export type UseNumberInputDecrementButtonSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseNumberInputDecrementButtonSlotOwnProps> & UseNumberInputDecrementButtonSlotOwnProps;
|
||||
export interface UseNumberInputReturnValue {
|
||||
/**
|
||||
* If `true`, the component will be disabled.
|
||||
* @default false
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* If `true`, the `input` will indicate an error by setting the `aria-invalid` attribute.
|
||||
* @default false
|
||||
*/
|
||||
error: boolean;
|
||||
/**
|
||||
* If `true`, the `input` will be focused.
|
||||
* @default false
|
||||
*/
|
||||
focused: boolean;
|
||||
/**
|
||||
* Return value from the `useFormControlContext` hook.
|
||||
*/
|
||||
formControlContext: FormControlState | undefined;
|
||||
/**
|
||||
* Resolver for the decrement button slot's props.
|
||||
* @param externalProps props for the decrement button slot
|
||||
* @returns props that should be spread on the decrement button slot
|
||||
*/
|
||||
getDecrementButtonProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseNumberInputDecrementButtonSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Resolver for the increment button slot's props.
|
||||
* @param externalProps props for the increment button slot
|
||||
* @returns props that should be spread on the increment button slot
|
||||
*/
|
||||
getIncrementButtonProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseNumberInputIncrementButtonSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Resolver for the input slot's props.
|
||||
* @param externalProps props for the input slot
|
||||
* @returns props that should be spread on the input slot
|
||||
*/
|
||||
getInputProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseNumberInputInputSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Resolver for the root slot's props.
|
||||
* @param externalProps props for the root slot
|
||||
* @returns props that should be spread on the root slot
|
||||
*/
|
||||
getRootProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseNumberInputRootSlotProps<ExternalProps>;
|
||||
/**
|
||||
* If `true`, the `input` will indicate that it's required.
|
||||
* @default false
|
||||
*/
|
||||
required: boolean;
|
||||
/**
|
||||
* The clamped `value` of the `input` element.
|
||||
*/
|
||||
value: number | null;
|
||||
/**
|
||||
* The dirty `value` of the `input` element when it is in focus.
|
||||
*/
|
||||
inputValue: string;
|
||||
/**
|
||||
* If `true`, the increment button will be disabled.
|
||||
* e.g. when the `value` is already at `max`
|
||||
* @default false
|
||||
*/
|
||||
isIncrementDisabled: boolean;
|
||||
/**
|
||||
* If `true`, the decrement button will be disabled.
|
||||
* e.g. when the `value` is already at `min`
|
||||
* @default false
|
||||
*/
|
||||
isDecrementDisabled: boolean;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare function clampStepwise(val: number, min?: number, max?: number, stepProp?: number): number;
|
||||
export declare function isNumber(val: unknown): val is number;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { clamp } from '@mui/utils';
|
||||
export function clampStepwise(val, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER, stepProp = NaN) {
|
||||
if (Number.isNaN(stepProp)) {
|
||||
return clamp(val, min, max);
|
||||
}
|
||||
const step = stepProp || 1;
|
||||
const remainder = val % step;
|
||||
const positivity = Math.sign(remainder);
|
||||
if (Math.abs(remainder) > step / 2) {
|
||||
return clamp(val + positivity * (step - Math.abs(remainder)), min, max);
|
||||
}
|
||||
return clamp(val - positivity * Math.abs(remainder), min, max);
|
||||
}
|
||||
export function isNumber(val) {
|
||||
return typeof val === 'number' && !Number.isNaN(val) && Number.isFinite(val);
|
||||
}
|
||||
Reference in new issue
Block a user