"use client"; import { memo, useState, useEffect, useRef } from "react"; import ChipView from "./Chip.view"; import type { ChipProps } from "./Chip.types"; import { normalizeChipPalette, normalizeChipSize, normalizeChipState, } from "../../../../lib/propNormalization"; const ChipContainer = memo( ({ label, state: stateProp = "Unselected", palette: paletteProp = "Default", size: sizeProp = "S", className = "", disabled, onClick, onRemove, onCheck, onClose, ariaLabel, }) => { const state = normalizeChipState(stateProp); const palette = normalizeChipPalette(paletteProp); const size = normalizeChipSize(sizeProp); const isDisabled = disabled ?? state === "disabled"; const isCustom = state === "custom"; // Manage input value for custom state const [inputValue, setInputValue] = useState(""); const inputRef = useRef(null); // Focus input when custom state is active useEffect(() => { if (isCustom && inputRef.current) { inputRef.current.focus(); } }, [isCustom]); const handleCheck = (value: string, event: React.MouseEvent) => { if (onCheck && value.trim()) { onCheck(value.trim(), event); // Reset input after successful check setInputValue(""); } }; const handleClose = (event: React.MouseEvent) => { if (onClose) { onClose(event); } else if (onRemove) { // Fallback to onRemove if onClose not provided onRemove(event); } // Reset input value when closing setInputValue(""); }; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === "Enter" && inputValue.trim() && onCheck) { event.preventDefault(); handleCheck(inputValue.trim(), event as unknown as React.MouseEvent); } else if (event.key === "Escape" && onClose) { event.preventDefault(); handleClose(event as unknown as React.MouseEvent); } }; return ( ); }, ); ChipContainer.displayName = "Chip"; export default ChipContainer;