@faststore/components 2.0.85-alpha.0 → 2.0.88-alpha.0

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.
Files changed (83) hide show
  1. package/dist/assets/MagnifyingGlass.d.ts +3 -2
  2. package/dist/assets/MagnifyingGlass.js +3 -4
  3. package/dist/assets/MagnifyingGlass.js.map +1 -1
  4. package/dist/hooks/index.d.ts +1 -0
  5. package/dist/hooks/index.js +1 -0
  6. package/dist/hooks/index.js.map +1 -1
  7. package/dist/hooks/useSearch.d.ts +7 -0
  8. package/dist/hooks/useSearch.js +10 -0
  9. package/dist/hooks/useSearch.js.map +1 -0
  10. package/dist/index.d.ts +9 -0
  11. package/dist/index.js +5 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/molecules/SearchAutoComplete/SearchAutoComplete.d.ts +1 -1
  14. package/dist/molecules/SearchAutoComplete/SearchAutoComplete.js +5 -1
  15. package/dist/molecules/SearchAutoComplete/SearchAutoComplete.js.map +1 -1
  16. package/dist/molecules/SearchDropdown/SearchDropdown.d.ts +14 -0
  17. package/dist/molecules/SearchDropdown/SearchDropdown.js +14 -0
  18. package/dist/molecules/SearchDropdown/SearchDropdown.js.map +1 -0
  19. package/dist/molecules/SearchDropdown/index.d.ts +2 -0
  20. package/dist/molecules/SearchDropdown/index.js +2 -0
  21. package/dist/molecules/SearchDropdown/index.js.map +1 -0
  22. package/dist/molecules/SearchHistory/SearchHistory.d.ts +1 -1
  23. package/dist/molecules/SearchHistory/SearchHistory.js +5 -1
  24. package/dist/molecules/SearchHistory/SearchHistory.js.map +1 -1
  25. package/dist/molecules/SearchInputField/SearchInputField.d.ts +36 -0
  26. package/dist/molecules/SearchInputField/SearchInputField.js +21 -0
  27. package/dist/molecules/SearchInputField/SearchInputField.js.map +1 -0
  28. package/dist/molecules/SearchInputField/index.d.ts +2 -0
  29. package/dist/molecules/SearchInputField/index.js +2 -0
  30. package/dist/molecules/SearchInputField/index.js.map +1 -0
  31. package/dist/molecules/SearchProducts/SearchProductItemContent.d.ts +2 -2
  32. package/dist/molecules/SearchProducts/SearchProducts.d.ts +1 -1
  33. package/dist/molecules/SearchProducts/SearchProducts.js +5 -1
  34. package/dist/molecules/SearchProducts/SearchProducts.js.map +1 -1
  35. package/dist/molecules/SearchProvider/SearchProvider.d.ts +29 -0
  36. package/dist/molecules/SearchProvider/SearchProvider.js +8 -0
  37. package/dist/molecules/SearchProvider/SearchProvider.js.map +1 -0
  38. package/dist/molecules/SearchProvider/index.d.ts +2 -0
  39. package/dist/molecules/SearchProvider/index.js +2 -0
  40. package/dist/molecules/SearchProvider/index.js.map +1 -0
  41. package/dist/molecules/SearchTop/SearchTop.d.ts +0 -8
  42. package/dist/molecules/SearchTop/SearchTop.js +8 -4
  43. package/dist/molecules/SearchTop/SearchTop.js.map +1 -1
  44. package/dist/molecules/SkuSelector/SkuSelector.d.ts +78 -0
  45. package/dist/molecules/SkuSelector/SkuSelector.js +31 -0
  46. package/dist/molecules/SkuSelector/SkuSelector.js.map +1 -0
  47. package/dist/molecules/SkuSelector/index.d.ts +2 -0
  48. package/dist/molecules/SkuSelector/index.js +2 -0
  49. package/dist/molecules/SkuSelector/index.js.map +1 -0
  50. package/dist/molecules/SkuSelector/useDefineVariant.d.ts +3 -0
  51. package/dist/molecules/SkuSelector/useDefineVariant.js +27 -0
  52. package/dist/molecules/SkuSelector/useDefineVariant.js.map +1 -0
  53. package/dist/molecules/SkuSelector/useSkuSlug.d.ts +6 -0
  54. package/dist/molecules/SkuSelector/useSkuSlug.js +23 -0
  55. package/dist/molecules/SkuSelector/useSkuSlug.js.map +1 -0
  56. package/dist/organisms/SearchInput/SearchInput.d.ts +26 -0
  57. package/dist/organisms/SearchInput/SearchInput.js +8 -0
  58. package/dist/organisms/SearchInput/SearchInput.js.map +1 -0
  59. package/dist/organisms/SearchInput/index.d.ts +2 -0
  60. package/dist/organisms/SearchInput/index.js +2 -0
  61. package/dist/organisms/SearchInput/index.js.map +1 -0
  62. package/package.json +2 -2
  63. package/src/assets/MagnifyingGlass.tsx +12 -29
  64. package/src/hooks/index.ts +1 -0
  65. package/src/hooks/useSearch.ts +12 -0
  66. package/src/index.ts +20 -0
  67. package/src/molecules/SearchAutoComplete/SearchAutoComplete.tsx +8 -1
  68. package/src/molecules/SearchDropdown/SearchDropdown.tsx +45 -0
  69. package/src/molecules/SearchDropdown/index.ts +2 -0
  70. package/src/molecules/SearchHistory/SearchHistory.tsx +8 -1
  71. package/src/molecules/SearchInputField/SearchInputField.tsx +100 -0
  72. package/src/molecules/SearchInputField/index.ts +5 -0
  73. package/src/molecules/SearchProducts/SearchProductItemContent.tsx +1 -1
  74. package/src/molecules/SearchProducts/SearchProducts.tsx +6 -1
  75. package/src/molecules/SearchProvider/SearchProvider.tsx +49 -0
  76. package/src/molecules/SearchProvider/index.ts +2 -0
  77. package/src/molecules/SearchTop/SearchTop.tsx +12 -21
  78. package/src/molecules/SkuSelector/SkuSelector.tsx +181 -0
  79. package/src/molecules/SkuSelector/index.tsx +2 -0
  80. package/src/molecules/SkuSelector/useDefineVariant.ts +36 -0
  81. package/src/molecules/SkuSelector/useSkuSlug.ts +49 -0
  82. package/src/organisms/SearchInput/SearchInput.tsx +55 -0
  83. package/src/organisms/SearchInput/index.ts +2 -0
@@ -0,0 +1,181 @@
1
+ /* eslint-disable @next/next/no-img-element */
2
+ import type { FunctionComponent, HTMLAttributes } from 'react'
3
+ import React, { forwardRef } from 'react'
4
+ import { Label, SROnly, Link, LinkProps, LinkElementType } from '../..'
5
+ import { useDefineVariant, Variant } from './useDefineVariant'
6
+ import { useSkuSlug } from './useSkuSlug'
7
+
8
+
9
+ // TODO: Change by ImageComponent when it be right
10
+ const ImageComponentFallback: SkuSelectorProps['ImageComponent'] = ({
11
+ src,
12
+ alt,
13
+ ...otherProps
14
+ }) => <img src={src} alt={alt} {...otherProps} />
15
+
16
+ export interface SkuOption {
17
+ /**
18
+ * Alternative text description of the image.
19
+ */
20
+ alt?: string
21
+ /**
22
+ * Image URL.
23
+ */
24
+ src?: string
25
+ /**
26
+ * Label to describe the option when selected. This is mandatory if you want to use the `image` variant.'
27
+ */
28
+ label: string
29
+ /**
30
+ * Current value for this option.
31
+ */
32
+ value: string
33
+ /**
34
+ * Specifies that this option should be disabled.
35
+ */
36
+ disabled?: boolean
37
+ /**
38
+ * Hex color code for this option. This is mandatory if you want to use the `Color` variant.
39
+ */
40
+ hexColor?: string
41
+ }
42
+
43
+ export interface SkuSelectorProps extends HTMLAttributes<HTMLDivElement> {
44
+ /**
45
+ * ID to find this component in testing tools (e.g.: cypress,
46
+ * testing-library, and jest).
47
+ */
48
+ testId?: string
49
+ /**
50
+ * ID of the current instance of the component.
51
+ */
52
+ id?: string
53
+ /**
54
+ * SKU options that should be rendered.
55
+ */
56
+ availableVariations: Record<string, SkuOption[]>
57
+ /**
58
+ * Name of the SKU property that this selector is relative to.
59
+ */
60
+ skuPropertyName: string
61
+ /**
62
+ * Currently active variation's value.
63
+ */
64
+ activeVariations: Record<string, string>
65
+ /**
66
+ * Optional variant type, when is not passed the type is inferred based on options properties
67
+ */
68
+ variant?: Variant
69
+ /**
70
+ * Extends all Link Props.
71
+ */
72
+ linkProps?: Partial<LinkProps<LinkElementType>>
73
+ /**
74
+ * Optional function to determines the href string.
75
+ */
76
+ getItemHref?: (option: SkuOption) => string
77
+ /**
78
+ * Maps property value combinations to their respective SKU's slug
79
+ */
80
+ slugsMap: Record<string, string>
81
+ /**
82
+ * Function that returns a React component that will be used to render images.
83
+ */
84
+ ImageComponent?: FunctionComponent<{
85
+ src: string
86
+ alt: string
87
+ }>
88
+ }
89
+
90
+ const SkuSelector = forwardRef<HTMLDivElement, SkuSelectorProps>(
91
+ function SkuSelector(
92
+ {
93
+ availableVariations,
94
+ skuPropertyName,
95
+ testId,
96
+ activeVariations,
97
+ linkProps,
98
+ slugsMap,
99
+ getItemHref: getItemHrefProp,
100
+ ImageComponent = ImageComponentFallback,
101
+ variant: variantProp,
102
+ ...otherProps
103
+ },
104
+ ref
105
+ ) {
106
+ const activeSelectorValue = activeVariations[skuPropertyName]
107
+ const options = availableVariations[skuPropertyName]
108
+
109
+ const variant = useDefineVariant(options, variantProp)
110
+
111
+ const { getItemHref } = useSkuSlug(activeVariations, slugsMap, skuPropertyName, getItemHrefProp)
112
+
113
+ return (
114
+ <div
115
+ ref={ref}
116
+ data-fs-sku-selector
117
+ data-testid={testId}
118
+ data-fs-sku-selector-variant={variant}
119
+ {...otherProps}
120
+ >
121
+ {skuPropertyName && (
122
+ <Label data-fs-sku-selector-title>
123
+ {skuPropertyName}: <strong>{activeSelectorValue}</strong>
124
+ </Label>
125
+ )}
126
+ <ul data-fs-sku-selector-list>
127
+ {options.map((option, index) => {
128
+ return (
129
+ <li
130
+ key={String(index)}
131
+ title={option.label}
132
+ data-fs-sku-selector-option
133
+ data-fs-sku-selector-disabled={option.disabled}
134
+ data-fs-sku-selector-checked={
135
+ option.value === activeVariations[skuPropertyName]
136
+ }
137
+ >
138
+ <Link
139
+ data-fs-sku-selector-option-link
140
+ href={getItemHref(option)}
141
+ {...linkProps}
142
+ >
143
+ <SROnly text={option.label} />
144
+ </Link>
145
+
146
+ {variant === 'label' && <span>{option.value}</span>}
147
+
148
+ {variant === 'image' && ImageComponent && (
149
+ <span>
150
+ <ImageComponent
151
+ src={option.src ?? ''}
152
+ alt={option.alt ?? ''}
153
+ data-fs-sku-selector-option-image
154
+ />
155
+ </span>
156
+ )}
157
+
158
+ {variant === 'color' && (
159
+ <span>
160
+ <div
161
+ data-fs-sku-selector-option-color
162
+ title={option.value}
163
+ style={
164
+ {
165
+ '--data-fs-sku-selector-option-color-bkg-color':
166
+ option.hexColor,
167
+ } as React.CSSProperties
168
+ }
169
+ ></div>
170
+ </span>
171
+ )}
172
+ </li>
173
+ )
174
+ })}
175
+ </ul>
176
+ </div>
177
+ )
178
+ }
179
+ )
180
+
181
+ export default SkuSelector
@@ -0,0 +1,2 @@
1
+ export { default } from './SkuSelector'
2
+ export type { SkuSelectorProps, SkuOption } from './SkuSelector'
@@ -0,0 +1,36 @@
1
+ import { useMemo } from 'react'
2
+ import { SkuOption } from './SkuSelector'
3
+
4
+ export type Variant = 'image' | 'color' | 'label'
5
+
6
+ const getImageName = (src: string) => {
7
+ const sourcePath = new URL(src).pathname
8
+ const imageName = sourcePath.split('/').at(-1)
9
+ return imageName
10
+ }
11
+
12
+ export const useDefineVariant = (options: SkuOption[], variant?: Variant): Variant =>
13
+ useMemo(() => {
14
+ if(variant) return variant
15
+
16
+ const allOptionsHaveHexColor = options.every((option) => option.hexColor)
17
+ if (allOptionsHaveHexColor) {
18
+ return 'color'
19
+ }
20
+
21
+ const firstImageName = options[0]?.src && getImageName(options[0].src)
22
+
23
+ const areSourcesEqualsOrNull = options.every((option) => {
24
+ if (!option.src) {
25
+ return true
26
+ }
27
+ const optionImageName = getImageName(option.src)
28
+ return optionImageName === firstImageName
29
+ })
30
+
31
+ if (!areSourcesEqualsOrNull) {
32
+ return 'image'
33
+ }
34
+
35
+ return 'label'
36
+ }, [options, variant])
@@ -0,0 +1,49 @@
1
+ import { useCallback } from 'react'
2
+ import { SkuOption } from './SkuSelector'
3
+
4
+ function getSkuSlug(
5
+ slugsMap: Record<string, string>,
6
+ selectedVariations: Record<string, string>,
7
+ dominantVariation: string
8
+ ) {
9
+ const slugsMapKey = Object.entries(selectedVariations).flat().join('-')
10
+
11
+ if (slugsMapKey in slugsMap) {
12
+ return slugsMap[slugsMapKey]
13
+ }
14
+
15
+ const possibleVariants = Object.keys(slugsMap)
16
+
17
+ const firstVariationForDominantValue = possibleVariants.find((slug) =>
18
+ slug.includes(
19
+ `${dominantVariation}-${selectedVariations[dominantVariation]}`
20
+ )
21
+ )
22
+
23
+ return slugsMap[firstVariationForDominantValue ?? possibleVariants[0]]
24
+ }
25
+
26
+ export const useSkuSlug = (
27
+ activeVariations: Record<string, string>,
28
+ slugsMap: Record<string, string>,
29
+ skuPropertyName: string,
30
+ getItemHrefProp?: (option: SkuOption) => string
31
+ ) => {
32
+ const getItemHref = useCallback(
33
+ (option: SkuOption) => {
34
+ if(getItemHrefProp) return { getItemHrefProp }
35
+
36
+ const currentItemHref = `/${getSkuSlug(
37
+ slugsMap,
38
+ {
39
+ ...activeVariations,
40
+ [skuPropertyName]: option.value,
41
+ },
42
+ skuPropertyName
43
+ )}/p`
44
+ return currentItemHref
45
+ },
46
+ [activeVariations, getItemHrefProp, slugsMap, skuPropertyName]
47
+ )
48
+ return { getItemHref }
49
+ }
@@ -0,0 +1,55 @@
1
+ import React, { forwardRef } from 'react'
2
+ import type { HTMLAttributes } from 'react'
3
+ import SearchProvider, { SearchProviderContextValue } from '../../molecules/SearchProvider'
4
+
5
+
6
+ export type SearchInputProps = {
7
+ /**
8
+ * ID to find this component in testing tools (e.g.: cypress,
9
+ * testing-library, and jest).
10
+ */
11
+ testId?: string
12
+ /**
13
+ * The current status of the Search Dropdown.
14
+ */
15
+ visibleDropdown: boolean
16
+ } & HTMLAttributes<HTMLDivElement> & SearchProviderContextValue
17
+
18
+ const SearchInput = forwardRef<HTMLDivElement, SearchInputProps>(
19
+ function SearchInput(
20
+ {
21
+ children,
22
+ visibleDropdown = false,
23
+ testId = 'fs-search-input',
24
+ isLoading,
25
+ products,
26
+ term,
27
+ terms,
28
+ onSearchSelection,
29
+ ...otherProps
30
+ },
31
+ ref
32
+ ) {
33
+ return (
34
+ <div
35
+ ref={ref}
36
+ data-fs-search-input
37
+ data-fs-search-input-dropdown-visible={visibleDropdown}
38
+ data-testid={testId}
39
+ {...otherProps}
40
+ >
41
+ <SearchProvider
42
+ onSearchSelection={onSearchSelection}
43
+ isLoading={isLoading}
44
+ term={term}
45
+ products={products}
46
+ terms={terms}
47
+ >
48
+ {children}
49
+ </SearchProvider>
50
+ </div>
51
+ )
52
+ }
53
+ )
54
+
55
+ export default SearchInput
@@ -0,0 +1,2 @@
1
+ export { default } from './SearchInput'
2
+ export type { SearchInputProps } from './SearchInput'