Skip to content

Commit

Permalink
Merge pull request #467 from ghiscoding/feat/reset-search-value-on-be…
Browse files Browse the repository at this point in the history
…fore-search-cancelled

refactor: reset to previous value when event cancelled enhanced #465
  • Loading branch information
ghiscoding authored Sep 4, 2021
2 parents b4215ac + 6f4ed36 commit 57e42e5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class Example09 {
// });
// this._bindingEventService.bind(gridContainerElm, 'onbeforesearchchange', (e) => {
// e.preventDefault();
// this.sgb.filterService.resetToPreviousSearchFilters(); // optionally reset filter input value
// return false;
// });
// this._bindingEventService.bind(gridContainerElm, 'onbeforepaginationchange', (e) => {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/global-grid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export const GlobalGridOptions: GridOption = {
rowHeight: 35,
topPanelHeight: 30,
translationNamespaceSeparator: ':',
resetFilterSearchValueAfterOnBeforeCancellation: true,
resizeByContentOnlyOnFirstLoad: true,
resizeByContentOptions: {
alwaysRecalculateColumnWidth: false,
Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/interfaces/gridOption.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ export interface GridOption {
/** Register any external Resources (Components, Services) like the ExcelExportService, TextExportService, SlickCompositeEditorComponent, ... */
registerExternalResources?: ExternalResource[];

/** Defaults to true, should we reset (rollback) the search filter input value to its previous value when the `onBeforeSearchChange` event bubbling is prevented? */
resetFilterSearchValueAfterOnBeforeCancellation?: boolean;

/**
* defaults to true, do we want to resize the grid by content only on the first page or anytime the data changes?
* Requires `enableAutoResizeColumnsByCellContent` to be set.
Expand Down
16 changes: 16 additions & 0 deletions packages/common/src/services/__tests__/filter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,22 @@ describe('FilterService', () => {
});
});

it('should call "resetToPreviousSearchFilters" when "onBeforeSearchChange" event is prevented from bubbling and "resetFilterSearchValueAfterOnBeforeCancellation" is set to true', async () => {
const pubSubSpy = jest.spyOn(pubSubServiceStub, 'publish').mockReturnValue(false);
const resetPrevSpy = jest.spyOn(service, 'resetToPreviousSearchFilters');

gridOptionMock.resetFilterSearchValueAfterOnBeforeCancellation = true;
service.init(gridStub);
service.bindLocalOnFilter(gridStub);
gridStub.onHeaderRowCellRendered.notify(mockArgs1 as any, new Slick.EventData(), gridStub);
gridStub.onHeaderRowCellRendered.notify(mockArgs2 as any, new Slick.EventData(), gridStub);
await service.updateFilters(mockNewFilters, false, false, true);

expect(pubSubSpy).toHaveBeenCalledWith('onBeforeSearchChange', expect.toBeObject());
expect(resetPrevSpy).toHaveBeenCalled();
jest.spyOn(pubSubServiceStub, 'publish').mockReturnValue(true);
});

it('should expect filters to be set in ColumnFilters when using "bindLocalOnFilter" without triggering a filter changed event when 2nd flag argument is set to false', async () => {
const clearSpy = jest.spyOn(service, 'clearFilters');
const emitSpy = jest.spyOn(service, 'emitFilterChanged');
Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/services/filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,13 @@ export class FilterService {
parsedSearchTerms,
grid: this._grid
} as OnSearchChangeEventArgs;
if (this.pubSubService.publish('onBeforeSearchChange', eventArgs) !== false) {

const onBeforeDispatchResult = this.pubSubService.publish('onBeforeSearchChange', eventArgs);
if (onBeforeDispatchResult === false) {
if (this._gridOptions.resetFilterSearchValueAfterOnBeforeCancellation) {
this.resetToPreviousSearchFilters();
}
} else {
this._onSearchChange.notify(eventArgs, eventData);
}
}
Expand Down

0 comments on commit 57e42e5

Please sign in to comment.