@faststore/ui 1.3.39 → 1.3.43
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 +41 -0
- package/dist/atoms/Incentive/Incentive.d.ts +11 -0
- package/dist/atoms/Incentive/index.d.ts +2 -0
- package/dist/atoms/Incentive/stories/Incentive.stories.d.ts +4 -0
- package/dist/atoms/Incentive/stories/assets/Icons.d.ts +2 -0
- package/dist/atoms/Link/Link.d.ts +19 -0
- package/dist/atoms/Link/index.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/molecules/QuantitySelector/QuantitySelector.d.ts +30 -0
- package/dist/molecules/QuantitySelector/index.d.ts +2 -0
- package/dist/molecules/QuantitySelector/stories/QuantitySelector.stories.d.ts +5 -0
- package/dist/molecules/QuantitySelector/stories/assets/Icons.d.ts +7 -0
- package/dist/typings/PolymorphicGenerics.d.ts +1 -0
- package/dist/typings/index.d.ts +1 -1
- package/dist/ui.cjs.development.js +57 -0
- 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 +55 -1
- package/dist/ui.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/atoms/Incentive/Incentive.test.tsx +36 -0
- package/src/atoms/Incentive/Incentive.tsx +23 -0
- package/src/atoms/Incentive/index.tsx +2 -0
- package/src/atoms/Incentive/stories/Incentive.mdx +24 -0
- package/src/atoms/Incentive/stories/Incentive.stories.tsx +26 -0
- package/src/atoms/Incentive/stories/assets/Icons.tsx +14 -0
- package/src/atoms/Link/Link.test.tsx +53 -0
- package/src/atoms/Link/Link.tsx +47 -0
- package/src/atoms/Link/index.tsx +2 -0
- package/src/atoms/Link/stories/Link.mdx +35 -0
- package/src/atoms/Link/stories/Link.stories.tsx +30 -0
- package/src/index.ts +9 -0
- package/src/molecules/QuantitySelector/QuantitySelector.test.tsx +93 -0
- package/src/molecules/QuantitySelector/QuantitySelector.tsx +82 -0
- package/src/molecules/QuantitySelector/index.ts +2 -0
- package/src/molecules/QuantitySelector/stories/QuantitySelector.mdx +26 -0
- package/src/molecules/QuantitySelector/stories/QuantitySelector.stories.tsx +77 -0
- package/src/molecules/QuantitySelector/stories/assets/Icons.tsx +52 -0
- package/src/typings/PolymorphicGenerics.ts +5 -0
- package/src/typings/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/ui",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.43",
|
|
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.3.
|
|
79
|
+
"@vtex/theme-b2c-tailwind": "^1.3.43",
|
|
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": "d5f68246cc8e3f3689774a2fcb27508bdbee36c7"
|
|
93
93
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { render } from '@testing-library/react'
|
|
2
|
+
import { axe } from 'jest-axe'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
|
|
5
|
+
import Incentive from './Incentive'
|
|
6
|
+
|
|
7
|
+
const TestIncentive = () => {
|
|
8
|
+
return (
|
|
9
|
+
<Incentive>
|
|
10
|
+
<span>Item title</span>
|
|
11
|
+
<span>Item description</span>
|
|
12
|
+
</Incentive>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('Incentive', () => {
|
|
17
|
+
let incentive: HTMLElement
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
const { getByTestId } = render(<TestIncentive />)
|
|
21
|
+
|
|
22
|
+
incentive = getByTestId('store-incentive')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('Data attributes', () => {
|
|
26
|
+
it('should have `data-store-incentive` attribute', () => {
|
|
27
|
+
expect(incentive).toHaveAttribute('data-store-incentive')
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
describe('Accessibility', () => {
|
|
32
|
+
it('should have no violations', async () => {
|
|
33
|
+
expect(await axe(incentive)).toHaveNoViolations()
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react'
|
|
2
|
+
import type { HTMLAttributes } from 'react'
|
|
3
|
+
|
|
4
|
+
export interface IncentiveProps extends HTMLAttributes<HTMLDivElement> {
|
|
5
|
+
/**
|
|
6
|
+
* ID to find this component in testing tools (e.g.: cypress,
|
|
7
|
+
* testing-library, and jest).
|
|
8
|
+
*/
|
|
9
|
+
testId?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const Incentive = forwardRef<HTMLDivElement, IncentiveProps>(function Incentive(
|
|
13
|
+
{ testId = 'store-incentive', children, ...otherProps },
|
|
14
|
+
ref
|
|
15
|
+
) {
|
|
16
|
+
return (
|
|
17
|
+
<div ref={ref} data-store-incentive data-testid={testId} {...otherProps}>
|
|
18
|
+
{children}
|
|
19
|
+
</div>
|
|
20
|
+
)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
export default Incentive
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Canvas, Props, Story, ArgsTable } from '@storybook/addon-docs'
|
|
2
|
+
|
|
3
|
+
import Incentive from '../Incentive'
|
|
4
|
+
|
|
5
|
+
# Incentive
|
|
6
|
+
|
|
7
|
+
<Canvas>
|
|
8
|
+
<Story id="atoms-incentive--incentive" />
|
|
9
|
+
</Canvas>
|
|
10
|
+
|
|
11
|
+
## Props
|
|
12
|
+
|
|
13
|
+
`Incentive` component support all attributes also supported by the `<div>` tag.
|
|
14
|
+
|
|
15
|
+
Besides those attributes, the following props are also supported:
|
|
16
|
+
|
|
17
|
+
<ArgsTable of={Incentive} />
|
|
18
|
+
|
|
19
|
+
## CSS Selectors
|
|
20
|
+
|
|
21
|
+
```css
|
|
22
|
+
[data-store-incentive] {
|
|
23
|
+
}
|
|
24
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Story, Meta } from '@storybook/react'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
4
|
+
import { SafetyIcon } from './assets/Icons'
|
|
5
|
+
import type { IncentiveProps } from '../Incentive'
|
|
6
|
+
import Component from '../Incentive'
|
|
7
|
+
import mdx from './Incentive.mdx'
|
|
8
|
+
|
|
9
|
+
const IncentiveTemplate: Story<IncentiveProps> = ({ testId }) => (
|
|
10
|
+
<Component testId={testId}>
|
|
11
|
+
<SafetyIcon />
|
|
12
|
+
<span>Trusted by SafeCompany</span>
|
|
13
|
+
</Component>
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
export const Incentive = IncentiveTemplate.bind({})
|
|
17
|
+
Incentive.storyName = 'Incentive'
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
title: 'Atoms/Incentive',
|
|
21
|
+
parameters: {
|
|
22
|
+
docs: {
|
|
23
|
+
page: mdx,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
} as Meta
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
export function SafetyIcon() {
|
|
4
|
+
return (
|
|
5
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="35" height="35" fill="none">
|
|
6
|
+
<path
|
|
7
|
+
fillRule="evenodd"
|
|
8
|
+
clipRule="evenodd"
|
|
9
|
+
d="M6 7h20v7c0 10-8 14-10 14S6 24 6 14V7Zm0-2a2 2 0 0 0-2 2v7c0 11 10 15 11 16h2c1-1 11-5 11-16V7a2 2 0 0 0-2-2H6Zm16 9a1 1 0 0 0-1-2l-7 7-3-3a1 1 0 0 0-1 1l4 4h1l7-7Z"
|
|
10
|
+
fill="#323845"
|
|
11
|
+
/>
|
|
12
|
+
</svg>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { render } from '@testing-library/react'
|
|
2
|
+
import { axe } from 'jest-axe'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
|
|
5
|
+
import type { LinkProps } from '.'
|
|
6
|
+
import LinkComponent from '.'
|
|
7
|
+
|
|
8
|
+
const TestLink = ({ href = '/', testId }: LinkProps<'a'>) => {
|
|
9
|
+
return (
|
|
10
|
+
<LinkComponent testId={testId} href={href}>
|
|
11
|
+
Link
|
|
12
|
+
</LinkComponent>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('Link', () => {
|
|
17
|
+
it('should receive a custom component', () => {
|
|
18
|
+
const { getByTestId } = render(
|
|
19
|
+
<LinkComponent
|
|
20
|
+
href="/"
|
|
21
|
+
as={(props: { href: string }) => {
|
|
22
|
+
return (
|
|
23
|
+
<div {...props}>
|
|
24
|
+
<a data-testid="custom-anchor" href={props.href}>
|
|
25
|
+
Link
|
|
26
|
+
</a>
|
|
27
|
+
</div>
|
|
28
|
+
)
|
|
29
|
+
}}
|
|
30
|
+
/>
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
expect(getByTestId('custom-anchor')).toHaveAttribute('href', '/')
|
|
34
|
+
expect(getByTestId('store-link')).toHaveAttribute('data-store-link')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('Data Attributes', () => {
|
|
38
|
+
it('should have `data-store-link` attribute', () => {
|
|
39
|
+
const { getByTestId } = render(<TestLink />)
|
|
40
|
+
|
|
41
|
+
expect(getByTestId('store-link')).toHaveAttribute('data-store-link')
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('Accessibility', () => {
|
|
46
|
+
it('should have no violations', async () => {
|
|
47
|
+
const { getByTestId } = render(<TestLink />)
|
|
48
|
+
|
|
49
|
+
expect(await axe(getByTestId('store-link'))).toHaveNoViolations()
|
|
50
|
+
expect(await axe(getByTestId('store-link'))).toHaveNoIncompletes()
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react'
|
|
2
|
+
import type { FC, ElementType, ReactElement } from 'react'
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
PolymorphicRef,
|
|
6
|
+
PolymorphicComponentPropsWithRef,
|
|
7
|
+
} from '../../typings'
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
/**
|
|
11
|
+
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
|
|
12
|
+
*/
|
|
13
|
+
testId?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type LinkProps<C extends ElementType> = PolymorphicComponentPropsWithRef<
|
|
17
|
+
C,
|
|
18
|
+
Props
|
|
19
|
+
>
|
|
20
|
+
|
|
21
|
+
type LinkComponent = <C extends ElementType = 'a'>(
|
|
22
|
+
props: LinkProps<C>
|
|
23
|
+
) => ReactElement | null
|
|
24
|
+
|
|
25
|
+
const Link: LinkComponent = forwardRef(function Link<
|
|
26
|
+
C extends ElementType = 'a'
|
|
27
|
+
>(
|
|
28
|
+
{ as, children, testId = 'store-link', ...otherProps }: LinkProps<C>,
|
|
29
|
+
ref?: PolymorphicRef<C>
|
|
30
|
+
) {
|
|
31
|
+
const Component = as ?? 'a'
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<Component ref={ref} data-store-link data-testid={testId} {...otherProps}>
|
|
35
|
+
{children}
|
|
36
|
+
</Component>
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* This is only being exported to make it easier to use in Storybook.
|
|
42
|
+
* **DON'T** import this directly to use this component, use the default export
|
|
43
|
+
* instead.
|
|
44
|
+
*/
|
|
45
|
+
export const StorybookLink = Link as FC<Props>
|
|
46
|
+
|
|
47
|
+
export default Link
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Canvas, Props, Story, ArgsTable } from '@storybook/addon-docs'
|
|
2
|
+
import { StorybookLink } from '../Link'
|
|
3
|
+
|
|
4
|
+
# Link
|
|
5
|
+
|
|
6
|
+
`Link` component performs the same behavior of a regular anchor tag (`<a>`), but can be used as any HTML tag.
|
|
7
|
+
|
|
8
|
+
## Default
|
|
9
|
+
|
|
10
|
+
<Canvas>
|
|
11
|
+
<Story id="atoms-link--default" />
|
|
12
|
+
</Canvas>
|
|
13
|
+
|
|
14
|
+
## As Div
|
|
15
|
+
|
|
16
|
+
<Canvas>
|
|
17
|
+
<Story id="atoms-link--as-div" />
|
|
18
|
+
</Canvas>
|
|
19
|
+
|
|
20
|
+
## Props
|
|
21
|
+
|
|
22
|
+
The component supports all attributes also supported by the choosen tag (through the prop `as`).
|
|
23
|
+
|
|
24
|
+
For example, in cases it is wanted to be used as a wrapper for any external link component (i.e., Next.js Link or Gatsby Link), the component can be used with the prop `as="div"`.
|
|
25
|
+
|
|
26
|
+
Besides those attributes, the following props are also supported:
|
|
27
|
+
|
|
28
|
+
<ArgsTable of={StorybookLink} />
|
|
29
|
+
|
|
30
|
+
## CSS Selectors
|
|
31
|
+
|
|
32
|
+
```css
|
|
33
|
+
[data-store-link] {
|
|
34
|
+
}
|
|
35
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Story, Meta } from '@storybook/react'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
4
|
+
import type { LinkProps } from '../Link'
|
|
5
|
+
import LinkComponent from '../Link'
|
|
6
|
+
import mdx from './Link.mdx'
|
|
7
|
+
|
|
8
|
+
const LinkDefaultTemplate: Story<LinkProps<'a'>> = () => (
|
|
9
|
+
<LinkComponent href="/">A default link</LinkComponent>
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
export const Default = LinkDefaultTemplate.bind({})
|
|
13
|
+
|
|
14
|
+
const LinkAsDivTemplate: Story<LinkProps<'div'>> = () => (
|
|
15
|
+
<LinkComponent as="div">
|
|
16
|
+
<span>A span inside the Link component</span>
|
|
17
|
+
<a href="/">A link inside the Link component</a>
|
|
18
|
+
</LinkComponent>
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
export const AsDiv = LinkAsDivTemplate.bind({})
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
title: 'Atoms/Link',
|
|
25
|
+
parameters: {
|
|
26
|
+
docs: {
|
|
27
|
+
page: mdx,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
} as Meta
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// Atoms
|
|
2
|
+
export { default as Link } from './atoms/Link'
|
|
3
|
+
export type { LinkProps } from './atoms/Link'
|
|
4
|
+
|
|
2
5
|
export { default as Button } from './atoms/Button'
|
|
3
6
|
export type { ButtonProps } from './atoms/Button'
|
|
4
7
|
|
|
@@ -47,6 +50,9 @@ export type { SpinnerProps } from './atoms/Spinner'
|
|
|
47
50
|
export { default as Label } from './atoms/Label'
|
|
48
51
|
export type { LabelProps } from './atoms/Label'
|
|
49
52
|
|
|
53
|
+
export { default as Incentive } from './atoms/Incentive'
|
|
54
|
+
export type { IncentiveProps } from './atoms/Incentive'
|
|
55
|
+
|
|
50
56
|
// Molecules
|
|
51
57
|
export { default as Bullets } from './molecules/Bullets'
|
|
52
58
|
export type { BulletsProps } from './molecules/Bullets'
|
|
@@ -127,6 +133,9 @@ export type { FormProps } from './molecules/Form'
|
|
|
127
133
|
export { default as Alert } from './molecules/Alert'
|
|
128
134
|
export type { AlertProps } from './molecules/Alert'
|
|
129
135
|
|
|
136
|
+
export { default as QuantitySelector } from './molecules/QuantitySelector'
|
|
137
|
+
export type { QuantitySelectorProps } from './molecules/QuantitySelector'
|
|
138
|
+
|
|
130
139
|
// Organisms
|
|
131
140
|
export {
|
|
132
141
|
default as Card,
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { render } from '@testing-library/react'
|
|
2
|
+
import React, { useState } from 'react'
|
|
3
|
+
import userEvent from '@testing-library/user-event'
|
|
4
|
+
import { axe } from 'jest-axe'
|
|
5
|
+
|
|
6
|
+
import QuantitySelector from '.'
|
|
7
|
+
|
|
8
|
+
describe('QuantitySelector', () => {
|
|
9
|
+
it('should render', () => {
|
|
10
|
+
const { getByTestId } = render(
|
|
11
|
+
<QuantitySelector
|
|
12
|
+
name="quantity-selector"
|
|
13
|
+
quantity={1}
|
|
14
|
+
leftButtonProps={{ icon: <span>-</span> }}
|
|
15
|
+
rightButtonProps={{ icon: <span>+</span> }}
|
|
16
|
+
inputProps={{ readOnly: true }}
|
|
17
|
+
/>
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
expect(getByTestId('store-quantity-selector')).toBeInTheDocument()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should render with the correct quantity', () => {
|
|
24
|
+
const { getByLabelText } = render(
|
|
25
|
+
<QuantitySelector
|
|
26
|
+
name="quantity-selector"
|
|
27
|
+
quantity={123}
|
|
28
|
+
leftButtonProps={{ icon: <span>-</span> }}
|
|
29
|
+
rightButtonProps={{ icon: <span>+</span> }}
|
|
30
|
+
inputProps={{ readOnly: true }}
|
|
31
|
+
/>
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
expect(getByLabelText('Quantity')).toHaveValue('123')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should update correctly according to event changes', () => {
|
|
38
|
+
const QuantitySelectorTest = () => {
|
|
39
|
+
const [quantity, setQuantity] = useState(1)
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<QuantitySelector
|
|
43
|
+
name="quantity-selector"
|
|
44
|
+
quantity={quantity}
|
|
45
|
+
leftButtonProps={{
|
|
46
|
+
icon: <span>-</span>,
|
|
47
|
+
onClick: () => {
|
|
48
|
+
setQuantity((curr) => curr - 1)
|
|
49
|
+
},
|
|
50
|
+
}}
|
|
51
|
+
rightButtonProps={{
|
|
52
|
+
icon: <span>+</span>,
|
|
53
|
+
onClick: () => {
|
|
54
|
+
setQuantity((curr) => curr + 1)
|
|
55
|
+
},
|
|
56
|
+
}}
|
|
57
|
+
inputProps={{ readOnly: true }}
|
|
58
|
+
/>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { getByLabelText } = render(<QuantitySelectorTest />)
|
|
63
|
+
|
|
64
|
+
expect(getByLabelText('Quantity')).toHaveValue('1')
|
|
65
|
+
|
|
66
|
+
const rightButton = getByLabelText('Increment Quantity')
|
|
67
|
+
const leftButton = getByLabelText('Decrement Quantity')
|
|
68
|
+
|
|
69
|
+
userEvent.click(rightButton)
|
|
70
|
+
expect(getByLabelText('Quantity')).toHaveValue('2')
|
|
71
|
+
userEvent.click(leftButton)
|
|
72
|
+
expect(getByLabelText('Quantity')).toHaveValue('1')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('QuantitySelector Accessibility', () => {
|
|
76
|
+
it('should pass the AXE tests', async () => {
|
|
77
|
+
render(
|
|
78
|
+
<main>
|
|
79
|
+
<QuantitySelector
|
|
80
|
+
name="quantity-selector"
|
|
81
|
+
quantity={1}
|
|
82
|
+
leftButtonProps={{ icon: <span>-</span> }}
|
|
83
|
+
rightButtonProps={{ icon: <span>+</span> }}
|
|
84
|
+
inputProps={{ readOnly: true }}
|
|
85
|
+
/>
|
|
86
|
+
</main>
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
expect(await axe(document.body)).toHaveNoViolations()
|
|
90
|
+
expect(await axe(document.body)).toHaveNoIncompletes()
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
})
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { InputHTMLAttributes } from 'react'
|
|
2
|
+
import React, { forwardRef } from 'react'
|
|
3
|
+
|
|
4
|
+
import type { InputProps } from '../../atoms/Input'
|
|
5
|
+
import Input from '../../atoms/Input'
|
|
6
|
+
import type { IconButtonProps } from '../IconButton'
|
|
7
|
+
import IconButton from '../IconButton'
|
|
8
|
+
|
|
9
|
+
export interface QuantitySelectorProps
|
|
10
|
+
extends InputHTMLAttributes<HTMLDivElement> {
|
|
11
|
+
/**
|
|
12
|
+
* Sets the current value that should be displayed on the input at the center of the quantity selector.
|
|
13
|
+
*/
|
|
14
|
+
quantity: number | string
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Object with properties that will be passed forward the inner IconButton atom at the left of the input.
|
|
18
|
+
*/
|
|
19
|
+
leftButtonProps: IconButtonProps
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Object with properties that will be passed forward the inner Input atom between the two buttons.
|
|
23
|
+
*/
|
|
24
|
+
inputProps: InputProps
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Object with properties that will be passed forward the inner IconButton atom at the right of the input.
|
|
28
|
+
*/
|
|
29
|
+
rightButtonProps: IconButtonProps
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
|
|
33
|
+
*
|
|
34
|
+
* @default 'store-quantity-selector'
|
|
35
|
+
*/
|
|
36
|
+
testId?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const QuantitySelector = forwardRef<HTMLDivElement, QuantitySelectorProps>(
|
|
40
|
+
function QuantitySelector(
|
|
41
|
+
{
|
|
42
|
+
quantity,
|
|
43
|
+
testId = 'store-quantity-selector',
|
|
44
|
+
leftButtonProps,
|
|
45
|
+
inputProps,
|
|
46
|
+
rightButtonProps,
|
|
47
|
+
...otherProps
|
|
48
|
+
}: QuantitySelectorProps,
|
|
49
|
+
ref
|
|
50
|
+
) {
|
|
51
|
+
return (
|
|
52
|
+
<div
|
|
53
|
+
data-store-quantity-selector
|
|
54
|
+
data-testid={testId}
|
|
55
|
+
ref={ref}
|
|
56
|
+
{...otherProps}
|
|
57
|
+
>
|
|
58
|
+
<IconButton
|
|
59
|
+
aria-controls="quantity-selector-input"
|
|
60
|
+
aria-label="Decrement Quantity"
|
|
61
|
+
data-quantity-selector-button="left"
|
|
62
|
+
{...leftButtonProps}
|
|
63
|
+
/>
|
|
64
|
+
<Input
|
|
65
|
+
aria-label="Quantity"
|
|
66
|
+
data-quantity-selector-input
|
|
67
|
+
id="quantity-selector-input"
|
|
68
|
+
value={quantity}
|
|
69
|
+
{...inputProps}
|
|
70
|
+
/>
|
|
71
|
+
<IconButton
|
|
72
|
+
aria-controls="quantity-selector-input"
|
|
73
|
+
aria-label="Increment Quantity"
|
|
74
|
+
data-quantity-selector-button="right"
|
|
75
|
+
{...rightButtonProps}
|
|
76
|
+
/>
|
|
77
|
+
</div>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
export default QuantitySelector
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'
|
|
2
|
+
|
|
3
|
+
import QuantitySelector from '../QuantitySelector'
|
|
4
|
+
|
|
5
|
+
# QuantitySelector
|
|
6
|
+
|
|
7
|
+
## Default Style
|
|
8
|
+
|
|
9
|
+
<Canvas>
|
|
10
|
+
<Story id="molecules-quantityselector--default-style" />
|
|
11
|
+
</Canvas>
|
|
12
|
+
|
|
13
|
+
# Props
|
|
14
|
+
|
|
15
|
+
<ArgsTable of={QuantitySelector} />
|
|
16
|
+
|
|
17
|
+
# CSS Selectors
|
|
18
|
+
|
|
19
|
+
```css
|
|
20
|
+
[data-store-quantity-selector] {
|
|
21
|
+
}
|
|
22
|
+
[data-quantity-selector-input] {
|
|
23
|
+
}
|
|
24
|
+
[data-quantity-selector-button='(left|right)'] {
|
|
25
|
+
}
|
|
26
|
+
```
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Meta, Story } from '@storybook/react'
|
|
2
|
+
import React, { useState } from 'react'
|
|
3
|
+
|
|
4
|
+
import type { QuantitySelectorProps } from '../QuantitySelector'
|
|
5
|
+
import Component from '../QuantitySelector'
|
|
6
|
+
import { PlusIcon, MinusIcon } from './assets/Icons'
|
|
7
|
+
import mdx from './QuantitySelector.mdx'
|
|
8
|
+
|
|
9
|
+
const MAX_QUANTITY = 10
|
|
10
|
+
const MIN_QUANTITY = 1
|
|
11
|
+
|
|
12
|
+
export const QuantitySelector: Story<QuantitySelectorProps> = () => {
|
|
13
|
+
const [quantity, setQuantity] = useState(MIN_QUANTITY)
|
|
14
|
+
|
|
15
|
+
function increase() {
|
|
16
|
+
setQuantity((currentQuantity) =>
|
|
17
|
+
validateQuantityBounds(currentQuantity + 1)
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function decrease() {
|
|
22
|
+
setQuantity((currentQuantity) =>
|
|
23
|
+
validateQuantityBounds(currentQuantity - 1)
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isLeftDisabled(): boolean {
|
|
28
|
+
return quantity === MIN_QUANTITY
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isRightDisabled(): boolean {
|
|
32
|
+
return quantity === MAX_QUANTITY
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function validateQuantityBounds(n: number): number {
|
|
36
|
+
return Math.min(Math.max(n, MIN_QUANTITY), MAX_QUANTITY)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function validateInput(e: React.FormEvent<HTMLInputElement>) {
|
|
40
|
+
const val = e.currentTarget.value
|
|
41
|
+
|
|
42
|
+
if (!Number.isNaN(Number(val))) {
|
|
43
|
+
setQuantity(validateQuantityBounds(Number(val)))
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<Component
|
|
49
|
+
quantity={quantity}
|
|
50
|
+
leftButtonProps={{
|
|
51
|
+
onClick: decrease,
|
|
52
|
+
disabled: isLeftDisabled(),
|
|
53
|
+
icon: <MinusIcon color={isLeftDisabled() ? '#898F9E' : '#2953B2'} />,
|
|
54
|
+
}}
|
|
55
|
+
rightButtonProps={{
|
|
56
|
+
onClick: increase,
|
|
57
|
+
disabled: isRightDisabled(),
|
|
58
|
+
icon: <PlusIcon color={isRightDisabled() ? '#898F9E' : '#2953B2'} />,
|
|
59
|
+
}}
|
|
60
|
+
inputProps={{
|
|
61
|
+
onChange: validateInput,
|
|
62
|
+
readOnly: false,
|
|
63
|
+
}}
|
|
64
|
+
/>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
QuantitySelector.storyName = 'QuantitySelector'
|
|
69
|
+
|
|
70
|
+
export default {
|
|
71
|
+
title: 'Molecules/QuantitySelector',
|
|
72
|
+
parameters: {
|
|
73
|
+
docs: {
|
|
74
|
+
page: mdx,
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
} as Meta
|