Enter numeric values such as quantities or prices.
Basic usage
Set bounds with min and max. onChange returns a number, or null when cleared. Partial minus signs and decimal points are preserved while typing; blur or Enter normalizes bounds and precision.
import { Form, FormField, InputNumber, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function InputNumberBasic() {
const [value, setValue] = useState<number | null>(3);
return (
<Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
<FormField label="Quantity">
<InputNumber name="quantity" min={0} max={20} value={value} onChange={setValue} />
</FormField>
<Text tone="muted" role="status">
Quantity: {value ?? 'Empty'}
</Text>
</Form>
);
}
Examples
Units and decimals
Display units with prefix and suffix, and set the step and decimal places with step and precision. Use controls={false} to hide step buttons.
import { Form, FormField, InputNumber } from '@ifhover/leaf-ui';
export function InputNumberUnits() {
return (
<Form layout="vertical">
<FormField label="Amount">
<InputNumber min={0} step={0.1} precision={2} defaultValue={19.9} prefix="$" />
</FormField>
<FormField label="Percentage">
<InputNumber defaultValue={50} min={0} max={100} suffix="%" controls={false} />
</FormField>
</Form>
);
}
Read only and disabled
Read-only fields retain their value without allowing edits; disabled fields cannot be interacted with.
import { Form, FormField, InputNumber } from '@ifhover/leaf-ui';
export function InputNumberStates() {
return (
<Form layout="vertical">
<FormField label="Read only">
<InputNumber defaultValue={12} readOnly />
</FormField>
<FormField label="Disabled">
<InputNumber defaultValue={8} disabled />
</FormField>
</Form>
);
}
Use grouping for localized digit grouping. For custom formats, formatter controls display and parser converts the entered text back into a value. Formatting does not change the canonical value submitted by a native form.
import { Form, FormField, InputNumber } from '@ifhover/leaf-ui';
export function InputNumberFormatting() {
return (
<Form layout="vertical">
<FormField label="Digit grouping">
<InputNumber defaultValue={12345.67} precision={2} grouping />
</FormField>
<FormField label="Custom percentage">
<InputNumber
defaultValue={15}
min={0}
max={100}
formatter={(value) => (value == null ? '' : `${value}%`)}
parser={(text) => text.replace('%', '')}
/>
</FormField>
</Form>
);
}
Exact decimal values
Use stringMode for values beyond JavaScript Number precision. Pass strings for value, defaultValue, step, min and max; onChange returns a string or null. Do not convert these values to Number first.
import { Form, FormField, InputNumber, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function InputNumberExact() {
const [value, setValue] = useState<string | null>('9007199254740993.01');
return (
<Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
<FormField label="Exact amount">
<InputNumber
stringMode
name="amount"
value={value}
onChange={setValue}
step="0.01"
precision={2}
/>
</FormField>
<Text tone="muted" role="status">
Current value: {value ?? 'Empty'}
</Text>
</Form>
);
}
API
Set name to submit the canonical value through FormData. Uncontrolled fields support form reset; reset controlled values in application state. Native attributes such as required and form reach the input. See Form for form integration.
Only mode-specific type differences are listed here; see the main API table for shared props.
Only mode-specific type differences are listed here; see the main API table for shared props.