polpo 0.1.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 (332) hide show
  1. package/.eslintrc.cjs +9 -0
  2. package/.storybook/decorators.tsx +61 -0
  3. package/.storybook/main.ts +47 -0
  4. package/.storybook/manager-head.html +2 -0
  5. package/.storybook/manager.ts +7 -0
  6. package/.storybook/preview-head.html +2 -0
  7. package/.storybook/preview.ts +38 -0
  8. package/.storybook/theme.ts +47 -0
  9. package/.turbo/daemon/f5c5c8fb195b01d0-turbo.log.2024-05-26 +0 -0
  10. package/.turbo/turbo-build$colon$watch.log +96 -0
  11. package/.turbo/turbo-build-storybook.log +0 -0
  12. package/.turbo/turbo-build.log +77 -0
  13. package/.turbo/turbo-lint$colon$fix.log +2 -0
  14. package/.turbo/turbo-lint.log +4 -0
  15. package/README.md +68 -0
  16. package/dist/chunk-M4KRSYE7.js +3 -0
  17. package/dist/chunk-M4KRSYE7.js.map +1 -0
  18. package/dist/chunk-U5XSMSKZ.js +3 -0
  19. package/dist/chunk-U5XSMSKZ.js.map +1 -0
  20. package/dist/get-modal-position-DPftPoU2.d.cts +28 -0
  21. package/dist/get-modal-position-DPftPoU2.d.ts +28 -0
  22. package/dist/helpers.cjs +3 -0
  23. package/dist/helpers.cjs.map +1 -0
  24. package/dist/helpers.d.cts +15 -0
  25. package/dist/helpers.d.ts +15 -0
  26. package/dist/helpers.js +3 -0
  27. package/dist/helpers.js.map +1 -0
  28. package/dist/hooks.cjs +3 -0
  29. package/dist/hooks.cjs.map +1 -0
  30. package/dist/hooks.d.cts +121 -0
  31. package/dist/hooks.d.ts +121 -0
  32. package/dist/hooks.js +3 -0
  33. package/dist/hooks.js.map +1 -0
  34. package/dist/ui.cjs +1987 -0
  35. package/dist/ui.cjs.map +1 -0
  36. package/dist/ui.d.cts +3932 -0
  37. package/dist/ui.d.ts +3932 -0
  38. package/dist/ui.js +1987 -0
  39. package/dist/ui.js.map +1 -0
  40. package/package.json +98 -0
  41. package/src/components/accordion/accordion-item.stories.tsx +128 -0
  42. package/src/components/accordion/accordion-item.tsx +119 -0
  43. package/src/components/accordion/accordion.stories.tsx +74 -0
  44. package/src/components/accordion/accordion.style.ts +42 -0
  45. package/src/components/accordion/accordion.tsx +56 -0
  46. package/src/components/accordion/index.ts +2 -0
  47. package/src/components/buttons/button/button.stories.tsx +103 -0
  48. package/src/components/buttons/button/button.style.ts +147 -0
  49. package/src/components/buttons/button/button.tsx +119 -0
  50. package/src/components/buttons/button/index.ts +1 -0
  51. package/src/components/buttons/index.ts +1 -0
  52. package/src/components/cards/flip-card/flip-card.stories.tsx +61 -0
  53. package/src/components/cards/flip-card/flip-card.style.ts +45 -0
  54. package/src/components/cards/flip-card/flip-card.tsx +55 -0
  55. package/src/components/cards/flip-card/index.ts +1 -0
  56. package/src/components/cards/hover-card/hover-card.stories.tsx +45 -0
  57. package/src/components/cards/hover-card/hover-card.style.ts +13 -0
  58. package/src/components/cards/hover-card/hover-card.tsx +71 -0
  59. package/src/components/cards/hover-card/index.ts +1 -0
  60. package/src/components/cards/index.ts +3 -0
  61. package/src/components/cards/slide-card/index.ts +1 -0
  62. package/src/components/cards/slide-card/slide-card.stories.tsx +47 -0
  63. package/src/components/cards/slide-card/slide-card.tsx +42 -0
  64. package/src/components/form/checkbox/checkbox.stories.tsx +34 -0
  65. package/src/components/form/checkbox/checkbox.style.ts +75 -0
  66. package/src/components/form/checkbox/checkbox.tsx +76 -0
  67. package/src/components/form/checkbox/index.ts +1 -0
  68. package/src/components/form/controller/controller.tsx +42 -0
  69. package/src/components/form/controller/index.ts +1 -0
  70. package/src/components/form/date-picker/date-picker.stories.tsx +38 -0
  71. package/src/components/form/date-picker/date-picker.tsx +67 -0
  72. package/src/components/form/date-picker/index.ts +1 -0
  73. package/src/components/form/field/field.stories.tsx +49 -0
  74. package/src/components/form/field/field.style.ts +79 -0
  75. package/src/components/form/field/field.tsx +83 -0
  76. package/src/components/form/field/field.types.ts +28 -0
  77. package/src/components/form/field/index.ts +2 -0
  78. package/src/components/form/form.stories.types.tsx +50 -0
  79. package/src/components/form/form.types.ts +37 -0
  80. package/src/components/form/index.ts +14 -0
  81. package/src/components/form/input/index.ts +1 -0
  82. package/src/components/form/input/input.stories.tsx +41 -0
  83. package/src/components/form/input/input.tsx +73 -0
  84. package/src/components/form/input-color/index.ts +1 -0
  85. package/src/components/form/input-color/input-color.stories.tsx +46 -0
  86. package/src/components/form/input-color/input-color.style.ts +93 -0
  87. package/src/components/form/input-color/input-color.tsx +159 -0
  88. package/src/components/form/input-file/index.ts +1 -0
  89. package/src/components/form/input-file/input-file.stories.tsx +40 -0
  90. package/src/components/form/input-file/input-file.style.ts +143 -0
  91. package/src/components/form/input-file/input-file.tsx +214 -0
  92. package/src/components/form/input-password/index.ts +1 -0
  93. package/src/components/form/input-password/input-password.stories.tsx +37 -0
  94. package/src/components/form/input-password/input-password.tsx +83 -0
  95. package/src/components/form/radio/index.ts +1 -0
  96. package/src/components/form/radio/radio.stories.tsx +43 -0
  97. package/src/components/form/radio/radio.style.ts +58 -0
  98. package/src/components/form/radio/radio.tsx +76 -0
  99. package/src/components/form/select/index.ts +1 -0
  100. package/src/components/form/select/options.tsx +140 -0
  101. package/src/components/form/select/select-option.tsx +84 -0
  102. package/src/components/form/select/select.stories.tsx +89 -0
  103. package/src/components/form/select/select.style.ts +164 -0
  104. package/src/components/form/select/select.tsx +327 -0
  105. package/src/components/form/select/select.types.ts +93 -0
  106. package/src/components/form/slider/index.ts +1 -0
  107. package/src/components/form/slider/slider.stories.tsx +40 -0
  108. package/src/components/form/slider/slider.style.ts +90 -0
  109. package/src/components/form/slider/slider.tsx +108 -0
  110. package/src/components/form/switch/index.ts +1 -0
  111. package/src/components/form/switch/switch.stories.tsx +38 -0
  112. package/src/components/form/switch/switch.style.ts +120 -0
  113. package/src/components/form/switch/switch.tsx +111 -0
  114. package/src/components/form/textarea/index.ts +1 -0
  115. package/src/components/form/textarea/textarea.stories.tsx +43 -0
  116. package/src/components/form/textarea/textarea.style.ts +7 -0
  117. package/src/components/form/textarea/textarea.tsx +76 -0
  118. package/src/components/icon/icon.stories.tsx +63 -0
  119. package/src/components/icon/icon.tsx +64 -0
  120. package/src/components/icon/icons/index.ts +18 -0
  121. package/src/components/icon/icons/object.tsx +482 -0
  122. package/src/components/icon/icons/social.tsx +72 -0
  123. package/src/components/icon/icons/symbol.tsx +776 -0
  124. package/src/components/icon/index.ts +5 -0
  125. package/src/components/image/image.stories.tsx +25 -0
  126. package/src/components/image/image.tsx +7 -0
  127. package/src/components/image/index.ts +1 -0
  128. package/src/components/index.ts +16 -0
  129. package/src/components/infinity-scroll/index.ts +1 -0
  130. package/src/components/infinity-scroll/infinity-scroll.stories.tsx +75 -0
  131. package/src/components/infinity-scroll/infinity-scroll.style.ts +30 -0
  132. package/src/components/infinity-scroll/infinity-scroll.tsx +56 -0
  133. package/src/components/line/index.ts +1 -0
  134. package/src/components/line/line.stories.tsx +67 -0
  135. package/src/components/line/line.style.ts +57 -0
  136. package/src/components/line/line.tsx +76 -0
  137. package/src/components/loaders/index.ts +1 -0
  138. package/src/components/loaders/simple-loader/index.ts +1 -0
  139. package/src/components/loaders/simple-loader/simple-loader.stories.tsx +21 -0
  140. package/src/components/loaders/simple-loader/simple-loader.style.ts +13 -0
  141. package/src/components/loaders/simple-loader/simple-loader.tsx +15 -0
  142. package/src/components/modals/action-modal/action-modal.stories.tsx +134 -0
  143. package/src/components/modals/action-modal/action-modal.style.ts +129 -0
  144. package/src/components/modals/action-modal/action-modal.tsx +150 -0
  145. package/src/components/modals/action-modal/index.ts +1 -0
  146. package/src/components/modals/aside-modal/aside-modal.stories.tsx +82 -0
  147. package/src/components/modals/aside-modal/aside-modal.style.ts +108 -0
  148. package/src/components/modals/aside-modal/aside-modal.tsx +66 -0
  149. package/src/components/modals/aside-modal/index.ts +1 -0
  150. package/src/components/modals/confirmation-modal/confirmation-modal.stories.tsx +50 -0
  151. package/src/components/modals/confirmation-modal/confirmation-modal.style.ts +17 -0
  152. package/src/components/modals/confirmation-modal/confirmation-modal.tsx +43 -0
  153. package/src/components/modals/confirmation-modal/index.ts +1 -0
  154. package/src/components/modals/index.ts +4 -0
  155. package/src/components/modals/modal/index.ts +1 -0
  156. package/src/components/modals/modal/modal.style.ts +10 -0
  157. package/src/components/modals/modal/modal.tsx +132 -0
  158. package/src/components/ripple/index.ts +1 -0
  159. package/src/components/ripple/ripple.stories.tsx +38 -0
  160. package/src/components/ripple/ripple.style.ts +33 -0
  161. package/src/components/ripple/ripple.tsx +63 -0
  162. package/src/components/smart-table/index.ts +1 -0
  163. package/src/components/smart-table/smart-table.column.tsx +63 -0
  164. package/src/components/smart-table/smart-table.helpers.tsx +59 -0
  165. package/src/components/smart-table/smart-table.hooks.ts +27 -0
  166. package/src/components/smart-table/smart-table.row.tsx +29 -0
  167. package/src/components/smart-table/smart-table.stories.tsx +301 -0
  168. package/src/components/smart-table/smart-table.style.ts +102 -0
  169. package/src/components/smart-table/smart-table.tsx +112 -0
  170. package/src/components/smart-table/smart-table.types.ts +41 -0
  171. package/src/components/tabs/index.ts +1 -0
  172. package/src/components/tabs/tabs-container.stories.tsx +159 -0
  173. package/src/components/tabs/tabs-list.tsx +131 -0
  174. package/src/components/tabs/tabs.stories.tsx +68 -0
  175. package/src/components/tabs/tabs.style.ts +132 -0
  176. package/src/components/tabs/tabs.tsx +117 -0
  177. package/src/components/tag/index.ts +1 -0
  178. package/src/components/tag/tag.stories.tsx +49 -0
  179. package/src/components/tag/tag.style.ts +24 -0
  180. package/src/components/tag/tag.tsx +44 -0
  181. package/src/components/tooltips/click-to-copy/click-to-copy.stories.tsx +39 -0
  182. package/src/components/tooltips/click-to-copy/click-to-copy.tsx +41 -0
  183. package/src/components/tooltips/click-to-copy/index.ts +1 -0
  184. package/src/components/tooltips/index.ts +2 -0
  185. package/src/components/tooltips/tooltip/index.ts +1 -0
  186. package/src/components/tooltips/tooltip/tooltip.stories.tsx +35 -0
  187. package/src/components/tooltips/tooltip/tooltip.style.ts +62 -0
  188. package/src/components/tooltips/tooltip/tooltip.tsx +45 -0
  189. package/src/components/typography/index.ts +2 -0
  190. package/src/components/typography/typography.constants.ts +43 -0
  191. package/src/components/typography/typography.stories.tsx +106 -0
  192. package/src/components/typography/typography.style.ts +76 -0
  193. package/src/components/typography/typography.tsx +79 -0
  194. package/src/contexts/fetch-context/fetch-context.tsx +114 -0
  195. package/src/contexts/fetch-context/index.ts +1 -0
  196. package/src/contexts/form-context/form-context.tsx +43 -0
  197. package/src/contexts/form-context/index.ts +1 -0
  198. package/src/contexts/index.ts +3 -0
  199. package/src/contexts/theme-context/index.ts +3 -0
  200. package/src/contexts/theme-context/theme-context.tsx +158 -0
  201. package/src/contexts/theme-context/theme.animations.ts +180 -0
  202. package/src/contexts/theme-context/theme.defaults.ts +205 -0
  203. package/src/contexts/theme-context/theme.style.ts +78 -0
  204. package/src/contexts/theme-context/themes.ts +96 -0
  205. package/src/core/http-client.d.ts +11 -0
  206. package/src/core/http-client.d.ts.map +1 -0
  207. package/src/core/http-client.ts +47 -0
  208. package/src/core/index.d.ts +2 -0
  209. package/src/core/index.d.ts.map +1 -0
  210. package/src/core/index.ts +1 -0
  211. package/src/core/variants/color.ts +36 -0
  212. package/src/core/variants/index.ts +3 -0
  213. package/src/core/variants/radius.ts +56 -0
  214. package/src/core/variants/size.ts +44 -0
  215. package/src/helpers/format-bytes.ts +11 -0
  216. package/src/helpers/format-dates.ts +11 -0
  217. package/src/helpers/get-modal-position.ts +66 -0
  218. package/src/helpers/index.ts +4 -0
  219. package/src/helpers/text/index.ts +1 -0
  220. package/src/helpers/text/to-capitalize.ts +17 -0
  221. package/src/hooks/index.ts +23 -0
  222. package/src/hooks/use-async.ts +88 -0
  223. package/src/hooks/use-classnames.ts +13 -0
  224. package/src/hooks/use-constant.ts +3 -0
  225. package/src/hooks/use-debounce.ts +15 -0
  226. package/src/hooks/use-dimensions.ts +22 -0
  227. package/src/hooks/use-event-listener.ts +71 -0
  228. package/src/hooks/use-file-reader.ts +69 -0
  229. package/src/hooks/use-hover.ts +17 -0
  230. package/src/hooks/use-in-view.ts +20 -0
  231. package/src/hooks/use-input-handlers.ts +49 -0
  232. package/src/hooks/use-media-query.ts +25 -0
  233. package/src/hooks/use-modal-in-container.ts +94 -0
  234. package/src/hooks/use-mouse-position.ts +16 -0
  235. package/src/hooks/use-observer.ts +18 -0
  236. package/src/hooks/use-on-click-outside-ref.ts +17 -0
  237. package/src/hooks/use-online-status.ts +12 -0
  238. package/src/hooks/use-render-count.ts +11 -0
  239. package/src/hooks/use-safe-dispatch.ts +22 -0
  240. package/src/hooks/use-scroll.ts +31 -0
  241. package/src/hooks/use-state-history.ts +22 -0
  242. package/src/hooks/use-toggle-values.ts +14 -0
  243. package/src/hooks/use-toggle.ts +11 -0
  244. package/src/hooks/use-viewport.ts +38 -0
  245. package/src/index.ts +5 -0
  246. package/src/layouts/flex/flex.tsx +75 -0
  247. package/src/layouts/flex/index.ts +1 -0
  248. package/src/layouts/grid/grid.tsx +86 -0
  249. package/src/layouts/grid/index.ts +1 -0
  250. package/src/layouts/index.ts +3 -0
  251. package/src/layouts/section-layout/index.ts +1 -0
  252. package/src/layouts/section-layout/section-layout.stories.tsx +55 -0
  253. package/src/layouts/section-layout/section-layout.style.ts +21 -0
  254. package/src/layouts/section-layout/section-layout.tsx +46 -0
  255. package/src/stories/GettingStarted.mdx +66 -0
  256. package/src/types/generics.ts +68 -0
  257. package/src/types/index.ts +1 -0
  258. package/svg/Name=airplane, Category=object.svg +3 -0
  259. package/svg/Name=arrow-circle, Category=symbol.svg +3 -0
  260. package/svg/Name=arrow-down, Category=symbol.svg +3 -0
  261. package/svg/Name=arrow-left, Category=symbol.svg +3 -0
  262. package/svg/Name=arrow-right, Category=symbol.svg +3 -0
  263. package/svg/Name=arrow-up, Category=symbol.svg +3 -0
  264. package/svg/Name=bell, Category=object.svg +3 -0
  265. package/svg/Name=bicycle, Category=object.svg +3 -0
  266. package/svg/Name=book-open, Category=object.svg +3 -0
  267. package/svg/Name=book-solid, Category=object.svg +6 -0
  268. package/svg/Name=box-shadow, Category=symbol.svg +5 -0
  269. package/svg/Name=calendar, Category=object.svg +3 -0
  270. package/svg/Name=camera, Category=object.svg +3 -0
  271. package/svg/Name=caret-down, Category=symbol.svg +3 -0
  272. package/svg/Name=caret-left, Category=symbol.svg +3 -0
  273. package/svg/Name=caret-right, Category=symbol.svg +3 -0
  274. package/svg/Name=caret-up, Category=symbol.svg +3 -0
  275. package/svg/Name=checkmark, Category=symbol.svg +3 -0
  276. package/svg/Name=clean-computer, Category=object.svg +10 -0
  277. package/svg/Name=codepen, Category=social.svg +3 -0
  278. package/svg/Name=creative, Category=symbol.svg +5 -0
  279. package/svg/Name=cross, Category=symbol.svg +4 -0
  280. package/svg/Name=crossed-flags, Category=object.svg +4 -0
  281. package/svg/Name=cv, Category=symbol.svg +5 -0
  282. package/svg/Name=design-ui, Category=symbol.svg +9 -0
  283. package/svg/Name=document, Category=object.svg +3 -0
  284. package/svg/Name=door-closed, Category=object.svg +3 -0
  285. package/svg/Name=door-open, Category=object.svg +3 -0
  286. package/svg/Name=double-caret-down, Category=symbol.svg +4 -0
  287. package/svg/Name=double-caret-left, Category=symbol.svg +4 -0
  288. package/svg/Name=double-caret-righ, Category=symbol.svg +4 -0
  289. package/svg/Name=double-caret-up, Category=symbol.svg +4 -0
  290. package/svg/Name=download, Category=symbol.svg +5 -0
  291. package/svg/Name=dropper, Category=object.svg +5 -0
  292. package/svg/Name=envelope, Category=object.svg +3 -0
  293. package/svg/Name=exclamation-close, Category=symbol.svg +4 -0
  294. package/svg/Name=exclamation-open, Category=symbol.svg +4 -0
  295. package/svg/Name=external-link, Category=symbol.svg +3 -0
  296. package/svg/Name=eye, Category=object.svg +4 -0
  297. package/svg/Name=eye-hidden, Category=object.svg +7 -0
  298. package/svg/Name=facebook, Category=social.svg +3 -0
  299. package/svg/Name=form, Category=symbol.svg +8 -0
  300. package/svg/Name=game-control, Category=object.svg +3 -0
  301. package/svg/Name=gear, Category=object.svg +3 -0
  302. package/svg/Name=github, Category=social.svg +3 -0
  303. package/svg/Name=house, Category=object.svg +3 -0
  304. package/svg/Name=hyphen, Category=symbol.svg +3 -0
  305. package/svg/Name=info, Category=symbol.svg +3 -0
  306. package/svg/Name=instagram, Category=social.svg +3 -0
  307. package/svg/Name=link, Category=symbol.svg +5 -0
  308. package/svg/Name=linkedin, Category=social.svg +3 -0
  309. package/svg/Name=magnifying-glass, Category=object.svg +3 -0
  310. package/svg/Name=message, Category=symbol.svg +4 -0
  311. package/svg/Name=moon, Category=object.svg +5 -0
  312. package/svg/Name=order-list, Category=symbol.svg +7 -0
  313. package/svg/Name=pencil, Category=object.svg +3 -0
  314. package/svg/Name=pin-location, Category=symbol.svg +3 -0
  315. package/svg/Name=question-mark-close, Category=symbol.svg +3 -0
  316. package/svg/Name=question-mark-open, Category=symbol.svg +3 -0
  317. package/svg/Name=share, Category=symbol.svg +5 -0
  318. package/svg/Name=spinner, Category=symbol.svg +9 -0
  319. package/svg/Name=star, Category=object.svg +3 -0
  320. package/svg/Name=star-empty, Category=object.svg +3 -0
  321. package/svg/Name=sun, Category=object.svg +11 -0
  322. package/svg/Name=text-shadow, Category=symbol.svg +4 -0
  323. package/svg/Name=thinking, Category=symbol.svg +3 -0
  324. package/svg/Name=trash-can, Category=object.svg +4 -0
  325. package/svg/Name=upload, Category=symbol.svg +5 -0
  326. package/svg/Name=user, Category=symbol.svg +4 -0
  327. package/svg/Name=warning, Category=symbol.svg +5 -0
  328. package/svg/Name=whatsapp, Category=social.svg +3 -0
  329. package/svgconfig.json +4 -0
  330. package/tsconfig.json +14 -0
  331. package/tsup.config.cjs +21 -0
  332. package/vite.config.ts +13 -0
@@ -0,0 +1,84 @@
1
+ import React, { useCallback } from 'react';
2
+
3
+ import { Icon } from '../../icon';
4
+ import { Checkbox } from '../checkbox';
5
+
6
+ import { OptionStyle } from './select.style';
7
+ import { SelectItem, SelectOptionProps, SelectOptionVariant } from './select.types';
8
+
9
+ import { useClassNames } from '@polpo/hooks';
10
+
11
+ export const SelectOption = <T extends SelectItem>({
12
+ id,
13
+ selected,
14
+ selectOption,
15
+ unselectOption,
16
+ data,
17
+ Component,
18
+ multiselect,
19
+ variant,
20
+ }: SelectOptionProps<T>) => {
21
+ const optionClassName = useClassNames({
22
+ 'selected-option': selected,
23
+ 'multiselect-icon': multiselect && variant === SelectOptionVariant.ICON,
24
+ 'multiselect-checkbox': multiselect && variant === SelectOptionVariant.CHECKBOX,
25
+ });
26
+
27
+ const handleKeyDown = useCallback(
28
+ (option: T) => (e: React.KeyboardEvent<HTMLLIElement>) => {
29
+ if (['Enter', ' '].includes(e.key)) {
30
+ e.preventDefault();
31
+
32
+ if (selected && multiselect) {
33
+ unselectOption(option);
34
+ } else {
35
+ selectOption(option);
36
+ }
37
+ }
38
+ },
39
+ [multiselect, selectOption, selected, unselectOption],
40
+ );
41
+
42
+ const handleCheckboxChange = useCallback(
43
+ (option: T) => (checked: boolean | '') => {
44
+ if (checked !== '' && checked) selectOption(option);
45
+ else unselectOption(option);
46
+ },
47
+ [selectOption, unselectOption],
48
+ );
49
+
50
+ const handleClick = useCallback(
51
+ (option: T) => (e: React.MouseEvent) => {
52
+ e.stopPropagation();
53
+
54
+ if (multiselect) {
55
+ if (selected) unselectOption(option);
56
+ else selectOption(option);
57
+ } else {
58
+ selectOption(option);
59
+ }
60
+ },
61
+ [multiselect, selected, selectOption, unselectOption],
62
+ );
63
+
64
+ return (
65
+ <OptionStyle
66
+ id={id}
67
+ role='option'
68
+ aria-selected={selected}
69
+ tabIndex={-1}
70
+ onKeyDown={handleKeyDown(data)}
71
+ className={optionClassName}
72
+ onClick={handleClick(data)}
73
+ >
74
+ {multiselect && variant === SelectOptionVariant.CHECKBOX && (
75
+ <Checkbox name={id} value={selected} setValue={handleCheckboxChange(data)} />
76
+ )}
77
+ <section className='option-content'>
78
+ <Component data={data} isSelected={selected} multiselect={multiselect} />
79
+ </section>
80
+ {multiselect && selected && variant === SelectOptionVariant.ICON && <Icon name='checkmark' />}
81
+ {multiselect && !selected && variant === SelectOptionVariant.ICON && <span />}
82
+ </OptionStyle>
83
+ );
84
+ };
@@ -0,0 +1,89 @@
1
+ import { useState } from 'react';
2
+
3
+ import { FieldSharedArgs, FieldSharedArgTypes } from '../field/field.stories';
4
+ import { ContainerDecorator, UnControlledComponentArgTypes } from '../form.stories.types';
5
+
6
+ import { Select } from './select';
7
+
8
+ import type { Meta, StoryObj } from '@storybook/react';
9
+
10
+ const meta: Meta<typeof Select> = {
11
+ title: 'Form/Select',
12
+ component: Select,
13
+ argTypes: {
14
+ ...FieldSharedArgTypes,
15
+ ...UnControlledComponentArgTypes,
16
+ options: { control: false },
17
+ renderOption: { control: false },
18
+ isEqualComparator: { control: false },
19
+ searchQueryValue: { control: 'text' },
20
+ searchQueryPlaceholder: { control: 'text' },
21
+ onSearchQuery: { control: false },
22
+ loadMore: { control: false },
23
+ isLoading: { control: 'boolean' },
24
+ hasNextPage: { control: 'boolean' },
25
+ maxOptions: { control: { type: 'number' } },
26
+ showClearOption: { control: 'boolean' },
27
+ multiselect: { control: false },
28
+ optionVariant: {
29
+ control: 'select',
30
+ options: [undefined, 'checkbox', 'icon', 'default'],
31
+ if: { arg: 'multiselect' },
32
+ },
33
+ },
34
+ args: {
35
+ label: 'Select',
36
+ searchQueryPlaceholder: 'Search',
37
+ ...FieldSharedArgs,
38
+ optionVariant: undefined,
39
+ },
40
+ decorators: [ContainerDecorator],
41
+ render: args => {
42
+ const [value, setValue] = useState<string | null>(null);
43
+
44
+ return (
45
+ <Select
46
+ {...args}
47
+ options={[
48
+ 'A',
49
+ 'B',
50
+ 'C',
51
+ 'D',
52
+ 'E',
53
+ 'F',
54
+ 'H',
55
+ 'I',
56
+ 'J',
57
+ 'K',
58
+ 'L',
59
+ 'M',
60
+ 'N',
61
+ 'O',
62
+ 'P',
63
+ 'Q',
64
+ 'R',
65
+ 'S',
66
+ 'T',
67
+ 'U',
68
+ 'V',
69
+ 'W',
70
+ 'X',
71
+ 'Y',
72
+ 'Z',
73
+ ]}
74
+ value={value}
75
+ renderOption={v => v}
76
+ optionVariant={undefined}
77
+ multiselect={false}
78
+ setValue={value => setValue(value)}
79
+ />
80
+ );
81
+ },
82
+ };
83
+
84
+ export default meta;
85
+ type Story = StoryObj<typeof Select>;
86
+
87
+ export const Default: Story = {
88
+ args: {},
89
+ };
@@ -0,0 +1,164 @@
1
+ import styled from 'styled-components';
2
+
3
+ export const SelectStyle = styled.section`
4
+ cursor: pointer;
5
+
6
+ .icon-close {
7
+ display: grid;
8
+ place-content: center;
9
+ align-self: center;
10
+ border-radius: 100%;
11
+ cursor: pointer;
12
+ font-size: 0.6em;
13
+ padding: 0.5em;
14
+
15
+ &:hover {
16
+ background: ${props => props.theme.colors.text.main}55;
17
+ }
18
+ }
19
+
20
+ .select-container {
21
+ display: grid;
22
+ grid-template-columns: 1fr auto;
23
+ gap: 0.5em;
24
+ align-items: center;
25
+ grid-auto-rows: auto;
26
+ position: relative;
27
+ cursor: pointer;
28
+
29
+ &.three-columns {
30
+ grid-template-columns: 1fr auto auto;
31
+ }
32
+ }
33
+
34
+ .input-button {
35
+ appearance: none;
36
+ -webkit-appearance: none;
37
+ -moz-appearance: none;
38
+ white-space: nowrap;
39
+ text-overflow: ellipsis;
40
+ overflow: hidden;
41
+ display: grid;
42
+ min-width: 0;
43
+ width: 100%;
44
+ border: 0;
45
+ margin: 0;
46
+ border-radius: 0;
47
+ background: transparent;
48
+ color: currentColor;
49
+ text-align: left;
50
+ -webkit-tap-highlight-color: transparent;
51
+ padding: 0;
52
+ grid-template-columns: 1fr auto;
53
+
54
+ &.placeholder {
55
+ color: #919191;
56
+ }
57
+ }
58
+ `;
59
+
60
+ export const OptionsStyle = styled.section`
61
+ position: fixed;
62
+ border-radius: 4px;
63
+ border: 1px solid ${props => props.theme.colors.primary.main};
64
+ box-shadow: 0 3px 7px 0 rgba(145, 145, 145, 0.13);
65
+ overflow: auto;
66
+ background: ${props => props.theme.colors.background.main};
67
+ color: ${props => props.theme.colors.text.main};
68
+ display: grid;
69
+ gap: 5px;
70
+ align-content: start;
71
+ outline: 0;
72
+ top: 50%;
73
+ left: 50%;
74
+ transform: translate(-50%, -50%);
75
+ width: 80vw;
76
+ max-height: 80dvh;
77
+ min-width: fit-content;
78
+
79
+ .options-list-container {
80
+ height: 100%;
81
+ overflow: auto;
82
+ outline: 0;
83
+ }
84
+
85
+ .options-list {
86
+ list-style: none;
87
+ display: grid;
88
+ margin: 0;
89
+ padding: 0;
90
+ outline: 0;
91
+ }
92
+
93
+ .options-selected {
94
+ border-bottom: 1px solid #9e9e9e;
95
+ padding-bottom: 5px;
96
+ }
97
+
98
+ .option-empty {
99
+ color: #919191;
100
+ }
101
+ `;
102
+
103
+ export const OptionStyle = styled.li`
104
+ padding: 0.5em 1em;
105
+ font-size: 0.8em;
106
+ white-space: nowrap;
107
+ text-overflow: ellipsis;
108
+ overflow: hidden;
109
+ display: grid;
110
+ grid-template-columns: 1fr;
111
+ gap: 1em;
112
+ align-items: center;
113
+ cursor: pointer;
114
+ outline: 0;
115
+
116
+ &.multiselect-icon {
117
+ grid-template-columns: 1fr auto;
118
+ }
119
+
120
+ &.multiselect-checkbox {
121
+ grid-template-columns: auto 1fr;
122
+ }
123
+
124
+ .option-content {
125
+ white-space: nowrap;
126
+ text-overflow: ellipsis;
127
+ overflow: hidden;
128
+ }
129
+
130
+ &:hover {
131
+ background: ${props => props.theme.colors.text.main}11;
132
+ }
133
+
134
+ &:focus {
135
+ background: ${props => props.theme.colors.text.main}22;
136
+ }
137
+
138
+ &.selected-option,
139
+ &[aria-selected='true'] {
140
+ background: ${props => props.theme.colors.primary.main}33;
141
+
142
+ &:hover {
143
+ background: ${props => props.theme.colors.primary.main}22;
144
+ }
145
+
146
+ &:focus {
147
+ background: ${props => props.theme.colors.primary.main}11;
148
+ }
149
+ }
150
+ `;
151
+
152
+ export const OptionsHeaderStyle = styled.section`
153
+ display: grid;
154
+ gap: 5px;
155
+ padding-bottom: 5px;
156
+
157
+ .input-search {
158
+ padding: 8px 6px;
159
+ border: 1px solid #b4b4b4;
160
+ font-size: 0.8em;
161
+ width: 100%;
162
+ border-radius: 4px;
163
+ }
164
+ `;
@@ -0,0 +1,327 @@
1
+ import React, { useCallback, useMemo } from 'react';
2
+ import { useTheme } from 'styled-components';
3
+
4
+ import { Icon } from '../../icon';
5
+ import { Typography } from '../../typography';
6
+ import { Controller } from '../controller';
7
+ import { Field } from '../field';
8
+ import { ControllerGeneratorProps } from '../form.types';
9
+
10
+ import { Options } from './options';
11
+ import { SelectStyle } from './select.style';
12
+ import {
13
+ ControllerGeneratorSelectProps,
14
+ MultiSelectProps,
15
+ MultiValue,
16
+ OptionComponentProps,
17
+ SelectItem,
18
+ SelectOptionVariant,
19
+ SelectValue,
20
+ SingleSelectProps,
21
+ SingleValue,
22
+ UnControlledSelectProps,
23
+ } from './select.types';
24
+
25
+ import { useMediaQuery, useModalInContainer } from '@polpo/hooks';
26
+
27
+ /*
28
+ *type SelectContextState<T extends SelectItem = unknown> = {
29
+ *value: T;
30
+ *};
31
+ *
32
+ *const SelectContext = createContext<SelectContextState | null>(null);
33
+ */
34
+
35
+ export const Select = <T extends SelectItem>({
36
+ // Select props
37
+ options,
38
+ renderOption,
39
+ isEqualComparator,
40
+ searchQueryValue,
41
+ searchQueryPlaceholder,
42
+ onSearchQuery,
43
+ loadMore,
44
+ isLoading,
45
+ hasNextPage,
46
+ multiselect,
47
+ maxOptions,
48
+ optionVariant = SelectOptionVariant.DEFAULT,
49
+ // Shared props
50
+ name,
51
+ value,
52
+ setValue,
53
+ onBlur,
54
+ onFocus,
55
+ className = '',
56
+ style = {},
57
+ showClearOption = false,
58
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
59
+ autoFocus = false,
60
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
61
+ readOnly = false,
62
+ disabled = false,
63
+ placeholder = '',
64
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
+ autoComplete = 'off',
66
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
67
+ isDirty = false,
68
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
69
+ isTouched = false,
70
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
71
+ invalid = false,
72
+ error,
73
+ // Field props
74
+ ...fieldProps
75
+ }: UnControlledSelectProps<T>) => {
76
+ const id = useMemo(() => crypto.randomUUID(), []);
77
+ const theme = useTheme();
78
+ const isMobile = useMediaQuery(`(min-width: ${theme.constants.breakpoints.mobileL})`);
79
+ const { modalRef, isVisible, setIsVisible, modalStyle, containerRef } = useModalInContainer({
80
+ position: 'bottom',
81
+ distancePercentage: 50,
82
+ offset: 5,
83
+ });
84
+
85
+ const openSelect = useCallback(
86
+ (open: boolean) => {
87
+ setIsVisible(open && !disabled);
88
+ },
89
+ [disabled, setIsVisible],
90
+ );
91
+
92
+ const compareValuesIsEqual = useCallback(
93
+ (a: T, b: T): boolean => {
94
+ if (['number', 'string'].includes(typeof a)) {
95
+ return a === b;
96
+ }
97
+
98
+ return !!isEqualComparator && isEqualComparator(a, b);
99
+ },
100
+ [isEqualComparator],
101
+ );
102
+
103
+ const compareValueOrValuesAreEqual = useCallback(
104
+ (a: T, b: SelectValue<T>): boolean => {
105
+ if (b === null) {
106
+ return true;
107
+ }
108
+
109
+ if (!Array.isArray(b)) {
110
+ return compareValuesIsEqual(a, b);
111
+ }
112
+
113
+ if (['number', 'string'].includes(typeof a)) {
114
+ return b.includes(a);
115
+ }
116
+
117
+ return b.some(item => !!isEqualComparator && isEqualComparator(a, item));
118
+ },
119
+ [compareValuesIsEqual, isEqualComparator],
120
+ );
121
+
122
+ const OptionComponent = useCallback(
123
+ ({ data }: OptionComponentProps<T>) => (
124
+ <Typography noPadding variant='label' nowrap>
125
+ {renderOption(data)}
126
+ </Typography>
127
+ ),
128
+ [renderOption],
129
+ );
130
+
131
+ const renderMultipleValue = useCallback(
132
+ (values: Array<T>): React.ReactNode => {
133
+ if (maxOptions) {
134
+ const limit = Math.min(maxOptions, options.length);
135
+
136
+ return `${values.length}/${limit} item${values.length === 1 ? '' : 's'} selected`;
137
+ }
138
+
139
+ return `${values.length} item${values.length === 1 ? '' : 's'} selected`;
140
+ },
141
+ [maxOptions, options.length],
142
+ );
143
+
144
+ const renderValue = useCallback(
145
+ (value: SelectValue<T>): React.ReactNode => {
146
+ if (value === null) {
147
+ return null;
148
+ }
149
+
150
+ if (Array.isArray(value)) {
151
+ return (
152
+ <Typography noPadding nowrap variant='label'>
153
+ {renderMultipleValue(value)}
154
+ </Typography>
155
+ );
156
+ }
157
+
158
+ return <OptionComponent data={value} isSelected={false} multiselect={!!multiselect} />;
159
+ },
160
+ [OptionComponent, multiselect, renderMultipleValue],
161
+ );
162
+
163
+ const unSelectOption = useCallback(
164
+ (unselectedOption: T) => {
165
+ if (multiselect) {
166
+ const filteredValues = value.filter(item => !compareValuesIsEqual(item, unselectedOption));
167
+ setValue(filteredValues.length === 0 ? [] : filteredValues);
168
+ } else {
169
+ setValue(null);
170
+ setIsVisible(false);
171
+ }
172
+ },
173
+ [compareValuesIsEqual, multiselect, setIsVisible, setValue, value],
174
+ );
175
+
176
+ const selectOption = useCallback(
177
+ (selectedOption: T) => {
178
+ if (multiselect) {
179
+ if (maxOptions && Array.isArray(value) && value.length >= maxOptions) {
180
+ return;
181
+ }
182
+
183
+ setValue([...(value as Array<T>), selectedOption] as MultiValue<T>);
184
+ } else {
185
+ setValue(selectedOption as SingleValue<T>);
186
+ setIsVisible(false);
187
+ }
188
+ },
189
+ [maxOptions, multiselect, setIsVisible, setValue, value],
190
+ );
191
+
192
+ const clearOption = useCallback(
193
+ (e: React.MouseEvent) => {
194
+ e.stopPropagation();
195
+
196
+ if (multiselect) {
197
+ setValue([]);
198
+ } else {
199
+ setValue(null);
200
+ }
201
+ },
202
+ [multiselect, setValue],
203
+ );
204
+
205
+ const valueNonEmpty = useMemo(() => {
206
+ if (multiselect) {
207
+ return value.length > 0;
208
+ } else {
209
+ return Boolean(value);
210
+ }
211
+ }, [multiselect, value]);
212
+
213
+ return (
214
+ <Field
215
+ id={id}
216
+ error={error}
217
+ isFocus={isVisible}
218
+ onClickLeftIcon={() => openSelect(true)}
219
+ onClickRightIcon={() => openSelect(true)}
220
+ ref={containerRef}
221
+ {...fieldProps}
222
+ >
223
+ <SelectStyle id={name} style={style} onBlur={onBlur} className={`${disabled ? 'disabled' : ''} ${className}`}>
224
+ <section
225
+ className={`select-container ${valueNonEmpty && showClearOption ? 'three-columns' : ''}`}
226
+ onClick={() => openSelect(true)}
227
+ >
228
+ <button
229
+ type='button'
230
+ className={`input-button ${(Array.isArray(value) ? value.length > 0 : value) ? '' : 'placeholder'}`}
231
+ aria-haspopup='listbox'
232
+ aria-expanded={isVisible}
233
+ onFocus={e => {
234
+ openSelect(true);
235
+ onFocus && onFocus(e);
236
+ }}
237
+ >
238
+ {(valueNonEmpty && renderValue(value)) || (
239
+ <Typography variant='label' noPadding nowrap>
240
+ {placeholder}
241
+ </Typography>
242
+ )}
243
+ </button>
244
+ {valueNonEmpty && showClearOption && (
245
+ <section className='icon-close' onClick={clearOption}>
246
+ <Icon name='cross' />
247
+ </section>
248
+ )}
249
+ <Icon name={isVisible ? 'caret-up' : 'caret-down'} />
250
+ </section>
251
+ <Options
252
+ modalRef={modalRef}
253
+ isOpen={isVisible}
254
+ value={value}
255
+ compareValueOrValuesAreEqual={compareValueOrValuesAreEqual}
256
+ Component={OptionComponent}
257
+ multiselect={multiselect}
258
+ isLoading={isLoading}
259
+ hasNextPage={hasNextPage}
260
+ loadMore={loadMore}
261
+ searchQueryValue={searchQueryValue}
262
+ onSearchQuery={onSearchQuery}
263
+ style={
264
+ (isMobile && {
265
+ width: containerRef.current?.offsetWidth,
266
+ transform: 'none',
267
+ maxHeight: '250px',
268
+ ...modalStyle,
269
+ }) ||
270
+ {}
271
+ }
272
+ searchQueryPlaceholder={searchQueryPlaceholder}
273
+ options={options}
274
+ selectOption={selectOption}
275
+ unselectOption={unSelectOption}
276
+ variant={optionVariant}
277
+ />
278
+ </SelectStyle>
279
+ </Field>
280
+ );
281
+ };
282
+
283
+ const MultiSelectController = <T extends SelectItem>({
284
+ rules,
285
+ ...props
286
+ }: ControllerGeneratorProps<MultiSelectProps<T>, MultiValue<T>>) => {
287
+ return (
288
+ <Controller<MultiSelectProps<T>, MultiValue<T>>
289
+ Component={Select}
290
+ defaultValue={[]}
291
+ inputProps={{
292
+ ...props,
293
+ multiselect: true,
294
+ }}
295
+ rules={rules}
296
+ />
297
+ );
298
+ };
299
+
300
+ const SingleSelectController = <T extends SelectItem>({
301
+ rules,
302
+ ...props
303
+ }: ControllerGeneratorProps<SingleSelectProps<T>, SingleValue<T>>) => {
304
+ return (
305
+ <Controller<SingleSelectProps<T>, SingleValue<T>>
306
+ Component={Select}
307
+ defaultValue={null}
308
+ inputProps={{
309
+ ...props,
310
+ multiselect: false,
311
+ }}
312
+ rules={rules}
313
+ />
314
+ );
315
+ };
316
+
317
+ const SelectController = <T extends SelectItem>(props: ControllerGeneratorSelectProps<T>) => {
318
+ const { multiselect } = props;
319
+
320
+ if (multiselect) {
321
+ return <MultiSelectController<T> {...props} multiselect={true} />;
322
+ }
323
+
324
+ return <SingleSelectController<T> {...props} multiselect={false} />;
325
+ };
326
+
327
+ Select.Controller = SelectController;