diff --git a/packages/cascader/src/main.vue b/packages/cascader/src/main.vue index 4e826e854..12949fe3c 100644 --- a/packages/cascader/src/main.vue +++ b/packages/cascader/src/main.vue @@ -15,6 +15,7 @@ v-clickoutside="handleClickoutside" > { + this.menu.inputWidth = this.$refs.input.$el.offsetWidth - 2; + }); }, hideMenu() { this.menu.visible = false; @@ -172,18 +177,24 @@ export default { }); if (filteredFlatOptions.length > 0) { - this.menu.options = filteredFlatOptions.map(optionStack => { + filteredFlatOptions = filteredFlatOptions.map(optionStack => { return { __IS__FLAT__OPTIONS: true, value: optionStack.map(item => item.value), - label: this.renderRenderFilteredOption(value, optionStack) + label: this.renderFilteredOptionLabel(value, optionStack) }; }); } else { - return [{ label: 'not found content', value: '', disabled: true }]; + filteredFlatOptions = [{ + __IS__FLAT__OPTIONS: true, + label: this.t('el.cascader.noMatch'), + value: '', + disabled: true + }]; } + this.menu.options = filteredFlatOptions; }, - renderRenderFilteredOption(inputValue, optionsStack) { + renderFilteredOptionLabel(inputValue, optionsStack) { return optionsStack.map(({ label }, index) => { const node = label.indexOf(inputValue) > -1 ? this.highlightKeyword(label, inputValue) : label; return index === 0 ? node : [' / ', node]; @@ -203,8 +214,7 @@ export default { const optionsStack = ancestor.concat(option); if (!option.children) { flatOptions.push(optionsStack); - } - if (option.children) { + } else { flatOptions = flatOptions.concat(this.flattenOptions(option.children, optionsStack)); } }); diff --git a/packages/cascader/src/menu.vue b/packages/cascader/src/menu.vue index 3b829c070..509e3dc23 100644 --- a/packages/cascader/src/menu.vue +++ b/packages/cascader/src/menu.vue @@ -4,6 +4,7 @@ data() { return { + inputWidth: 0, options: [], visible: false, activeValue: [], @@ -56,34 +57,19 @@ }, methods: { - selectItem(item, menuIndex) { - const len = this.activeOptions.length; - const closeMenu = !item.children; - + select(item, menuIndex) { if (item.__IS__FLAT__OPTIONS) { - this.activeValue.splice(menuIndex, len, ...item.value); + this.activeValue = item.value; } else { - this.activeValue.splice(menuIndex, len, item.value); - } - - if (this.changeOnSelect) { - this.$emit('pick', this.activeValue, closeMenu); + this.activeValue.splice(menuIndex, 1, item.value); } + this.$emit('pick', this.activeValue); }, - expandItem(item, menuIndex) { + activeItem(item, menuIndex) { const len = this.activeOptions.length; - if (item.children) { - this.activeValue.splice(menuIndex, len, item.value); - this.activeOptions.splice(menuIndex + 1, len, item.children); - } - }, - handleItemClick(item, menuIndex) { - this.expandItem(item, menuIndex); - this.selectItem(item, menuIndex); - - if (!item.children && !this.changeOnSelect) { - this.$emit('pick', this.activeValue); - } + this.activeValue.splice(menuIndex, len, item.value); + this.activeOptions.splice(menuIndex + 1, len, item.children); + if (this.changeOnSelect) this.$emit('pick', this.activeValue, false); } }, @@ -96,17 +82,24 @@ popperClass } = this; - const menus = this._l(activeOptions, (menu, index) => { + const menus = this._l(activeOptions, (menu, menuIndex) => { + let isFlat = false; const items = this._l(menu, item => { const events = { on: {} }; + if (item.__IS__FLAT__OPTIONS) isFlat = true; + if (!item.disabled) { - if (expandTrigger === 'click' || !item.children) { - events.on['click'] = () => { this.handleItemClick(item, index); }; + if (item.children) { + let triggerEvent = { + click: 'click', + hover: 'mouseenter' + }[expandTrigger]; + events.on[triggerEvent] = () => { this.activeItem(item, menuIndex); }; } else { - events.on['mouseenter'] = () => { this.expandItem(item, index); }; + events.on.click = () => { this.select(item, menuIndex); }; } } @@ -115,7 +108,7 @@ class={{ 'el-cascader-menu__item': true, 'el-cascader-menu__item--extensible': item.children, - 'is-active': item.value === activeValue[index], + 'is-active': item.value === activeValue[menuIndex], 'is-disabled': item.disabled }} {...events} @@ -124,11 +117,31 @@ ); }); - return ; + let menuStyle = {}; + if (isFlat) { + menuStyle.width = this.inputWidth + 'px'; + } + + return ( + + ); }); return ( -
+
{menus}
diff --git a/packages/theme-default/src/cascader.css b/packages/theme-default/src/cascader.css index 7062b735c..35dc81e7d 100644 --- a/packages/theme-default/src/cascader.css +++ b/packages/theme-default/src/cascader.css @@ -150,5 +150,15 @@ } } } + + @m flexible { + height: auto; + max-height: 180px; + overflow: auto; + + .el-cascader-menu__item { + overflow: visible; + } + } } } diff --git a/src/locale/lang/en.js b/src/locale/lang/en.js index c8b63b5af..701b9d00c 100644 --- a/src/locale/lang/en.js +++ b/src/locale/lang/en.js @@ -56,6 +56,10 @@ export default { noData: 'No data', placeholder: 'Select' }, + cascader: { + noMatch: 'No matching data', + placeholder: 'Select' + }, pagination: { goto: 'Go to', pagesize: '/page', diff --git a/src/locale/lang/zh-CN.js b/src/locale/lang/zh-CN.js index ce927f132..5cc3050df 100644 --- a/src/locale/lang/zh-CN.js +++ b/src/locale/lang/zh-CN.js @@ -56,6 +56,10 @@ export default { noData: '无数据', placeholder: '请选择' }, + cascader: { + noMatch: '无匹配数据', + placeholder: '请选择' + }, pagination: { goto: '前往', pagesize: '条/页',