number-input.tsx


Overview

number-input.tsx defines a reusable React functional component named NumberInput. This component provides a user interface for numeric input with increment (+) and decrement (-) buttons flanking a central text input field. It allows users to adjust numeric values either by clicking the buttons or typing directly into the input box, while enforcing numeric-only input. The component supports controlled and uncontrolled usage patterns and customizable styling, including height and additional CSS classes.

This component is typically used in forms or interactive UI elements where numeric input with quick increment/decrement controls is required, such as quantity selectors, rating inputs, or numeric filters.


Component: NumberInput

Signature

const NumberInput: React.FC<NumberInputProps>

Props: NumberInputProps

Prop

Type

Optional

Description

className

string

Yes

Additional CSS classes to apply to the root container for styling.

value

number

Yes

Initial or controlled numeric value of the input.

onChange

(value: number) => void

Yes

Callback invoked whenever the numeric value changes.

height

number or string

Yes

Custom height for the input and buttons, supports px or numeric.

State

Behavior and Methods

useEffect

handleDecrement(): void

handleIncrement(): void

handleChange(e: React.ChangeEvent<HTMLInputElement>): void

handleInput(e: React.ChangeEvent<HTMLInputElement>): void

style: React.CSSProperties

Render Structure

Each button and the input share the same height style for uniform appearance.

Usage Example

import NumberInput from './number-input';

function QuantitySelector() {
  const [quantity, setQuantity] = React.useState(3);

  return (
    <NumberInput
      value={quantity}
      onChange={(val) => setQuantity(val)}
      height={40}
      className="my-custom-class"
    />
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Component Structure and Data Flow

componentDiagram
    component NumberInput {
        +className?: string
        +value?: number
        +onChange?: (number) => void
        +height?: number|string
    }
    component DecrementButton
    component NumericInput
    component IncrementButton

    NumberInput --> DecrementButton : onClick handleDecrement
    NumberInput --> NumericInput : value, onChange handleChange, onInput handleInput
    NumberInput --> IncrementButton : onClick handleIncrement
    NumberInput <.. ParentComponent : props.value, props.onChange

Summary

number-input.tsx implements a clean, accessible, and reusable numeric input React component with increment and decrement buttons and direct numeric input support. It is customizable in styling and supports flexible usage modes, making it ideal for quantity selectors and similar numeric UI elements. The component leverages React state hooks, controlled input patterns, and input validation to deliver a robust user experience.