Skip to content

Commit

Permalink
Merge pull request #1116 from nextcloud-libraries/feat/file-list-filt…
Browse files Browse the repository at this point in the history
…ers-reset

feat: Add `reset` method to file list filters
  • Loading branch information
susnux authored Nov 13, 2024
2 parents 056e208 + c3f92ac commit 34ba275
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 14 additions & 0 deletions __tests__/fileListFilter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 16 additions & 4 deletions lib/fileListFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,28 @@ export interface IFileListFilter extends TypedEventTarget<IFileListFilterEvents>
*/
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<IFileListFilterEvents> implements IFileListFilter {
Expand Down

0 comments on commit 34ba275

Please sign in to comment.