@faststore/ui 1.3.27 → 1.3.28

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.
@@ -84,6 +84,43 @@ describe('List', () => {
84
84
  expect(getByTestId('store-list')).toBeInstanceOf(HTMLDListElement)
85
85
  })
86
86
 
87
+ it('should respect the HTML tag passed as "as" prop', () => {
88
+ const { getByTestId, rerender } = render(
89
+ <List variant="unordered" as="div">
90
+ {optionsArray.map((value) => {
91
+ return <li key={value}>{value}</li>
92
+ })}
93
+ </List>
94
+ )
95
+
96
+ expect(getByTestId('store-list')).toBeInstanceOf(HTMLDivElement)
97
+
98
+ rerender(
99
+ <List variant="ordered" as="div">
100
+ {optionsArray.map((value) => {
101
+ return <li key={value}>{value}</li>
102
+ })}
103
+ </List>
104
+ )
105
+
106
+ expect(getByTestId('store-list')).toBeInstanceOf(HTMLDivElement)
107
+
108
+ rerender(
109
+ <List variant="description" as="div">
110
+ {optionsArray.map((value, index) => {
111
+ return (
112
+ <Fragment key={index}>
113
+ <dt key={`${index}--term`}>{value}</dt>
114
+ <dd key={`${index}--details`}>option</dd>
115
+ </Fragment>
116
+ )
117
+ })}
118
+ </List>
119
+ )
120
+
121
+ expect(getByTestId('store-list')).toBeInstanceOf(HTMLDivElement)
122
+ })
123
+
87
124
  describe('Accessibility', () => {
88
125
  it('should have no violations when rendering the default variant', async () => {
89
126
  const { getByTestId } = render(
@@ -1,55 +1,29 @@
1
- import type {
2
- ElementType,
3
- ComponentPropsWithRef,
4
- ReactElement,
5
- FC,
6
- } from 'react'
7
1
  import React, { forwardRef } from 'react'
2
+ import type { ElementType, HTMLAttributes } from 'react'
8
3
 
9
- import type { PolymorphicComponentPropsWithRef } from '../../typings'
10
-
11
- export type ListVariant = 'description' | 'ordered' | 'unordered'
12
-
13
- const variantToType = (variant: ListVariant) => {
14
- switch (variant) {
15
- case 'description':
16
- return 'dl'
17
-
18
- case 'unordered':
19
- return 'ul'
20
-
21
- case 'ordered':
22
- return 'ol'
23
-
24
- default:
25
- return 'ul'
26
- }
4
+ const variantToElement = {
5
+ description: 'dl',
6
+ unordered: 'ul',
7
+ ordered: 'ol',
27
8
  }
28
9
 
29
- interface Props {
10
+ export interface ListProps<T = HTMLElement> extends HTMLAttributes<T> {
11
+ /**
12
+ * ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
13
+ */
30
14
  testId?: string
31
- variant?: ListVariant
15
+ variant?: 'description' | 'ordered' | 'unordered'
16
+ as?: ElementType
32
17
  }
33
18
 
34
- export type ListProps<C extends ElementType> = PolymorphicComponentPropsWithRef<
35
- C,
36
- Props
37
- >
38
-
39
- type ListComponent = <C extends ElementType = 'ul'>(
40
- props: ListProps<C>
41
- ) => ReactElement | null
42
-
43
- const List: ListComponent = forwardRef(function List<
44
- C extends ElementType = 'ul'
45
- >(
19
+ const List = forwardRef<HTMLUListElement, ListProps>(function List(
46
20
  {
47
21
  testId = 'store-list',
48
22
  variant = 'unordered',
49
- as,
50
- ...rawProps
51
- }: ListProps<C>,
52
- ref: ComponentPropsWithRef<C>['ref']
23
+ as: MaybeComponent,
24
+ ...otherProps
25
+ },
26
+ ref
53
27
  ) {
54
28
  const dataAttributes = {
55
29
  'data-testid': testId,
@@ -57,16 +31,9 @@ const List: ListComponent = forwardRef(function List<
57
31
  [`data-${variant}`]: true,
58
32
  }
59
33
 
60
- const Component = as ?? variantToType(variant)
34
+ const Component = MaybeComponent ?? variantToElement[variant] ?? 'ul'
61
35
 
62
- return <Component ref={ref} {...dataAttributes} {...rawProps} />
36
+ return <Component ref={ref} {...dataAttributes} {...otherProps} />
63
37
  })
64
38
 
65
- /**
66
- * This is only being exported to make it easier to use in Storybook.
67
- * **DON'T** import this directly to use this component, use the default export
68
- * instead.
69
- */
70
- export const StorybookList = List as FC<Props>
71
-
72
39
  export default List
@@ -1,5 +1,5 @@
1
1
  import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'
2
- import { StorybookList } from '../List'
2
+ import List from '../List'
3
3
 
4
4
  # List
5
5
 
@@ -23,7 +23,7 @@ import { StorybookList } from '../List'
23
23
 
24
24
  ## Props
25
25
 
26
- <ArgsTable of={StorybookList} />
26
+ <ArgsTable of={List} />
27
27
 
28
28
  ## CSS Selectors
29
29
 
@@ -6,8 +6,8 @@ import type { ListProps } from '../List'
6
6
  import Component from '../List'
7
7
  import mdx from './List.mdx'
8
8
 
9
- type ListStoryProps = ListProps<'ul'> & { options: string[] }
10
- type DescriptionListStoryProps = ListProps<'dl'> & { options: string[][] }
9
+ type ListStoryProps = ListProps & { options: string[] }
10
+ type DescriptionListStoryProps = ListProps & { options: string[][] }
11
11
 
12
12
  const ListTemplate: Story<ListStoryProps> = ({ options, ...props }) => {
13
13
  return (