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-select/DropdownMenu.jsx

182 lines
4.9 KiB

7 years ago
import PropTypes from '../_util/vue-types'
import Menu from '../vc-menu'
import scrollIntoView from 'dom-scroll-into-view'
7 years ago
import { getSelectKeys, preventDefaultEvent } from './util'
import { cloneElement } from '../_util/vnode'
import BaseMixin from '../_util/BaseMixin'
7 years ago
import { getSlotOptions } from '../_util/props-util'
7 years ago
export default {
7 years ago
name: 'DropdownMenu',
mixins: [BaseMixin],
7 years ago
props: {
defaultActiveFirstOption: PropTypes.bool,
value: PropTypes.any,
dropdownMenuStyle: PropTypes.object,
multiple: PropTypes.bool,
7 years ago
// onPopupFocus: PropTypes.func,
// onPopupScroll: PropTypes.func,
// onMenuDeSelect: PropTypes.func,
// onMenuSelect: PropTypes.func,
7 years ago
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) {
7 years ago
this.lastVisible = val
7 years ago
}
},
},
updated () {
const props = this.$props
7 years ago
if (!this.prevVisible && props.visible) {
7 years ago
this.$nextTick(() => {
this.scrollActiveItemToView()
})
}
this.lastVisible = props.visible
this.lastInputValue = props.inputValue
7 years ago
this.prevVisible = this.visible
7 years ago
},
methods: {
scrollActiveItemToView () {
// scroll into view
7 years ago
const itemComponent = this.$refs.firstActiveItem && this.$refs.firstActiveItem.$el
7 years ago
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 () {
7 years ago
const props = this.$props
7 years ago
const {
menuItems,
defaultActiveFirstOption,
value,
prefixCls,
multiple,
inputValue,
firstActiveValue,
7 years ago
dropdownMenuStyle,
7 years ago
} = props
7 years ago
const { menuDeselect, menuSelect, popupScroll } = this.$listeners
7 years ago
if (menuItems && menuItems.length) {
7 years ago
const selectedKeys = getSelectKeys(menuItems, value)
const menuProps = {
props: {
multiple,
defaultActiveFirst: defaultActiveFirstOption,
selectedKeys,
prefixCls: `${prefixCls}-menu`,
},
on: {},
style: dropdownMenuStyle,
ref: 'menuRef',
}
7 years ago
if (popupScroll) {
menuProps.on.scroll = popupScroll
}
7 years ago
if (multiple) {
7 years ago
menuProps.on.deselect = menuDeselect
menuProps.on.select = menuSelect
7 years ago
} else {
7 years ago
menuProps.on.click = menuSelect
7 years ago
}
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, {
7 years ago
ref: 'firstActiveItem',
7 years ago
})
}
return item
}
clonedMenuItems = menuItems.map(item => {
7 years ago
if (getSlotOptions(item).isMenuItemGroup) {
const children = item.componentOptions.children.map(clone)
return cloneElement(item, { children })
7 years ago
}
return clone(item)
})
}
// clear activeKey when inputValue change
const lastValue = value && value[value.length - 1]
if (inputValue !== this.lastInputValue && (!lastValue || !lastValue.backfill)) {
activeKeyProps.activeKey = ''
}
7 years ago
menuProps.props = { ...menuProps.props, ...activeKeyProps }
7 years ago
return (
7 years ago
<Menu {...menuProps}>
7 years ago
{clonedMenuItems}
</Menu>
)
}
return null
},
},
render () {
const renderMenu = this.renderMenu()
7 years ago
const { popupFocus, popupScroll } = this.$listeners
7 years ago
return renderMenu ? (
<div
style={{ overflow: 'auto' }}
7 years ago
onFocus={popupFocus}
7 years ago
onMousedown={preventDefaultEvent}
7 years ago
onScroll={popupScroll}
ref='menuContainer'
7 years ago
>
{renderMenu}
</div>
) : null
},
}