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.