2022-04-06 16:26:46 +10:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useWatch } from "react-hook-form";
|
2022-05-04 19:10:19 +10:00
|
|
|
import { startCase } from "lodash-es";
|
2022-04-06 16:26:46 +10:00
|
|
|
import {
|
|
|
|
|
ShortTextComponent,
|
|
|
|
|
IShortTextComponentProps,
|
|
|
|
|
} from "@rowy/form-builder";
|
|
|
|
|
|
|
|
|
|
export interface ITableNameProps extends IShortTextComponentProps {
|
|
|
|
|
watchedField?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function TableName({ watchedField, ...props }: ITableNameProps) {
|
|
|
|
|
const {
|
|
|
|
|
field: { onChange },
|
|
|
|
|
useFormMethods: { control },
|
|
|
|
|
disabled,
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
const watchedValue = useWatch({ control, name: watchedField } as any);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!disabled && typeof watchedValue === "string" && !!watchedValue)
|
2022-05-04 19:10:19 +10:00
|
|
|
onChange(startCase(watchedValue));
|
2022-04-06 16:26:46 +10:00
|
|
|
}, [watchedValue, disabled]);
|
|
|
|
|
|
|
|
|
|
return <ShortTextComponent {...props} />;
|
|
|
|
|
}
|