Skip to content

Commit 9abd6db

Browse files
feat: add internal has-input-value-changed event (#4276)
* feat: introduce input value populated * Apply suggestions from code review Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com> * Change property name * Change event name * Doesn't dispatch event from initial value * Apply suggestions from code review Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com> * Rename conflicting method Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent b216a80 commit 9abd6db

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎packages/field-base/src/input-mixin.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,24 @@ export const InputMixin = dedupingMixin(
5252
observer: '_valueChanged',
5353
notify: true,
5454
},
55+
56+
/**
57+
* When true, the input element has a non-empty value entered by the user.
58+
* @protected
59+
*/
60+
_hasInputValue: {
61+
type: Boolean,
62+
value: false,
63+
observer: '_hasInputValueChanged',
64+
},
5565
};
5666
}
5767

5868
constructor() {
5969
super();
6070

6171
this._boundOnInput = this._onInput.bind(this);
62-
this._boundOnChange = this._onChange.bind(this);
72+
this._boundOnChange = this.__onChange.bind(this);
6373
}
6474

6575
/**
@@ -126,6 +136,17 @@ export const InputMixin = dedupingMixin(
126136
}
127137
}
128138

139+
/**
140+
* Observer to notify about the change of private property.
141+
*
142+
* @private
143+
*/
144+
_hasInputValueChanged(hasValue, oldHasValue) {
145+
if (hasValue || oldHasValue) {
146+
this.dispatchEvent(new CustomEvent('has-input-value-changed'));
147+
}
148+
}
149+
129150
/**
130151
* An input event listener used to update the field value.
131152
*
@@ -147,6 +168,18 @@ export const InputMixin = dedupingMixin(
147168
*/
148169
_onChange(_event) {}
149170

171+
/**
172+
* A change event listener used to update `_hasInputValue` property.
173+
* Do not override this method.
174+
*
175+
* @param {Event} event
176+
* @private
177+
*/
178+
__onChange(event) {
179+
this._hasInputValue = event.target.value.length > 0;
180+
this._onChange(event);
181+
}
182+
150183
/**
151184
* Toggle the has-value attribute based on the value property.
152185
* @param {boolean} hasValue

‎packages/field-base/test/input-mixin.test.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ const runTests = (baseClass) => {
110110
});
111111

112112
describe('events', () => {
113-
let eventsTag, inputSpy, changeSpy;
113+
let eventsTag, inputSpy, changeSpy, hasInputValueSpy;
114114

115115
before(() => {
116116
inputSpy = sinon.spy();
117117
changeSpy = sinon.spy();
118+
hasInputValueSpy = sinon.spy();
118119

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

135136
beforeEach(async () => {
136137
element = fixtureSync(`<${eventsTag}></${eventsTag}>`);
138+
element.addEventListener('has-input-value-changed', hasInputValueSpy);
137139
await nextRender();
138140
input = document.createElement('input');
139141
element.appendChild(input);
@@ -144,6 +146,7 @@ const runTests = (baseClass) => {
144146
afterEach(() => {
145147
inputSpy.resetHistory();
146148
changeSpy.resetHistory();
149+
hasInputValueSpy.resetHistory();
147150
});
148151

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

162+
it('should fire has-input-value-changed event on user value change', async () => {
163+
input.value = 'foo';
164+
input.dispatchEvent(new CustomEvent('change'));
165+
await nextFrame();
166+
167+
input.value = '';
168+
input.dispatchEvent(new CustomEvent('change'));
169+
await nextFrame();
170+
171+
expect(hasInputValueSpy.calledTwice).to.be.true;
172+
});
173+
159174
it('should not call an input event listener when input is unset', async () => {
160175
element.removeChild(input);
161176
element._setInputElement(undefined);

‎packages/time-picker/src/vaadin-time-picker.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
111111
auto-open-disabled="[[autoOpenDisabled]]"
112112
position-target="[[_inputContainer]]"
113113
theme$="[[_theme]]"
114-
on-change="__onChange"
114+
on-change="__onComboBoxChange"
115115
>
116116
<vaadin-input-container
117117
part="input-field"
@@ -592,7 +592,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
592592
}
593593

594594
/** @private */
595-
__onChange(event) {
595+
__onComboBoxChange(event) {
596596
event.stopPropagation();
597597

598598
this.validate();

0 commit comments

Comments
 (0)