Skip to content

Commit

Permalink
feat: add methods to make setting invalid overridable (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi authored Apr 17, 2023
1 parent 3eb1b2e commit 92931ed
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 14 deletions.
25 changes: 23 additions & 2 deletions src/vaadin-text-field-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@
}

if (!required && !minlength && !maxlength && !pattern) {
this.invalid = false;
this._setInvalid(false);
} else {
this.validate();
}
Expand Down Expand Up @@ -712,6 +712,27 @@
});
}

/**
* @param {boolean} invalid
* @protected
*/
_setInvalid(invalid) {
if (this._shouldSetInvalid(invalid)) {
this.invalid = invalid;
}
}

/**
* Override this method to define whether the given `invalid` state should be set.
*
* @param {boolean} _invalid
* @return {boolean}
* @protected
*/
_shouldSetInvalid(_invalid) {
return true;
}

/**
* Validates the field and sets the `invalid` property based on the result.
*
Expand All @@ -721,7 +742,7 @@
*/
validate() {
const isValid = this.checkValidity();
this.invalid = !isValid;
this._setInvalid(!isValid);
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}}));
return isValid;
}
Expand Down
22 changes: 20 additions & 2 deletions test/email-field-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="email-field">
<template>
<vaadin-email-field></vaadin-email-field>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let emailField;

beforeEach(() => {
emailField = fixture('basic');
emailField = fixture('email-field');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let emailField;

beforeEach(async() => {
emailField = fixture('email-field');
emailField._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
emailField.required = true;
emailField.validate();
expect(emailField.invalid).to.be.true;
emailField.value = 'email@example.com';
emailField.validate();
expect(emailField.invalid).to.be.true;
});
});
</script>
</body>
22 changes: 20 additions & 2 deletions test/integer-field-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="integer-field">
<template>
<vaadin-integer-field></vaadin-integer-field>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let integerField;

beforeEach(() => {
integerField = fixture('basic');
integerField = fixture('integer-field');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let integerField;

beforeEach(async() => {
integerField = fixture('integer-field');
integerField._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
integerField.required = true;
integerField.validate();
expect(integerField.invalid).to.be.true;
integerField.value = 5;
integerField.validate();
expect(integerField.invalid).to.be.true;
});
});
</script>
</body>
22 changes: 20 additions & 2 deletions test/number-field-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="number-field">
<template>
<vaadin-number-field></vaadin-number-field>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let numberField;

beforeEach(() => {
numberField = fixture('basic');
numberField = fixture('number-field');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let numberField;

beforeEach(async() => {
numberField = fixture('number-field');
numberField._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
numberField.required = true;
numberField.validate();
expect(numberField.invalid).to.be.true;
numberField.value = 5;
numberField.validate();
expect(numberField.invalid).to.be.true;
});
});
</script>
</body>
22 changes: 20 additions & 2 deletions test/password-field-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="password-field">
<template>
<vaadin-password-field></vaadin-password-field>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let passwordField;

beforeEach(() => {
passwordField = fixture('basic');
passwordField = fixture('password-field');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let passwordField;

beforeEach(async() => {
passwordField = fixture('password-field');
passwordField._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
passwordField.required = true;
passwordField.validate();
expect(passwordField.invalid).to.be.true;
passwordField.value = 'value';
passwordField.validate();
expect(passwordField.invalid).to.be.true;
});
});
</script>
</body>
22 changes: 20 additions & 2 deletions test/text-area-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="text-area">
<template>
<vaadin-text-area></vaadin-text-area>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let textArea;

beforeEach(() => {
textArea = fixture('basic');
textArea = fixture('text-area');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let textArea;

beforeEach(async() => {
textArea = fixture('text-area');
textArea._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
textArea.required = true;
textArea.validate();
expect(textArea.invalid).to.be.true;
textArea.value = 'value';
textArea.validate();
expect(textArea.invalid).to.be.true;
});
});
</script>
</body>
22 changes: 20 additions & 2 deletions test/text-field-validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</head>

<body>
<test-fixture id="basic">
<test-fixture id="text-field">
<template>
<vaadin-text-field></vaadin-text-field>
</template>
Expand Down Expand Up @@ -55,7 +55,7 @@
let textField;

beforeEach(() => {
textField = fixture('basic');
textField = fixture('text-field');
});

it('should fire a validated event on validation success', () => {
Expand All @@ -79,5 +79,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let textField;

beforeEach(async() => {
textField = fixture('text-field');
textField._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
textField.required = true;
textField.validate();
expect(textField.invalid).to.be.true;
textField.value = 'value';
textField.validate();
expect(textField.invalid).to.be.true;
});
});
</script>
</body>

0 comments on commit 92931ed

Please sign in to comment.