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
  • Loading branch information
web-padawan committed Oct 24, 2024
1 parent 3740391 commit 52643b1
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 @@ -182,7 +182,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 @@ -281,7 +281,7 @@ export const CustomFieldMixin = (superClass) =>
}

const parseFn = this.parseValue || defaultParseValue;
const valuesArray = parseFn.apply(this, [value]);
const valuesArray = parseFn.apply(this, [value || '\t']);
if (!valuesArray || valuesArray.length === 0) {
console.warn('Value parser has not provided values array');
return;
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('aria-required', () => {
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 52643b1

Please sign in to comment.