Skip to content

Commit

Permalink
fix: apply initially set custom field value to inputs (#7882)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Sep 30, 2024
1 parent 9c57813 commit 783abaa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/custom-field/src/vaadin-custom-field-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const CustomFieldMixin = (superClass) =>
inputs: {
type: Array,
readOnly: true,
observer: '__inputsChanged',
},

/**
Expand Down Expand Up @@ -249,7 +250,20 @@ export const CustomFieldMixin = (superClass) =>
/** @private */
__setInputsFromSlot() {
this._setInputs(this.__getInputsFromSlot());
this.__setValue();
}

/** @private */
__inputsChanged(inputs, oldInputs) {
if (inputs.length === 0) {
return;
}

// When inputs are first initialized, apply value set with property.
if (this.value && this.value !== '\t' && (!oldInputs || oldInputs.length === 0)) {
this.__applyInputsValue(this.value);
} else {
this.__setValue();
}
}

/** @private */
Expand All @@ -265,18 +279,25 @@ export const CustomFieldMixin = (superClass) =>
return;
}

this.__applyInputsValue(value);

if (oldValue !== undefined) {
this.validate();
}
}

/** @private */
__applyInputsValue(value) {
const parseFn = this.parseValue || defaultParseValue;
const valuesArray = parseFn.apply(this, [value]);

if (!valuesArray || valuesArray.length === 0) {
console.warn('Value parser has not provided values array');
return;
}

this.inputs.forEach((input, id) => {
input.value = valuesArray[id];
this.inputs.forEach((input, idx) => {
input.value = valuesArray[idx];
});
if (oldValue !== undefined) {
this.validate();
}
}
};
21 changes: 21 additions & 0 deletions packages/custom-field/test/custom-field.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ describe('custom field', () => {
});
});

describe('value set with attribute', () => {
beforeEach(async () => {
customField = fixtureSync(`
<vaadin-custom-field value="01\t25">
<input type="number" />
<input type="number" />
</vaadin-custom-field>
`);
await nextRender();
});

it('should not reset value set using attribute', () => {
expect(customField.value).to.equal('01\t25');
});

it('should apply value set using attribute to inputs', () => {
expect(customField.inputs[0].value).to.equal('01');
expect(customField.inputs[1].value).to.equal('25');
});
});

describe('aria-required', () => {
it('should toggle aria-required attribute on required property change', async () => {
customField.required = true;
Expand Down
24 changes: 24 additions & 0 deletions test/integration/custom-field-lazy-inputs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from '@vaadin/chai-plugins';
import { fixtureSync } from '@vaadin/testing-helpers';
import '@vaadin/custom-field';

describe('custom-field inputs', () => {
let customField;

beforeEach(async () => {
customField = fixtureSync(`
<vaadin-custom-field value="01\t25">
<vaadin-integer-field></vaadin-integer-field>
<vaadin-integer-field></vaadin-integer-field>
</vaadin-custom-field>
`);

// Ensure lazy custom element upgrade for slotted inputs.
await import('@vaadin/integer-field');
});

it('should apply value set using attribute to inputs', () => {
expect(customField.inputs[0].value).to.equal('01');
expect(customField.inputs[1].value).to.equal('25');
});
});

0 comments on commit 783abaa

Please sign in to comment.