From fd7342bde67dcb3e27e2132217563b511e5d13c6 Mon Sep 17 00:00:00 2001 From: Liril Date: Tue, 9 May 2017 18:16:15 +0800 Subject: [PATCH] Cascader: Add before-filter hook --- examples/docs/en-US/cascader.md | 5 +++-- examples/docs/zh-CN/cascader.md | 3 ++- packages/cascader/src/main.vue | 25 ++++++++++++++++++++++++- src/locale/lang/en.js | 1 + src/locale/lang/zh-CN.js | 1 + 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/examples/docs/en-US/cascader.md b/examples/docs/en-US/cascader.md index 3d7b6f524..96e9c81d9 100644 --- a/examples/docs/en-US/cascader.md +++ b/examples/docs/en-US/cascader.md @@ -1380,7 +1380,7 @@ Load child options when their parent option is clicked or hovered over. } }; }, - + methods: { handleItemChange(val) { console.log('active item:', val); @@ -1656,9 +1656,10 @@ Search and select options with a keyword. | value | specify which key of option object is used as the option's value | string | — | — | | children | specify which key of option object is used as the option's child options | string | — | — | | disabled | specify which key of option object indicates if the option is disabled | string | — | — | +| before-filter | hook function before filtering with the value to be filtered as its parameter. If `false` or a `Promise` is returned, filtering will be aborted | function(value) | — | — | ### Events | Event Name | Description | Parameters | |---------- |-------- |---------- | | change | triggers when the binding value changes | value | -| active-item-change | triggers when active option changes, only works when `change-on-select` is `false` | an array of active options | \ No newline at end of file +| active-item-change | triggers when active option changes, only works when `change-on-select` is `false` | an array of active options | diff --git a/examples/docs/zh-CN/cascader.md b/examples/docs/zh-CN/cascader.md index e91560894..c68dec706 100644 --- a/examples/docs/zh-CN/cascader.md +++ b/examples/docs/zh-CN/cascader.md @@ -1687,9 +1687,10 @@ | label | 指定选项标签为选项对象的某个属性值 | string | — | — | | children | 指定选项的子选项为选项对象的某个属性值 | string | — | — | | disabled | 指定选项的禁用为选项对象的某个属性值 | string | — | — | +| before-filter | 可选参数, 筛选之前的钩子,参数为输入的值,若返回 false 或者 Promise 则停止筛选。 | function(value) | — | — | ### Events | 事件名称 | 说明 | 回调参数 | |---------- |-------- |---------- | | change | 当绑定值变化时触发的事件 | 当前值 | -| active-item-change | 当父级选项变化时触发的事件,仅在 `change-on-select` 为 `false` 时可用 | 各父级选项组成的数组 | \ No newline at end of file +| active-item-change | 当父级选项变化时触发的事件,仅在 `change-on-select` 为 `false` 时可用 | 各父级选项组成的数组 | diff --git a/packages/cascader/src/main.vue b/packages/cascader/src/main.vue index 84c51a29d..9cf93c1bd 100644 --- a/packages/cascader/src/main.vue +++ b/packages/cascader/src/main.vue @@ -139,6 +139,10 @@ export default { debounce: { type: Number, default: 300 + }, + beforeFilter: { + type: Function, + default: null } }, @@ -328,7 +332,26 @@ export default { created() { this.debouncedInputChange = debounce(this.debounce, value => { - this.handleInputChange(value); + const before = this.beforeFilter(value); + + if (before && before.then) { + this.menu.options = [{ + __IS__FLAT__OPTIONS: true, + label: this.t('el.cascader.loading'), + value: '', + disabled: true + }]; + before + .then(() => { + this.$nextTick(() => { + this.handleInputChange(value); + }); + }); + } else if (before !== false) { + this.$nextTick(() => { + this.handleInputChange(value); + }); + } }); }, diff --git a/src/locale/lang/en.js b/src/locale/lang/en.js index b4da84732..e3ad7510b 100644 --- a/src/locale/lang/en.js +++ b/src/locale/lang/en.js @@ -62,6 +62,7 @@ export default { }, cascader: { noMatch: 'No matching data', + loading: 'Loading', placeholder: 'Select' }, pagination: { diff --git a/src/locale/lang/zh-CN.js b/src/locale/lang/zh-CN.js index e01a438de..96d226b5d 100644 --- a/src/locale/lang/zh-CN.js +++ b/src/locale/lang/zh-CN.js @@ -62,6 +62,7 @@ export default { }, cascader: { noMatch: '无匹配数据', + loading: '加载中', placeholder: '请选择' }, pagination: {