fix create more specific component
This commit is contained in:
1 parent
b9afca3608
commit
104cf194ac
1 file changed
+66
-59
@@ -1,81 +1,88 @@
|
|||||||
import { Typography, Select } from "@mui/material";
|
import { Typography, Select } from "@mui/material";
|
||||||
import MenuItem from "@mui/material/MenuItem";
|
import MenuItem from "@mui/material/MenuItem";
|
||||||
import { forwardRef } from "react";
|
|
||||||
import type { SelectProps } from "@mui/material/Select";
|
import type { SelectProps } from "@mui/material/Select";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
||||||
type ItemId = string | number;
|
|
||||||
|
export const SelectInput: React.FC<SelectProps> = ({ ...props }) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
return (
|
||||||
|
<Select
|
||||||
|
{...props}
|
||||||
|
sx={{
|
||||||
|
height: "34px",
|
||||||
|
"& .MuiOutlinedInput-notchedOutline": {
|
||||||
|
borderRadius: theme.shape.borderRadius,
|
||||||
|
borderColor: theme.palette.primary.lowContrast,
|
||||||
|
},
|
||||||
|
"&:hover .MuiOutlinedInput-notchedOutline": {
|
||||||
|
borderColor: theme.palette.primary.lowContrast,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
type ItemTypes = string | number;
|
||||||
interface SelectItem {
|
interface SelectItem {
|
||||||
_id: ItemId;
|
_id: ItemTypes;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
export type CustomSelectProps = SelectProps<ItemId> & {
|
export type CustomSelectProps = SelectProps & {
|
||||||
items: SelectItem[];
|
items: SelectItem[];
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
isHidden?: boolean;
|
isHidden?: boolean;
|
||||||
hasError?: boolean;
|
hasError?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const SelectInput = forwardRef<HTMLDivElement, CustomSelectProps>(
|
export const SelectFromItems: React.FC<CustomSelectProps> = ({
|
||||||
function SelectInput(
|
items,
|
||||||
{ items, placeholder, isHidden = false, hasError = false, ...props },
|
placeholder,
|
||||||
ref
|
isHidden = false,
|
||||||
) {
|
hasError = false,
|
||||||
const theme = useTheme();
|
...props
|
||||||
return (
|
}) => {
|
||||||
<Select
|
return (
|
||||||
sx={{
|
<SelectInput
|
||||||
height: "34px",
|
error={hasError}
|
||||||
"& .MuiSelect-select": {
|
IconComponent={KeyboardArrowDownIcon}
|
||||||
padding: "0",
|
displayEmpty
|
||||||
},
|
MenuProps={{ disableScrollLock: true }}
|
||||||
"& .MuiOutlinedInput-notchedOutline": {
|
renderValue={(selected) => {
|
||||||
borderRadius: theme.shape.borderRadius,
|
if (!selected) {
|
||||||
borderColor: theme.palette.primary.lowContrast,
|
|
||||||
},
|
|
||||||
"&:hover .MuiOutlinedInput-notchedOutline": {
|
|
||||||
borderColor: theme.palette.primary.lowContrast,
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
error={hasError}
|
|
||||||
ref={ref}
|
|
||||||
IconComponent={KeyboardArrowDownIcon}
|
|
||||||
displayEmpty
|
|
||||||
MenuProps={{ disableScrollLock: true }}
|
|
||||||
renderValue={(selected) => {
|
|
||||||
if (!selected) {
|
|
||||||
return (
|
|
||||||
<Typography
|
|
||||||
noWrap
|
|
||||||
color="text.secondary"
|
|
||||||
>
|
|
||||||
{placeholder ?? ""}
|
|
||||||
</Typography>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const selectedItem = items.find((item) => item._id === selected);
|
|
||||||
const displayName = selectedItem ? selectedItem.name : placeholder;
|
|
||||||
return (
|
return (
|
||||||
<Typography
|
<Typography
|
||||||
noWrap
|
noWrap
|
||||||
title={displayName}
|
color="text.secondary"
|
||||||
>
|
>
|
||||||
{displayName}
|
{placeholder ?? ""}
|
||||||
</Typography>
|
</Typography>
|
||||||
);
|
);
|
||||||
}}
|
}
|
||||||
{...props}
|
const selectedItem = items.find((item) => item._id === selected);
|
||||||
>
|
const displayName = selectedItem ? selectedItem.name : placeholder;
|
||||||
{items.map((item) => (
|
return (
|
||||||
<MenuItem
|
<Typography
|
||||||
key={item._id}
|
noWrap
|
||||||
value={item._id}
|
title={displayName}
|
||||||
>
|
>
|
||||||
{item.name}
|
{displayName}
|
||||||
</MenuItem>
|
</Typography>
|
||||||
))}
|
);
|
||||||
</Select>
|
}}
|
||||||
);
|
{...props}
|
||||||
}
|
>
|
||||||
);
|
{items.map((item) => (
|
||||||
|
<MenuItem
|
||||||
|
key={item._id}
|
||||||
|
value={item._id}
|
||||||
|
>
|
||||||
|
{item.name}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</SelectInput>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
SelectInput.displayName = "SelectInput";
|
SelectInput.displayName = "SelectInput";
|
||||||
|
SelectFromItems.displayName = "SelectFromItems";
|
||||||
Reference in new issue
Block a user