Files
Checkmate/client/src/Components/inputs/Select.tsx
T

120 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-01-23 12:33:40 -08:00
import Select from "@mui/material/Select";
import type { SelectProps } from "@mui/material/Select";
2026-01-29 22:20:50 +00:00
import React, { forwardRef } from "react";
import { useTheme } from "@mui/material/styles";
2026-01-23 12:33:40 -08:00
import Stack from "@mui/material/Stack";
import { FieldLabel } from "./FieldLabel";
import { ChevronDown } from "lucide-react";
import Typography from "@mui/material/Typography";
interface SelectInputProps<T> extends Omit<SelectProps<T>, "label"> {
fieldLabel?: string;
required?: boolean;
placeholder?: string;
2026-08-21 15:13:34 -07:00
helperText?: string;
2026-01-23 12:33:40 -08:00
}
2026-01-29 22:20:50 +00:00
const SelectInputInner = <T,>(
2026-08-21 15:13:34 -07:00
{ fieldLabel, required, placeholder, helperText, ...props }: SelectInputProps<T>,
2026-01-29 22:20:50 +00:00
ref: React.ForwardedRef<HTMLDivElement>
) => {
2026-01-23 12:33:40 -08:00
const theme = useTheme();
const emptyPlaceholderColor = theme.palette.text.secondary;
2026-01-23 12:33:40 -08:00
2026-01-29 22:20:50 +00:00
const renderValue = (selected: unknown) => {
const isMultiple = Boolean((props as { multiple?: boolean }).multiple);
2026-01-23 12:33:40 -08:00
const isEmpty = isMultiple
? !Array.isArray(selected) || selected.length === 0
: selected === undefined || selected === null || selected === "";
if (isEmpty && placeholder) {
2026-08-21 15:13:34 -07:00
return <Typography color={emptyPlaceholderColor}>{placeholder}</Typography>;
2026-01-23 12:33:40 -08:00
}
if (isMultiple) {
const items: string[] = Array.isArray(selected) ? selected : [];
const capitalized = items.map(
(item) => item.charAt(0).toUpperCase() + item.slice(1)
);
2026-01-29 22:20:50 +00:00
return <Typography>{capitalized.join(" | ")}</Typography>;
2026-01-23 12:33:40 -08:00
}
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 = (
<Select<T>
{...props}
2026-01-29 22:20:50 +00:00
ref={ref}
2026-01-23 12:33:40 -08:00
displayEmpty
renderValue={renderValue}
inputProps={{
...(props.inputProps || {}),
"aria-placeholder": placeholder,
}}
IconComponent={() => (
<ChevronDown
size={18}
strokeWidth={1.5}
style={{ marginRight: theme.spacing(3) }}
/>
)}
sx={{
height: "34px",
2026-02-05 18:53:51 +00:00
"& .MuiSelect-select": {
display: "flex",
alignItems: "center",
height: "100%",
boxSizing: "border-box",
},
2026-01-23 12:33:40 -08:00
"& .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,
}}
/>
);
2026-08-21 15:13:34 -07:00
const helper = helperText ? (
<Typography
variant="caption"
color={props.error ? theme.palette.error.main : theme.palette.text.secondary}
>
{helperText}
</Typography>
) : null;
if (fieldLabel || helper) {
2026-01-23 12:33:40 -08:00
return (
<Stack spacing={theme.spacing(2)}>
2026-08-21 15:13:34 -07:00
{fieldLabel && <FieldLabel required={required}>{fieldLabel}</FieldLabel>}
2026-01-23 12:33:40 -08:00
{select}
2026-08-21 15:13:34 -07:00
{helper}
2026-01-23 12:33:40 -08:00
</Stack>
);
}
return select;
};
2026-01-29 22:20:50 +00:00
export const SelectInput = forwardRef(SelectInputInner) as <T>(
props: SelectInputProps<T> & { ref?: React.ForwardedRef<HTMLDivElement> }
) => React.ReactElement;