feat(Cascader): add events

This commit is contained in:
chenjiahan 2020-12-20 14:20:21 +08:00 committed by neverland
parent 3f429142ac
commit 95cae6021c
3 changed files with 73 additions and 41 deletions

View File

@ -49,9 +49,10 @@ export default {
### Events ### Events
| 事件 | 说明 | 回调参数 | | 事件 | 说明 | 回调参数 |
| ------- | ---------------- | -------- | | ------ | ---------------------- | -------------------------------------- |
| change | 选中项变化时触发 | - | | change | 选中项变化时触发 | `{ value, selectedOptions, tabIndex }` |
| confirm | 确认选择时触发 | - | | finish | 全部选项选择完成后触发 | `{ value, selectedOptions, tabIndex }` |
| close | 点击关闭图标时触发 | - |
### Slots ### Slots

View File

@ -5,14 +5,16 @@
is-link is-link
readonly readonly
:label="t('area')" :label="t('area')"
:value="base.value"
:placeholder="t('selectArea')" :placeholder="t('selectArea')"
@click="showBase = true" @click="base.show = true"
/> />
<van-popup v-model="showBase" round position="bottom"> <van-popup v-model="base.show" round position="bottom">
<van-cascader <van-cascader
:title="t('selectArea')" :title="t('selectArea')"
:options="t('options')" :options="t('options')"
@close="showBase = false" @close="base.show = false"
@finish="onFinish"
/> />
</van-popup> </van-popup>
</demo-block> </demo-block>
@ -37,8 +39,18 @@ export default {
data() { data() {
return { return {
showBase: false, base: {
show: false,
value: '',
},
}; };
}, },
methods: {
onFinish({ selectedOptions }) {
this.base.show = false;
this.base.value = selectedOptions.map((option) => option.text).join('/');
},
},
}; };
</script> </script>

View File

@ -46,17 +46,15 @@ export default createComponent({
} else { } else {
this.tabs = [ this.tabs = [
{ {
title: this.placeholder || this.t('placeholder'),
options: this.options, options: this.options,
selected: null, selectedOption: null,
}, },
]; ];
} }
}, },
onSelect(option, tabIndex) { onSelect(option, tabIndex) {
this.tabs[tabIndex].title = option.text; this.tabs[tabIndex].selectedOption = option;
this.tabs[tabIndex].selected = option.value;
if (this.tabs.length > tabIndex + 1) { if (this.tabs.length > tabIndex + 1) {
this.tabs = this.tabs.slice(0, tabIndex + 1); this.tabs = this.tabs.slice(0, tabIndex + 1);
@ -64,9 +62,8 @@ export default createComponent({
if (option.children) { if (option.children) {
const nextTab = { const nextTab = {
title: this.placeholder || this.t('placeholder'),
options: option.children, options: option.children,
selected: null, selectedOption: null,
}; };
if (this.tabs[tabIndex + 1]) { if (this.tabs[tabIndex + 1]) {
@ -79,6 +76,17 @@ export default createComponent({
this.activeTab++; this.activeTab++;
}); });
} }
const eventParams = {
value: option.value,
tabIndex,
selectedOptions: this.tabs.map((tab) => tab.selectedOption),
};
this.$emit('change', eventParams);
if (!option.children) {
this.$emit('finish', eventParams);
}
}, },
onClose() { onClose() {
@ -100,11 +108,11 @@ export default createComponent({
); );
}, },
renderOptions(options, selected, tabIndex) { renderOptions(options, selectedOption, tabIndex) {
return ( const renderOption = (option) => {
<ul class={bem('options')}> const isSelected =
{options.map((option) => { selectedOption && option.value === selectedOption.value;
const isSelected = option.value === selected;
return ( return (
<li <li
class={bem('option', { selected: isSelected })} class={bem('option', { selected: isSelected })}
@ -119,8 +127,26 @@ export default createComponent({
) : null} ) : null}
</li> </li>
); );
};
return <ul class={bem('options')}>{options.map(renderOption)}</ul>;
},
renderTab(item, tabIndex) {
const { options, selectedOption } = item;
const title = selectedOption
? selectedOption.text
: this.placeholder || this.t('placeholder');
return (
<Tab
title={title}
titleClass={bem('tab-title', {
unselected: !selectedOption,
})} })}
</ul> >
{this.renderOptions(options, selectedOption, tabIndex)}
</Tab>
); );
}, },
@ -133,14 +159,7 @@ export default createComponent({
class={bem('tabs')} class={bem('tabs')}
color={this.activeColor} color={this.activeColor}
> >
{this.tabs.map((item, tabIndex) => ( {this.tabs.map(this.renderTab)}
<Tab
title={item.title}
titleClass={bem('tab-title', { unselected: !item.selected })}
>
{this.renderOptions(item.options, item.selected, tabIndex)}
</Tab>
))}
</Tabs> </Tabs>
); );
}, },