Files
probo/apps/console2/src/components/form/ControlledField.tsx
Jonathan 2e6741942b Add policies version history
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
2025-06-13 13:23:42 -07:00

57 lines
1.2 KiB
TypeScript

import type { ComponentProps, JSX, JSXElementConstructor } from "react";
import { Field } from "@probo/ui";
import { Controller, type Control } from "react-hook-form";
import { Select } from "@probo/ui";
type Props<T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>> =
ComponentProps<T> & {
control: Control<any>;
name: string;
};
export function ControlledField({
control,
name,
...props
}: Props<typeof Field>) {
return (
<Controller
control={control}
name={name}
render={({ field }) => (
<>
<Field
{...props}
{...field}
// TODO : Find a better way to handle this case (comparing number and string for select create issues)
value={field.value ? field.value.toString() : ""}
onValueChange={field.onChange}
/>
</>
)}
/>
);
}
export function ControlledSelect({
control,
name,
...props
}: Props<typeof Select>) {
return (
<Controller
control={control}
name={name}
render={({ field }) => (
<Select
id={name}
{...props}
{...field}
onValueChange={field.onChange}
value={field.value ?? ""}
/>
)}
/>
);
}