add keydown

This commit is contained in:
tangjinzhou 2018-01-09 14:21:15 +08:00
parent c782de1fe1
commit 2c164770b6
10 changed files with 33 additions and 71 deletions

View File

@ -1,8 +0,0 @@
export default (node, nodeProps) => {
const { props, style, class: cls, attrs, key } = nodeProps
if (node.componentOptions) {
const propsData = node.componentOptions.propsData
Object.assign(propsData, nodeProps)
}
return node
}

View File

@ -78,11 +78,18 @@ export function cloneElement (n, nodeProps, clone) {
const { style = data.style, const { style = data.style,
class: cls = data.class, class: cls = data.class,
attrs = data.attrs, attrs = data.attrs,
ref,
} = nodeProps } = nodeProps
node.data = Object.assign(data, { style, attrs, class: cls, on: { ...(data.on || {}), ...on }}) node.data = Object.assign(data, { style, attrs, class: cls, on: { ...(data.on || {}), ...on }})
if (key !== undefined) { if (key !== undefined) {
node.key = key node.key = key
node.data.key = key node.data.key = key
} }
if (typeof ref === 'string') {
node.data.ref = ref
}
return node return node
} }
export function getComponentName (opts) {
return opts && (opts.Ctor.options.name || opts.tag)
}

View File

@ -1,8 +1,6 @@
<script> <script>
import { cloneElement } from '../../_util/vnode'
import Clone from '../../_util/Clone' import Clone from '../../_util/Clone'
import Menu, { SubMenu, Item as MenuItem, Divider } from '../src/index' import Menu, { SubMenu, Item as MenuItem, Divider } from '../src/index'
import { Icon } from 'antd'
import animate from 'css-animation' import animate from 'css-animation'
function handleSelect (info) { function handleSelect (info) {
@ -74,17 +72,16 @@ export default {
</SubMenu> </SubMenu>
</SubMenu> </SubMenu>
</SubMenu>) </SubMenu>)
const onOpenChange = (value) => {
function onOpenChange (value) { console.log('onOpenChange', value, this.$refs)
console.log('onOpenChange', value)
} }
const commonMenu = () => ( const commonMenu = () => (
<Menu onSelect={handleSelect} onOpenChange={onOpenChange}> <Menu onSelect={handleSelect} onOpenChange={onOpenChange}>
<SubMenu key='1' title={<span>sub menu</span>}> <SubMenu ref='test' key='1' title={<span>sub menu</span>}>
<MenuItem key='1-1'> <MenuItem key='1-1'>
0-1 0-1
</MenuItem> </MenuItem>
<MenuItem key='1-2'>0-2</MenuItem> <MenuItem key='1-2' disabled>0-2</MenuItem>
</SubMenu> </SubMenu>
{nestSubMenu()} {nestSubMenu()}
<MenuItem key='2'>1</MenuItem> <MenuItem key='2'>1</MenuItem>
@ -130,6 +127,7 @@ export default {
<div style={{ margin: '20px', width: '400px' }}><Clone childProps={{ <div style={{ margin: '20px', width: '400px' }}><Clone childProps={{
mode: 'inline', mode: 'inline',
defaultOpenKeys: ['1'], defaultOpenKeys: ['1'],
defaultSelectedKeys: ['1-2', '4-1'],
openAnimation: animation, openAnimation: animation,
}} > }} >
{commonMenu()} {commonMenu()}

View File

@ -33,6 +33,7 @@ export default {
const Tag = this.$props.tag const Tag = this.$props.tag
const tagProps = { const tagProps = {
attr: { ...otherProps, ...this.$attrs }, attr: { ...otherProps, ...this.$attrs },
on: this.$listeners,
} }
return <Tag {...tagProps} class={this.class}>{this.$slots.default}</Tag> return <Tag {...tagProps} class={this.class}>{this.$slots.default}</Tag>
}, },

View File

@ -163,7 +163,7 @@ const Menu = {
const { sOpenKeys } = this.$data const { sOpenKeys } = this.$data
if (sOpenKeys.length) { if (sOpenKeys.length) {
lastOpen = this.getFlatInstanceArray().filter((c) => { lastOpen = this.getFlatInstanceArray().filter((c) => {
return c && sOpenKeys.indexOf(c.props.eventKey) !== -1 return c && sOpenKeys.indexOf(c.eventKey) !== -1
}) })
} }
return lastOpen[0] return lastOpen[0]

View File

@ -14,6 +14,7 @@ const MenuItem = {
selectedKeys: PropTypes.array, selectedKeys: PropTypes.array,
disabled: PropTypes.bool, disabled: PropTypes.bool,
title: PropTypes.string, title: PropTypes.string,
index: PropTypes.number,
inlineIndent: PropTypes.number.def(24), inlineIndent: PropTypes.number.def(24),
level: PropTypes.number.def(1), level: PropTypes.number.def(1),
mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']).def('vertical'), mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']).def('vertical'),

View File

@ -9,10 +9,8 @@ function allDisabled (arr) {
if (!arr.length) { if (!arr.length) {
return true return true
} }
return arr.every(c => { return arr.every(c => {
const propsData = c.componentOptions.propsData || {} return !!c.disabled
return !!propsData.disabled
}) })
} }
@ -53,10 +51,6 @@ export default {
deep: true, deep: true,
}, },
}, },
created () {
this.instanceArray = []
},
methods: { methods: {
getActiveKey (originalActiveKey) { getActiveKey (originalActiveKey) {
let activeKey = originalActiveKey let activeKey = originalActiveKey
@ -86,23 +80,13 @@ export default {
} }
return activeKey return activeKey
}, },
saveRef (index, subIndex, c) {
if (c) {
if (subIndex !== undefined) {
this.instanceArray[index] = this.instanceArray[index] || []
this.instanceArray[index][subIndex] = c
} else {
this.instanceArray[index] = c
}
}
},
// all keyboard events callbacks run from here at first // all keyboard events callbacks run from here at first
onKeyDown (e, callback) { onKeyDown (e) {
const keyCode = e.keyCode const keyCode = e.keyCode
let handled let handled
this.getFlatInstanceArray().forEach((obj) => { this.getFlatInstanceArray().forEach((obj) => {
if (obj && obj.$props.active) { if (obj && obj.active) {
handled = this.$emit('keydown', e) handled = obj.onKeyDown(e)
} }
}) })
if (handled) { if (handled) {
@ -115,15 +99,11 @@ export default {
if (activeItem) { if (activeItem) {
e.preventDefault() e.preventDefault()
this.setState({ this.setState({
sActiveKey: activeItem.$props.eventKey, sActiveKey: activeItem.eventKey,
}, () => { }, () => {
scrollIntoView(activeItem.$el, this.$el, { scrollIntoView(activeItem.$el, this.$el, {
onlyScrollIfNeeded: true, onlyScrollIfNeeded: true,
}) })
// https://github.com/react-component/menu/commit/9899a9672f6f028ec3cdf773f1ecea5badd2d33e
if (typeof callback === 'function') {
callback(activeItem)
}
}) })
return 1 return 1
} else if (activeItem === undefined) { } else if (activeItem === undefined) {
@ -143,22 +123,13 @@ export default {
}, },
getFlatInstanceArray () { getFlatInstanceArray () {
let instanceArray = this.instanceArray let instance = []
const hasInnerArray = instanceArray.some((a) => { try {
return Array.isArray(a) instance = this.$children[0].$children || []
}) } catch (error) {
if (hasInnerArray) {
instanceArray = []
this.instanceArray.forEach((a) => {
if (Array.isArray(a)) {
instanceArray.push.apply(instanceArray, a)
} else {
instanceArray.push(a)
}
})
this.instanceArray = instanceArray
} }
return instanceArray return instance
}, },
renderCommonMenuItem (child, i, subIndex, extraProps) { renderCommonMenuItem (child, i, subIndex, extraProps) {
@ -175,7 +146,6 @@ export default {
renderMenuItem: this.renderMenuItem, renderMenuItem: this.renderMenuItem,
rootPrefixCls: props.prefixCls, rootPrefixCls: props.prefixCls,
index: i, index: i,
// parentMenu: this,
eventKey: key, eventKey: key,
active: !childProps.disabled && isActive, active: !childProps.disabled && isActive,
multiple: props.multiple, multiple: props.multiple,
@ -195,11 +165,7 @@ export default {
destroy: this.onDestroy, destroy: this.onDestroy,
select: this.onSelect, select: this.onSelect,
}, },
// ref: childProps.disabled ? undefined : child.ref,
// ref: childProps.disabled ? undefined
// : createChainedFunction(child.ref, saveRef.bind(this, i, subIndex)),
} }
// !childProps.disabled && this.saveRef(i, subIndex, child.ref)
if (props.mode === 'inline') { if (props.mode === 'inline') {
newChildProps.props.triggerSubMenuAction = 'click' newChildProps.props.triggerSubMenuAction = 'click'
} }
@ -210,7 +176,6 @@ export default {
}, },
renderRoot (props, children = []) { renderRoot (props, children = []) {
this.instanceArray = []
const className = { const className = {
[props.prefixCls]: true, [props.prefixCls]: true,
[props.class]: true, [props.class]: true,
@ -236,7 +201,7 @@ export default {
domProps.attrs.tabIndex = '0' domProps.attrs.tabIndex = '0'
domProps.on.keydown = this.onKeyDown domProps.on.keydown = this.onKeyDown
} }
const newChildren = children.map(this.renderMenuItem) const newChildren = children.map((c, i) => this.renderMenuItem(c, i))
return ( return (
<DOMWrap <DOMWrap
{...domProps} {...domProps}
@ -259,8 +224,7 @@ export default {
// find current activeIndex // find current activeIndex
let activeIndex = -1 let activeIndex = -1
children.every((c, ci) => { children.every((c, ci) => {
const propsData = c.componentOptions.propsData || {} if (c && c.eventKey === sActiveKey) {
if (c && propsData.eventKey === sActiveKey) {
activeIndex = ci activeIndex = ci
return false return false
} }
@ -275,8 +239,7 @@ export default {
let i = start let i = start
for (; ;) { for (; ;) {
const child = children[i] const child = children[i]
const propsData = child.componentOptions.propsData || {} if (!child || child.disabled) {
if (!child || propsData.disabled) {
i = (i + 1 + len) % len i = (i + 1 + len) % len
// complete a loop // complete a loop
if (i === start) { if (i === start) {

View File

@ -31,6 +31,7 @@ export default {
multiple: PropTypes.bool, multiple: PropTypes.bool,
active: PropTypes.bool, // TODO: remove active: PropTypes.bool, // TODO: remove
isRootMenu: PropTypes.bool, isRootMenu: PropTypes.bool,
index: PropTypes.number,
// onItemHover: PropTypes.func, // onItemHover: PropTypes.func,
// onSelect: PropTypes.func, // onSelect: PropTypes.func,
triggerSubMenuAction: PropTypes.string, triggerSubMenuAction: PropTypes.string,
@ -100,7 +101,7 @@ export default {
onKeyDown (e) { onKeyDown (e) {
const keyCode = e.keyCode const keyCode = e.keyCode
const menu = this.menuInstance const menu = this.$refs.menuInstance
const isOpen = this.isOpen() const isOpen = this.isOpen()
if (keyCode === KeyCode.ENTER) { if (keyCode === KeyCode.ENTER) {

View File

@ -258,7 +258,6 @@ export default {
}, },
getRootDomNode () { getRootDomNode () {
console.log('this.$el.children', this.$el.children)
return this.$el.children ? this.$el.children[0] : this.$el return this.$el.children ? this.$el.children[0] : this.$el
}, },

View File

@ -67,16 +67,16 @@
"style-loader": "^0.18.2", "style-loader": "^0.18.2",
"stylelint": "^8.1.1", "stylelint": "^8.1.1",
"stylelint-config-standard": "^17.0.0", "stylelint-config-standard": "^17.0.0",
"vue": "^2.4.4",
"vue-loader": "^13.0.5", "vue-loader": "^13.0.5",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",
"vue-template-compiler": "^2.4.4", "vue-template-compiler": "^2.5.13",
"webpack": "^3.6.0", "webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2" "webpack-dev-server": "^2.8.2"
}, },
"dependencies": { "dependencies": {
"add-dom-event-listener": "^1.0.2", "add-dom-event-listener": "^1.0.2",
"css-animation": "^1.4.1", "css-animation": "^1.4.1",
"dom-align": "^1.6.6",
"dom-scroll-into-view": "^1.2.1", "dom-scroll-into-view": "^1.2.1",
"eslint-plugin-vue": "^3.13.0", "eslint-plugin-vue": "^3.13.0",
"lodash.clonedeep": "^4.5.0", "lodash.clonedeep": "^4.5.0",