From c3f92ac7432520b852f441ec4e14e09b41b3be5e Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 13 Nov 2024 13:07:35 +0100 Subject: [PATCH] feat: Add `reset` method to file list filters This allows the files app to reset filters if needed (e.g. on navigation) instead all filters to listen to different events. Signed-off-by: Ferdinand Thiessen --- __tests__/fileListFilter.spec.ts | 14 ++++++++++++++ lib/fileListFilters.ts | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/__tests__/fileListFilter.spec.ts b/__tests__/fileListFilter.spec.ts index 6e08263f..b1691733 100644 --- a/__tests__/fileListFilter.spec.ts +++ b/__tests__/fileListFilter.spec.ts @@ -64,6 +64,20 @@ describe('File list filter class', () => { const filter = new TestFilter('my:id') expect(() => filter.filter([])).toThrowError() }) + + test('emits chips updated event', () => { + const filter = new TestFilter('my:id', 50) + const chips: IFileListFilterChip[] = [{ text: 'my chip', onclick: () => {} }] + const spy = vi.fn() + + filter.addEventListener('update:chips', spy) + filter.testUpdateChips(chips) + + expect(spy).toBeCalled() + expect(spy.mock.calls[0][0]).toBeInstanceOf(CustomEvent) + expect(spy.mock.calls[0][0].type).toBe('update:chips') + expect(spy.mock.calls[0][0].detail).toBe(chips) + }) }) describe('File list filter functions', () => { diff --git a/lib/fileListFilters.ts b/lib/fileListFilters.ts index b30db488..80cae55d 100644 --- a/lib/fileListFilters.ts +++ b/lib/fileListFilters.ts @@ -67,16 +67,28 @@ export interface IFileListFilter extends TypedEventTarget */ readonly order: number + /** + * Filter function to decide if a node is shown. + * + * @param nodes Nodes to filter + * @return Subset of the `nodes` parameter to show + */ + filter(nodes: INode[]): INode[] + /** * If the filter needs a visual element for settings it can provide a function to mount it. + * @param el The DOM element to mount to */ - readonly mount?: (el: HTMLElement) => void + mount?(el: HTMLElement): void /** - * Filter function to decide if a node is shown - * @return The nodes to be shown + * Reset the filter to the initial state. + * This is called by the files app. + * Implementations should make sure,that if they provide chips they need to emit the `update:chips` event. + * + * @since 3.10.0 */ - filter(node: INode[]): INode[] + reset?(): void } export class FileListFilter extends TypedEventTarget implements IFileListFilter {