Skip to content

Commit

Permalink
fix(tree): custom icons cannot be clicked without child nodes (#4638)
Browse files Browse the repository at this point in the history
* fix(tree): custom icons cannot be clicked without child nodes

close #4571

* test: add test example
  • Loading branch information
RSS1102 authored Oct 11, 2024
1 parent e5096e6 commit ad5a482
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/tree/__tests__/expand.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { mount } from '@vue/test-utils';
import Tree from '@/src/tree/index.ts';
import { delay } from './kit';
import { ref } from './adapt';
import { Icon } from 'tdesign-icons-vue-next';
import { nextTick } from 'vue';
import { get } from 'lodash';

describe('Tree:expand', () => {
vi.useRealTimers();
Expand Down Expand Up @@ -376,6 +379,37 @@ describe('Tree:expand', () => {
expect(wrapper.find('[data-value="t1.1"]').exists()).toBe(true);
});

it('自定义图标-点击父节点图标可触发展开子节点', async () => {
const data = [
{
value: 't1',
children: [
{
value: 't1.1',
},
],
},
];
const wrapper = mount({
render() {
return <Tree transition={false} data={data} icon={() => <Icon name="folder" />}></Tree>;
},
});
// 测试: 点击一级 一级展开 二级存在
wrapper.find('[data-value="t1"] .t-tree__icon').trigger('click');
await delay(10);
const t1d1 = wrapper.find('[data-value="t1.1"]');
expect(t1d1.attributes('class')).toContain('t-tree__item--visible');
const t1 = wrapper.find('[data-value="t1"]');
expect(t1.attributes('class')).toContain('t-tree__item--open');

// 测试:点击二级 二级状态不展开
wrapper.find('[data-value="t1.1"] .t-tree__icon').trigger('click');
await delay(10);
const t1d1_two = wrapper.find('[data-value="t1.1"]');
expect(t1d1_two.attributes('class')).not.toContain('t-tree__item--open');
});

it('点击已展开的父节点图标,可触发收起子节点', async () => {
const data = [
{
Expand Down
3 changes: 3 additions & 0 deletions src/tree/hooks/useTreeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export default function useTreeAction(state: TypeTreeState) {

const toggleExpanded = (item: TypeTargetNode): TreeNodeValue[] => {
const node = getNode(store, item);

if (!node.children) return;

return setExpanded(node, !node.isExpanded());
};

Expand Down

0 comments on commit ad5a482

Please sign in to comment.