Skip to content

Commit

Permalink
fix: clear input values when setting value property to null (#8017) (#…
Browse files Browse the repository at this point in the history
…8018)

Co-authored-by: Serhii Kulykov <[email protected]>
  • Loading branch information
vaadin-bot and web-padawan authored Oct 24, 2024
1 parent 0cf89ed commit b643907
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/custom-field/src/vaadin-custom-field-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const CustomFieldMixin = (superClass) =>
checkValidity() {
const invalidFields = this.inputs.filter((input) => !(input.validate || input.checkValidity).call(input));

if (invalidFields.length || (this.required && !this.value.trim())) {
if (invalidFields.length || (this.required && !(this.value && this.value.trim()))) {
// Either 1. one of the input fields is invalid or
// 2. the custom field itself is required but doesn't have a value
return false;
Expand Down Expand Up @@ -294,7 +294,7 @@ export const CustomFieldMixin = (superClass) =>
return;
}

this.__applyInputsValue(value);
this.__applyInputsValue(value || '\t');

if (oldValue !== undefined) {
this.validate();
Expand Down
11 changes: 11 additions & 0 deletions packages/custom-field/test/custom-field.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ describe('custom field', () => {
await nextUpdate(customField);
expect(customField.hasAttribute('has-value')).to.be.true;
});

it('should clear input values when set to null', async () => {
customField.value = '1\t1';
await nextUpdate(customField);

customField.value = null;
await nextUpdate(customField);
customField.inputs.forEach((el) => {
expect(el.value).to.equal('');
});
});
});

describe('value set with attribute', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/custom-field/test/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ describe('validation', () => {
expect(customField.invalid).to.be.true;
});

it('should return false on checkValidity call when value is set to null', () => {
customField.value = null;
expect(customField.checkValidity()).to.be.false;
});

it('should become valid after receiving a non-empty value from "change" event', () => {
customField.inputs[0].value = 'foo';
fire(customField.inputs[0], 'change');
Expand Down

0 comments on commit b643907

Please sign in to comment.