You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/vc-cascader/Menus.jsx

151 lines
4.0 KiB

7 years ago
import PropTypes from '../_util/vue-types'
7 years ago
import arrayTreeFilter from 'array-tree-filter'
7 years ago
import BaseMixin from '../_util/BaseMixin'
export default {
mixins: [BaseMixin],
props: {
value: PropTypes.array.def([]),
activeValue: PropTypes.array.def([]),
options: PropTypes.array.isRequired,
prefixCls: PropTypes.string.def('rc-cascader-menus'),
expandTrigger: PropTypes.string.def('click'),
// onSelect: PropTypes.func,
visible: PropTypes.bool.def(false),
dropdownMenuColumnStyle: PropTypes.object,
},
data () {
7 years ago
this.menuItems = {}
7 years ago
return {}
},
mounted () {
this.$nextTick(() => {
7 years ago
this.scrollActiveItemToView()
7 years ago
})
},
watch: {
visible (val) {
if (val) {
this.$nextTick(() => {
this.scrollActiveItemToView()
})
7 years ago
}
7 years ago
},
},
methods: {
getOption (option, menuIndex) {
const { prefixCls, expandTrigger } = this
const onSelect = (e) => {
this.__emit('select', option, menuIndex, e)
}
const expandProps = {
attrs: {
},
on: {
click: onSelect,
},
key: option.value.toString(),
7 years ago
}
let menuItemCls = `${prefixCls}-menu-item`
const hasChildren = option.children && option.children.length > 0
if (hasChildren || option.isLeaf === false) {
menuItemCls += ` ${prefixCls}-menu-item-expand`
}
if (expandTrigger === 'hover' && hasChildren) {
expandProps.on = {
mouseenter: this.delayOnSelect.bind(this, onSelect),
mouseleave: this.delayOnSelect.bind(this),
click: onSelect,
}
}
if (this.isActiveOption(option, menuIndex)) {
menuItemCls += ` ${prefixCls}-menu-item-active`
expandProps.ref = this.getMenuItemRef(menuIndex)
}
if (option.disabled) {
menuItemCls += ` ${prefixCls}-menu-item-disabled`
}
if (option.loading) {
menuItemCls += ` ${prefixCls}-menu-item-loading`
}
let title = ''
if (option.title) {
title = option.title
} else if (typeof option.label === 'string') {
title = option.label
}
expandProps.attrs.title = title
expandProps.class = menuItemCls
return (
<li {...expandProps}>
{option.label}
</li>
)
},
getActiveOptions (values) {
const activeValue = values || this.activeValue
const options = this.options
return arrayTreeFilter(options, (o, level) => o.value === activeValue[level])
},
getShowOptions () {
const { options } = this
const result = this.getActiveOptions()
.map(activeOption => activeOption.children)
.filter(activeOption => !!activeOption)
result.unshift(options)
return result
},
delayOnSelect (onSelect, ...args) {
if (this.delayTimer) {
clearTimeout(this.delayTimer)
7 years ago
this.delayTimer = null
7 years ago
}
if (typeof onSelect === 'function') {
this.delayTimer = setTimeout(() => {
onSelect(args)
this.delayTimer = null
}, 150)
}
},
7 years ago
7 years ago
scrollActiveItemToView () {
7 years ago
// scroll into view
7 years ago
const optionsLength = this.getShowOptions().length
for (let i = 0; i < optionsLength; i++) {
const itemComponent = this.$refs[`menuItems_${i}`]
if (itemComponent) {
const target = itemComponent
target.parentNode.scrollTop = target.offsetTop
}
7 years ago
}
7 years ago
},
7 years ago
7 years ago
isActiveOption (option, menuIndex) {
const { activeValue = [] } = this
return activeValue[menuIndex] === option.value
},
7 years ago
7 years ago
getMenuItemRef (index) {
return `menuItems_${index}`
},
},
7 years ago
render () {
7 years ago
const { prefixCls, dropdownMenuColumnStyle } = this
7 years ago
return (
<div>
{this.getShowOptions().map((options, menuIndex) =>
7 years ago
<ul class={`${prefixCls}-menu`} key={menuIndex} style={dropdownMenuColumnStyle}>
7 years ago
{options.map(option => this.getOption(option, menuIndex))}
</ul>
)}
</div>
)
7 years ago
},
7 years ago
}