import React, { useState, useRef, useEffect, CSSProperties } from "react"; import { SelectOption, SelectProps } from "@app/models/components"; import "./select-styles.scss"; import Option from "../option/Option"; const Select: React.FC = ({ id, name, defaultValue, placeholder, value, onChange, disabled, label, options, multiple, onClear, iconStart, externalShowOptions = true, handleSelectAll, }) => { const [showOptions, setShowOptions] = useState(false); const selectRef = useRef(null); const optionsContainerRef = useRef(null); const handleClickOutside = (event: MouseEvent) => { if ( selectRef.current && optionsContainerRef.current && !selectRef.current.contains(event.target as Node) && !optionsContainerRef.current.contains(event.target as Node) ) { setShowOptions(false); } }; const handleSelectAllClick = () => { if (!multiple || !options) return; // Check if "Select All" is already selected const isSelectAllSelected = checkIsSelectAllSelected(); if (isSelectAllSelected) { // If already selected, clear all values onChange?.(name, ""); } else { // Otherwise, select all available options const allValues = options.map((option) => option.value).join(","); onChange?.(name, allValues); } }; const checkIsSelectAllSelected = () => { if (multiple && value && options) { const selectedValues = value.split(","); return options?.every((option) => selectedValues.includes(option.value)); } return false; }; const renderOptions = () => { if (!options?.length) return; const optionsToRender = []; // Render "Select All" option for multiple select if (multiple) { optionsToRender.push(