NumberControl 是一个增强的 HTML input[type="number"] 组件,用于在 WordPress 中处理数字输入。该组件目前处于实验阶段,可能发生重大变更。
import { __experimentalNumberControl as NumberControl } from '@wordpress/components';
const Example = () => {
const [ value, setValue ] = useState( 10 );
return (
<NumberControl
__next40pxDefaultSize
isShiftStepEnabled={ true }
onChange={ setValue }
shiftStep={ 10 }
value={ value }
/>
);
};NumberControl is an enhanced HTML input[type="number"] element.
import { __experimentalNumberControl as NumberControl } from '@wordpress/components';
const Example = () => {
const [ value, setValue ] = useState( 10 );
return (
<NumberControl
__next40pxDefaultSize
isShiftStepEnabled={ true }
onChange={ setValue }
shiftStep={ 10 }
value={ value }
/>
);
};
Determines the drag axis to increment/decrement the value.
Directions: n | e | s | w
StringnIf isDragEnabled is true, this controls the amount of px to have been dragged before the value changes.
Number10The type of spin controls to display. These are buttons that allow the user to
quickly increment and decrement the number.
input controls.String'native'If true, enables mouse drag gesture to increment/decrement the number value. Holding SHIFT while dragging will increase the value by the shiftStep.
BooleanIf true, pressing UP or DOWN along with the SHIFT key will increment the value by the shiftStep value.
BooleantrueIf this property is added, a label will be generated using label property as the content.
StringThe position of the label (top, side, bottom, or edge).
StringThe maximum value allowed.
NumberInfinityThe minimum value allowed.
Number-InfinityCallback fired whenever the value of the input changes.
The callback receives two arguments:
newValue: the new value of the inputextra: an object containing, under the event key, the original browser event.Note that the value received as the first argument of the callback is not guaranteed to be a valid value (e.g. it could be outside of the range defined by the [min, max] props, or it could not match the step). In order to check the value’s validity, check the event.target?.validity.valid property from the callback’s second argument.
(newValue, extra) => voidIf true enforces a valid number within the control’s min/max range. If false allows an empty string as a valid value.
BooleanfalseAmount to increment by when the SHIFT key is held down. This shift value is a multiplier to the step value. For example, if the step value is 5, and shiftStep is 10, each jump would increment/decrement by 50.
Number10Amount by which the value is changed when incrementing/decrementing. It is also a factor in validation as value must be a multiple of step (offset by min, if specified) to be valid. Accepts the special string value any that voids the validation constraint and causes stepping actions to increment/decrement by 1.
Number | "any"1Start opting into the larger default height that will become the default size in a future version.
Booleanfalse