-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.jsx
193 lines (179 loc) · 4.96 KB
/
Menu.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import PropTypes from '../_util/vue-types'
import MenuMixin from './MenuMixin'
import BaseMixin from '../_util/BaseMixin'
import hasProp from '../_util/props-util'
import commonPropsType from './commonPropsType'
const Menu = {
name: 'Menu',
props: {
...commonPropsType,
selectable: PropTypes.bool.def(true),
},
mixins: [BaseMixin, MenuMixin],
data () {
const props = this.$props
let sSelectedKeys = props.defaultSelectedKeys
let sOpenKeys = props.defaultOpenKeys
if (hasProp(this, 'selectedKeys')) {
sSelectedKeys = props.selectedKeys || []
}
if (hasProp(this, 'openKeys')) {
sOpenKeys = props.openKeys || []
}
// this.isRootMenu = true
return {
sSelectedKeys,
sOpenKeys,
}
},
watch: {
'$props': {
handler: function (nextProps) {
if (hasProp(this, 'selectedKeys')) {
this.setState({
sSelectedKeys: nextProps.selectedKeys || [],
})
}
if (hasProp(this, 'openKeys')) {
this.setState({
sOpenKeys: nextProps.openKeys || [],
})
}
},
deep: true,
},
},
methods: {
// onDestroy (key) {
// const state = this.$data
// const sSelectedKeys = state.sSelectedKeys
// const sOpenKeys = state.sOpenKeys
// let index = sSelectedKeys.indexOf(key)
// if (!hasProp(this, 'selectedKeys') && index !== -1) {
// sSelectedKeys.splice(index, 1)
// }
// index = sOpenKeys.indexOf(key)
// if (!hasProp(this, 'openKeys') && index !== -1) {
// sOpenKeys.splice(index, 1)
// }
// },
onSelect (selectInfo) {
const props = this.$props
if (props.selectable) {
// root menu
let sSelectedKeys = this.$data.sSelectedKeys
const selectedKey = selectInfo.key
if (props.multiple) {
sSelectedKeys = sSelectedKeys.concat([selectedKey])
} else {
sSelectedKeys = [selectedKey]
}
if (!hasProp(this, 'selectedKeys')) {
this.setState({
sSelectedKeys,
})
}
this.__emit('select', {
...selectInfo,
selectedKeys: sSelectedKeys,
})
}
},
onClick (e) {
this.__emit('click', e)
},
onOpenChange (event) {
const sOpenKeys = this.$data.sOpenKeys.concat()
let changed = false
const processSingle = (e) => {
let oneChanged = false
if (e.open) {
oneChanged = sOpenKeys.indexOf(e.key) === -1
if (oneChanged) {
sOpenKeys.push(e.key)
}
} else {
const index = sOpenKeys.indexOf(e.key)
oneChanged = index !== -1
if (oneChanged) {
sOpenKeys.splice(index, 1)
}
}
changed = changed || oneChanged
}
if (Array.isArray(event)) {
// batch change call
event.forEach(processSingle)
} else {
processSingle(event)
}
if (changed) {
if (!hasProp(this, 'openKeys')) {
this.setState({ sOpenKeys })
}
this.__emit('openChange', sOpenKeys)
}
},
onDeselect (selectInfo) {
const props = this.$props
if (props.selectable) {
const sSelectedKeys = this.$data.sSelectedKeys.concat()
const selectedKey = selectInfo.key
const index = sSelectedKeys.indexOf(selectedKey)
if (index !== -1) {
sSelectedKeys.splice(index, 1)
}
if (!hasProp(this, 'selectedKeys')) {
this.setState({
sSelectedKeys,
})
}
this.__emit('deselect', {
...selectInfo,
selectedKeys: sSelectedKeys,
})
}
},
getOpenTransitionName () {
const props = this.$props
let transitionName = props.openTransitionName
const animationName = props.openAnimation
if (!transitionName && typeof animationName === 'string') {
transitionName = `${props.prefixCls}-open-${animationName}`
}
return transitionName
},
isInlineMode () {
return this.$props.mode === 'inline'
},
lastOpenSubMenu () {
let lastOpen = []
const { sOpenKeys } = this.$data
if (sOpenKeys.length) {
lastOpen = this.getFlatInstanceArray().filter((c) => {
return c && sOpenKeys.indexOf(c.eventKey) !== -1
})
}
return lastOpen[0]
},
renderMenuItem (c, i, subIndex) {
if (!c) {
return null
}
const state = this.$data
const extraProps = {
openKeys: state.sOpenKeys,
selectedKeys: state.sSelectedKeys,
triggerSubMenuAction: this.$props.triggerSubMenuAction,
isRootMenu: this.isRootMenu,
}
return this.renderCommonMenuItem(c, i, subIndex, extraProps)
},
},
render () {
const props = { ...this.$props }
props.class = ` ${props.prefixCls}-root`
return this.renderRoot(props, this.$slots.default)
},
}
export default Menu