2018-09-05 21:28:54 +08:00
|
|
|
import raf from 'raf'
|
2018-02-06 19:00:34 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import Menu from '../vc-menu'
|
|
|
|
import scrollIntoView from 'dom-scroll-into-view'
|
2018-02-06 21:27:23 +08:00
|
|
|
import { getSelectKeys, preventDefaultEvent } from './util'
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-02-09 18:42:56 +08:00
|
|
|
import { getSlotOptions } from '../_util/props-util'
|
2018-02-06 19:00:34 +08:00
|
|
|
|
|
|
|
export default {
|
2018-02-06 21:27:23 +08:00
|
|
|
name: 'DropdownMenu',
|
|
|
|
mixins: [BaseMixin],
|
2018-02-06 19:00:34 +08:00
|
|
|
props: {
|
|
|
|
defaultActiveFirstOption: PropTypes.bool,
|
|
|
|
value: PropTypes.any,
|
|
|
|
dropdownMenuStyle: PropTypes.object,
|
|
|
|
multiple: PropTypes.bool,
|
2018-02-06 21:27:23 +08:00
|
|
|
// onPopupFocus: PropTypes.func,
|
|
|
|
// onPopupScroll: PropTypes.func,
|
|
|
|
// onMenuDeSelect: PropTypes.func,
|
|
|
|
// onMenuSelect: PropTypes.func,
|
2018-02-06 19:00:34 +08:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
menuItems: PropTypes.any,
|
|
|
|
inputValue: PropTypes.string,
|
|
|
|
visible: PropTypes.bool,
|
2018-09-05 21:28:54 +08:00
|
|
|
backfillValue: PropTypes.any,
|
2018-02-06 19:00:34 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
beforeMount () {
|
|
|
|
this.lastInputValue = this.$props.inputValue
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollActiveItemToView()
|
|
|
|
})
|
|
|
|
this.lastVisible = this.$props.visible
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
visible (val) {
|
|
|
|
if (!val) {
|
2018-02-27 21:58:55 +08:00
|
|
|
this.lastVisible = val
|
2018-02-06 19:00:34 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
updated () {
|
|
|
|
const props = this.$props
|
2018-02-07 23:03:47 +08:00
|
|
|
if (!this.prevVisible && props.visible) {
|
2018-02-06 19:00:34 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollActiveItemToView()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.lastVisible = props.visible
|
|
|
|
this.lastInputValue = props.inputValue
|
2018-02-27 21:58:55 +08:00
|
|
|
this.prevVisible = this.visible
|
2018-02-06 19:00:34 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
scrollActiveItemToView () {
|
|
|
|
// scroll into view
|
2018-09-05 21:28:54 +08:00
|
|
|
const itemComponent = this.firstActiveItem && this.firstActiveItem.$el
|
2018-02-06 19:00:34 +08:00
|
|
|
const props = this.$props
|
|
|
|
|
|
|
|
if (itemComponent) {
|
|
|
|
const scrollIntoViewOpts = {
|
|
|
|
onlyScrollIfNeeded: true,
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(!props.value || props.value.length === 0) &&
|
|
|
|
props.firstActiveValue
|
|
|
|
) {
|
|
|
|
scrollIntoViewOpts.alignWithTop = true
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
// Delay to scroll since current frame item position is not ready when pre view is by filter
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11268#issuecomment-406634462
|
|
|
|
raf(() => {
|
|
|
|
scrollIntoView(
|
|
|
|
itemComponent,
|
|
|
|
this.$refs.menuRef.$el,
|
|
|
|
scrollIntoViewOpts
|
|
|
|
)
|
|
|
|
})
|
2018-02-06 19:00:34 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renderMenu () {
|
2018-02-06 21:27:23 +08:00
|
|
|
const props = this.$props
|
2018-02-06 19:00:34 +08:00
|
|
|
const {
|
|
|
|
menuItems,
|
|
|
|
defaultActiveFirstOption,
|
|
|
|
value,
|
|
|
|
prefixCls,
|
|
|
|
multiple,
|
|
|
|
inputValue,
|
|
|
|
firstActiveValue,
|
2018-02-06 21:27:23 +08:00
|
|
|
dropdownMenuStyle,
|
2018-09-05 21:28:54 +08:00
|
|
|
backfillValue,
|
2018-02-06 19:00:34 +08:00
|
|
|
} = props
|
2018-02-27 21:58:55 +08:00
|
|
|
const { menuDeselect, menuSelect, popupScroll } = this.$listeners
|
2018-02-06 19:00:34 +08:00
|
|
|
if (menuItems && menuItems.length) {
|
2018-02-06 21:27:23 +08:00
|
|
|
const selectedKeys = getSelectKeys(menuItems, value)
|
|
|
|
const menuProps = {
|
|
|
|
props: {
|
|
|
|
multiple,
|
|
|
|
defaultActiveFirst: defaultActiveFirstOption,
|
|
|
|
selectedKeys,
|
|
|
|
prefixCls: `${prefixCls}-menu`,
|
|
|
|
},
|
|
|
|
on: {},
|
|
|
|
style: dropdownMenuStyle,
|
|
|
|
ref: 'menuRef',
|
2018-09-05 21:28:54 +08:00
|
|
|
attrs: {
|
|
|
|
role: 'listbox',
|
|
|
|
},
|
2018-02-06 21:27:23 +08:00
|
|
|
}
|
2018-02-27 21:58:55 +08:00
|
|
|
if (popupScroll) {
|
|
|
|
menuProps.on.scroll = popupScroll
|
|
|
|
}
|
2018-02-06 19:00:34 +08:00
|
|
|
if (multiple) {
|
2018-02-07 18:56:58 +08:00
|
|
|
menuProps.on.deselect = menuDeselect
|
|
|
|
menuProps.on.select = menuSelect
|
2018-02-06 19:00:34 +08:00
|
|
|
} else {
|
2018-02-07 18:56:58 +08:00
|
|
|
menuProps.on.click = menuSelect
|
2018-02-06 19:00:34 +08:00
|
|
|
}
|
|
|
|
const activeKeyProps = {}
|
|
|
|
|
|
|
|
let clonedMenuItems = menuItems
|
|
|
|
if (selectedKeys.length || firstActiveValue) {
|
|
|
|
if (props.visible && !this.lastVisible) {
|
|
|
|
activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue
|
|
|
|
}
|
|
|
|
let foundFirst = false
|
|
|
|
// set firstActiveItem via cloning menus
|
|
|
|
// for scroll into view
|
|
|
|
const clone = item => {
|
|
|
|
if (
|
|
|
|
(!foundFirst && selectedKeys.indexOf(item.key) !== -1) ||
|
|
|
|
(!foundFirst &&
|
|
|
|
!selectedKeys.length &&
|
|
|
|
firstActiveValue.indexOf(item.key) !== -1)
|
|
|
|
) {
|
|
|
|
foundFirst = true
|
|
|
|
return cloneElement(item, {
|
2018-09-05 21:28:54 +08:00
|
|
|
directives: [{
|
|
|
|
name: 'ref',
|
|
|
|
value: ref => {
|
|
|
|
this.firstActiveItem = ref
|
|
|
|
},
|
|
|
|
}],
|
2018-02-06 19:00:34 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
clonedMenuItems = menuItems.map(item => {
|
2018-02-09 18:42:19 +08:00
|
|
|
if (getSlotOptions(item).isMenuItemGroup) {
|
|
|
|
const children = item.componentOptions.children.map(clone)
|
|
|
|
return cloneElement(item, { children })
|
2018-02-06 19:00:34 +08:00
|
|
|
}
|
|
|
|
return clone(item)
|
|
|
|
})
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
|
|
|
// Clear firstActiveItem when dropdown menu items was empty
|
|
|
|
// Avoid `Unable to find node on an unmounted component`
|
|
|
|
// https://github.com/ant-design/ant-design/issues/10774
|
|
|
|
this.firstActiveItem = null
|
2018-02-06 19:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear activeKey when inputValue change
|
|
|
|
const lastValue = value && value[value.length - 1]
|
2018-09-05 21:28:54 +08:00
|
|
|
if (inputValue !== this.lastInputValue && (!lastValue || lastValue !== backfillValue)) {
|
2018-02-06 19:00:34 +08:00
|
|
|
activeKeyProps.activeKey = ''
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
menuProps.props = { ...activeKeyProps, ...menuProps.props }
|
2018-02-06 19:00:34 +08:00
|
|
|
return (
|
2018-02-06 21:27:23 +08:00
|
|
|
<Menu {...menuProps}>
|
2018-02-06 19:00:34 +08:00
|
|
|
{clonedMenuItems}
|
|
|
|
</Menu>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render () {
|
|
|
|
const renderMenu = this.renderMenu()
|
2018-02-26 22:19:48 +08:00
|
|
|
const { popupFocus, popupScroll } = this.$listeners
|
2018-02-06 19:00:34 +08:00
|
|
|
return renderMenu ? (
|
|
|
|
<div
|
|
|
|
style={{ overflow: 'auto' }}
|
2018-02-07 18:56:58 +08:00
|
|
|
onFocus={popupFocus}
|
2018-02-06 21:27:23 +08:00
|
|
|
onMousedown={preventDefaultEvent}
|
2018-02-26 22:19:48 +08:00
|
|
|
onScroll={popupScroll}
|
2018-02-26 18:44:06 +08:00
|
|
|
ref='menuContainer'
|
2018-02-06 19:00:34 +08:00
|
|
|
>
|
|
|
|
{renderMenu}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|