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

fix: update vaadin-grid-filter to use input event instead of value-changed (#7275) (CP: 24.3) #7283

Merged
merged 1 commit into from
Mar 28, 2024
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
12 changes: 2 additions & 10 deletions packages/grid/src/vaadin-grid-filter-element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ export const GridFilterElementMixin = (superClass) =>

this._filterController = new SlotController(this, '', 'vaadin-text-field', {
initializer: (field) => {
field.addEventListener('value-changed', (e) => {
if (field.__previousValue === undefined && e.detail.value === '') {
field.__previousValue = e.detail.value;
return;
}
this.value = e.detail.value;
field.addEventListener('input', (e) => {
this.value = e.target.value;
});

this._textField = field;
Expand All @@ -88,12 +84,8 @@ export const GridFilterElementMixin = (superClass) =>
if (path === undefined || value === undefined || !textField) {
return;
}
if (this._previousValue === undefined && value === '') {
return;
}

textField.value = value;
this._previousValue = value;

this._debouncerFilterChanged = Debouncer.debounce(this._debouncerFilterChanged, timeOut.after(200), () => {
this.dispatchEvent(new CustomEvent('filter-changed', { bubbles: true }));
Expand Down
45 changes: 36 additions & 9 deletions packages/grid/test/filtering.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { fire, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
import { fire, fixtureSync, nextFrame, nextRender, oneEvent } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import { html, LitElement } from 'lit';
import { flushGrid, getBodyCellContent, getHeaderCellContent, getVisibleItems, scrollToEnd } from './helpers.js';
Expand All @@ -25,14 +25,16 @@ class FilterWrapper extends LitElement {
display: block;
}
</style>
<vaadin-grid-filter path="foo" .value="${this._filterValue}">
<input @input="${this._onFilterInput}" />
</vaadin-grid-filter>
<vaadin-grid-filter
path="foo"
.value="${this._filterValue}"
@value-changed="${this._onValueChanged}"
></vaadin-grid-filter>
`;
}

_onFilterInput(e) {
this._filterValue = e.target.value;
_onValueChanged(e) {
this._filterValue = e.detail.value;
}
}

Expand All @@ -51,9 +53,9 @@ describe('filter', () => {

beforeEach(async () => {
filterWrapper = fixtureSync('<filter-wrapper></filter-wrapper>');
await filterWrapper.updateComplete;
await nextRender();
filter = filterWrapper.shadowRoot.querySelector('vaadin-grid-filter');
clock = sinon.useFakeTimers();
clock = sinon.useFakeTimers({ shouldClearNativeTimers: true });
});

afterEach(() => {
Expand All @@ -68,6 +70,30 @@ describe('filter', () => {
expect(spy.calledOnce).to.be.true;
});

it('should fire `filter-changed` on field input event', async () => {
const spy = sinon.spy();
const input = filter.querySelector('input');
filter.addEventListener('filter-changed', spy);
input.value = 'foo';
fire(input, 'input');
await clock.tickAsync(200);
expect(spy.calledOnce).to.be.true;
});

it('should fire `filter-changed` on field input event with empty string', async () => {
const spy = sinon.spy();
const input = filter.querySelector('input');
filter.addEventListener('filter-changed', spy);
input.value = 'foo';
fire(input, 'input');
await clock.tickAsync(200);
spy.resetHistory();
input.value = '';
fire(input, 'input');
await clock.tickAsync(200);
expect(spy.calledOnce).to.be.true;
});

it('should fire `filter-changed` on path change', async () => {
filter.value = 'foo';
await clock.tickAsync(200);
Expand Down Expand Up @@ -245,7 +271,8 @@ describe('filtering', () => {
});

it('should apply the input fields value to the filter', async () => {
filterTextField.value = 'foo';
filterTextField.inputElement.value = 'foo';
fire(filterTextField.inputElement, 'input');
await nextFrame();
expect(filter.value).to.equal('foo');
});
Expand Down
Loading