Files
tansy-ui/src/components/NumberInput.tsx
T
thekhalidsha 23e67006d3 Make every component and the landing page responsive (v0.3.1)
Every story and the landing page now fit from a 320px phone to a wide
desktop without scrolling sideways.

- Inputs in flex rows (Input, FeaturedInput, NumberInput, Combobox) shrink
  instead of holding their native width
- Tab rows and ToggleGroup scroll within themselves on narrow screens;
  vertical tabs stack above their panel on phones
- Positioned popups cap at Base UI's --available-width
- Landing page: tablet header no longer crowds; code sample fits at 1024px
- Storybook a11y panel skips page-level rules that can't apply to a story
- check-stories.mjs gains --width and fails on horizontal overflow; all
  423 stories pass at 320, 375, 768 and desktop widths in both modes
2026-09-28 22:47:26 +05:30

130 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NumberField } from '@base-ui/react/number-field'
import { Minus, Plus } from 'lucide-react'
import type { ReactNode } from 'react'
import { cn } from '../lib/cn'
import { controlBox, controlHeight, controlPaddingX, iconSize, type Size } from '../lib/styles'
import { Field } from './Field'
import { FIELD_BOX_DISABLED_INPUT, FIELD_BOX_RING_INPUT } from './featuredinput.tokens'
import { NUMBER_ADDON, NUMBER_STEPPER, NUMBER_STEPPER_WIDTH } from './numberinput.tokens'
export type NumberInputProps = {
/** Visible label, linked to the input. */
label?: ReactNode
/** Help text under the input. */
description?: ReactNode
/** Error message; marks the input invalid and replaces `description`. */
error?: ReactNode
/** Shows the required mark and requires a value. */
required?: boolean
disabled?: boolean
/** Shows the value but blocks changes. */
readOnly?: boolean
size?: Size
/** Lowest allowed value. Stepping clamps to it. */
min?: number
/** Highest allowed value. Stepping clamps to it. */
max?: number
/** Amount the buttons and arrow keys change the value by. Shift steps by 10×. */
step?: number
value?: number | null
defaultValue?: number | null
/** Fires with the new number, or `null` when the input is cleared. */
onValueChange?: (value: number | null) => void
/** How the value is displayed, e.g. `{ style: 'currency', currency: 'USD' }`. */
format?: Intl.NumberFormatOptions
/** Locale for formatting and parsing. Defaults to the user's. */
locale?: Intl.LocalesArgument
/** Text or element attached before the input, such as `$`. */
startAddon?: ReactNode
/** Text or element attached after the input, such as `kg`. */
endAddon?: ReactNode
/** Identifies the field in a form. */
name?: string
/** Classes for the outer field wrapper. */
className?: string
/** Classes for the input itself. */
inputClassName?: string
}
/**
* A number field on Base UI NumberField: typing is parsed and formatted,
* arrow keys and the − / + buttons step the value, and min / max clamp it.
*/
export function NumberInput({
label,
description,
error,
required,
disabled,
readOnly,
size = 'md',
min,
max,
step = 1,
value,
defaultValue,
onValueChange,
format,
locale,
startAddon,
endAddon,
name,
className,
inputClassName,
}: NumberInputProps) {
return (
<Field
label={label}
description={description}
error={error}
required={required}
disabled={disabled}
name={name}
className={className}
>
<NumberField.Root
value={value}
defaultValue={defaultValue ?? undefined}
onValueChange={onValueChange ? (next) => onValueChange(next) : undefined}
min={min}
max={max}
step={step}
format={format}
locale={locale}
required={required}
disabled={disabled}
readOnly={readOnly}
className="w-full"
>
<NumberField.Group
className={cn(
'flex w-full items-stretch overflow-hidden',
controlHeight[size],
iconSize[size],
controlBox,
'has-[[aria-invalid=true]]:border-danger-500',
FIELD_BOX_DISABLED_INPUT,
FIELD_BOX_RING_INPUT,
)}
>
{startAddon != null && <span className={cn(NUMBER_ADDON, 'border-r')}>{startAddon}</span>}
<NumberField.Input
className={cn(
'w-0 min-w-0 flex-1 bg-transparent text-fg tabular-nums outline-none placeholder:text-fg-subtle disabled:cursor-not-allowed',
controlPaddingX[size],
inputClassName,
)}
/>
{endAddon != null && <span className={cn(NUMBER_ADDON, 'border-l')}>{endAddon}</span>}
<NumberField.Decrement aria-label="Decrease" className={cn(NUMBER_STEPPER, NUMBER_STEPPER_WIDTH[size])}>
<Minus aria-hidden="true" />
</NumberField.Decrement>
<NumberField.Increment aria-label="Increase" className={cn(NUMBER_STEPPER, NUMBER_STEPPER_WIDTH[size])}>
<Plus aria-hidden="true" />
</NumberField.Increment>
</NumberField.Group>
</NumberField.Root>
</Field>
)
}