"use client"; import { type PropsWithChildren, useState } from "react"; import { useLocale } from "@/hooks/useLocale"; import { cn } from "@/libs/utils"; import { Button } from "../button/button"; import Card from "../card/card"; import LIcon from "../lucid-icon/lucid-icon"; import { Tooltip } from "../tooltip/tooltip"; interface CounterCommonType { min?: number; max?: number; minText?: string | null; maxText?: string | null; } interface ButtonWrapperProps extends PropsWithChildren, CounterCommonType { count: number; } export interface CounterProps extends CounterCommonType { onChange: (value: number) => void; value: number; } const ButtonWrapper = ({ children, min, max, minText, maxText, count, }: ButtonWrapperProps) => { const { t } = useLocale(); const customText = minText ?? maxText; const criteriaValue = min ?? max; const defaultText = min ? t("Value cannot be less than {{value}}", { value: criteriaValue, }) : t("Value cannot be greater than {{value}}", { value: criteriaValue, }); const isTooltip = ((minText || minText === null) && count === min) || ((maxText || maxText === null) && count === max); return isTooltip ? ( {children} ) : ( children ); }; const Counter = (props: CounterProps) => { const { min, max, value = 0, onChange, minText, maxText } = props; const [count, setCount] = useState(value); const isMinDisabled = min === count; const isMaxDisabled = max === count; const handleIncreaseCount = () => { if (max) { if (count + 1 <= max) { setCount(count + 1); onChange(count + 1); } } else { setCount(count + 1); onChange(count + 1); } }; const handleDecreaseCount = () => { if (min !== undefined) { if (count - 1 >= min) { setCount(count - 1); onChange(count - 1); } } else { setCount(count - 1); onChange(count - 1); } }; const isMinDisabledWithText = min ? value <= min && !minText : false; const isMaxDisabledWithText = max ? value >= max && !maxText : false; return (
{count}
); }; export default Counter;