From 9a5bb0dc47fb9e775adee1f697c074a01f49076e Mon Sep 17 00:00:00 2001 From: Ng Zhao Loong Date: Wed, 5 Jan 2022 09:16:16 +0800 Subject: [PATCH] Fix/demo form state (#515) * fix: preserve state while navigating * fix: sneaky date-fns bug * fix: update stories --- .../DemoCreateFormItem.stories.tsx | 5 +++ .../DemoCreateFormItem/index.tsx | 36 +++++++++++-------- .../Demo/DemoCreate/DemoCreateForm/data.tsx | 2 +- .../Demo/DemoCreate/DemoCreateForm/index.tsx | 1 + .../contexts/DemoFormContext/index.tsx | 1 - src/components/Demo/DemoCreate/index.tsx | 3 +- src/pages/demoCreate.tsx | 9 +++-- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/DemoCreateFormItem.stories.tsx b/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/DemoCreateFormItem.stories.tsx index 187290b14..976d4f833 100644 --- a/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/DemoCreateFormItem.stories.tsx +++ b/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/DemoCreateFormItem.stories.tsx @@ -1,6 +1,7 @@ import React from "react"; import { DemoCreateFormItem } from "."; +import { data } from "../data"; export default { title: "Demo/FormItem", @@ -21,6 +22,7 @@ export const FormItemAccordion = () => { formItemName="" onChange={() => {}} formItemIndex={1} + data={data} /> ); }; @@ -36,6 +38,7 @@ export const FormItemWithLabel = () => { formItemName="" onChange={() => {}} formItemIndex={1} + data={data} /> ); }; @@ -51,6 +54,7 @@ export const FormItemWithoutLabel = () => { formItemName="" onChange={() => {}} formItemIndex={1} + data={data} /> ); }; @@ -66,6 +70,7 @@ export const FormItemUpload = () => { formItemName="" onChange={() => {}} formItemIndex={1} + data={data} /> ); }; diff --git a/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/index.tsx b/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/index.tsx index 63e2f4bdd..8a4e82021 100644 --- a/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/index.tsx +++ b/src/components/Demo/DemoCreate/DemoCreateForm/DemoCreateFormItem/index.tsx @@ -4,7 +4,6 @@ import { FunctionComponent } from "react"; import Dropzone from "react-dropzone"; import { AccordionItem } from "../../../../UI/Accordion"; import { getFormValue } from "../../utils"; -import { data } from "../data"; import { FormItemSchema } from "../types"; interface DemoCreateFormItemProps { @@ -12,6 +11,7 @@ interface DemoCreateFormItemProps { formItem: FormItemSchema; formItemName: string; formItemIndex: number; + data: Record; } export const DemoCreateFormItem: FunctionComponent = ({ @@ -19,6 +19,7 @@ export const DemoCreateFormItem: FunctionComponent = ({ formItemName, onChange, formItemIndex, + data, }) => { const renderProperties = () => { if (formItem.type === "object" && formItem.properties) { @@ -33,6 +34,7 @@ export const DemoCreateFormItem: FunctionComponent = ({ formItemName={itemName} formItem={item} formItemIndex={index} + data={data} /> ); }); @@ -68,18 +70,20 @@ export const DemoCreateFormItem: FunctionComponent = ({ ); }; - const renderTextArea = () => { + const RenderTextArea = () => { + const [value, setValue] = useState(getFormValue(data, formItemName)); if (formItem.type === "string") { - const defaultValue = getFormValue(data, formItemName); - return (