mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { Select, type SelectProps } from "@mantine/core";
|
|
import { type FieldValues, type UseControllerProps, useController } from "react-hook-form";
|
|
|
|
export type MantineSelectProps<T extends FieldValues> = UseControllerProps<T> &
|
|
Omit<SelectProps, "value" | "defaultValue">;
|
|
|
|
// @todo: change is not triggered correctly
|
|
export function MantineSelect<T extends FieldValues>({
|
|
name,
|
|
control,
|
|
defaultValue,
|
|
rules,
|
|
shouldUnregister,
|
|
onChange,
|
|
...props
|
|
}: MantineSelectProps<T>) {
|
|
const {
|
|
field: { value, onChange: fieldOnChange, ...field },
|
|
fieldState,
|
|
} = useController<T>({
|
|
name,
|
|
control,
|
|
defaultValue,
|
|
rules,
|
|
shouldUnregister,
|
|
});
|
|
|
|
return (
|
|
<Select
|
|
value={value}
|
|
onChange={async (e) => {
|
|
//console.log("change1", name, field.name, e);
|
|
await fieldOnChange({
|
|
...new Event("change", { bubbles: true, cancelable: true }),
|
|
target: {
|
|
value: e,
|
|
name: field.name,
|
|
},
|
|
});
|
|
// @ts-ignore
|
|
onChange?.(e);
|
|
}}
|
|
error={fieldState.error?.message}
|
|
{...field}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|