ant-design-vue/components/vc-select/DropdownMenu.vue

188 lines
5.0 KiB
Vue
Raw Normal View History

2018-02-06 19:00:34 +08:00
<script>
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,
},
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 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
},
methods: {
scrollActiveItemToView () {
// scroll into view
2018-02-07 23:03:47 +08:00
const itemComponent = this.$refs.firstActiveItem && this.$refs.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
}
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
2018-02-07 18:56:58 +08:00
const { menuDeselect, menuSelect } = 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-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-02-06 21:27:23 +08:00
ref: 'firstActiveItem',
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)
})
}
// 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
},
},
render () {
const renderMenu = this.renderMenu()
2018-02-07 23:03:47 +08:00
this.prevVisible = this.visible
2018-02-07 18:56:58 +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-07 18:56:58 +08:00
onScroll={popupScroll}
2018-02-06 19:00:34 +08:00
>
{renderMenu}
</div>
) : null
},
}
</script>