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

Add 'filter'-event to Tree.vue #4874

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
22 changes: 21 additions & 1 deletion components/lib/tree/Tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ export interface TreeSelectionKeys {
[key: string]: any;
}

/**
* Custom filter event.
* @see {@link TreeEmits.filter}
*/
export interface TreeFilterEvent {
/**
* Original event
*/
originalEvent: Event;
/**
* Filter value
*/
value: string;
}

/**
* Custom passthrough(pt) options.
* @see {@link TreeProps.pt}
Expand Down Expand Up @@ -424,7 +439,7 @@ export interface TreeEmits {
* Emitted when the selection keys change.
* @param {TreeSelectionKeys} value - New selection keys.
*/
'update:selectionKeys'(event: TreeSelectionKeys): void;
'update:selectionKeys'(value: TreeSelectionKeys): void;
/**
* Callback to invoke when a node is selected.
* @param {TreeNode} node - Node instance.
Expand All @@ -445,6 +460,11 @@ export interface TreeEmits {
* @param {TreeNode} node - Node instance.
*/
'node-collapse'(node: TreeNode): void;
/**
* Callback to invoke on filter input.
* @param {TreeFilterEvent} event - Custom filter event.
*/
'filter'(event: TreeFilterEvent): void;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions components/lib/tree/Tree.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { mount } from '@vue/test-utils';
import PrimeVue from 'primevue/config';
import { nextTick } from 'vue';
import Tree from './Tree.vue';

describe('Tree.vue', () => {
Expand Down Expand Up @@ -42,4 +40,18 @@ describe('Tree.vue', () => {

expect(wrapper.emitted('keydown')).toBeFalsy();
});

it('emits update:filterValue on filter input', async () => {
wrapper = mount(Tree, {
props: {
filter: true,
},
});

let searchField = wrapper.find('input.p-tree-filter');

await searchField.trigger('keydown.space');

expect(wrapper.emitted('filter')).toBeTruthy();
})
});
4 changes: 3 additions & 1 deletion components/lib/tree/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import TreeNode from './TreeNode.vue';
export default {
name: 'Tree',
extends: BaseTree,
emits: ['node-expand', 'node-collapse', 'update:expandedKeys', 'update:selectionKeys', 'node-select', 'node-unselect'],
emits: ['node-expand', 'node-collapse', 'update:expandedKeys', 'update:selectionKeys', 'node-select', 'node-unselect', 'filter'],
data() {
return {
d_expandedKeys: this.expandedKeys || {},
Expand Down Expand Up @@ -166,6 +166,8 @@ export default {
if (event.code === 'Enter') {
event.preventDefault();
}

this.$emit('filter', { originalEvent: event, value: event.target.value });
},
findFilteredNodes(node, paramsWithoutNode) {
if (node) {
Expand Down
Loading