-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopupMenu.js
177 lines (173 loc) · 4.58 KB
/
PopupMenu.js
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
import React from 'react'
import PropTypes from 'prop-types'
import { css } from 'styled-components'
import { types, getParentOfType, isAlive } from 'mobx-state-tree'
import {
metaElement,
cssInject,
createModel,
plugin,
} from '@hoc/plugins-core'
import { Tooltip } from './Tooltip'
import { MenuItem } from './MenuItem'
import { List } from './List'
import { Button } from './Button'
export const PopupMenu = plugin('PopupMenu', {
tooltip: Tooltip.Model,
bound: types.maybeNull(types.string),
}).views(self => ({
get menuItems () {
return self.list.items
},
get list () {
return self.tooltip.links.content
},
get hasHighlighedMenuItem () {
return self.menuItems.filter(({ autoFocus }) => autoFocus).length !== 0
},
get isVisible () {
return self.tooltip.visible
},
})).actions(self => {
let _boundData
function boundToData (data) {
_boundData = data
self.onDestroy(() => {
_boundData = null
})
}
function getBoundData () {
return _boundData
}
return { boundToData, getBoundData }
}).actions(self => ({
boundToNode (nodeId) {
self.bound = nodeId
},
setDoNotPropagateEvents (flag) {
if (flag) self.tooltip.portal.setDoNotPropagateEvents()
else self.tooltip.portal.setDoNotPropagateEvents([])
},
setTestId (testid) {
self.tooltip.setTestId(testid)
},
setTestKey (testkey) {
self.tooltip.setTestKey(testkey)
},
highlightItemByIdx (idx) {
const items = self.menuItems
if (idx < 0 || idx > items.lenght) return
// Known issue:
// make sure only one menu item will have autofocus enabled
// otherwise PopupMenu may hide at the wrong moment
// as event.relatedTarget can be null even if focus
// is switching from first to second menu item
items.forEach((menuItem, menuItemIdx) => {
menuItem.setAutoFocus(idx === menuItemIdx)
})
},
setMenuItems (menuItems) {
self.list.setItems(menuItems)
// by default focus is on first menu item
if (!self.hasHighlighedMenuItem) self.highlightItemByIdx(0)
},
show (relativeItem, offset) {
self.tooltip.showTooltip(relativeItem, offset)
},
hide () {
self.tooltip.hide()
},
})).component(class PopupMenuClass extends React.Component {
static propTypes = { data: PropTypes.object }
render () {
const { data } = this.props
const { tooltip } = data
return metaElement(tooltip)
}
}).constructor((model, props) => {
const { testid, testkey, arrow, items, innerCss, relativePos, ...restProps } = props
const res = createModel(model, {
type: 'PopupMenu',
// TODO: remove if has no effect
innerCss: cssInject(css`
width: 10rem;
`),
tooltip: Tooltip.create({
relativePos: relativePos || 'top-left',
arrow: arrow,
content: List.create({
testid: testid,
testkey: testkey,
innerCss: innerCss || undefined,
events: {
onBlur: 'PopupMenu.Blur',
/* intercept click event here to hide menu after MenuItem
is clicked. Works if user defined onClick on menuitem too */
onClick: 'PopupMenu.Click',
},
}),
}),
...restProps,
})
res.setMenuItems(items)
return res
}).demo(true, () => List.create({
items: [
Button.create({
name: 'Demo Menu',
events: {
onClick: 'PopupMenu.DemoShow',
},
}),
PopupMenu.create({
relativePos: 'top-left',
items: [
MenuItem.create({
icon: 'pencil',
text: 'Fake Edit',
events: {
onClick: 'PopupMenu.DemoFakeEdit',
},
}),
MenuItem.create({
text: 'hello',
events: {
onClick: 'PopupMenu.DemoHello',
},
}),
],
}),
],
mapping: {
button: 0,
menu: 1,
},
})).events({
Blur: ({ data, event }) => {
let closeMenu = true
const popupMenu = PopupMenu.self(data)
if (event.relatedTarget) {
const menuItemsElement = popupMenu.list.element
const focusedElem = event.relatedTarget
// close if focus moved outside of menu
closeMenu = !menuItemsElement.contains(focusedElem)
}
if (closeMenu) popupMenu.hide()
},
Click: ({ data, event }) => {
if (isAlive(data) && !data.disabled) PopupMenu.self(data).hide()
else event.stopPropagation()
},
DemoShow: ({ event, data }) => {
const parentList = getParentOfType(data, List.Model)
const menu = parentList.itemByMapping('menu')
menu.show(data)
},
DemoFakeEdit: () => {
console.log('Fake Edit clicked')
},
DemoHello: () => {
console.log('Hello clicked')
},
})
export default PopupMenu