@faststore/ui 1.6.23 → 1.6.26
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/CHANGELOG.md +30 -0
- package/dist/molecules/Dropdown/DropdownMenu.d.ts +2 -2
- package/dist/ui.cjs.development.js +46 -22
- package/dist/ui.cjs.development.js.map +1 -1
- package/dist/ui.cjs.production.min.js +1 -1
- package/dist/ui.cjs.production.min.js.map +1 -1
- package/dist/ui.esm.js +46 -22
- package/dist/ui.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/molecules/Dropdown/Dropdown.test.tsx +119 -8
- package/src/molecules/Dropdown/Dropdown.tsx +44 -7
- package/src/molecules/Dropdown/DropdownMenu.tsx +8 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/ui",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.26",
|
|
4
4
|
"description": "A lightweight, framework agnostic component library for React",
|
|
5
5
|
"author": "emersonlaurentino",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"@types/jest-axe": "^3.5.3",
|
|
77
77
|
"@types/tabbable": "^3.1.1",
|
|
78
78
|
"@types/testing-library__jest-dom": "^5.9.5",
|
|
79
|
-
"@vtex/theme-b2c-tailwind": "^1.6.
|
|
79
|
+
"@vtex/theme-b2c-tailwind": "^1.6.26",
|
|
80
80
|
"@vtex/tsconfig": "^0.5.0",
|
|
81
81
|
"chalk": "^5.0.0",
|
|
82
82
|
"jest-axe": "^5.0.1",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"tsdx": "^0.14.1",
|
|
90
90
|
"typescript": "^4.2.4"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "55979a744634e1119bab71f7363ab03ab00ae875"
|
|
93
93
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { fireEvent, render } from '@testing-library/react'
|
|
2
2
|
import { axe } from 'jest-axe'
|
|
3
3
|
import React from 'react'
|
|
4
|
+
import { act } from 'react-dom/test-utils'
|
|
4
5
|
|
|
5
6
|
import Dropdown, { DropdownButton, DropdownItem, DropdownMenu } from '.'
|
|
6
7
|
|
|
@@ -36,6 +37,36 @@ describe('Dropdown', () => {
|
|
|
36
37
|
expect(queryByTestId('store-dropdown-menu')).toBeInTheDocument()
|
|
37
38
|
})
|
|
38
39
|
|
|
40
|
+
it('Should render DropdownMenu in correct position', () => {
|
|
41
|
+
const { getByText, getByTestId } = render(
|
|
42
|
+
<>
|
|
43
|
+
<div style={{ height: 200, width: 200 }}>Large element</div>
|
|
44
|
+
<SimpleDropdown />
|
|
45
|
+
</>
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
49
|
+
|
|
50
|
+
fireEvent.click(dropdownButton)
|
|
51
|
+
|
|
52
|
+
const dropdownMenu = getByTestId('store-dropdown-menu')
|
|
53
|
+
|
|
54
|
+
expect(dropdownMenu).toBeInTheDocument()
|
|
55
|
+
|
|
56
|
+
const buttonRect = dropdownButton?.getBoundingClientRect()
|
|
57
|
+
|
|
58
|
+
const topLevel = buttonRect?.top ?? 0
|
|
59
|
+
const topOffset = buttonRect?.height ?? 0
|
|
60
|
+
const topPosition = topLevel + topOffset
|
|
61
|
+
const leftPosition = buttonRect?.left ?? 0
|
|
62
|
+
|
|
63
|
+
const topMenuPosition = dropdownMenu.style.top
|
|
64
|
+
const leftMenuPosition = dropdownMenu.style.left
|
|
65
|
+
|
|
66
|
+
expect(topMenuPosition).toEqual(`${topPosition}px`)
|
|
67
|
+
expect(leftMenuPosition).toEqual(`${leftPosition}px`)
|
|
68
|
+
})
|
|
69
|
+
|
|
39
70
|
it('Should render 3 dropdownItems when DropdownMenu is opened', () => {
|
|
40
71
|
const { getByText, queryAllByTestId } = render(<SimpleDropdown />)
|
|
41
72
|
|
|
@@ -46,6 +77,23 @@ describe('Dropdown', () => {
|
|
|
46
77
|
expect(queryAllByTestId('store-dropdown-item')).toHaveLength(3)
|
|
47
78
|
})
|
|
48
79
|
|
|
80
|
+
it('Should close DropdownMenu and emit onDismiss when DropdownButton is clicked twice', () => {
|
|
81
|
+
const onDismissMock = jest.fn()
|
|
82
|
+
|
|
83
|
+
const { getByText, queryByTestId } = render(
|
|
84
|
+
<SimpleDropdown onDismiss={onDismissMock} />
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
88
|
+
|
|
89
|
+
fireEvent.click(dropdownButton)
|
|
90
|
+
expect(queryByTestId('store-dropdown-menu')).toBeInTheDocument()
|
|
91
|
+
|
|
92
|
+
fireEvent.click(dropdownButton)
|
|
93
|
+
expect(queryByTestId('store-dropdown-menu')).not.toBeInTheDocument()
|
|
94
|
+
expect(onDismissMock).toHaveBeenCalledTimes(1)
|
|
95
|
+
})
|
|
96
|
+
|
|
49
97
|
it('Should close menu and emit onDismiss event when Overlay is clicked', () => {
|
|
50
98
|
const onDismissMock = jest.fn()
|
|
51
99
|
const { getByText, queryByTestId, getByTestId } = render(
|
|
@@ -54,25 +102,87 @@ describe('Dropdown', () => {
|
|
|
54
102
|
|
|
55
103
|
const dropdownButton = getByText(/Dropdown Button/g)
|
|
56
104
|
|
|
57
|
-
|
|
105
|
+
dropdownButton.click()
|
|
106
|
+
|
|
107
|
+
const dropdownMenu = queryByTestId('store-dropdown-menu')
|
|
108
|
+
|
|
109
|
+
expect(dropdownMenu).toBeInTheDocument()
|
|
110
|
+
|
|
111
|
+
const overlay = getByTestId('store-dropdown-menu-overlay')
|
|
112
|
+
|
|
113
|
+
act(() => {
|
|
114
|
+
fireEvent.click(overlay)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
expect(queryByTestId('store-dropdown-menu')).not.toBeInTheDocument()
|
|
118
|
+
expect(onDismissMock).toHaveBeenCalledTimes(1)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('Should close menu and emit onDismiss event when other element is clicked', () => {
|
|
122
|
+
const onDismissMock = jest.fn()
|
|
123
|
+
const { getByText, queryByTestId } = render(
|
|
124
|
+
<>
|
|
125
|
+
<span>Other Element</span>
|
|
126
|
+
<SimpleDropdown onDismiss={onDismissMock} />
|
|
127
|
+
</>
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
131
|
+
|
|
132
|
+
dropdownButton.click()
|
|
58
133
|
|
|
59
134
|
const dropdownMenu = queryByTestId('store-dropdown-menu')
|
|
60
135
|
|
|
61
136
|
expect(dropdownMenu).toBeInTheDocument()
|
|
62
137
|
|
|
63
|
-
const
|
|
138
|
+
const otherElement = getByText(/Other Element/g)
|
|
64
139
|
|
|
65
|
-
|
|
140
|
+
act(() => {
|
|
141
|
+
otherElement.click()
|
|
142
|
+
})
|
|
66
143
|
|
|
67
144
|
expect(dropdownMenu).not.toBeInTheDocument()
|
|
68
|
-
expect(onDismissMock).
|
|
145
|
+
expect(onDismissMock).toHaveBeenCalledTimes(1)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('Should close menu, emit onDismiss and emit onClose, event when an item is clicked', () => {
|
|
149
|
+
const onDismissMock = jest.fn()
|
|
150
|
+
const onClickItemMock = jest.fn()
|
|
151
|
+
|
|
152
|
+
const { getByText, queryByTestId, getByTestId } = render(
|
|
153
|
+
<Dropdown onDismiss={onDismissMock}>
|
|
154
|
+
<DropdownButton>Dropdown Button</DropdownButton>
|
|
155
|
+
<DropdownMenu>
|
|
156
|
+
<DropdownItem onClick={onClickItemMock}>Dropdown Item 1</DropdownItem>
|
|
157
|
+
</DropdownMenu>
|
|
158
|
+
</Dropdown>
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
162
|
+
|
|
163
|
+
dropdownButton.click()
|
|
164
|
+
|
|
165
|
+
const dropdownMenu = queryByTestId('store-dropdown-menu')
|
|
166
|
+
|
|
167
|
+
expect(dropdownMenu).toBeInTheDocument()
|
|
168
|
+
|
|
169
|
+
const firstItem = getByTestId('store-dropdown-item')
|
|
170
|
+
|
|
171
|
+
act(() => {
|
|
172
|
+
firstItem.click()
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
expect(dropdownMenu).not.toBeInTheDocument()
|
|
176
|
+
expect(onDismissMock).toHaveBeenCalledTimes(1)
|
|
177
|
+
expect(onClickItemMock).toHaveBeenCalledTimes(1)
|
|
69
178
|
})
|
|
70
179
|
})
|
|
71
180
|
|
|
72
181
|
describe('Accessibility', () => {
|
|
73
182
|
const getDropdownStructures = () => {
|
|
183
|
+
const onDismissMock = jest.fn()
|
|
74
184
|
const { getByText, queryAllByTestId, getByTestId, queryByTestId } = render(
|
|
75
|
-
<SimpleDropdown />
|
|
185
|
+
<SimpleDropdown onDismiss={onDismissMock} />
|
|
76
186
|
)
|
|
77
187
|
|
|
78
188
|
const dropdownButton = getByText(/Dropdown Button/g)
|
|
@@ -80,10 +190,10 @@ describe('Accessibility', () => {
|
|
|
80
190
|
fireEvent.click(dropdownButton)
|
|
81
191
|
|
|
82
192
|
const dropdownItems = queryAllByTestId('store-dropdown-item')
|
|
83
|
-
const overlay = getByTestId('store-overlay')
|
|
193
|
+
const overlay = getByTestId('store-dropdown-menu-overlay')
|
|
84
194
|
const menu = queryByTestId('store-dropdown-menu')
|
|
85
195
|
|
|
86
|
-
return { dropdownItems, overlay, menu }
|
|
196
|
+
return { dropdownItems, overlay, menu, onDismissMock }
|
|
87
197
|
}
|
|
88
198
|
|
|
89
199
|
it('Should not have violations', async () => {
|
|
@@ -102,13 +212,14 @@ describe('Accessibility', () => {
|
|
|
102
212
|
})
|
|
103
213
|
|
|
104
214
|
it('Should close Dropdown menu when Escape key is pressed', async () => {
|
|
105
|
-
const { overlay, menu } = getDropdownStructures()
|
|
215
|
+
const { overlay, menu, onDismissMock } = getDropdownStructures()
|
|
106
216
|
|
|
107
217
|
fireEvent.keyDown(overlay, {
|
|
108
218
|
key: 'Escape',
|
|
109
219
|
})
|
|
110
220
|
|
|
111
221
|
expect(menu).not.toBeInTheDocument()
|
|
222
|
+
expect(onDismissMock).toHaveBeenCalledTimes(1)
|
|
112
223
|
})
|
|
113
224
|
|
|
114
225
|
it('Should focus on second DropdownItem when ArrowDown key is pressed', async () => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReactNode } from 'react'
|
|
2
|
-
import React, { useRef, useMemo, useState, useEffect } from 'react'
|
|
2
|
+
import React, { useRef, useMemo, useState, useEffect, useCallback } from 'react'
|
|
3
3
|
|
|
4
4
|
import DropdownContext from './contexts/DropdownContext'
|
|
5
5
|
|
|
@@ -21,17 +21,26 @@ const Dropdown = ({
|
|
|
21
21
|
const selectedDropdownItemIndexRef = useRef(0)
|
|
22
22
|
const dropdownButtonRef = useRef<HTMLButtonElement>(null)
|
|
23
23
|
|
|
24
|
-
const close = () => {
|
|
24
|
+
const close = useCallback(() => {
|
|
25
25
|
setIsOpen(false)
|
|
26
|
-
|
|
26
|
+
onDismiss?.()
|
|
27
|
+
dropdownButtonRef.current?.focus()
|
|
28
|
+
}, [onDismiss])
|
|
27
29
|
|
|
28
30
|
const open = () => {
|
|
29
31
|
setIsOpen(true)
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
const toggle = () => {
|
|
33
|
-
setIsOpen((old) =>
|
|
34
|
-
|
|
34
|
+
const toggle = useCallback(() => {
|
|
35
|
+
setIsOpen((old) => {
|
|
36
|
+
if (old) {
|
|
37
|
+
onDismiss?.()
|
|
38
|
+
dropdownButtonRef.current?.focus()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return !old
|
|
42
|
+
})
|
|
43
|
+
}, [onDismiss])
|
|
35
44
|
|
|
36
45
|
useEffect(() => {
|
|
37
46
|
setIsOpen(isOpenDefault)
|
|
@@ -41,6 +50,34 @@ const Dropdown = ({
|
|
|
41
50
|
isOpen && dropdownItemsRef?.current[0]?.focus()
|
|
42
51
|
}, [isOpen])
|
|
43
52
|
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
let firstClick = true
|
|
55
|
+
|
|
56
|
+
const event = (e: MouseEvent) => {
|
|
57
|
+
const someItemWasClicked = dropdownItemsRef?.current.some(
|
|
58
|
+
(item) => e.target === item
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
if (firstClick) {
|
|
62
|
+
firstClick = false
|
|
63
|
+
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
!someItemWasClicked && close()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isOpen) {
|
|
71
|
+
document.addEventListener('click', event)
|
|
72
|
+
} else {
|
|
73
|
+
document.removeEventListener('click', event)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
document.removeEventListener('click', event)
|
|
78
|
+
}
|
|
79
|
+
}, [close, isOpen])
|
|
80
|
+
|
|
44
81
|
const value = useMemo(() => {
|
|
45
82
|
return {
|
|
46
83
|
isOpen,
|
|
@@ -53,7 +90,7 @@ const Dropdown = ({
|
|
|
53
90
|
dropdownItemsRef,
|
|
54
91
|
id,
|
|
55
92
|
}
|
|
56
|
-
}, [id, isOpen, onDismiss])
|
|
93
|
+
}, [close, id, isOpen, onDismiss, toggle])
|
|
57
94
|
|
|
58
95
|
return (
|
|
59
96
|
<DropdownContext.Provider value={value}>
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
3
|
KeyboardEvent,
|
|
4
|
-
MouseEvent,
|
|
5
4
|
PropsWithChildren,
|
|
6
|
-
|
|
5
|
+
MouseEvent,
|
|
6
|
+
ReactNode,
|
|
7
7
|
} from 'react'
|
|
8
8
|
import React from 'react'
|
|
9
9
|
import { createPortal } from 'react-dom'
|
|
10
10
|
|
|
11
|
-
import Overlay from '../../atoms/Overlay'
|
|
12
11
|
import type { ModalContentProps } from '../Modal/ModalContent'
|
|
13
12
|
import { useDropdown } from './hooks/useDropdown'
|
|
14
13
|
import { useDropdownPosition } from './hooks/useDropdownPosition'
|
|
@@ -30,7 +29,7 @@ export interface DropdownMenuProps extends ModalContentProps {
|
|
|
30
29
|
*/
|
|
31
30
|
onDismiss?: (event: MouseEvent | KeyboardEvent) => void
|
|
32
31
|
|
|
33
|
-
children:
|
|
32
|
+
children: ReactNode[] | ReactNode
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
/*
|
|
@@ -48,7 +47,6 @@ const DropdownMenu = ({
|
|
|
48
47
|
const {
|
|
49
48
|
isOpen,
|
|
50
49
|
close,
|
|
51
|
-
onDismiss,
|
|
52
50
|
dropdownItemsRef,
|
|
53
51
|
selectedDropdownItemIndexRef,
|
|
54
52
|
id,
|
|
@@ -56,17 +54,7 @@ const DropdownMenu = ({
|
|
|
56
54
|
|
|
57
55
|
const dropdownPosition = useDropdownPosition()
|
|
58
56
|
|
|
59
|
-
const childrenLength = children.length
|
|
60
|
-
|
|
61
|
-
const handleBackdropClick = (event: MouseEvent) => {
|
|
62
|
-
if (event.defaultPrevented) {
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
event.stopPropagation()
|
|
67
|
-
onDismiss?.()
|
|
68
|
-
close?.()
|
|
69
|
-
}
|
|
57
|
+
const childrenLength = React.Children.toArray(children).length
|
|
70
58
|
|
|
71
59
|
const handleDownPress = () => {
|
|
72
60
|
if (selectedDropdownItemIndexRef!.current < childrenLength - 1) {
|
|
@@ -99,7 +87,6 @@ const DropdownMenu = ({
|
|
|
99
87
|
}
|
|
100
88
|
|
|
101
89
|
const handleEscapePress = () => {
|
|
102
|
-
onDismiss?.()
|
|
103
90
|
close?.()
|
|
104
91
|
}
|
|
105
92
|
|
|
@@ -131,10 +118,11 @@ const DropdownMenu = ({
|
|
|
131
118
|
|
|
132
119
|
return isOpen
|
|
133
120
|
? createPortal(
|
|
134
|
-
<
|
|
121
|
+
<div
|
|
122
|
+
role="presentation"
|
|
135
123
|
data-store-dropdown-overlay
|
|
136
|
-
onClick={handleBackdropClick}
|
|
137
124
|
onKeyDown={handleBackdropKeyDown}
|
|
125
|
+
data-testid={`${testId}-overlay`}
|
|
138
126
|
>
|
|
139
127
|
<div
|
|
140
128
|
role="menu"
|
|
@@ -147,7 +135,7 @@ const DropdownMenu = ({
|
|
|
147
135
|
>
|
|
148
136
|
{children}
|
|
149
137
|
</div>
|
|
150
|
-
</
|
|
138
|
+
</div>,
|
|
151
139
|
document.body
|
|
152
140
|
)
|
|
153
141
|
: clearChildrenReferences()
|