Skip to content

Commit

Permalink
#1649 Added a new parameter in setPropertyValues() to set default pro…
Browse files Browse the repository at this point in the history
…perties (#1650)
  • Loading branch information
nmgokhale authored Dec 19, 2023
1 parent 331195f commit 1f02a44
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import datasetMetadata from "../test_resources/json/datasetMetadata.json";
import structureListEditorParamDef from "../test_resources/paramDefs/structurelisteditor_paramDef.json";
import structureTableParamDef from "../test_resources/paramDefs/structuretable_paramDef.json";
import checkboxsetParamDef from "../test_resources/paramDefs/checkboxset_paramDef.json";
import checkboxParamDef from "../test_resources/paramDefs/checkbox_paramDef.json";
import actionParamDef from "../test_resources/paramDefs/action_paramDef.json";
import numberfieldParamDef from "../test_resources/paramDefs/numberfield_paramDef.json";
import structuretablePropertyValues from "../test_resources/json/structuretable_propertyValues.json";
Expand Down Expand Up @@ -1193,6 +1194,24 @@ describe("Properties Controller handlers", () => {
const actual = controller.getPropertyValues();
expect(actual).to.eql(values);
});
it("should set default values when setPropertyValues() is called with option { setDefaultValues: true }", () => {
const renderedObject = testUtils.flyoutEditorForm(checkboxParamDef);
controller = renderedObject.controller;

const filteredValues = controller.getPropertyValues({ filterHiddenDisabled: true });
// We filtered hidden and disabled properties, so "checkbox_hidden" property doesn't exist in filteredValues
expect(filteredValues).not.to.have.property("checkbox_hidden");

// setDefaultValues is not set
controller.setPropertyValues(filteredValues);
// Verify value is not set for checkbox_hidden
expect(controller.getPropertyValues()).not.to.have.property("checkbox_hidden");

// setDefaultValues is set to true
controller.setPropertyValues(filteredValues, { setDefaultValues: true });
// Verify a value is set for checkbox_hidden
expect(controller.getPropertyValues()).to.have.property("checkbox_hidden", true);
});
it("should fire event on updatePropertyValue", () => {
controller.updatePropertyValue({ name: "param_int" }, 10);
expect(propertyListener).to.have.property("callCount", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"checkbox_error": false,
"checkbox_warning": false,
"disable": true,
"hide": true,
"checkbox_hidden": false
"hide": true
},
"parameters": [
{
Expand Down Expand Up @@ -69,7 +68,8 @@
{
"id": "checkbox_hidden",
"type": "boolean",
"required": true
"required": true,
"default": true
}
],
"complex_types": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class PropertiesController {
}
// Set the opening dataset(s), during which multiples are flattened and compound names generated if necessary
this.setDatasetMetadata(datasetMetadata);
this.setPropertyValues(propertyValues, true); // needs to be after setDatasetMetadata to run conditions
this.setPropertyValues(propertyValues, { isInitProps: true }); // needs to be after setDatasetMetadata to run conditions
this.differentProperties = [];
if (sameParameterDefRendered) {
// When a parameterDef is dynamically updated, set difference between old and new controls
Expand Down Expand Up @@ -1207,7 +1207,12 @@ export default class PropertiesController {
return returnValues;
}

setPropertyValues(values, isInitProps) {
/*
* options - optional object of config options where
* setDefaultValues: true - set default values from parameter definition
* isInitProps: true - Function is called during initial load. This is only used internally in setForm().
*/
setPropertyValues(values, options) {
let inValues = cloneDeep(values);

// convert currentParameters of type:object to array values
Expand All @@ -1229,7 +1234,7 @@ export default class PropertiesController {

this.propertiesStore.setPropertyValues(inValues);

if (isInitProps) {
if (options && options.isInitProps) {
// Evaluate conditional defaults based on current_parameters upon loading of view
// For a given parameter_ref, if multiple conditions evaluate to true only the first one is used.
const conditionalDefaultValues = {};
Expand Down Expand Up @@ -1264,6 +1269,10 @@ export default class PropertiesController {
}
);
}

if (options && options.setDefaultValues) {
this._addToControlValues(true);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"checkbox_error": false,
"checkbox_warning": false,
"disable": true,
"hide": true,
"checkbox_hidden": false
"hide": true
},
"parameters": [
{
Expand Down Expand Up @@ -69,7 +68,8 @@
{
"id": "checkbox_hidden",
"type": "boolean",
"required": true
"required": true,
"default": true
}
],
"complex_types": [
Expand Down

0 comments on commit 1f02a44

Please sign in to comment.