import Select from "@mui/material/Select"; import type { SelectProps } from "@mui/material/Select"; import React, { forwardRef } from "react"; import { useTheme } from "@mui/material/styles"; import Stack from "@mui/material/Stack"; import { FieldLabel } from "./FieldLabel"; import { ChevronDown } from "lucide-react"; import Typography from "@mui/material/Typography"; interface SelectInputProps extends Omit, "label"> { fieldLabel?: string; required?: boolean; placeholder?: string; helperText?: string; } const SelectInputInner = ( { fieldLabel, required, placeholder, helperText, ...props }: SelectInputProps, ref: React.ForwardedRef ) => { const theme = useTheme(); const emptyPlaceholderColor = theme.palette.text.secondary; const renderValue = (selected: unknown) => { const isMultiple = Boolean((props as { multiple?: boolean }).multiple); const isEmpty = isMultiple ? !Array.isArray(selected) || selected.length === 0 : selected === undefined || selected === null || selected === ""; if (isEmpty && placeholder) { return {placeholder}; } if (isMultiple) { const items: string[] = Array.isArray(selected) ? selected : []; const capitalized = items.map( (item) => item.charAt(0).toUpperCase() + item.slice(1) ); return {capitalized.join(" | ")}; } const nodes = React.Children.toArray(props.children as React.ReactNode); for (const node of nodes) { if (!React.isValidElement(node)) continue; const el = node as React.ReactElement<{ value?: unknown; children?: React.ReactNode; }>; if (el.props?.value === selected) { return (el.props.children ?? selected) as React.ReactNode; } } return selected as React.ReactNode; }; const select = ( {...props} ref={ref} displayEmpty renderValue={renderValue} inputProps={{ ...(props.inputProps || {}), "aria-placeholder": placeholder, }} IconComponent={() => ( )} sx={{ height: "34px", "& .MuiSelect-select": { display: "flex", alignItems: "center", height: "100%", boxSizing: "border-box", }, "& .MuiSelect-icon": { right: theme.spacing(3), }, "& .MuiOutlinedInput-notchedOutline": { borderRadius: theme.shape.borderRadius, borderColor: theme.palette.divider, }, "&:hover .MuiOutlinedInput-notchedOutline": { borderColor: theme.palette.divider, }, ...props.sx, }} /> ); const helper = helperText ? ( {helperText} ) : null; if (fieldLabel || helper) { return ( {fieldLabel && {fieldLabel}} {select} {helper} ); } return select; }; export const SelectInput = forwardRef(SelectInputInner) as ( props: SelectInputProps & { ref?: React.ForwardedRef } ) => React.ReactElement;