import React, { Children, type ReactElement, type ReactNode } from "react"; import SelectDropdown from "../SelectDropdown"; import SelectOption from "../SelectOption"; import type { SelectOptionData } from "./Select.types"; export interface SelectViewProps { label?: string; placeholder: string; size: "small" | "medium" | "large"; state: "default" | "hover" | "focus"; disabled: boolean; error: boolean; labelVariant: "default" | "horizontal"; className: string; options?: SelectOptionData[]; children?: ReactNode; // Computed props from container selectId: string; labelId: string; isOpen: boolean; selectedValue: string; displayText: string; selectClasses: string; labelClasses: string; containerClasses: string; chevronClasses: string; // Callbacks onButtonClick: () => void; onButtonKeyDown: (e: React.KeyboardEvent) => void; onOptionClick: (value: string, text: string) => void; // Refs selectRef: React.RefObject; menuRef: React.RefObject; // Additional props ariaLabelledby?: string; ariaInvalid?: boolean; } export function SelectView({ label, placeholder, size, disabled, error, labelVariant, options, children, selectId, labelId, isOpen, selectedValue, displayText, selectClasses, labelClasses, containerClasses, chevronClasses, onButtonClick, onButtonKeyDown, onOptionClick, selectRef, menuRef, ariaLabelledby, ariaInvalid, ...props }: SelectViewProps) { return (
{label && ( )}
{isOpen && (
{options && Array.isArray(options) ? options.map((option) => ( onOptionClick(option.value, option.label) } > {option.label} )) : Children.map(children, (child) => { if ( React.isValidElement(child) && child.type === "option" ) { const optionProps = child.props as { value: string; children: ReactNode; }; return ( onOptionClick( optionProps.value, String(optionProps.children), ) } > {optionProps.children} ); } return child; })}
)}
); }