Getting Started
Components
- Advanced Toast
- ArrowButton
- Arrows
- Avatar
- Backdrop
- Basic Toast
- Breadcrumb
- Button
- CardBackground
- Card
- ChatButton
- Checkbox
- Checkboxes
- Chip
- ChoiceExpander
- Clickable
- Date Field
- Dropdown
- ElasticHeight
- ExpandableCard
- Filter
- FormCheckbox
- Form Control
- Form Label
- HamburgerIcon
- Heading
- Image
- Link
- Loader
- Number Field
- Pagination
- Password
- Phone Field
- Popin
- Portal
- Prose
- Radio Group
- Radio
- Range
- Select
- SidebarLayout
- Spinner
- Switch
- Tabs
- Tag
- TextField
- ValidationMessage
'use client';
import { NumberField } from '@/ui/forms/NumberField';
export function NumberFieldDemo() {
return (
<NumberField
label="Quantity"
description="Select the number of items"
min={0}
max={10}
value={3}
labels={{
increase: 'Increase',
decrease: 'Decrease',
input: 'Input',
}}
/>
);
}
Installation
npx shadcn@latest add https://develop.trident-ui.pro.clubmed/r/number-field.json
Usage
'use client';
import { useState } from 'react';
import { NumberField } from '@/docs/lib/ui/forms/number-field';
export function Component() {
const [quantity, setQuantity] = useState(3);
return (
<NumberField
label="Quantity"
description="Select the number of items"
value={quantity}
onChange={(name, value) => setQuantity(value)}
min={0}
max={10}
required
/>
);
}API Reference
NumberField
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | undefined | The label text displayed above the input field. |
description | string | undefined | Optional helper text displayed below the label to provide additional context. |
id | string | Auto-generated | Unique identifier for the input element. Auto-generated if not provided. |
name | string | Same as id | The name attribute for the input field, used in form submissions. Defaults to the id value. |
value | number | 0 | The current value of the number field. |
onChange | (name: string, value: number) => void | undefined | Callback function invoked when the value changes. Receives the field name and new numeric value. |
min | number | 0 | The minimum allowed value for the input. |
max | number | 10 | The maximum allowed value for the input. |
disabled | boolean | false | If true, disables the input field and all buttons, preventing user interaction. |
required | boolean | false | If true, marks the field as required with an asterisk indicator. |
hideRequiredStar | boolean | false | If true, hides the asterisk indicator even when the field is required. |
validationStatus | "default" | "error" | "success" | "default" | Visual state of the field indicating validation status. |
errorMessage | string | undefined | Error message displayed below the input when validationStatus is set to "error". |
dataTestId | string | "NumberField" | Test identifier for the component, used for testing purposes. |
className | string | undefined | Additional CSS classes to apply to the FormControl container. |
The NumberField also supports all standard HTML input attributes via ComponentPropsWithoutRef<"input">.
Notes
- Range Validation: The component automatically enforces the min/max range. Values entered directly in the input field are only accepted if they fall within the specified range.
- Button Controls: Plus and minus buttons provide an intuitive way to adjust values. These buttons are automatically disabled when the value reaches the min or max boundary.
- Disabled State: When disabled, the input field shows a grey background color (
bg-pearl text-grey), and all interactive elements (input and buttons) become non-functional. - FormControl Integration: NumberField uses the FormControl component internally, inheriting features like labels, descriptions, error messages, and validation states.
- Automatic ID Generation: If no
idprop is provided, a unique ID is automatically generated using React'suseIdhook. - Value Management: The component uses the internal
useValuehook for state management, which handles value formatting and change callbacks. - Fixed Input Size: The numeric input has a fixed width and height (28px × 28px) for consistent appearance.
- Keyboard Support: Users can type values directly into the input field, with validation applied on change.
- Client Component: This component must be used in a client component context (
"use client"). - Accessibility: Both increment and decrement buttons have appropriate
aria-labelattributes ("Increase" and "Decrease") for screen reader support. - Data Attributes: The component includes
data-name="NumberField"anddata-testidattributes for testing and debugging.