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

feat: fire a validated event on validation (CP: 14) #827

Merged
merged 3 commits into from
Apr 18, 2023
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
22 changes: 17 additions & 5 deletions src/vaadin-date-picker-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,20 @@
}

/**
* Returns true if `value` is valid, and sets the `invalid` flag appropriately.
* Validates the field and sets the `invalid` property based on the result.
*
* @param {string=} value Value to validate. Optional, defaults to user's input value.
* @return {boolean} True if the value is valid and sets the `invalid` flag appropriately
* The method fires a `validated` event with the result of the validation.
*
* @return {boolean} True if the value is valid.
*/
validate() {
// Note (Yuriy): Workaround `this._inputValue` is used in order
// to avoid breaking change on custom `checkValidity`.
// Can be removed with next major.
return !(this.invalid = !this.checkValidity(this._inputValue));
const isValid = this.checkValidity(this._inputValue);
this.invalid = !isValid;
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}}));
return isValid;
}

/**
Expand Down Expand Up @@ -858,7 +862,7 @@
}
}

return inputValid && minMaxValid && inputValidity;
return !!(inputValid && minMaxValid && inputValidity);
}

/** @private */
Expand Down Expand Up @@ -1056,5 +1060,13 @@
*
* @event change
*/

/**
* Fired whenever the field is validated.
*
* @event validated
* @param {Object} detail
* @param {boolean} detail.valid the result of the validation.
*/
};
</script>
3 changes: 2 additions & 1 deletion test/test-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ window.VaadinDatePickerSuites = [
'keyboard-navigation.html',
'keyboard-input.html',
'theme-propagation.html',
'wai-aria.html'
'wai-aria.html',
'validation.html'
];

if (isPolymer2) {
Expand Down
48 changes: 48 additions & 0 deletions test/validation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!doctype html>

<head>
<meta charset="UTF-8">
<title>vaadin-date-picker validation tests</title>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../vaadin-date-picker.html">
</head>

<body>
<test-fixture id="date-picker">
<template>
<vaadin-date-picker></vaadin-date-picker>
</template>
</test-fixture>
<script>
describe('basic', () => {
let datePicker;

beforeEach(() => {
datePicker = fixture('date-picker');
});

it('should fire a validated event on validation success', () => {
const validatedSpy = sinon.spy();
datePicker.addEventListener('validated', validatedSpy);
datePicker.validate();

expect(validatedSpy.calledOnce).to.be.true;
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.true;
});

it('should fire a validated event on validation failure', () => {
const validatedSpy = sinon.spy();
datePicker.addEventListener('validated', validatedSpy);
datePicker.required = true;
datePicker.validate();

expect(validatedSpy.calledOnce).to.be.true;
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.false;
});
});
</script>
</body>