From 62e2fadccfef11e9d5206bd53ec1a35f3bd76bd1 Mon Sep 17 00:00:00 2001 From: Rob Odil Date: Wed, 7 Dec 2022 14:56:26 -0700 Subject: [PATCH] fix: broken logic in validation of array items --- .../BookCreateForm.jsx | 2 +- ...studio-ui-codegen-react-forms.test.ts.snap | 54 +++++++++---------- .../lib/utils/forms/array-field-component.ts | 26 +++++---- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/packages/codegen-ui-golden-files/lib/react/forms/datastore-create-form-with-has-one-relationship/BookCreateForm.jsx b/packages/codegen-ui-golden-files/lib/react/forms/datastore-create-form-with-has-one-relationship/BookCreateForm.jsx index a16a5fc3e..302ec4190 100644 --- a/packages/codegen-ui-golden-files/lib/react/forms/datastore-create-form-with-has-one-relationship/BookCreateForm.jsx +++ b/packages/codegen-ui-golden-files/lib/react/forms/datastore-create-form-with-has-one-relationship/BookCreateForm.jsx @@ -50,7 +50,7 @@ function ArrayField({ setSelectedBadgeIndex(undefined); }; const addItem = async () => { - if ((currentFieldValue !== undefined || currentFieldValue !== '') && !hasError) { + if (currentFieldValue !== undefined && currentFieldValue !== null && currentFieldValue !== '' && !hasError) { const newItems = [...items]; if (selectedBadgeIndex !== undefined) { newItems[selectedBadgeIndex] = currentFieldValue; 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 e864f47bd..ba59eeba6 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 @@ -813,9 +813,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -1633,9 +1633,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -2188,9 +2188,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -2867,9 +2867,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -4308,9 +4308,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -4858,9 +4858,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -5393,9 +5393,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -6239,9 +6239,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; @@ -7134,9 +7134,9 @@ function ArrayField({ }; const addItem = async () => { if ( - (currentFieldValue !== undefined || - currentFieldValue !== null || - currentFieldValue !== \\"\\") && + currentFieldValue !== undefined && + currentFieldValue !== null && + currentFieldValue !== \\"\\" && !hasError ) { const newItems = [...items]; diff --git a/packages/codegen-ui-react/lib/utils/forms/array-field-component.ts b/packages/codegen-ui-react/lib/utils/forms/array-field-component.ts index 2f80d55d5..c97560b1a 100644 --- a/packages/codegen-ui-react/lib/utils/forms/array-field-component.ts +++ b/packages/codegen-ui-react/lib/utils/forms/array-field-component.ts @@ -253,28 +253,26 @@ export const generateArrayFieldComponent = () => { [ factory.createIfStatement( factory.createBinaryExpression( - factory.createParenthesizedExpression( + factory.createBinaryExpression( factory.createBinaryExpression( factory.createBinaryExpression( - factory.createBinaryExpression( - factory.createIdentifier('currentFieldValue'), - factory.createToken(SyntaxKind.ExclamationEqualsEqualsToken), - factory.createIdentifier('undefined'), - ), - factory.createToken(SyntaxKind.BarBarToken), - factory.createBinaryExpression( - factory.createIdentifier('currentFieldValue'), - factory.createToken(SyntaxKind.ExclamationEqualsEqualsToken), - factory.createNull(), - ), + factory.createIdentifier('currentFieldValue'), + factory.createToken(SyntaxKind.ExclamationEqualsEqualsToken), + factory.createIdentifier('undefined'), ), - factory.createToken(SyntaxKind.BarBarToken), + factory.createToken(SyntaxKind.AmpersandAmpersandToken), factory.createBinaryExpression( factory.createIdentifier('currentFieldValue'), factory.createToken(SyntaxKind.ExclamationEqualsEqualsToken), - factory.createStringLiteral(''), + factory.createNull(), ), ), + factory.createToken(SyntaxKind.AmpersandAmpersandToken), + factory.createBinaryExpression( + factory.createIdentifier('currentFieldValue'), + factory.createToken(SyntaxKind.ExclamationEqualsEqualsToken), + factory.createStringLiteral(''), + ), ), factory.createToken(SyntaxKind.AmpersandAmpersandToken), factory.createPrefixUnaryExpression(