import { useState, type ChangeEvent } from 'react'; import { Box, IconButton, InputBase, Typography } from '@mui/material'; import AddIcon from '@mui/icons-material/Add'; import RemoveIcon from '@mui/icons-material/Remove'; import clsx from 'clsx'; import "./rds-counter.scss"; export interface RdsCounterProps { value?: number; onChange?: (value: number) => void; defaultValue?: number; min?: number; max?: number; step?: number; disabled?: boolean; size?: 'small' | 'medium' | 'large'; showInput?: boolean; titleText?: string; variant?: 'default' | 'compact'; layout?: 'right-side' | 'side-to-side' | 'bottom'; placeholder?: string; controlsClassName?: string; showTitle?: boolean; isMandatory?: boolean; selected?: boolean; state?: 'default' | 'selected' | 'disabled'; } const RdsCounter = ({ value, onChange, defaultValue, min = 0, max = Infinity, step = 1, disabled = false, size = 'medium', showInput = true, titleText, variant = 'default', layout = 'side-to-side', placeholder='00', controlsClassName, showTitle = true, isMandatory = false, selected = false, state = 'default', }: RdsCounterProps) => { const isControlled = typeof value === 'number' && typeof onChange === 'function'; const [internalValue, setInternalValue] = useState( typeof defaultValue === 'number' ? defaultValue : undefined ); const currentValue = isControlled ? value : internalValue; const updateValue = (newValue: number | undefined) => { if (isControlled) { if (typeof newValue === 'number') onChange && onChange(newValue); } else { setInternalValue(newValue); } }; const handleIncrement = () => { const base = typeof currentValue === 'number' ? currentValue : min; const newValue = Math.min(base + step, max); updateValue(newValue); }; const handleDecrement = () => { const base = typeof currentValue === 'number' ? currentValue : min; const newValue = Math.max(base - step, min); updateValue(newValue); }; const handleInputChange = (event: ChangeEvent) => { const raw = event.target.value.trim(); if (raw === '') { updateValue(undefined); return; } const newValue = parseInt(raw, 10); if (!isNaN(newValue) && newValue >= min && newValue <= max) updateValue(newValue); }; const demoSelected = state === 'selected' || selected; const demoDisabled = state === 'disabled' || disabled; const effectiveDisabled = demoDisabled; const containerClasses = clsx( 'rds-counter', `rds-counter--${variant}`, `rds-counter--${size}`, demoSelected && 'rds-counter--selected', demoDisabled && 'rds-counter--disabled', `rds-counter--layout-${layout}`, ); const buildValue = (valueVariant: 'compact' | 'default') => ( showInput ? ( ) : ( {typeof currentValue === 'number' ? currentValue : placeholder} ) ); const decButton = ( ); const incButton = ( = max : false)} size={size === 'small' ? 'small' : 'medium'} className={clsx( 'rds-counter__button', 'rds-counter__button--increment', `rds-counter__button--${variant}`, `rds-counter__button--${size}`, (effectiveDisabled || (typeof currentValue === 'number' && currentValue >= max)) && 'rds-counter__button--disabled', )} > ); const renderControls = () => { const base = clsx( 'rds-counter__controls', `rds-counter__controls--${variant}`, `rds-counter__controls--${size}`, `rds-counter__controls--layout-${layout}`, controlsClassName, ); const valueNode = buildValue(variant === 'compact' ? 'compact' : 'default'); if (layout === 'right-side') { return {valueNode}{decButton}{incButton}; } if (layout === 'bottom') { return ( {valueNode} {decButton}{incButton} ); } return {decButton}{valueNode}{incButton}; }; const controlsElement = renderControls(); return ( {showTitle && typeof titleText === 'string' && titleText && ( {titleText} * )} {controlsElement} ); }; RdsCounter.displayName = 'RdsCounter'; export default RdsCounter;