[improvement] TreeSelect: rename event name (#3892)

This commit is contained in:
neverland 2019-07-18 20:51:16 +08:00 committed by GitHub
parent ee09f34bc2
commit 6aaab466af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 24 deletions

View File

@ -17,8 +17,8 @@ Vue.use(TreeSelect);
:items="items" :items="items"
:main-active-index="mainActiveIndex" :main-active-index="mainActiveIndex"
:active-id="activeId" :active-id="activeId"
@navclick="onNavClick" @click-nav="onClickNav"
@itemclick="onItemClick" @click-item="onClickItem"
/> />
``` ```
@ -34,10 +34,10 @@ export default {
}; };
}, },
methods: { methods: {
onNavClick(index) { onClickNav(index) {
this.mainActiveIndex = index; this.mainActiveIndex = index;
}, },
onItemClick(data) { onClickItem(data) {
this.activeId = data.id; this.activeId = data.id;
} }
} }
@ -59,8 +59,8 @@ export default {
| Event | Description | Arguments | | Event | Description | Arguments |
|------|------|------| |------|------|------|
| navclick | triggered when parent node is selected | index: index of selected parent | | click-nav | triggered when parent node is selected | index: index of selected parent |
| itemclick | triggered when item is selected | data: selected item | | click-item | triggered when item is selected | data: selected item |
### Data Structure of Item ### Data Structure of Item

View File

@ -17,8 +17,8 @@ Vue.use(TreeSelect);
:items="items" :items="items"
:main-active-index="mainActiveIndex" :main-active-index="mainActiveIndex"
:active-id="activeId" :active-id="activeId"
@navclick="onNavClick" @click-nav="onClickNav"
@itemclick="onItemClick" @click-item="onClickItem"
/> />
``` ```
@ -34,10 +34,10 @@ export default {
}; };
}, },
methods: { methods: {
onNavClick(index) { onClickNav(index) {
this.mainActiveIndex = index; this.mainActiveIndex = index;
}, },
onItemClick(data) { onClickItem(data) {
this.activeId = data.id; this.activeId = data.id;
} }
} }
@ -59,8 +59,8 @@ export default {
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
|------|------|------| |------|------|------|
| navclick | 左侧导航点击触发的事件 | index被点击的导航的索引 | | click-nav | 点击左侧导航时触发 | index被点击的导航的索引 |
| itemclick | 右侧选择项被点击,会触发的事件 | data: 该点击项的数据 | | click-item | 点击右侧选择项时触发 | data: 该点击项的数据 |
### Item 数据结构 ### Item 数据结构

View File

@ -53,6 +53,9 @@ function TreeSelect(
]} ]}
onClick={() => { onClick={() => {
if (!item.disabled) { if (!item.disabled) {
emit(ctx, 'click-nav', index);
// compatible for old usage, should be removed in next major version
emit(ctx, 'navclick', index); emit(ctx, 'navclick', index);
} }
}} }}
@ -74,6 +77,9 @@ function TreeSelect(
]} ]}
onClick={() => { onClick={() => {
if (!item.disabled) { if (!item.disabled) {
emit(ctx, 'click-item', item);
// compatible for old usage, should be removed in next major version
emit(ctx, 'itemclick', item); emit(ctx, 'itemclick', item);
} }
}} }}

View File

@ -5,8 +5,9 @@ test('empty list', () => {
expect(mount(TreeSelect)).toMatchSnapshot(); expect(mount(TreeSelect)).toMatchSnapshot();
}); });
test('select item', () => { test('click-nav event', () => {
const onItemClick = jest.fn(); const onNavClick = jest.fn();
const onClickNav = jest.fn();
const item = { const item = {
text: 'city1', text: 'city1',
id: 1 id: 1
@ -14,17 +15,48 @@ test('select item', () => {
const wrapper = mount(TreeSelect, { const wrapper = mount(TreeSelect, {
propsData: { propsData: {
items: [{ items: [
text: 'group1', {
children: [ text: 'group1',
item, children: [item]
{ ...item, disabled: true } }
] ]
}]
}, },
context: { context: {
on: { on: {
itemclick: onItemClick navclick: onNavClick,
'click-nav': onClickNav
}
}
});
const items = wrapper.findAll('.van-tree-select__nav-item');
items.at(0).trigger('click');
expect(onNavClick).toHaveBeenCalledWith(0);
expect(onClickNav).toHaveBeenCalledWith(0);
});
test('click-item event', () => {
const onItemClick = jest.fn();
const onClickItem = jest.fn();
const item = {
text: 'city1',
id: 1
};
const wrapper = mount(TreeSelect, {
propsData: {
items: [
{
text: 'group1',
children: [item]
}
]
},
context: {
on: {
itemclick: onItemClick,
'click-item': onClickItem
} }
} }
}); });
@ -32,6 +64,63 @@ test('select item', () => {
const items = wrapper.findAll('.van-tree-select__item'); const items = wrapper.findAll('.van-tree-select__item');
items.at(0).trigger('click'); items.at(0).trigger('click');
expect(onItemClick).toHaveBeenCalledWith(item); expect(onItemClick).toHaveBeenCalledWith(item);
items.at(1).trigger('click'); expect(onClickItem).toHaveBeenCalledWith(item);
expect(onItemClick).toHaveBeenCalledTimes(1); });
test('click disabled nav', () => {
const onClickNav = jest.fn();
const item = {
text: 'city1',
id: 1
};
const wrapper = mount(TreeSelect, {
propsData: {
items: [
{
text: 'group1',
children: [item],
disabled: true
}
]
},
context: {
on: {
'click-nav': onClickNav
}
}
});
const items = wrapper.findAll('.van-tree-select__nav-item');
items.at(0).trigger('click');
expect(onClickNav).toHaveBeenCalledTimes(0);
});
test('click disabled item', () => {
const onClickItem = jest.fn();
const wrapper = mount(TreeSelect, {
propsData: {
items: [
{
text: 'group1',
children: [
{
text: 'city1',
id: 1,
disabled: true
}
]
}
]
},
context: {
on: {
'click-item': onClickItem
}
}
});
const items = wrapper.findAll('.van-tree-select__item');
items.at(0).trigger('click');
expect(onClickItem).toHaveBeenCalledTimes(0);
}); });