From 623a1faed9b051c694f6c87f5b3124cb888d1027 Mon Sep 17 00:00:00 2001 From: joepuzzo Date: Wed, 9 Mar 2022 13:45:48 -0800 Subject: [PATCH] issue with changing options on schema fields --- CHANGELOG.md | 6 ++++++ src/components/FormField.js | 17 ++++++++++++----- src/utils.js | 12 ++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6008efeb..48259932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 4.5.7 (March 9th, 2022) + +### Fixed + +- issue with changing options on schema fields ( undefined would clobber defined values in option merge ) + ## 4.5.6 (March 8th, 2022) ### Fixed diff --git a/src/components/FormField.js b/src/components/FormField.js index 64f64631..052e0536 100644 --- a/src/components/FormField.js +++ b/src/components/FormField.js @@ -2,7 +2,8 @@ import React, { useMemo, useEffect, useState } from 'react'; import { computeFieldFromProperty, getSchemaPathFromJsonPath, - checkCondition + checkCondition, + sanitize } from '../utils'; import { ObjectMap } from '../ObjectMap'; import { useFormController } from '../hooks/useFormController'; @@ -98,7 +99,10 @@ const FormField = ({ name, schema, ...rest }) => { // } if (target === name) { - logger('Updating field props for', target); + logger( + `Updating field props for ${target}`, + computeFieldFromProperty(name, property) + ); setCondProp(computeFieldFromProperty(name, property)); } }; @@ -127,9 +131,12 @@ const FormField = ({ name, schema, ...rest }) => { const condProps = condProp.props; // Lay those on top of existing ones - const newProps = { ...schemaProps, ...condProps, ...rest }; - - logger(`New Props for ${name}`, condProps); + const newSchemaProps = sanitize(schemaProps); + const newCondProps = sanitize(condProps); + const newProps = { ...newSchemaProps, ...newCondProps, ...rest }; + logger(`Schema Props for ${name}`, newSchemaProps); + logger(`Cond Props for ${name}`, newCondProps); + logger(`New Props for ${name}`, newProps); return newProps; }, [condProp] diff --git a/src/utils.js b/src/utils.js index 061db490..2c99bc6c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -172,6 +172,18 @@ export const generateValue = ({ fieldType, maskedValue, multiple, value }) => { } }; +export const sanitize = obj => { + if (obj) { + const cleaned = JSON.parse( + JSON.stringify(obj, (key, value) => { + return value === undefined ? undefined : value; + }) + ); + return cleaned; + } + return obj; +}; + /* -------------------------- Error Utils ----------------------------- */ export const yupToFormErrors = yupError => {