From 0045ee8f65de27307056f9e15d2448d489498b98 Mon Sep 17 00:00:00 2001 From: Joe Buono Date: Thu, 27 Jul 2023 18:24:19 -0500 Subject: [PATCH] fix: fields with json type display 'null' (#1051) Co-authored-by: Joe Buono --- ...studio-ui-codegen-react-forms.test.ts.snap | 13 +++++++----- .../forms/form-renderer-helper/form-state.ts | 20 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap index 942ffe2d..b2292c44 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap @@ -5141,12 +5141,13 @@ export default function MyPostForm(props) { setProfile_url(cleanValues.profile_url); setPost_url(cleanValues.post_url); setMetadata( - typeof cleanValues.metadata === \\"string\\" + typeof cleanValues.metadata === \\"string\\" || cleanValues.metadata === null ? cleanValues.metadata : JSON.stringify(cleanValues.metadata) ); setNonModelField( - typeof cleanValues.nonModelField === \\"string\\" + typeof cleanValues.nonModelField === \\"string\\" || + cleanValues.nonModelField === null ? cleanValues.nonModelField : JSON.stringify(cleanValues.nonModelField) ); @@ -30490,12 +30491,13 @@ export default function MyPostForm(props) { setProfile_url(cleanValues.profile_url); setPost_url(cleanValues.post_url); setMetadata( - typeof cleanValues.metadata === \\"string\\" + typeof cleanValues.metadata === \\"string\\" || cleanValues.metadata === null ? cleanValues.metadata : JSON.stringify(cleanValues.metadata) ); setNonModelField( - typeof cleanValues.nonModelField === \\"string\\" + typeof cleanValues.nonModelField === \\"string\\" || + cleanValues.nonModelField === null ? cleanValues.nonModelField : JSON.stringify(cleanValues.nonModelField) ); @@ -35858,7 +35860,8 @@ export default function InputGalleryUpdateForm(props) { setJsonArray(cleanValues.jsonArray ?? []); setCurrentJsonArrayValue(\\"\\"); setJsonField( - typeof cleanValues.jsonField === \\"string\\" + typeof cleanValues.jsonField === \\"string\\" || + cleanValues.jsonField === null ? cleanValues.jsonField : JSON.stringify(cleanValues.jsonField) ); diff --git a/packages/codegen-ui-react/lib/forms/form-renderer-helper/form-state.ts b/packages/codegen-ui-react/lib/forms/form-renderer-helper/form-state.ts index 56e4f35d..15d89a19 100644 --- a/packages/codegen-ui-react/lib/forms/form-renderer-helper/form-state.ts +++ b/packages/codegen-ui-react/lib/forms/form-renderer-helper/form-state.ts @@ -438,19 +438,29 @@ export const resetStateFunction = (fieldConfigs: Record { return factory.createConditionalExpression( factory.createBinaryExpression( - factory.createTypeOfExpression(value), - factory.createToken(SyntaxKind.EqualsEqualsEqualsToken), - factory.createStringLiteral('string'), + factory.createBinaryExpression( + factory.createTypeOfExpression(value), + factory.createToken(SyntaxKind.EqualsEqualsEqualsToken), + factory.createStringLiteral('string'), + ), + factory.createToken(SyntaxKind.BarBarToken), + factory.createBinaryExpression( + value, + factory.createToken(SyntaxKind.EqualsEqualsEqualsToken), + factory.createNull(), + ), ), factory.createToken(SyntaxKind.QuestionToken), value,