Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply initially set custom field value to inputs #7882

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});