Number Input
The number input is a text input component that only allows numerical values and it has controls for incrementing or decrementing them.
Name | Type | Description | Default |
---|---|---|---|
defaultValue | string | Initial value of the input element, only when it is uncontrolled. | - |
value | string | Value of the input element. If undefined, the component will be uncontrolled and the value will be managed internally by the component. | - |
label | string | Text to be placed above the number input. | - |
name | string | Name attribute of the input element. | - |
helperText | string | Helper text to be placed above the number. | - |
placeholder | string | Text to be put as placeholder of the number. | - |
disabled | boolean | If true, the component will be disabled. | false |
optional | boolean | If true, the number will be optional, showing the text '(Optional)' next to the label. Otherwise, the field will be considered required and an error will be passed as a parameter to the onBlur and onChange functions when it has not been filled. | false |
readOnly | boolean | If true, the component will not be mutable, meaning the user can not edit the control. The value won't change when pressing on the up or down arrows and neither on the spin buttons. | false |
prefix | string | Prefix to be placed before the number value. | - |
suffix | string | Suffix to be placed after the number value. | - |
min | number | Minimum value allowed by the number input. If the typed value by the user is lower than min, the onBlur and onChange functions will be called with the current value and an internal error informing that the current value is not correct. If a valid state is reached, the error parameter will not be defined in both events. | - |
max | number | Maximum value allowed by the number input. If the typed value by the user surpasses max, the onBlur and onChange functions will be called with the current value and an internal error informing that the current value is not correct. If a valid state is reached, the error parameter will not be defined in both events. | - |
step | number | The step interval to use when using the up and down arrows to adjust the value. | 1 |
onChange | (val: { value: string; error?: string }) => void | This function will be called when the user types within the input element of the component. An object including the current value and the error (if the value entered is not valid) will be passed to this function. If there is no error, error will not be defined. | - |
onBlur | (val: { value: string; error?: string }) => void | This function will be called when the input element loses the focus. An object including the input value and the error (if the value entered is not valid) will be passed to this function. If there is no error, error will not be defined. | - |
error | string | If it is a defined value and also a truthy string, the component will change its appearance, showing the error below the input component. If the defined value is an empty string, it will reserve a space below the component for a future error, but it would not change its look. In case of being undefined or null, both the appearance and the space for the error message would not be modified. | - |
autocomplete | string | HTML autocomplete attribute. Lets the user specify if any permission the user agent has to provide automated assistance in filling out the input value. Its value must be one of all the possible values of the HTML autocomplete attribute. See MDN for further information. | 'off' |
margin | 'xxsmall' | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'xxlarge' | Margin | Size of the margin to be applied to the component. You can pass an object with 'top', 'bottom', 'left' and 'right' properties in order to specify different margin sizes. | - |
size | 'small' | 'medium' | 'large' | 'fillParent' | Size of the component. | 'medium' |
tabIndex | number | Value of the tabindex attribute. | 0 |
ref | React.Ref<HTMLDivElement> | Reference to the component. | - |
ariaLabel | string | Specifies a string to be used as the name for the number input element when no label is provided. | 'Number input' |
New showControls | boolean | Decides whether the number input has actions to increase or decrease the value, following the defined step. | true |
() => { const [value, setValue] = useState(""); const onChange = ({ value }) => { setValue(value); }; const onBlur = ({ value }) => { setValue(value); }; return ( <DxcInset space="2rem"> <DxcNumberInput label="Enter your age" value={value} onChange={onChange} onBlur={onBlur} /> </DxcInset> ); }
() => { const inputRef = useRef(); const handleSubmit = () => { const input = inputRef.current.getElementsByTagName("input")[0]; console.log(input.value); }; return ( <DxcInset space="2rem"> <DxcFlex direction="column" gap="2rem" alignItems="flex-start"> <DxcNumberInput label="Enter your age" ref={inputRef} defaultValue={10} /> <DxcButton onClick={handleSubmit} label="Submit"></DxcButton> </DxcFlex> </DxcInset> ); }
() => { const [value, setValue] = useState(""); const [errorMessage, setErrorMessage] = useState(); const onChange = ({ value, error }) => { setValue(value); setErrorMessage(error == undefined ? "" : "Error onChange"); }; const onBlur = ({ value, error }) => { setValue(value); setErrorMessage(error == undefined ? "" : "Error onBlur"); }; return ( <DxcInset space="2rem"> <DxcNumberInput label="Enter your age" value={value} onChange={onChange} onBlur={onBlur} error={errorMessage} min={5} max={20} /> </DxcInset> ); }