Skip to content

Commit

Permalink
fix(gv-schema-form-control): update simple properties when is required
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Jun 10, 2021
1 parent 6502e7b commit ca929e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
35 changes: 25 additions & 10 deletions src/organisms/gv-schema-form-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export class GvSchemaFormControl extends LitElement {
};
}

constructor() {
super();
// When form has errors, the control is not update to conserve the error state.
// This list is used to force the update of properties even in error case.
this._observedProperties = ['disabled', 'readonly', 'required'];
}

isExpressionLanguage() {
return this.control['x-schema-form'] && this.control['x-schema-form']['expression-language'] != null;
}
Expand Down Expand Up @@ -97,12 +104,22 @@ export class GvSchemaFormControl extends LitElement {
return this.control.type === 'array' && !this.control.items.enum;
}

_updateProperties(element) {
if (element != null) {
this._observedProperties.forEach((property) => {
if (this[property] != null) {
element[property] = this[property];
}
});
}
}

_renderControl() {
const elementName = this.getElementName();
const element = document.createElement(elementName);
element.skeleton = this.skeleton;
element.id = this.id;
element.name = this.id;
element.skeleton = this.skeleton;
element.classList.add('form__control');

if (this.value != null) {
Expand All @@ -121,15 +138,9 @@ export class GvSchemaFormControl extends LitElement {
} else if (this.isComplexArray()) {
element.schema = this.control.items;
}
if (this.required != null) {
element.required = this.required;
}
if (this.disabled != null) {
element.disabled = this.disabled;
}
if (this.readonly != null) {
element.readonly = this.readonly;
}

this._updateProperties(element);

if (this.control.title) {
element.label = this.control.title;
element.title = this.control.title;
Expand Down Expand Up @@ -269,6 +280,10 @@ export class GvSchemaFormControl extends LitElement {
}

shouldUpdate(changedProperties) {
if (this._observedProperties.find((property) => changedProperties.has(property)) != null) {
this._updateProperties(this.getControl());
}

if (changedProperties.has('errors') && this.errors != null) {
// Set errors to complex controls
this.getControls().forEach((control) => {
Expand Down
18 changes: 8 additions & 10 deletions src/organisms/gv-schema-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,7 @@ export class GvSchemaForm extends LitElement {
// This is require to clean cache of <gv-schema-form-control>
const control = { ...this.schema.properties[key] };
const isRequired = this.schema.required && this.schema.required.includes(key);

let isDisabled = this.schema.disabled && this.schema.disabled.includes(key);
const condition = control['x-schema-form'] && control['x-schema-form'].disabled;
if (!isDisabled && condition) {
// test 'x-scheam-form.disabled' only if the field isn't explicitly defined into the 'disabled' array
isDisabled = this._evaluateDisabledCondition(condition);
}
const isDisabled = (this.schema.disabled && this.schema.disabled.includes(key)) || this._evaluateCondition(control, 'disabled');

const value = get(this._values, key);
return html`<gv-schema-form-control
Expand All @@ -256,10 +250,14 @@ export class GvSchemaForm extends LitElement {
></gv-schema-form-control>`;
}

_evaluateDisabledCondition(condition) {
_evaluateCondition(control, conditionKey) {
if (control['x-schema-form'] == null || control['x-schema-form'][conditionKey] == null) {
return false;
}
const condition = control['x-schema-form'][conditionKey];
if (!Array.isArray(condition)) {
// condition isn't an array, ignore the condition
console.warn("'disable' attribute of 'x-schema-form' should be an array");
console.warn(`'${conditionKey}' attribute of 'x-schema-form' should be an array`);
return false;
}

Expand All @@ -281,7 +279,7 @@ export class GvSchemaForm extends LitElement {
result = result && this._evaluateDefCondition(operation);
break;
default:
console.warn("Unsupported operator '" + operator + "' on disable condition");
console.warn(`Unsupported operator '${operator}' on disable condition`);
result = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion stories/resources/schemas/mixed.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
}
},
"disabled": {
"title": "Disbaled field if 'Simple select' is 'a'",
"title": "Disabled field if 'Simple select' is 'a'",
"type": "string",
"x-schema-form": {
"disabled": [{ "$def": "select" }, { "$eq": { "select": "a" } }]
Expand Down

0 comments on commit ca929e3

Please sign in to comment.