fix: radio group widget options (#7313)

* Update RadioGroupComponent.tsx

* fix: radio widget validation

* fix: add test for default param
This commit is contained in:
Tolulope Adetula 2021-09-20 08:13:37 +01:00 committed by GitHub
parent fae8de553d
commit 3c4ef11df2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import { RadioOption } from "../constants"; import { RadioOption } from "../constants";
import { ValidationTypes } from "constants/WidgetValidation"; import { ValidationTypes } from "constants/WidgetValidation";
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory"; import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
import { isArray } from "lodash";
class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> { class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> {
static getPropertyPaneConfig() { static getPropertyPaneConfig() {
@ -26,6 +27,7 @@ class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> {
type: ValidationTypes.ARRAY, type: ValidationTypes.ARRAY,
unique: ["value"], unique: ["value"],
params: { params: {
default: [],
children: { children: {
type: ValidationTypes.OBJECT, type: ValidationTypes.OBJECT,
params: { params: {
@ -143,7 +145,7 @@ class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> {
key={this.props.widgetId} key={this.props.widgetId}
label={`${this.props.label}`} label={`${this.props.label}`}
onRadioSelectionChange={this.onRadioSelectionChange} onRadioSelectionChange={this.onRadioSelectionChange}
options={this.props.options || []} options={isArray(this.props.options) ? this.props.options : []}
selectedOptionValue={this.props.selectedOptionValue} selectedOptionValue={this.props.selectedOptionValue}
widgetId={this.props.widgetId} widgetId={this.props.widgetId}
/> />
@ -167,7 +169,7 @@ class RadioGroupWidget extends BaseWidget<RadioGroupWidgetProps, WidgetState> {
export interface RadioGroupWidgetProps extends WidgetProps { export interface RadioGroupWidgetProps extends WidgetProps {
label: string; label: string;
options?: RadioOption[]; options: RadioOption[];
selectedOptionValue: string; selectedOptionValue: string;
onSelectionChange: string; onSelectionChange: string;
defaultOptionValue: string; defaultOptionValue: string;

View File

@ -373,6 +373,7 @@ describe("Validate Validators", () => {
'{ "key": "value" }', '{ "key": "value" }',
["a", "b", "a", "c"], ["a", "b", "a", "c"],
"", "",
"[]",
]; ];
const config = { const config = {
type: ValidationTypes.ARRAY, type: ValidationTypes.ARRAY,
@ -455,6 +456,11 @@ describe("Validate Validators", () => {
message: message:
"This property is required for the widget to function correctly", "This property is required for the widget to function correctly",
}, },
{
isValid: true,
parsed: [],
message: "",
},
]; ];
inputs.forEach((input, index) => { inputs.forEach((input, index) => {
const result = validate(config, input, DUMMY_WIDGET); const result = validate(config, input, DUMMY_WIDGET);
@ -855,6 +861,25 @@ describe("Validate Validators", () => {
expect(result).toStrictEqual(expected[index]); expect(result).toStrictEqual(expected[index]);
}); });
}); });
it("correctly validates array when default is given", () => {
const inputs = [undefined, null, ""];
const config = {
type: ValidationTypes.ARRAY,
params: {
required: true,
unique: true,
default: [],
},
};
const expected = {
isValid: true,
parsed: [],
};
inputs.forEach((input) => {
const result = validate(config, input, DUMMY_WIDGET);
expect(result).toStrictEqual(expected);
});
});
}); });
// describe("Color Picker Text validator", () => { // describe("Color Picker Text validator", () => {

View File

@ -549,18 +549,27 @@ export const VALIDATORS: Record<ValidationTypes, Validator> = {
message: `${WIDGET_TYPE_VALIDATION_ERROR} ${getExpectedType(config)}`, message: `${WIDGET_TYPE_VALIDATION_ERROR} ${getExpectedType(config)}`,
}; };
if (value === undefined || value === null || value === "") { if (value === undefined || value === null || value === "") {
if (config.params && config.params.required) { if (
config.params &&
config.params.required &&
!isArray(config.params.default)
) {
invalidResponse.message = invalidResponse.message =
"This property is required for the widget to function correctly"; "This property is required for the widget to function correctly";
return invalidResponse; return invalidResponse;
} }
if (value === "") { if (value === "") {
return { return {
isValid: true, isValid: true,
parsed: config.params?.default || [], parsed: config.params?.default || [],
}; };
} }
if (config.params && isArray(config.params.default)) {
return {
isValid: true,
parsed: config.params?.default,
};
}
return { return {
isValid: true, isValid: true,