2018-02-06 19:00:34 +08:00
|
|
|
<script>
|
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import toArray from 'rc-util/lib/Children/toArray'
|
|
|
|
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-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,
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeMount () {
|
|
|
|
this.lastInputValue = this.$props.inputValue
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollActiveItemToView()
|
|
|
|
})
|
|
|
|
this.lastVisible = this.$props.visible
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
visible (val) {
|
|
|
|
if (!val) {
|
|
|
|
this.lastVisible = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// shouldComponentUpdate (nextProps) {
|
|
|
|
// if (!nextProps.visible) {
|
|
|
|
// this.lastVisible = false
|
|
|
|
// }
|
|
|
|
// // freeze when hide
|
|
|
|
// return nextProps.visible
|
|
|
|
// }
|
|
|
|
|
|
|
|
updated () {
|
|
|
|
const prevProps = this.prevProps
|
|
|
|
const props = this.$props
|
|
|
|
if (!prevProps.visible && props.visible) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollActiveItemToView()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.lastVisible = props.visible
|
|
|
|
this.lastInputValue = props.inputValue
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
scrollActiveItemToView () {
|
|
|
|
// scroll into view
|
|
|
|
const itemComponent = this.$refs.firstActiveItem.$el
|
|
|
|
const props = this.$props
|
|
|
|
|
|
|
|
if (itemComponent) {
|
|
|
|
const scrollIntoViewOpts = {
|
|
|
|
onlyScrollIfNeeded: true,
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(!props.value || props.value.length === 0) &&
|
|
|
|
props.firstActiveValue
|
|
|
|
) {
|
|
|
|
scrollIntoViewOpts.alignWithTop = true
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollIntoView(
|
|
|
|
itemComponent,
|
|
|
|
this.$refs.menuRef.$el,
|
|
|
|
scrollIntoViewOpts
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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-02-06 19:00:34 +08:00
|
|
|
} = props
|
|
|
|
if (menuItems && menuItems.length) {
|
2018-02-06 21:27:23 +08:00
|
|
|
const selectedKeys = getSelectKeys(menuItems, value)
|
|
|
|
const menuProps = {
|
|
|
|
props: {
|
|
|
|
multiple,
|
|
|
|
defaultActiveFirst: defaultActiveFirstOption,
|
|
|
|
focusable: false,
|
|
|
|
selectedKeys,
|
|
|
|
prefixCls: `${prefixCls}-menu`,
|
|
|
|
},
|
|
|
|
on: {},
|
|
|
|
style: dropdownMenuStyle,
|
|
|
|
ref: 'menuRef',
|
|
|
|
}
|
2018-02-06 19:00:34 +08:00
|
|
|
if (multiple) {
|
2018-02-06 21:27:23 +08:00
|
|
|
menuProps.on.deselect = this.onMenuDeselect
|
|
|
|
menuProps.on.select = this.onMenuSelect
|
2018-02-06 19:00:34 +08:00
|
|
|
} else {
|
2018-02-06 21:27:23 +08:00
|
|
|
menuProps.on.click = this.onMenuSelect
|
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-02-06 21:27:23 +08:00
|
|
|
ref: 'firstActiveItem',
|
2018-02-06 19:00:34 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
clonedMenuItems = menuItems.map(item => {
|
|
|
|
if (item.type.isMenuItemGroup) {
|
|
|
|
const children = toArray(item.props.children).map(clone)
|
|
|
|
return cloneElement(item, {}, children)
|
|
|
|
}
|
|
|
|
return clone(item)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear activeKey when inputValue change
|
|
|
|
const lastValue = value && value[value.length - 1]
|
|
|
|
if (inputValue !== this.lastInputValue && (!lastValue || !lastValue.backfill)) {
|
|
|
|
activeKeyProps.activeKey = ''
|
|
|
|
}
|
2018-02-06 21:27:23 +08:00
|
|
|
menuProps.props = { ...menuProps.props, ...activeKeyProps }
|
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
|
|
|
|
},
|
2018-02-06 21:27:23 +08:00
|
|
|
onPopupFocus (e) {
|
|
|
|
this.__emit('popupFocus', e)
|
|
|
|
},
|
|
|
|
onPopupScroll (e) {
|
|
|
|
this.__emit('popupScroll', e)
|
|
|
|
},
|
|
|
|
onMenuDeselect () {
|
|
|
|
this.__emit('menuDeselect', ...arguments)
|
|
|
|
},
|
|
|
|
onMenuSelect () {
|
|
|
|
this.__emit('menuSelect', ...arguments)
|
|
|
|
},
|
2018-02-06 19:00:34 +08:00
|
|
|
},
|
|
|
|
render () {
|
|
|
|
const renderMenu = this.renderMenu()
|
|
|
|
return renderMenu ? (
|
|
|
|
<div
|
|
|
|
style={{ overflow: 'auto' }}
|
2018-02-06 21:27:23 +08:00
|
|
|
onFocus={this.onPopupFocus}
|
|
|
|
onMousedown={preventDefaultEvent}
|
|
|
|
onScroll={this.onPopupScroll}
|
2018-02-06 19:00:34 +08:00
|
|
|
>
|
|
|
|
{renderMenu}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|