pull/9/head
tjz 2018-02-06 21:27:23 +08:00
parent 1681033ceb
commit 493d37ad8e
2 changed files with 47 additions and 41 deletions

View File

@ -3,18 +3,22 @@ import PropTypes from '../_util/vue-types'
import toArray from 'rc-util/lib/Children/toArray' import toArray from 'rc-util/lib/Children/toArray'
import Menu from '../vc-menu' import Menu from '../vc-menu'
import scrollIntoView from 'dom-scroll-into-view' import scrollIntoView from 'dom-scroll-into-view'
import { getSelectKeys, preventDefaultEvent, saveRef } from './util' import { getSelectKeys, preventDefaultEvent } from './util'
import { cloneElement } from '../_util/vnode'
import BaseMixin from '../_util/BaseMixin'
export default { export default {
name: 'DropdownMenu',
mixins: [BaseMixin],
props: { props: {
defaultActiveFirstOption: PropTypes.bool, defaultActiveFirstOption: PropTypes.bool,
value: PropTypes.any, value: PropTypes.any,
dropdownMenuStyle: PropTypes.object, dropdownMenuStyle: PropTypes.object,
multiple: PropTypes.bool, multiple: PropTypes.bool,
onPopupFocus: PropTypes.func, // onPopupFocus: PropTypes.func,
onPopupScroll: PropTypes.func, // onPopupScroll: PropTypes.func,
onMenuDeSelect: PropTypes.func, // onMenuDeSelect: PropTypes.func,
onMenuSelect: PropTypes.func, // onMenuSelect: PropTypes.func,
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
menuItems: PropTypes.any, menuItems: PropTypes.any,
inputValue: PropTypes.string, inputValue: PropTypes.string,
@ -84,27 +88,37 @@ export default {
}, },
renderMenu () { renderMenu () {
const props = this.props const props = this.$props
const { const {
menuItems, menuItems,
defaultActiveFirstOption, defaultActiveFirstOption,
value, value,
prefixCls, prefixCls,
multiple, multiple,
onMenuSelect,
inputValue, inputValue,
firstActiveValue, firstActiveValue,
dropdownMenuStyle,
} = props } = props
if (menuItems && menuItems.length) { if (menuItems && menuItems.length) {
const menuProps = {}
if (multiple) {
menuProps.onDeselect = props.onMenuDeselect
menuProps.onSelect = onMenuSelect
} else {
menuProps.onClick = onMenuSelect
}
const selectedKeys = getSelectKeys(menuItems, value) const selectedKeys = getSelectKeys(menuItems, value)
const menuProps = {
props: {
multiple,
defaultActiveFirst: defaultActiveFirstOption,
focusable: false,
selectedKeys,
prefixCls: `${prefixCls}-menu`,
},
on: {},
style: dropdownMenuStyle,
ref: 'menuRef',
}
if (multiple) {
menuProps.on.deselect = this.onMenuDeselect
menuProps.on.select = this.onMenuSelect
} else {
menuProps.on.click = this.onMenuSelect
}
const activeKeyProps = {} const activeKeyProps = {}
let clonedMenuItems = menuItems let clonedMenuItems = menuItems
@ -124,9 +138,7 @@ export default {
) { ) {
foundFirst = true foundFirst = true
return cloneElement(item, { return cloneElement(item, {
ref: ref => { ref: 'firstActiveItem',
this.firstActiveItem = ref
},
}) })
} }
return item return item
@ -146,34 +158,36 @@ export default {
if (inputValue !== this.lastInputValue && (!lastValue || !lastValue.backfill)) { if (inputValue !== this.lastInputValue && (!lastValue || !lastValue.backfill)) {
activeKeyProps.activeKey = '' activeKeyProps.activeKey = ''
} }
menuProps.props = { ...menuProps.props, ...activeKeyProps }
return ( return (
<Menu <Menu {...menuProps}>
ref={saveRef(this, 'menuRef')}
style={this.props.dropdownMenuStyle}
defaultActiveFirst={defaultActiveFirstOption}
{...activeKeyProps}
multiple={multiple}
focusable={false}
{...menuProps}
selectedKeys={selectedKeys}
prefixCls={`${prefixCls}-menu`}
>
{clonedMenuItems} {clonedMenuItems}
</Menu> </Menu>
) )
} }
return null return null
}, },
onPopupFocus (e) {
this.__emit('popupFocus', e)
},
onPopupScroll (e) {
this.__emit('popupScroll', e)
},
onMenuDeselect () {
this.__emit('menuDeselect', ...arguments)
},
onMenuSelect () {
this.__emit('menuSelect', ...arguments)
},
}, },
render () { render () {
const renderMenu = this.renderMenu() const renderMenu = this.renderMenu()
return renderMenu ? ( return renderMenu ? (
<div <div
style={{ overflow: 'auto' }} style={{ overflow: 'auto' }}
onFocus={this.props.onPopupFocus} onFocus={this.onPopupFocus}
onMouseDown={preventDefaultEvent} onMousedown={preventDefaultEvent}
onScroll={this.props.onPopupScroll} onScroll={this.onPopupScroll}
> >
{renderMenu} {renderMenu}
</div> </div>
@ -181,6 +195,4 @@ export default {
}, },
} }
DropdownMenu.displayName = 'DropdownMenu'
</script> </script>

View File

@ -157,9 +157,3 @@ export function validateOptionValue (value, props) {
) )
} }
} }
export function saveRef (instance, name) {
return (node) => {
instance[name] = node
}
}