'use client'; import { Button, Icon, LoaderSpinner, Select } from '@theme/components'; import clsx from 'clsx'; import { useMemo } from 'react'; type QuantityDisplayType = 'buttons' | 'dropdown'; type QuantityStyles = { width?: string; height?: string; color?: string; 'background-color'?: string; 'border-color'?: string; 'border-width'?: string; 'border-radius'?: string; 'font-size'?: string; 'font-weight'?: string | number; 'button-color'?: string; 'button-hover-color'?: string; }; interface QuantitySelectorProps { quantity: number; onChange?: (newQuantity: number) => void; min?: number; max?: number; isLoading?: boolean; disabled?: boolean; className?: string; displayType?: QuantityDisplayType; maxDropdownOptions?: number; styles?: QuantityStyles; isSelected?: boolean; onDesignerClick?: () => void; } /** * QuantitySelector Component * * Supports two display modes: * - 'buttons': Plus/minus buttons with number display (default) * - 'dropdown': Select dropdown with quantity options * * Styles can be customized via Theme Editor through the styles prop. */ export const QuantitySelector = ({ quantity, onChange, min = 1, max = 999, isLoading = false, disabled = false, className = '', displayType = 'buttons', maxDropdownOptions = 10, styles = {}, isSelected = false, onDesignerClick }: QuantitySelectorProps) => { const handleDecrease = () => { if (quantity > min && onChange) { onChange(quantity - 1); } }; const handleIncrease = () => { if (quantity < max && onChange) { onChange(quantity + 1); } }; const handleDropdownChange = (e: React.ChangeEvent) => { const newQuantity = parseInt(e.target.value, 10); if (!isNaN(newQuantity) && onChange) { onChange(newQuantity); } }; // Generate dropdown options const dropdownOptions = useMemo(() => { const optionCount = Math.min(max - min + 1, maxDropdownOptions); return Array.from({ length: optionCount }, (_, i) => ({ value: String(min + i), label: String(min + i) })); }, [min, max, maxDropdownOptions]); // Handle designer click for section selection const handleContainerClick = (e: React.MouseEvent) => { if (onDesignerClick) { e.preventDefault(); e.stopPropagation(); onDesignerClick(); } }; if (displayType === 'dropdown') { return (
{isLoading ? (
) : (