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.vue

188 lines
4.9 KiB

7 years ago
<script>
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
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) {
this.lastVisible = false
}
},
},
// shouldComponentUpdate (nextProps) {
// if (!nextProps.visible) {
// this.lastVisible = false
// }
// // freeze when hide
// return nextProps.visible
// }
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
},
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 } = this.$listeners
7 years ago
if (menuItems && menuItems.length) {
7 years ago
const selectedKeys = getSelectKeys(menuItems, value)
const menuProps = {
props: {
multiple,
defaultActiveFirst: defaultActiveFirstOption,
focusable: false,
selectedKeys,
prefixCls: `${prefixCls}-menu`,
},
on: {},
style: dropdownMenuStyle,
ref: 'menuRef',
}
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 => {
if (item.type.isMenuItemGroup) {
7 years ago
const children = item.$slots.default.map(clone)
7 years ago
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 = ''
}
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
this.prevVisible = this.visible
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}
7 years ago
>
{renderMenu}
</div>
) : null
},
}
</script>