forward ref
This commit is contained in:
1 parent
4b92dbd19b
commit
84e4ea7b64
1 file changed
+20
-12
@@ -1,7 +1,7 @@
|
|||||||
import Select from "@mui/material/Select";
|
import Select from "@mui/material/Select";
|
||||||
import React from "react";
|
|
||||||
import { useTheme } from "@mui/material/styles";
|
|
||||||
import type { SelectProps } 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 Stack from "@mui/material/Stack";
|
||||||
import { FieldLabel } from "./FieldLabel";
|
import { FieldLabel } from "./FieldLabel";
|
||||||
import { ChevronDown } from "lucide-react";
|
import { ChevronDown } from "lucide-react";
|
||||||
@@ -14,18 +14,21 @@ interface SelectInputProps<T> extends Omit<SelectProps<T>, "label"> {
|
|||||||
placeholderColor?: string;
|
placeholderColor?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SelectInput = <T,>({
|
const SelectInputInner = <T,>(
|
||||||
fieldLabel,
|
{
|
||||||
required,
|
fieldLabel,
|
||||||
placeholder,
|
required,
|
||||||
placeholderColor,
|
placeholder,
|
||||||
...props
|
placeholderColor,
|
||||||
}: SelectInputProps<T>) => {
|
...props
|
||||||
|
}: SelectInputProps<T>,
|
||||||
|
ref: React.ForwardedRef<HTMLDivElement>
|
||||||
|
) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const emptyPlaceholderColor = placeholderColor || theme.palette.text.disabled;
|
const emptyPlaceholderColor = placeholderColor || theme.palette.text.disabled;
|
||||||
|
|
||||||
const renderValue = (selected: any) => {
|
const renderValue = (selected: unknown) => {
|
||||||
const isMultiple = Boolean((props as any).multiple);
|
const isMultiple = Boolean((props as { multiple?: boolean }).multiple);
|
||||||
const isEmpty = isMultiple
|
const isEmpty = isMultiple
|
||||||
? !Array.isArray(selected) || selected.length === 0
|
? !Array.isArray(selected) || selected.length === 0
|
||||||
: selected === undefined || selected === null || selected === "";
|
: selected === undefined || selected === null || selected === "";
|
||||||
@@ -39,7 +42,7 @@ export const SelectInput = <T,>({
|
|||||||
const capitalized = items.map(
|
const capitalized = items.map(
|
||||||
(item) => item.charAt(0).toUpperCase() + item.slice(1)
|
(item) => item.charAt(0).toUpperCase() + item.slice(1)
|
||||||
);
|
);
|
||||||
return (<Typography>{capitalized.join(" | ")}</Typography>) as any;
|
return <Typography>{capitalized.join(" | ")}</Typography>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = React.Children.toArray(props.children as React.ReactNode);
|
const nodes = React.Children.toArray(props.children as React.ReactNode);
|
||||||
@@ -59,6 +62,7 @@ export const SelectInput = <T,>({
|
|||||||
const select = (
|
const select = (
|
||||||
<Select<T>
|
<Select<T>
|
||||||
{...props}
|
{...props}
|
||||||
|
ref={ref}
|
||||||
displayEmpty
|
displayEmpty
|
||||||
renderValue={renderValue}
|
renderValue={renderValue}
|
||||||
inputProps={{
|
inputProps={{
|
||||||
@@ -100,3 +104,7 @@ export const SelectInput = <T,>({
|
|||||||
|
|
||||||
return select;
|
return select;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SelectInput = forwardRef(SelectInputInner) as <T>(
|
||||||
|
props: SelectInputProps<T> & { ref?: React.ForwardedRef<HTMLDivElement> }
|
||||||
|
) => React.ReactElement;
|
||||||
Reference in new issue
Block a user