@targetprocess/popover 12.20.1-alpha.2 → 12.20.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BodyDomNode.tsx +79 -76
- package/CHANGELOG.md +22 -22
- package/Popover.tsx +191 -188
- package/PopoverBody.tsx +40 -37
- package/PopoverContainer.tsx +325 -322
- package/PopoverPosition.ts +62 -59
- package/PositioningTarget.tsx +29 -26
- package/StatlessPopover.tsx +98 -101
- package/__tests__/BodyDomNode.tests.tsx +287 -284
- package/__tests__/Popover.tests.tsx +66 -63
- package/__tests__/PositioningTarget.tests.tsx +46 -43
- package/dist/BodyDomNode.d.ts +3 -0
- package/dist/Popover.d.ts +3 -0
- package/dist/PopoverBody.d.ts +3 -0
- package/dist/PopoverContainer.d.ts +3 -0
- package/dist/PositioningTarget.d.ts +3 -0
- package/dist/StatlessPopover.d.ts +4 -1
- package/dist/index.d.ts +3 -0
- package/dist/popover.es.js +22 -11
- package/dist/popover.js +22 -11
- package/dist/types.d.ts +3 -0
- package/index.ts +17 -14
- package/package.json +3 -3
- package/style/index.css +107 -104
- package/types.ts +40 -37
|
@@ -1,284 +1,287 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
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
|
-
const
|
|
156
|
-
|
|
157
|
-
mouseEvents
|
|
158
|
-
keyboardEvents
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* © Copyright IBM Corp. 2024
|
|
3
|
+
*********************************************************************/
|
|
4
|
+
import React from 'react'
|
|
5
|
+
import ReactDOM from 'react-dom'
|
|
6
|
+
import {mount, MountRendererProps} from 'enzyme'
|
|
7
|
+
import {BodyDomNode, BodyDomNodeProps} from '../BodyDomNode'
|
|
8
|
+
import {
|
|
9
|
+
whenComponentDidMountCalled,
|
|
10
|
+
whenComponentDidUpdateCalled,
|
|
11
|
+
whenComponentWillUnmountCalled
|
|
12
|
+
} from '../../../testUtils/componentLifecycleUtils'
|
|
13
|
+
import {cleanDocumentBody} from '../../../testUtils/domUtils'
|
|
14
|
+
|
|
15
|
+
const originalBodyDomNodeComponentDidMount = BodyDomNode.prototype.componentDidMount
|
|
16
|
+
const originalBodyDomNodeComponentWillUnmount = BodyDomNode.prototype.componentWillUnmount
|
|
17
|
+
const originalBodyAppendChild = document.body.appendChild
|
|
18
|
+
|
|
19
|
+
const stubComponentDidMount = () => whenComponentDidMountCalled(BodyDomNode)
|
|
20
|
+
const stubComponentWillUnmount = () => whenComponentWillUnmountCalled(BodyDomNode)
|
|
21
|
+
const stubComponentDidUpdate = () => whenComponentDidUpdateCalled(BodyDomNode)
|
|
22
|
+
|
|
23
|
+
const defaultRenderProp = () => null
|
|
24
|
+
const mountComponent = (
|
|
25
|
+
props: Partial<BodyDomNodeProps> = {},
|
|
26
|
+
mountOptions?: MountRendererProps
|
|
27
|
+
) => {
|
|
28
|
+
const {children, positioningTarget = document.createElement('dov'), ...other} = props
|
|
29
|
+
const renderProp = children || defaultRenderProp
|
|
30
|
+
|
|
31
|
+
return mount(
|
|
32
|
+
<BodyDomNode {...other} positioningTarget={positioningTarget} containerElement={document.body}>
|
|
33
|
+
{renderProp}
|
|
34
|
+
</BodyDomNode>,
|
|
35
|
+
mountOptions
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const addDivToBody = () => {
|
|
40
|
+
const div = document.createElement('div')
|
|
41
|
+
|
|
42
|
+
document.body.appendChild(div)
|
|
43
|
+
|
|
44
|
+
return div
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const dispatchNativeMouseEvent = (target: Element, event: string = 'click') =>
|
|
48
|
+
target.dispatchEvent(new MouseEvent(event, {bubbles: true}))
|
|
49
|
+
const dispatchNativeKeyboardEvent = (target: Element, event: string = 'keydown') =>
|
|
50
|
+
target.dispatchEvent(new KeyboardEvent(event, {bubbles: true}))
|
|
51
|
+
|
|
52
|
+
describe('BodyDomNode', () => {
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
BodyDomNode.prototype.componentDidMount = originalBodyDomNodeComponentDidMount
|
|
55
|
+
BodyDomNode.prototype.componentWillUnmount = originalBodyDomNodeComponentWillUnmount
|
|
56
|
+
document.body.appendChild = originalBodyAppendChild
|
|
57
|
+
|
|
58
|
+
cleanDocumentBody()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should call render prop with null initially', () => {
|
|
62
|
+
const renderProp = jest.fn(() => null)
|
|
63
|
+
|
|
64
|
+
mountComponent({children: renderProp})
|
|
65
|
+
|
|
66
|
+
expect(renderProp).toHaveBeenCalled()
|
|
67
|
+
expect(renderProp.mock.calls[0][0]).toBeNull()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should render element returned by render prop', () => {
|
|
71
|
+
const wrapper = mountComponent({children: () => <button>Test</button>})
|
|
72
|
+
|
|
73
|
+
expect(wrapper.find('button')).toHaveLength(1)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should call render prop with mounted dom node', async () => {
|
|
77
|
+
const appendChild = jest.fn(child => originalBodyAppendChild.call(document.body, child))
|
|
78
|
+
const renderProp = jest.fn(() => null)
|
|
79
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
80
|
+
|
|
81
|
+
document.body.appendChild = appendChild
|
|
82
|
+
|
|
83
|
+
mountComponent({children: renderProp})
|
|
84
|
+
|
|
85
|
+
await componentDidMountCalled
|
|
86
|
+
|
|
87
|
+
expect(document.body.children).toHaveLength(1)
|
|
88
|
+
expect(renderProp.mock.calls[1][0]).toBe(appendChild.mock.calls[0][0])
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('should remove dom node after component will be unmounted', async () => {
|
|
92
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
93
|
+
const componentWillUnmountCalled = stubComponentWillUnmount()
|
|
94
|
+
const wrapper = mountComponent()
|
|
95
|
+
|
|
96
|
+
await componentDidMountCalled
|
|
97
|
+
|
|
98
|
+
expect(document.body.children).toHaveLength(1)
|
|
99
|
+
|
|
100
|
+
wrapper.unmount()
|
|
101
|
+
|
|
102
|
+
await componentWillUnmountCalled
|
|
103
|
+
|
|
104
|
+
expect(document.body.children).toHaveLength(0)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('should call onOuterAction callback for every event from outerActionEvents prop', async () => {
|
|
108
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
109
|
+
const onOuterAction = jest.fn()
|
|
110
|
+
|
|
111
|
+
mountComponent({
|
|
112
|
+
onOuterAction,
|
|
113
|
+
outerActionEvents: ['click', 'keydown']
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
await componentDidMountCalled
|
|
117
|
+
|
|
118
|
+
const eventTargetNode = addDivToBody()
|
|
119
|
+
|
|
120
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
121
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
122
|
+
|
|
123
|
+
expect(onOuterAction).toBeCalledTimes(2)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('should not call onOuterAction callback if outerActionEvents is not specified', async () => {
|
|
127
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
128
|
+
const onOuterAction = jest.fn()
|
|
129
|
+
|
|
130
|
+
mountComponent({
|
|
131
|
+
onOuterAction
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
await componentDidMountCalled
|
|
135
|
+
|
|
136
|
+
const eventTargetNode = addDivToBody()
|
|
137
|
+
|
|
138
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
139
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
140
|
+
|
|
141
|
+
expect(onOuterAction).not.toBeCalled()
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('should not call onOuterAction callback for events that is not specified in outerActionEvents', async () => {
|
|
145
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
146
|
+
const onOuterAction = jest.fn()
|
|
147
|
+
|
|
148
|
+
mountComponent({
|
|
149
|
+
onOuterAction,
|
|
150
|
+
outerActionEvents: ['click', 'keydown']
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
await componentDidMountCalled
|
|
154
|
+
|
|
155
|
+
const eventTargetNode = addDivToBody()
|
|
156
|
+
|
|
157
|
+
const mouseEvents = ['mousedown', 'mouseup', 'mousein', 'mouseout']
|
|
158
|
+
const keyboardEvents = ['keypress', 'keyup']
|
|
159
|
+
|
|
160
|
+
mouseEvents.forEach(event => dispatchNativeMouseEvent(eventTargetNode, event))
|
|
161
|
+
keyboardEvents.forEach(event => dispatchNativeKeyboardEvent(eventTargetNode, event))
|
|
162
|
+
|
|
163
|
+
expect(onOuterAction).not.toBeCalled()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('should not call onOuterAction after component will be unmounted', async () => {
|
|
167
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
168
|
+
const componentWillUnmountCalled = stubComponentWillUnmount()
|
|
169
|
+
const onOuterAction = jest.fn()
|
|
170
|
+
|
|
171
|
+
const wrapper = mountComponent({
|
|
172
|
+
onOuterAction,
|
|
173
|
+
outerActionEvents: ['click', 'keydown']
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
await componentDidMountCalled
|
|
177
|
+
|
|
178
|
+
const eventTargetNode = addDivToBody()
|
|
179
|
+
|
|
180
|
+
wrapper.unmount()
|
|
181
|
+
|
|
182
|
+
await componentWillUnmountCalled
|
|
183
|
+
|
|
184
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
185
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
186
|
+
|
|
187
|
+
expect(onOuterAction).not.toBeCalled()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('should not call onOuterAction for events inside component children', async () => {
|
|
191
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
192
|
+
const onOuterAction = jest.fn()
|
|
193
|
+
const componentContainer = addDivToBody()
|
|
194
|
+
|
|
195
|
+
const wrapper = mountComponent(
|
|
196
|
+
{
|
|
197
|
+
children: node =>
|
|
198
|
+
node ? ReactDOM.createPortal(<div className="i-role-event-target" />, node) : null,
|
|
199
|
+
onOuterAction,
|
|
200
|
+
outerActionEvents: ['click', 'keydown']
|
|
201
|
+
},
|
|
202
|
+
{attachTo: componentContainer}
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
await componentDidMountCalled
|
|
206
|
+
|
|
207
|
+
const eventTargetNode = document.querySelector('.i-role-event-target')
|
|
208
|
+
|
|
209
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
210
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
211
|
+
|
|
212
|
+
expect(onOuterAction).not.toBeCalled()
|
|
213
|
+
|
|
214
|
+
wrapper.detach()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('should not call previous onOuterAction after prop update', async () => {
|
|
218
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
219
|
+
const componentDidUpdateCalled = stubComponentDidUpdate()
|
|
220
|
+
|
|
221
|
+
const onOuterActionBeforeUpdate = jest.fn()
|
|
222
|
+
const onOuterActionAfterUpdate = jest.fn()
|
|
223
|
+
|
|
224
|
+
const wrapper = mountComponent({
|
|
225
|
+
onOuterAction: onOuterActionBeforeUpdate,
|
|
226
|
+
outerActionEvents: ['click']
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
await componentDidMountCalled
|
|
230
|
+
|
|
231
|
+
wrapper.setProps({onOuterAction: onOuterActionAfterUpdate})
|
|
232
|
+
|
|
233
|
+
await componentDidUpdateCalled
|
|
234
|
+
|
|
235
|
+
const eventTargetNode = addDivToBody()
|
|
236
|
+
|
|
237
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
238
|
+
|
|
239
|
+
expect(onOuterActionBeforeUpdate).toBeCalledTimes(0)
|
|
240
|
+
expect(onOuterActionAfterUpdate).toBeCalledTimes(1)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('should not call onOuterAction for removed events after outerActiveEvents array change', async () => {
|
|
244
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
245
|
+
const componentDidUpdateCalled = stubComponentDidUpdate()
|
|
246
|
+
|
|
247
|
+
const onOuterAction = jest.fn()
|
|
248
|
+
|
|
249
|
+
const wrapper = mountComponent({
|
|
250
|
+
onOuterAction,
|
|
251
|
+
outerActionEvents: ['click']
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
await componentDidMountCalled
|
|
255
|
+
|
|
256
|
+
wrapper.setProps({outerActionEvents: ['keydown']})
|
|
257
|
+
|
|
258
|
+
await componentDidUpdateCalled
|
|
259
|
+
|
|
260
|
+
const eventTargetNode = addDivToBody()
|
|
261
|
+
|
|
262
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
263
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
264
|
+
|
|
265
|
+
expect(onOuterAction).toBeCalledTimes(1)
|
|
266
|
+
expect(onOuterAction.mock.calls[0][0].type).toBe('keydown')
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
it('should not call onOuterAction for event inside positioningTarget tree', async () => {
|
|
270
|
+
const componentDidMountCalled = stubComponentDidMount()
|
|
271
|
+
const onOuterAction = jest.fn()
|
|
272
|
+
const eventTargetNode = addDivToBody()
|
|
273
|
+
|
|
274
|
+
mountComponent({
|
|
275
|
+
onOuterAction,
|
|
276
|
+
outerActionEvents: ['click', 'keydown'],
|
|
277
|
+
positioningTarget: eventTargetNode
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
await componentDidMountCalled
|
|
281
|
+
|
|
282
|
+
dispatchNativeMouseEvent(eventTargetNode)
|
|
283
|
+
dispatchNativeKeyboardEvent(eventTargetNode)
|
|
284
|
+
|
|
285
|
+
expect(onOuterAction).not.toBeCalled()
|
|
286
|
+
})
|
|
287
|
+
})
|