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: add internal has-input-value-changed event (CP: 14) #829

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions src/vaadin-date-picker-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@
computed: '_isNoInput(_fullscreen, _ios, i18n, i18n.*, opened, autoOpenDisabled)'
},

/**
* When true, the input element has a non-empty value entered by the user.
* @protected
*/
_hasInputValue: {
type: Boolean,
value: false,
observer: '_hasInputValueChanged',
},

/** @private */
_ios: {
type: Boolean,
Expand Down Expand Up @@ -399,6 +409,7 @@
});
this.addEventListener('keydown', this._onKeydown.bind(this));
this.addEventListener('input', this._onUserInput.bind(this));
this._inputElement.addEventListener('input', this.__onUserInput.bind(this));
vursen marked this conversation as resolved.
Show resolved Hide resolved
this.addEventListener('focus', e => this._noInput && e.target.blur());
this.addEventListener('blur', e => {
if (!this.opened) {
Expand Down Expand Up @@ -1035,6 +1046,38 @@
return parsedDate;
}

/**
* Sets the `_hasInputValue` property based on the `input` event.
*
* @param {InputEvent} event
* @protected
*/
_setHasInputValue(event) {
this._hasInputValue = event.target.value.length > 0;
}

/**
* An input event listener used to update `_hasInputValue` property.
* Do not override this method.
*
* @param {Event} event
* @private
*/
__onUserInput(event) {
this._setHasInputValue(event);
}

/**
* Observer to notify about the change of private property.
*
* @private
*/
_hasInputValueChanged(hasValue, oldHasValue) {
if (hasValue || oldHasValue) {
this.dispatchEvent(new CustomEvent('has-input-value-changed'));
}
}

/** @private */
_onUserInput(e) {
if (!this.opened && this._inputElement.value && !this.autoOpenDisabled) {
Expand Down
55 changes: 55 additions & 0 deletions test/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>

<head>
<meta charset="UTF-8">
<title>vaadin-date-picker events 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('events', () => {
let datePicker, input;

describe('has-input-value-changed event', () => {
beforeEach(async() => {
datePicker = fixture('date-picker');
input = datePicker._inputElement;
});

it('should fire the event on input value presence change', async() => {
const hasInputValueChangedSpy = sinon.spy();
datePicker.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '2000-02-01';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;

hasInputValueChangedSpy.reset();
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
});

it('should not fire the event on input if input value presence has not changed', async() => {
const hasInputValueChangedSpy = sinon.spy();
datePicker.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '2000-02-01';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();

input.value = '2000-02-20';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
});
});
});
</script>
</body>
3 changes: 2 additions & 1 deletion test/test-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ window.VaadinDatePickerSuites = [
'keyboard-input.html',
'theme-propagation.html',
'wai-aria.html',
'validation.html'
'validation.html',
'events.html'
];

if (isPolymer2) {
Expand Down