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

182 lines
4.9 KiB
React
Raw Normal View History

2018-03-19 02:16:27 +00:00
2018-02-06 11:00:34 +00:00
import PropTypes from '../_util/vue-types'
import Menu from '../vc-menu'
import scrollIntoView from 'dom-scroll-into-view'
2018-02-06 13:27:23 +00:00
import { getSelectKeys, preventDefaultEvent } from './util'
import { cloneElement } from '../_util/vnode'
import BaseMixin from '../_util/BaseMixin'
2018-02-09 10:42:56 +00:00
import { getSlotOptions } from '../_util/props-util'
2018-02-06 11:00:34 +00:00
export default {
2018-02-06 13:27:23 +00:00
name: 'DropdownMenu',
mixins: [BaseMixin],
2018-02-06 11:00:34 +00:00
props: {
defaultActiveFirstOption: PropTypes.bool,
value: PropTypes.any,
dropdownMenuStyle: PropTypes.object,
multiple: PropTypes.bool,
2018-02-06 13:27:23 +00:00
// onPopupFocus: PropTypes.func,
// onPopupScroll: PropTypes.func,
// onMenuDeSelect: PropTypes.func,
// onMenuSelect: PropTypes.func,
2018-02-06 11:00:34 +00: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) {
2018-02-27 13:58:55 +00:00
this.lastVisible = val
2018-02-06 11:00:34 +00:00
}
},
},
updated () {
const props = this.$props
2018-02-07 15:03:47 +00:00
if (!this.prevVisible && props.visible) {
2018-02-06 11:00:34 +00:00
this.$nextTick(() => {
this.scrollActiveItemToView()
})
}
this.lastVisible = props.visible
this.lastInputValue = props.inputValue
2018-02-27 13:58:55 +00:00
this.prevVisible = this.visible
2018-02-06 11:00:34 +00:00
},
methods: {
scrollActiveItemToView () {
// scroll into view
2018-02-07 15:03:47 +00:00
const itemComponent = this.$refs.firstActiveItem && this.$refs.firstActiveItem.$el
2018-02-06 11:00:34 +00: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 13:27:23 +00:00
const props = this.$props
2018-02-06 11:00:34 +00:00
const {
menuItems,
defaultActiveFirstOption,
value,
prefixCls,
multiple,
inputValue,
firstActiveValue,
2018-02-06 13:27:23 +00:00
dropdownMenuStyle,
2018-02-06 11:00:34 +00:00
} = props
2018-02-27 13:58:55 +00:00
const { menuDeselect, menuSelect, popupScroll } = this.$listeners
2018-02-06 11:00:34 +00:00
if (menuItems && menuItems.length) {
2018-02-06 13:27:23 +00:00
const selectedKeys = getSelectKeys(menuItems, value)
const menuProps = {
props: {
multiple,
defaultActiveFirst: defaultActiveFirstOption,
selectedKeys,
prefixCls: `${prefixCls}-menu`,
},
on: {},
style: dropdownMenuStyle,
ref: 'menuRef',
}
2018-02-27 13:58:55 +00:00
if (popupScroll) {
menuProps.on.scroll = popupScroll
}
2018-02-06 11:00:34 +00:00
if (multiple) {
2018-02-07 10:56:58 +00:00
menuProps.on.deselect = menuDeselect
menuProps.on.select = menuSelect
2018-02-06 11:00:34 +00:00
} else {
2018-02-07 10:56:58 +00:00
menuProps.on.click = menuSelect
2018-02-06 11:00:34 +00: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 13:27:23 +00:00
ref: 'firstActiveItem',
2018-02-06 11:00:34 +00:00
})
}
return item
}
clonedMenuItems = menuItems.map(item => {
2018-02-09 10:42:19 +00:00
if (getSlotOptions(item).isMenuItemGroup) {
const children = item.componentOptions.children.map(clone)
return cloneElement(item, { children })
2018-02-06 11:00:34 +00: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 13:27:23 +00:00
menuProps.props = { ...menuProps.props, ...activeKeyProps }
2018-02-06 11:00:34 +00:00
return (
2018-02-06 13:27:23 +00:00
<Menu {...menuProps}>
2018-02-06 11:00:34 +00:00
{clonedMenuItems}
</Menu>
)
}
return null
},
},
render () {
const renderMenu = this.renderMenu()
2018-02-26 14:19:48 +00:00
const { popupFocus, popupScroll } = this.$listeners
2018-02-06 11:00:34 +00:00
return renderMenu ? (
<div
style={{ overflow: 'auto' }}
2018-02-07 10:56:58 +00:00
onFocus={popupFocus}
2018-02-06 13:27:23 +00:00
onMousedown={preventDefaultEvent}
2018-02-26 14:19:48 +00:00
onScroll={popupScroll}
2018-02-26 10:44:06 +00:00
ref='menuContainer'
2018-02-06 11:00:34 +00:00
>
{renderMenu}
</div>
) : null
},
}