Skip to content

Instantly share code, notes, and snippets.

@bryanltobing
Created June 9, 2022 14:17
Show Gist options
  • Save bryanltobing/5c352951efd08463f1cbd1cfab3ad23d to your computer and use it in GitHub Desktop.
Save bryanltobing/5c352951efd08463f1cbd1cfab3ad23d to your computer and use it in GitHub Desktop.
import { memo } from "react";
import { TextFieldProps, TextField, MenuItem } from "@mui/material";
import { Path, Control, Controller } from "react-hook-form";
export type HookFormSelectProps<TFormValues> = {
name: Path<TFormValues>;
control: Control<TFormValues, any>;
children: React.ReactNode;
} & TextFieldProps;
const HookFormSelect = <TFormValues extends Record<string, any>>(
props: HookFormSelectProps<TFormValues>
) => {
const { name, control, ...restProps } = props;
return (
<Controller
name={name}
control={control}
render={({ field: { ref, ...restField } }) => (
<TextField {...restProps} inputRef={ref} select {...restField}>
{props.children}
</TextField>
)}
/>
);
};
const HookFormSelectMemo = memo(HookFormSelect) as typeof HookFormSelect;
export { HookFormSelectMemo as HookFormSelect };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment