Skip to content

Commit

Permalink
Dynamic Form: boolean fields related fixes (getredash#4586)
Browse files Browse the repository at this point in the history
* Fix: when default value is false make sure it's still stringified.

* Fix: when extra field is of type boolean make sure it's different from default value to decide if it's open.

* Use isNil to check the default value

* Restyled by prettier (getredash#4704)

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
  • Loading branch information
arikfr and restyled-io[bot] authored Mar 3, 2020
1 parent d687bef commit 78201c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions client/app/components/dynamic-form/DynamicForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ class DynamicForm extends React.Component {
super(props);

const hasFilledExtraField = some(props.fields, field => {
const { extra, initialValue } = field;
return extra && (!isEmpty(initialValue) || isNumber(initialValue) || (isBoolean(initialValue) && initialValue));
const { extra, initialValue, placeholder } = field;
return (
extra &&
(!isEmpty(initialValue) ||
isNumber(initialValue) ||
(isBoolean(initialValue) && initialValue.toString() !== placeholder))
);
});

const inProgressActions = {};
Expand Down Expand Up @@ -95,7 +100,7 @@ class DynamicForm extends React.Component {
this.props.form.validateFieldsAndScroll((err, values) => {
Object.entries(values).forEach(([key, value]) => {
const initialValue = this.props.fields.find(f => f.name === key).initialValue;
if ((initialValue === null || initialValue === undefined || initialValue === '') && value === '') {
if ((initialValue === null || initialValue === undefined || initialValue === "") && value === "") {
values[key] = null;
}
});
Expand Down
4 changes: 2 additions & 2 deletions client/app/components/dynamic-form/dynamicFormHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { each, includes, isUndefined, isEmpty, map } from "lodash";
import { each, includes, isUndefined, isEmpty, isNil, map } from "lodash";

function orderedInputs(properties, order, targetOptions) {
const inputs = new Array(order.length);
Expand All @@ -9,7 +9,7 @@ function orderedInputs(properties, order, targetOptions) {
name: key,
title: properties[key].title,
type: properties[key].type,
placeholder: properties[key].default && properties[key].default.toString(),
placeholder: isNil(properties[key].default) ? null : properties[key].default.toString(),
required: properties[key].required,
extra: properties[key].extra,
initialValue: targetOptions[key],
Expand Down

0 comments on commit 78201c6

Please sign in to comment.