@faststore/ui 1.4.23 → 1.4.27

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/ui",
3
- "version": "1.4.23",
3
+ "version": "1.4.27",
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.4.23",
79
+ "@vtex/theme-b2c-tailwind": "^1.4.27",
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": "d674d623842f2fb7fa42d582c9f0e33b51c3cae6"
92
+ "gitHead": "ee7116fe197d3f75318eacd831ab2af9d7dccb82"
93
93
  }
@@ -94,6 +94,38 @@ describe('Accordion', () => {
94
94
  expect(panelsMock[1]).toBeVisible()
95
95
  })
96
96
 
97
+ it('should prevent duplicate IDs when using the `prefixID` attribute', () => {
98
+ const { getAllByTestId } = render(
99
+ <>
100
+ <Accordion indices={[]} onChange={() => {}}>
101
+ <AccordionItem prefixId="filter">
102
+ <AccordionButton testId="data-accordion-button" />
103
+ <AccordionPanel testId="data-accordion-panel" />
104
+ </AccordionItem>
105
+ </Accordion>
106
+ <Accordion indices={[]} onChange={() => {}}>
107
+ <AccordionItem prefixId="footer">
108
+ <AccordionButton testId="data-accordion-button" />
109
+ <AccordionPanel testId="data-accordion-panel" />
110
+ </AccordionItem>
111
+ </Accordion>
112
+ </>
113
+ )
114
+
115
+ const allPanels = getAllByTestId('data-accordion-panel')
116
+ const allButtons = getAllByTestId('data-accordion-button')
117
+
118
+ // Attributes
119
+ expect(allPanels[0]).toHaveAttribute('id', 'filter-panel--0')
120
+ expect(allPanels[1]).toHaveAttribute('id', 'footer-panel--0')
121
+ expect(allButtons[0]).toHaveAttribute('id', 'filter-button--0')
122
+ expect(allButtons[1]).toHaveAttribute('id', 'footer-button--0')
123
+
124
+ // Equality assertions
125
+ expect(allPanels[0].id).not.toEqual(allPanels[1].id)
126
+ expect(allButtons[0].id).not.toEqual(allButtons[1].id)
127
+ })
128
+
97
129
  describe('Data attributes', () => {
98
130
  it('`Accordion` component should have `data-store-accordion` attribute', () => {
99
131
  expect(accordion).toHaveAttribute('data-store-accordion')
@@ -19,7 +19,7 @@ const AccordionButton = forwardRef<HTMLButtonElement, AccordionButtonProps>(
19
19
  ref
20
20
  ) {
21
21
  const { indices, onChange, numberOfItems } = useAccordion()
22
- const { index, panel, button } = useAccordionItem()
22
+ const { index, panel, button, prefixId } = useAccordionItem()
23
23
 
24
24
  const onKeyDown = (event: React.KeyboardEvent) => {
25
25
  if (!['ArrowDown', 'ArrowUp'].includes(event.key)) {
@@ -29,14 +29,18 @@ const AccordionButton = forwardRef<HTMLButtonElement, AccordionButtonProps>(
29
29
  const getNext = () => {
30
30
  const next = Number(index) + 1 === numberOfItems ? 0 : Number(index) + 1
31
31
 
32
- return document.getElementById(`button--${next}`)
32
+ return document.getElementById(
33
+ `${prefixId && `${prefixId}-`}button--${next}`
34
+ )
33
35
  }
34
36
 
35
37
  const getPrevious = () => {
36
38
  const previous =
37
39
  Number(index) - 1 < 0 ? numberOfItems - 1 : Number(index) - 1
38
40
 
39
- return document.getElementById(`button--${previous}`)
41
+ return document.getElementById(
42
+ `${prefixId && `${prefixId}-`}button--${previous}`
43
+ )
40
44
  }
41
45
 
42
46
  switch (event.key) {
@@ -5,6 +5,7 @@ interface AccordionItemContext {
5
5
  index: number
6
6
  panel: string
7
7
  button: string
8
+ prefixId: string
8
9
  }
9
10
 
10
11
  const AccordionItemContext = createContext<AccordionItemContext | undefined>(
@@ -21,17 +22,29 @@ export interface AccordionItemProps extends HTMLAttributes<HTMLDivElement> {
21
22
  * Index of the current accordion item within the accordion.
22
23
  */
23
24
  index?: number
25
+ /**
26
+ * Namespace ID prefix for the current Accordion item's panel and button
27
+ * to avoid ID duplication when multiple instances are on the same page.
28
+ */
29
+ prefixId?: string
24
30
  }
25
31
 
26
32
  const AccordionItem = forwardRef<HTMLDivElement, AccordionItemProps>(
27
33
  function AccordionItem(
28
- { testId = 'store-accordion-item', children, index = 0, ...otherProps },
34
+ {
35
+ testId = 'store-accordion-item',
36
+ children,
37
+ prefixId = '',
38
+ index = 0,
39
+ ...otherProps
40
+ },
29
41
  ref
30
42
  ) {
31
43
  const context = {
32
44
  index,
33
- panel: `panel--${index}`,
34
- button: `button--${index}`,
45
+ prefixId,
46
+ panel: `${prefixId && `${prefixId}-`}panel--${index}`,
47
+ button: `${prefixId && `${prefixId}-`}button--${index}`,
35
48
  }
36
49
 
37
50
  return (