From 58a7a189ed357f37876f7c0ea0746b80525452e9 Mon Sep 17 00:00:00 2001 From: Sidney Alcantara Date: Wed, 6 Apr 2022 16:26:46 +1000 Subject: [PATCH] create table: set default table name to be based on selected collection --- src/components/TableSettings/TableId.tsx | 65 +++++----------------- src/components/TableSettings/TableName.tsx | 27 +++++++++ src/components/TableSettings/form.tsx | 3 +- src/components/TableSettings/index.tsx | 35 +++++++----- 4 files changed, 63 insertions(+), 67 deletions(-) create mode 100644 src/components/TableSettings/TableName.tsx diff --git a/src/components/TableSettings/TableId.tsx b/src/components/TableSettings/TableId.tsx index 4da7d20c..febf7380 100644 --- a/src/components/TableSettings/TableId.tsx +++ b/src/components/TableSettings/TableId.tsx @@ -1,33 +1,22 @@ import { useEffect } from "react"; import { useWatch } from "react-hook-form"; import _camelCase from "lodash/camelCase"; +import { + ShortTextComponent, + IShortTextComponentProps, +} from "@rowy/form-builder"; -import { TextField, TextFieldProps } from "@mui/material"; -import { IFieldComponentProps, FieldAssistiveText } from "@rowy/form-builder"; - -export interface ICamelCaseIdProps - extends IFieldComponentProps, - Omit< - TextFieldProps, - "variant" | "name" | "label" | "onBlur" | "onChange" | "value" | "ref" - > { +export interface ITableIdProps extends IShortTextComponentProps { watchedField?: string; } -export default function CamelCaseId({ - field: { onChange, onBlur, value, ref }, +export default function TableId({ watchedField, ...props }: ITableIdProps) { + const { + field: { onChange }, + useFormMethods: { control }, + disabled, + } = props; - name, - useFormMethods: { control }, - - errorMessage, - assistiveText, - - disabled, - - watchedField, - ...props -}: ICamelCaseIdProps) { const watchedValue = useWatch({ control, name: watchedField } as any); useEffect(() => { if (!disabled && typeof watchedValue === "string" && !!watchedValue) @@ -35,37 +24,9 @@ export default function CamelCaseId({ }, [watchedValue, disabled]); return ( - - {errorMessage} - - - {assistiveText} - - - ) - } - FormHelperTextProps={{ component: "div" } as any} - name={name} - id={`field-${name}`} - sx={{ "& .MuiInputBase-input": { fontFamily: "mono" } }} + ); } diff --git a/src/components/TableSettings/TableName.tsx b/src/components/TableSettings/TableName.tsx new file mode 100644 index 00000000..2a96654f --- /dev/null +++ b/src/components/TableSettings/TableName.tsx @@ -0,0 +1,27 @@ +import { useEffect } from "react"; +import { useWatch } from "react-hook-form"; +import _startCase from "lodash/startCase"; +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) + onChange(_startCase(watchedValue)); + }, [watchedValue, disabled]); + + return ; +} diff --git a/src/components/TableSettings/form.tsx b/src/components/TableSettings/form.tsx index 8db56e25..09b4ef19 100644 --- a/src/components/TableSettings/form.tsx +++ b/src/components/TableSettings/form.tsx @@ -194,10 +194,11 @@ export const tableSettings = ( // Step 2: Display { step: "display", - type: FieldType.shortText, + type: "tableName", name: "name", label: "Table name", required: true, + watchedField: "collection", assistiveText: "User-facing name for this table", autoFocus: true, gridCols: { xs: 12, sm: 6 }, diff --git a/src/components/TableSettings/index.tsx b/src/components/TableSettings/index.tsx index a1f2fa26..619705fd 100644 --- a/src/components/TableSettings/index.tsx +++ b/src/components/TableSettings/index.tsx @@ -9,6 +9,7 @@ import { DialogContentText, Stack, Typography } from "@mui/material"; import { FormDialog, FormFields } from "@rowy/form-builder"; import { tableSettings } from "./form"; +import TableName from "./TableName"; import TableId from "./TableId"; import SuggestedRules from "./SuggestedRules"; import SteppedAccordion from "@src/components/SteppedAccordion"; @@ -32,7 +33,25 @@ export enum TableSettingsDialogModes { create, update, } -export interface ICreateTableDialogProps { +const customComponents = { + tableName: { + component: TableName, + defaultValue: "", + validation: [["string"]], + }, + tableId: { + component: TableId, + defaultValue: "", + validation: [["string"]], + }, + suggestedRules: { + component: SuggestedRules, + defaultValue: "", + validation: [["string"]], + }, +}; + +export interface ITableSettingsProps { mode: TableSettingsDialogModes | null; clearDialog: () => void; data: Table | null; @@ -42,7 +61,7 @@ export default function TableSettings({ mode, clearDialog, data, -}: ICreateTableDialogProps) { +}: ITableSettingsProps) { const { settingsActions, roles, tables, rowyRun } = useProjectContext(); const sectionNames = Array.from( new Set((tables ?? []).map((t) => t.section)) @@ -206,18 +225,6 @@ export default function TableSettings({ ), Array.isArray(collections) ? collections.filter((x) => x !== CONFIG) : null ); - const customComponents = { - tableId: { - component: TableId, - defaultValue: "", - validation: [["string"]], - }, - suggestedRules: { - component: SuggestedRules, - defaultValue: "", - validation: [["string"]], - }, - }; return (