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 #4276

Merged
merged 7 commits into from
Jul 28, 2022
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
35 changes: 34 additions & 1 deletion packages/field-base/src/input-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,24 @@ export const InputMixin = dedupingMixin(
observer: '_valueChanged',
notify: true,
},

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

constructor() {
super();

this._boundOnInput = this._onInput.bind(this);
this._boundOnChange = this._onChange.bind(this);
this._boundOnChange = this.__onChange.bind(this);
}

/**
Expand Down Expand Up @@ -126,6 +136,17 @@ export const InputMixin = dedupingMixin(
}
}

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

/**
* An input event listener used to update the field value.
*
Expand All @@ -147,6 +168,18 @@ export const InputMixin = dedupingMixin(
*/
_onChange(_event) {}

/**
* A change event listener used to update `_hasInputValue` property.
* Do not override this method.
*
* @param {Event} event
* @private
*/
__onChange(event) {
this._hasInputValue = event.target.value.length > 0;
this._onChange(event);
}

/**
* Toggle the has-value attribute based on the value property.
* @param {boolean} hasValue
Expand Down
17 changes: 16 additions & 1 deletion packages/field-base/test/input-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ const runTests = (baseClass) => {
});

describe('events', () => {
let eventsTag, inputSpy, changeSpy;
let eventsTag, inputSpy, changeSpy, hasInputValueSpy;

before(() => {
inputSpy = sinon.spy();
changeSpy = sinon.spy();
hasInputValueSpy = sinon.spy();

eventsTag = define[baseClass](
'input-mixin-events',
Expand All @@ -134,6 +135,7 @@ const runTests = (baseClass) => {

beforeEach(async () => {
element = fixtureSync(`<${eventsTag}></${eventsTag}>`);
element.addEventListener('has-input-value-changed', hasInputValueSpy);
await nextRender();
input = document.createElement('input');
element.appendChild(input);
Expand All @@ -144,6 +146,7 @@ const runTests = (baseClass) => {
afterEach(() => {
inputSpy.resetHistory();
changeSpy.resetHistory();
hasInputValueSpy.resetHistory();
});

it('should call an input event listener', () => {
Expand All @@ -156,6 +159,18 @@ const runTests = (baseClass) => {
expect(changeSpy.calledOnce).to.be.true;
});

it('should fire has-input-value-changed event on user value change', async () => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('change'));
await nextFrame();

input.value = '';
input.dispatchEvent(new CustomEvent('change'));
await nextFrame();

expect(hasInputValueSpy.calledTwice).to.be.true;
});

it('should not call an input event listener when input is unset', async () => {
element.removeChild(input);
element._setInputElement(undefined);
Expand Down
4 changes: 2 additions & 2 deletions packages/time-picker/src/vaadin-time-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
auto-open-disabled="[[autoOpenDisabled]]"
position-target="[[_inputContainer]]"
theme$="[[_theme]]"
on-change="__onChange"
on-change="__onComboBoxChange"
>
<vaadin-input-container
part="input-field"
Expand Down Expand Up @@ -581,7 +581,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
}

/** @private */
__onChange(event) {
__onComboBoxChange(event) {
event.stopPropagation();

this.validate();
Expand Down