import type { RefAttributes } from 'react'; import React, { useState } from 'react'; import { useObjectRef, VisuallyHidden } from 'react-aria'; import classNames from 'classnames'; import { useTranslations } from '@shoptet/ui-core-web'; import type { Size } from '../../../types'; import { dictionary } from '../NumberInput/dictionary'; import { NumberInput } from '../NumberInput/NumberInput'; import type { CommonInputProps } from '../types'; export interface CounterInputProps extends RefAttributes, Omit, 'value' | 'defaultValue' | 'loading'> { /** * The controlled value of the counter input. */ value?: number | null; /** * The uncontrolled default value of the counter input. */ defaultValue?: number | null; /** * Minimum allowed value. */ min?: number | string; /** * Maximum allowed value. */ max?: number | string; /** * Step value for increment/decrement. * @default 1 */ step?: number; /** * Width of the number input. * @default 'xs' */ width?: Extract; } export const CounterInput = ({ value, defaultValue, min, max, step = 1, width = 'xs', disabled, error, warning, onChange, onBlur, onFocus, label, ref: forwardedRef, ...props }: CounterInputProps) => { const inputRef = useObjectRef(forwardedRef); const translations = useTranslations(dictionary); const [announcement, setAnnouncement] = useState(''); const handleChange = (next: number | null, event?: React.ChangeEvent) => { onChange?.(next, event); if (event === undefined && typeof next === 'number') { setAnnouncement(translations.currentValueIs(label, next)); } }; return ( {announcement} ); }; CounterInput.displayName = 'CounterInput';