@faststore/ui 1.3.42 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/ui",
3
- "version": "1.3.42",
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.42",
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": "dbc712d9aee5d9165f6f238da3ca786aa44b014b"
92
+ "gitHead": "d5f68246cc8e3f3689774a2fcb27508bdbee36c7"
93
93
  }
package/src/index.ts CHANGED
@@ -133,6 +133,9 @@ export type { FormProps } from './molecules/Form'
133
133
  export { default as Alert } from './molecules/Alert'
134
134
  export type { AlertProps } from './molecules/Alert'
135
135
 
136
+ export { default as QuantitySelector } from './molecules/QuantitySelector'
137
+ export type { QuantitySelectorProps } from './molecules/QuantitySelector'
138
+
136
139
  // Organisms
137
140
  export {
138
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,2 @@
1
+ export { default } from './QuantitySelector'
2
+ export type { QuantitySelectorProps } from './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
@@ -0,0 +1,52 @@
1
+ import React from 'react'
2
+
3
+ type QuantitySelectorIconProps = {
4
+ color: string
5
+ }
6
+
7
+ export function PlusIcon(props: QuantitySelectorIconProps) {
8
+ return (
9
+ <svg
10
+ width="32"
11
+ height="32"
12
+ viewBox="0 0 32 32"
13
+ fill="none"
14
+ xmlns="http://www.w3.org/2000/svg"
15
+ >
16
+ <path
17
+ d="M11 16H21"
18
+ stroke={props.color}
19
+ strokeWidth="1.5"
20
+ strokeLinecap="round"
21
+ strokeLinejoin="round"
22
+ />
23
+ <path
24
+ d="M16 11V21"
25
+ stroke={props.color}
26
+ strokeWidth="1.5"
27
+ strokeLinecap="round"
28
+ strokeLinejoin="round"
29
+ />
30
+ </svg>
31
+ )
32
+ }
33
+
34
+ export function MinusIcon(props: QuantitySelectorIconProps) {
35
+ return (
36
+ <svg
37
+ width="32"
38
+ height="32"
39
+ viewBox="0 0 32 32"
40
+ fill="none"
41
+ xmlns="http://www.w3.org/2000/svg"
42
+ >
43
+ <path
44
+ d="M11 16H21"
45
+ stroke={props.color}
46
+ strokeWidth="1.5"
47
+ strokeLinecap="round"
48
+ strokeLinejoin="round"
49
+ />
50
+ </svg>
51
+ )
52
+ }