From 924e86fc9e7ed8cb535f315c269eef2a9d1ae088 Mon Sep 17 00:00:00 2001 From: Xavier Rutayisire Date: Thu, 25 Jan 2024 12:18:06 +0100 Subject: [PATCH] fix(group field): Allow to configure repeat property in the UI (#1269) --- .../lib/models/common/widgets/Group/Form.jsx | 70 +++++++++++++++++++ .../lib/models/common/widgets/Group/index.ts | 8 ++- .../src/features/autoSave/useAutoSave.tsx | 4 +- 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 packages/slice-machine/lib/models/common/widgets/Group/Form.jsx diff --git a/packages/slice-machine/lib/models/common/widgets/Group/Form.jsx b/packages/slice-machine/lib/models/common/widgets/Group/Form.jsx new file mode 100644 index 0000000000..ee283ebab9 --- /dev/null +++ b/packages/slice-machine/lib/models/common/widgets/Group/Form.jsx @@ -0,0 +1,70 @@ +import { Checkbox, Label, Box } from "theme-ui"; + +import { Col, Flex as FlexGrid } from "@components/Flex"; +import { DefaultFields } from "@lib/forms/defaults"; +import WidgetFormField from "@lib/builders/common/EditModal/Field"; +import { CheckBox } from "@lib/forms/fields"; +import { createFieldNameFromKey } from "@lib/forms"; + +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const FormFields = { + ...DefaultFields, + repeat: CheckBox("Repeatable", false, true), +}; + +const Form = (props) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const { initialValues, fields, values, setFieldValue } = props; + + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const { + config: { repeat }, + } = values; + + return ( + + { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + Object.entries(FormFields) + .filter(([key]) => key !== "repeat") + .map(([key, field]) => ( + + + + )) + } + + + + + + + + ); +}; + +export { FormFields }; +export default Form; diff --git a/packages/slice-machine/lib/models/common/widgets/Group/index.ts b/packages/slice-machine/lib/models/common/widgets/Group/index.ts index f25f80dce6..6e54dde808 100644 --- a/packages/slice-machine/lib/models/common/widgets/Group/index.ts +++ b/packages/slice-machine/lib/models/common/widgets/Group/index.ts @@ -1,9 +1,9 @@ import * as yup from "yup"; -import { DefaultFields } from "@lib/forms/defaults"; import { MdPlaylistAdd } from "react-icons/md"; import { Widget } from "../Widget"; import CustomListItem from "./ListItem"; import { GroupSM } from "@lib/models/common/Group"; +import Form, { FormFields } from "./Form"; const Meta = { icon: MdPlaylistAdd, @@ -20,18 +20,20 @@ const schema = yup.object().shape({ fields: yup.array(), label: yup.string(), placeholder: yup.string(), + repeat: yup.boolean().optional(), }), }); export const GroupWidget: Widget = { Meta, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - FormFields: DefaultFields, + Form, + FormFields, schema, create: (label: string) => ({ type: "Group", config: { label, + repeat: true, fields: [], }, }), diff --git a/packages/slice-machine/src/features/autoSave/useAutoSave.tsx b/packages/slice-machine/src/features/autoSave/useAutoSave.tsx index a40049e536..bb0d94b659 100644 --- a/packages/slice-machine/src/features/autoSave/useAutoSave.tsx +++ b/packages/slice-machine/src/features/autoSave/useAutoSave.tsx @@ -98,12 +98,12 @@ export const useAutoSave = ( if (autoSaveStatusActual === "saving") { setAutoSaveStatusDelayed("saving"); } else { - const debounceTimeout = setTimeout(() => { + const delayedTimeout = setTimeout(() => { setAutoSaveStatusDelayed(autoSaveStatusActual); }, autoSaveStatusDelay); return () => { - clearTimeout(debounceTimeout); + clearTimeout(delayedTimeout); }; }