From 7d6bc6e2e58251bcfcd7f187c7014240c631c47d Mon Sep 17 00:00:00 2001 From: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com> Date: Tue, 22 Mar 2022 12:03:27 +0100 Subject: [PATCH 1/5] fix: default value with number --- .../MultiSelectWidgetV2/widget/index.test.tsx | 14 +++++++- .../MultiSelectWidgetV2/widget/index.tsx | 34 +++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx index 9762e057a2..06f8fad878 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx @@ -14,7 +14,7 @@ describe("defaultOptionValueValidation - ", () => { }); }); - it("should get tested with array of strings|number", () => { + it("should get tested with array of strings", () => { const input = ["green", "red"]; expect( @@ -26,6 +26,18 @@ describe("defaultOptionValueValidation - ", () => { }); }); + it("should get tested with a number", () => { + const input = 2022; + + expect( + defaultOptionValueValidation(input, {} as MultiSelectWidgetProps, _), + ).toEqual({ + isValid: true, + parsed: [input], + messages: [""], + }); + }); + it("should get tested with array json string", () => { const input = `["green", "red"]`; diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx index 37bfd7599a..23ff0a1d46 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx @@ -23,7 +23,7 @@ export function defaultOptionValueValidation( _: any, ): ValidationResponse { let isValid; - let parsed; + let parsed: any[]; let message = ""; /* @@ -67,11 +67,10 @@ export function defaultOptionValueValidation( } } - if (_.isString(value) && (value as string).trim() === "") { - isValid = true; - parsed = []; - message = ""; - } else if (Array.isArray(value)) { + /* + * When value is "['green', 'red']", "[{label: 'green', value: 'green'}]" and "green, red" + */ + if (Array.isArray(value)) { if (value.every((val) => _.isString(val) || _.isFinite(val))) { /* * When value is ["green", "red"] @@ -107,6 +106,29 @@ export function defaultOptionValueValidation( message = "value should match: Array | Array<{label: string, value: string | number}>"; } + } + + /* + * When value is an empty string + */ + if (_.isString(value) && (value as string).trim() === "") { + isValid = true; + parsed = []; + message = ""; + } else if (_.isNumber(value)) { + /* + * When value is a number + */ + isValid = true; + parsed = [value]; + message = ""; + } else if (_.isString(value)) { + /* + * When value is just a single string e.g "Blue" + */ + isValid = true; + parsed = [value]; + message = ""; } else { /* * When value is undefined, null, {} etc. From 15c827bf87243dba3dd0a861df7d4785286cdee1 Mon Sep 17 00:00:00 2001 From: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com> Date: Tue, 22 Mar 2022 13:55:06 +0100 Subject: [PATCH 2/5] FIX: BUILD ISSUE --- .../src/widgets/MultiSelectWidgetV2/widget/index.tsx | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx index 23ff0a1d46..3dc079333b 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx @@ -22,8 +22,8 @@ export function defaultOptionValueValidation( props: MultiSelectWidgetProps, _: any, ): ValidationResponse { - let isValid; - let parsed: any[]; + let isValid = false; + let parsed: any[] = []; let message = ""; /* @@ -129,14 +129,6 @@ export function defaultOptionValueValidation( isValid = true; parsed = [value]; message = ""; - } else { - /* - * When value is undefined, null, {} etc. - */ - isValid = false; - parsed = []; - message = - "value should match: Array | Array<{label: string, value: string | number}>"; } return { From 90dfa002fa2494eb6ab521a76fa6183fa2c15196 Mon Sep 17 00:00:00 2001 From: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com> Date: Wed, 23 Mar 2022 07:25:20 +0100 Subject: [PATCH 3/5] fix: build issue --- .../widgets/MultiSelectWidgetV2/widget/index.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx index 3dc079333b..977e98c427 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx @@ -106,6 +106,12 @@ export function defaultOptionValueValidation( message = "value should match: Array | Array<{label: string, value: string | number}>"; } + + return { + isValid, + parsed, + messages: [message], + }; } /* @@ -129,6 +135,14 @@ export function defaultOptionValueValidation( isValid = true; parsed = [value]; message = ""; + } else { + /* + * When value is undefined, null, {} etc. + */ + isValid = false; + parsed = []; + message = + "value should match: Array | Array<{label: string, value: string | number}>"; } return { From aa806de7baecacce7ce0e0c20ede73b30fa19000 Mon Sep 17 00:00:00 2001 From: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com> Date: Wed, 23 Mar 2022 13:08:32 +0100 Subject: [PATCH 4/5] fix: parsing unnecessary value --- .../MultiSelectWidgetV2/widget/index.test.tsx | 23 ++++++++++- .../MultiSelectWidgetV2/widget/index.tsx | 41 +++++++------------ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx index 06f8fad878..79f08067ee 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.test.tsx @@ -37,6 +37,19 @@ describe("defaultOptionValueValidation - ", () => { messages: [""], }); }); + it("should get tested with a string", () => { + const inputs = ["2022", "true", "null", "test", "undefined"]; + + inputs.forEach((input) => { + expect( + defaultOptionValueValidation(input, {} as MultiSelectWidgetProps, _), + ).toEqual({ + isValid: true, + parsed: [input], + messages: [""], + }); + }); + }); it("should get tested with array json string", () => { const input = `["green", "red"]`; @@ -80,8 +93,9 @@ describe("defaultOptionValueValidation - ", () => { }); }); - it("should get tested with comma seperated strings", () => { + it("should get tested with comma separated strings", () => { const input = "green, red"; + const input2 = "1, 2"; expect( defaultOptionValueValidation(input, {} as MultiSelectWidgetProps, _), @@ -90,6 +104,13 @@ describe("defaultOptionValueValidation - ", () => { parsed: ["green", "red"], messages: [""], }); + expect( + defaultOptionValueValidation(input2, {} as MultiSelectWidgetProps, _), + ).toEqual({ + isValid: true, + parsed: ["1", "2"], + messages: [""], + }); }); it("should get tested with simple string", () => { diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx index 977e98c427..08ff79d45e 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx @@ -2,7 +2,7 @@ import React from "react"; import BaseWidget, { WidgetProps, WidgetState } from "widgets/BaseWidget"; import { WidgetType } from "constants/WidgetConstants"; import { EventType } from "constants/AppsmithActionConstants/ActionConstants"; -import { isArray, isString, isNumber } from "lodash"; +import { isArray, isString, isNumber, LoDashStatic } from "lodash"; import { ValidationResponse, ValidationTypes, @@ -20,7 +20,7 @@ import { AutocompleteDataType } from "utils/autocomplete/TernServer"; export function defaultOptionValueValidation( value: unknown, props: MultiSelectWidgetProps, - _: any, + _: LoDashStatic, ): ValidationResponse { let isValid = false; let parsed: any[] = []; @@ -51,12 +51,16 @@ export function defaultOptionValueValidation( /* * When value is "['green', 'red']", "[{label: 'green', value: 'green'}]" and "green, red" */ - if (_.isString(value) && (value as string).trim() !== "") { + if (_.isString(value) && value.trim() !== "") { try { /* * when value is "['green', 'red']", "[{label: 'green', value: 'green'}]" */ - value = JSON.parse(value as string); + const parsedValue = JSON.parse(value); + // Only parse value if resulting value is an array or string + if (Array.isArray(parsedValue) || _.isString(parsedValue)) { + value = parsedValue; + } } catch (e) { /* * when value is "green, red", JSON.parse throws error @@ -75,12 +79,10 @@ export function defaultOptionValueValidation( /* * When value is ["green", "red"] */ - if (hasUniqueValues(value as [])) { + if (hasUniqueValues(value)) { isValid = true; parsed = value; - message = ""; } else { - isValid = false; parsed = []; message = "values must be unique. Duplicate values found"; } @@ -88,12 +90,10 @@ export function defaultOptionValueValidation( /* * When value is [{label: "green", value: "red"}] */ - if (hasUniqueValues(value.map((val) => val.value) as [])) { + if (hasUniqueValues(value.map((val) => val.value))) { isValid = true; parsed = value; - message = ""; } else { - isValid = false; parsed = []; message = "path:value must be unique. Duplicate values found"; } @@ -101,7 +101,6 @@ export function defaultOptionValueValidation( /* * When value is [true, false], [undefined, undefined] etc. */ - isValid = false; parsed = []; message = "value should match: Array | Array<{label: string, value: string | number}>"; @@ -117,29 +116,19 @@ export function defaultOptionValueValidation( /* * When value is an empty string */ - if (_.isString(value) && (value as string).trim() === "") { + if (_.isString(value) && value.trim() === "") { isValid = true; parsed = []; - message = ""; - } else if (_.isNumber(value)) { + } else if (_.isNumber(value) || _.isString(value)) { /* - * When value is a number + * When value is a number or just a single string e.g "Blue" */ isValid = true; parsed = [value]; - message = ""; - } else if (_.isString(value)) { - /* - * When value is just a single string e.g "Blue" - */ - isValid = true; - parsed = [value]; - message = ""; } else { /* * When value is undefined, null, {} etc. */ - isValid = false; parsed = []; message = "value should match: Array | Array<{label: string, value: string | number}>"; @@ -450,9 +439,7 @@ class MultiSelectWidget extends BaseWidget< const { componentWidth } = this.getComponentDimensions(); const values: LabelValueType[] = this.props.selectedOptions ? this.props.selectedOptions.map((o) => - isString(o) || isNumber(o) - ? { label: o, value: o } - : { label: o.label, value: o.value }, + isString(o) || isNumber(o) ? { value: o } : { value: o.value }, ) : []; const isInvalid = From 71284e2fc99ac1787f92ccbda8667af149f56b22 Mon Sep 17 00:00:00 2001 From: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com> Date: Sun, 27 Mar 2022 07:06:34 +0100 Subject: [PATCH 5/5] fix: add default error message --- .../src/widgets/MultiSelectWidgetV2/widget/index.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx index 08ff79d45e..cb05cce6c7 100644 --- a/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx +++ b/app/client/src/widgets/MultiSelectWidgetV2/widget/index.tsx @@ -26,6 +26,9 @@ export function defaultOptionValueValidation( let parsed: any[] = []; let message = ""; + const DEFAULT_ERROR_MESSAGE = + "value should match: Array | Array<{label: string, value: string | number}>"; + /* * Function to check if the object has `label` and `value` */ @@ -102,8 +105,7 @@ export function defaultOptionValueValidation( * When value is [true, false], [undefined, undefined] etc. */ parsed = []; - message = - "value should match: Array | Array<{label: string, value: string | number}>"; + message = DEFAULT_ERROR_MESSAGE; } return { @@ -130,8 +132,7 @@ export function defaultOptionValueValidation( * When value is undefined, null, {} etc. */ parsed = []; - message = - "value should match: Array | Array<{label: string, value: string | number}>"; + message = DEFAULT_ERROR_MESSAGE; } return {