@chotot/clad-ui 0.16.1

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 (405) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +75 -0
  3. package/brand/ChototLogoText.d.ts +5 -0
  4. package/brand/CtC2C.d.ts +5 -0
  5. package/brand/CtProperty.d.ts +5 -0
  6. package/brand/CtVehicle.d.ts +5 -0
  7. package/brand/Tot.d.ts +5 -0
  8. package/brand/index.d.ts +5 -0
  9. package/components/Announcer/Announcer.d.ts +30 -0
  10. package/components/Announcer/Announcer.js +163 -0
  11. package/components/Announcer/Announcer.stories.js +388 -0
  12. package/components/Announcer/Announcer.styles.d.ts +20 -0
  13. package/components/Announcer/Announcer.styles.js +214 -0
  14. package/components/Box/Box.d.ts +79 -0
  15. package/components/Box/Box.js +314 -0
  16. package/components/Box/Box.spec.js +425 -0
  17. package/components/Box/Box.stories.js +491 -0
  18. package/components/Box/Box.styles.d.ts +5 -0
  19. package/components/Box/Box.styles.js +71 -0
  20. package/components/Breadcrumbs/Breadcrumbs.d.ts +20 -0
  21. package/components/Breadcrumbs/Breadcrumbs.js +92 -0
  22. package/components/Breadcrumbs/Breadcrumbs.stories.js +170 -0
  23. package/components/Breadcrumbs/Breadcrumbs.styles.d.ts +6 -0
  24. package/components/Breadcrumbs/Breadcrumbs.styles.js +44 -0
  25. package/components/Button/Button.d.ts +34 -0
  26. package/components/Button/Button.js +165 -0
  27. package/components/Button/Button.stories.js +370 -0
  28. package/components/Button/Button.styles.d.ts +2 -0
  29. package/components/Button/Button.styles.js +327 -0
  30. package/components/Button/ButtonLink.d.ts +29 -0
  31. package/components/Button/ButtonLink.js +89 -0
  32. package/components/Button/ButtonLink.stories.js +31 -0
  33. package/components/Button/IconButton.d.ts +28 -0
  34. package/components/Button/IconButton.js +81 -0
  35. package/components/Button/IconButton.stories.js +62 -0
  36. package/components/Checkbox/Checkbox.d.ts +30 -0
  37. package/components/Checkbox/Checkbox.js +106 -0
  38. package/components/Checkbox/Checkbox.stories.js +194 -0
  39. package/components/Checkbox/Checkbox.styles.d.ts +4 -0
  40. package/components/Checkbox/Checkbox.styles.js +112 -0
  41. package/components/Chip/Chip.d.ts +26 -0
  42. package/components/Chip/Chip.js +84 -0
  43. package/components/Chip/Chip.stories.js +130 -0
  44. package/components/Chip/Chip.styles.d.ts +5 -0
  45. package/components/Chip/Chip.styles.js +41 -0
  46. package/components/CurrencyInput/CurrencyInput.d.ts +53 -0
  47. package/components/CurrencyInput/CurrencyInput.js +369 -0
  48. package/components/CurrencyInput/CurrencyInput.spec.js +151 -0
  49. package/components/CurrencyInput/CurrencyInput.stories.js +202 -0
  50. package/components/CurrencyInput/CurrencyInput.styles.d.ts +18 -0
  51. package/components/CurrencyInput/CurrencyInput.styles.js +169 -0
  52. package/components/CurrencyInput/utils/_cleanValue.d.ts +11 -0
  53. package/components/CurrencyInput/utils/_cleanValue.js +55 -0
  54. package/components/CurrencyInput/utils/_cleanValue.spec.js +20 -0
  55. package/components/CurrencyInput/utils/_formatValue.d.ts +5 -0
  56. package/components/CurrencyInput/utils/_formatValue.js +99 -0
  57. package/components/CurrencyInput/utils/_formatValue.spec.js +32 -0
  58. package/components/Dropdown/Dropdown.d.ts +47 -0
  59. package/components/Dropdown/Dropdown.js +214 -0
  60. package/components/Dropdown/Dropdown.stories.js +323 -0
  61. package/components/Grid/Col.d.ts +30 -0
  62. package/components/Grid/Col.js +93 -0
  63. package/components/Grid/Col.spec.js +249 -0
  64. package/components/Grid/Grid.d.ts +36 -0
  65. package/components/Grid/Grid.js +180 -0
  66. package/components/Grid/Grid.spec.js +524 -0
  67. package/components/Grid/Grid.styles.d.ts +12 -0
  68. package/components/Grid/Grid.styles.js +228 -0
  69. package/components/Grid/aGrid.stories.js +577 -0
  70. package/components/Grid/bCol.stories.js +217 -0
  71. package/components/Input/Input.d.ts +50 -0
  72. package/components/Input/Input.js +342 -0
  73. package/components/Input/Input.spec.js +177 -0
  74. package/components/Input/Input.stories.js +424 -0
  75. package/components/Input/Input.styles.d.ts +20 -0
  76. package/components/Input/Input.styles.js +236 -0
  77. package/components/RadioButton/RadioButton.d.ts +30 -0
  78. package/components/RadioButton/RadioButton.js +107 -0
  79. package/components/RadioButton/RadioButton.stories.js +208 -0
  80. package/components/ShapeIcon/ShapeIcon.d.ts +36 -0
  81. package/components/ShapeIcon/ShapeIcon.js +74 -0
  82. package/components/ShapeIcon/ShapeIcon.stories.js +94 -0
  83. package/components/Snack/Snack.d.ts +20 -0
  84. package/components/Snack/Snack.js +82 -0
  85. package/components/Snack/Snack.spec.js +24 -0
  86. package/components/Snack/Snack.stories.js +235 -0
  87. package/components/Snack/Snack.styles.d.ts +6 -0
  88. package/components/Snack/Snack.styles.js +69 -0
  89. package/components/Tabs/Tab.d.ts +34 -0
  90. package/components/Tabs/Tab.js +145 -0
  91. package/components/Tabs/Tabs.d.ts +24 -0
  92. package/components/Tabs/Tabs.js +107 -0
  93. package/components/Tabs/Tabs.spec.js +57 -0
  94. package/components/Tabs/Tabs.stories.js +165 -0
  95. package/components/Tabs/Tabs.styles.d.ts +18 -0
  96. package/components/Tabs/Tabs.styles.js +86 -0
  97. package/components/TextArea/TextArea.d.ts +48 -0
  98. package/components/TextArea/TextArea.js +211 -0
  99. package/components/TextArea/TextArea.stories.js +411 -0
  100. package/components/TextArea/TextArea.styles.d.ts +16 -0
  101. package/components/TextArea/TextArea.styles.js +150 -0
  102. package/components/Tooltip/Tooltip.d.ts +34 -0
  103. package/components/Tooltip/Tooltip.js +188 -0
  104. package/components/Tooltip/Tooltip.spec.js +26 -0
  105. package/components/Tooltip/Tooltip.stories.js +467 -0
  106. package/components/Tooltip/Tooltip.styles.d.ts +5 -0
  107. package/components/Tooltip/Tooltip.styles.js +57 -0
  108. package/components/components-index.spec.js +29 -0
  109. package/components/index.d.ts +20 -0
  110. package/components/index.js +20 -0
  111. package/css/__snapshots__/baseline.spec.js.snap +3 -0
  112. package/css/baseline.d.ts +14 -0
  113. package/css/baseline.js +420 -0
  114. package/css/baseline.spec.js +32 -0
  115. package/declarations.d.ts +1 -0
  116. package/docs/1.1-intro.stories.mdx +18 -0
  117. package/docs/1.2-getting-started.stories.mdx +111 -0
  118. package/docs/1.3-theming.stories.mdx +193 -0
  119. package/docs/1.4-development-guide.stories.mdx +448 -0
  120. package/docs/1.5-contribution-guide.stories.mdx +19 -0
  121. package/docs/1.6-solution-rationale.stories.mdx +155 -0
  122. package/docs/1.7-team-members.stories.mdx +138 -0
  123. package/docs/2.1-tokens-intro.stories.mdx +51 -0
  124. package/docs/2.2-colors.stories.mdx +124 -0
  125. package/docs/2.2-typography.stories.mdx +126 -0
  126. package/docs/2.4-others.stories.mdx +180 -0
  127. package/docs/2.5-icons.stories.mdx +64 -0
  128. package/docs/2.6-baselinecss.stories.mdx +439 -0
  129. package/docs/2.7-layout-breakpoints.stories.mdx +185 -0
  130. package/icons/ArrowDown.d.ts +5 -0
  131. package/icons/ArrowDown.js +21 -0
  132. package/icons/ArrowDownB.d.ts +5 -0
  133. package/icons/ArrowDownB.js +21 -0
  134. package/icons/ArrowLeft.d.ts +5 -0
  135. package/icons/ArrowLeft.js +24 -0
  136. package/icons/ArrowRight.d.ts +5 -0
  137. package/icons/ArrowRight.js +24 -0
  138. package/icons/Back.d.ts +5 -0
  139. package/icons/Back.js +21 -0
  140. package/icons/Bell.d.ts +5 -0
  141. package/icons/Bell.js +21 -0
  142. package/icons/BellOutline.d.ts +5 -0
  143. package/icons/BellOutline.js +21 -0
  144. package/icons/BinOutline.d.ts +5 -0
  145. package/icons/BinOutline.js +25 -0
  146. package/icons/Bookmark.d.ts +5 -0
  147. package/icons/Bookmark.js +21 -0
  148. package/icons/BookmarkOutline.d.ts +5 -0
  149. package/icons/BookmarkOutline.js +21 -0
  150. package/icons/BoyFace.d.ts +5 -0
  151. package/icons/BoyFace.js +25 -0
  152. package/icons/BoyFaceOutline.d.ts +5 -0
  153. package/icons/BoyFaceOutline.js +21 -0
  154. package/icons/Bump.d.ts +5 -0
  155. package/icons/Bump.js +33 -0
  156. package/icons/BumpTime.d.ts +5 -0
  157. package/icons/BumpTime.js +35 -0
  158. package/icons/CallOutline.d.ts +5 -0
  159. package/icons/CallOutline.js +24 -0
  160. package/icons/Camera.d.ts +5 -0
  161. package/icons/Camera.js +31 -0
  162. package/icons/CardPayment.d.ts +5 -0
  163. package/icons/CardPayment.js +21 -0
  164. package/icons/Certificate.d.ts +5 -0
  165. package/icons/Certificate.js +25 -0
  166. package/icons/Chart.d.ts +5 -0
  167. package/icons/Chart.js +27 -0
  168. package/icons/ChartImpression.d.ts +5 -0
  169. package/icons/ChartImpression.js +28 -0
  170. package/icons/ChartView.d.ts +5 -0
  171. package/icons/ChartView.js +27 -0
  172. package/icons/Chat.d.ts +5 -0
  173. package/icons/Chat.js +29 -0
  174. package/icons/Check.d.ts +5 -0
  175. package/icons/Check.js +18 -0
  176. package/icons/CircleCheck.d.ts +5 -0
  177. package/icons/CircleCheck.js +27 -0
  178. package/icons/CircleCopylink.d.ts +5 -0
  179. package/icons/CircleCopylink.js +23 -0
  180. package/icons/CircleFacebook.d.ts +5 -0
  181. package/icons/CircleFacebook.js +22 -0
  182. package/icons/CircleMessenger.d.ts +5 -0
  183. package/icons/CircleMessenger.js +19 -0
  184. package/icons/CircleSms.d.ts +5 -0
  185. package/icons/CircleSms.js +22 -0
  186. package/icons/CircleUser.d.ts +5 -0
  187. package/icons/CircleUser.js +24 -0
  188. package/icons/CircleViber.d.ts +5 -0
  189. package/icons/CircleViber.js +28 -0
  190. package/icons/CircleZalo.d.ts +5 -0
  191. package/icons/CircleZalo.js +63 -0
  192. package/icons/Clock.d.ts +5 -0
  193. package/icons/Clock.js +21 -0
  194. package/icons/Close.d.ts +5 -0
  195. package/icons/Close.js +21 -0
  196. package/icons/CtCoin.d.ts +5 -0
  197. package/icons/CtCoin.js +218 -0
  198. package/icons/Download.d.ts +5 -0
  199. package/icons/Download.js +25 -0
  200. package/icons/Edit.d.ts +5 -0
  201. package/icons/Edit.js +23 -0
  202. package/icons/EditAd.d.ts +5 -0
  203. package/icons/EditAd.js +22 -0
  204. package/icons/Ellipsis.d.ts +5 -0
  205. package/icons/Ellipsis.js +21 -0
  206. package/icons/EllipsisOutline.d.ts +5 -0
  207. package/icons/EllipsisOutline.js +21 -0
  208. package/icons/FacebookOutline.d.ts +5 -0
  209. package/icons/FacebookOutline.js +32 -0
  210. package/icons/Follow.d.ts +5 -0
  211. package/icons/Follow.js +38 -0
  212. package/icons/Gallery.d.ts +5 -0
  213. package/icons/Gallery.js +24 -0
  214. package/icons/Gift.d.ts +5 -0
  215. package/icons/Gift.js +24 -0
  216. package/icons/GiftOutline.d.ts +5 -0
  217. package/icons/GiftOutline.js +21 -0
  218. package/icons/Heart.d.ts +5 -0
  219. package/icons/Heart.js +23 -0
  220. package/icons/HeartOutline.d.ts +5 -0
  221. package/icons/HeartOutline.js +23 -0
  222. package/icons/HideAd.d.ts +5 -0
  223. package/icons/HideAd.js +22 -0
  224. package/icons/Hot.d.ts +5 -0
  225. package/icons/Hot.js +21 -0
  226. package/icons/House.d.ts +5 -0
  227. package/icons/House.js +21 -0
  228. package/icons/HouseOutline.d.ts +5 -0
  229. package/icons/HouseOutline.js +21 -0
  230. package/icons/Impression.d.ts +5 -0
  231. package/icons/Impression.js +56 -0
  232. package/icons/Info.d.ts +5 -0
  233. package/icons/Info.js +21 -0
  234. package/icons/InsertAd.d.ts +5 -0
  235. package/icons/InsertAd.js +21 -0
  236. package/icons/InternationalPayment.d.ts +5 -0
  237. package/icons/InternationalPayment.js +43 -0
  238. package/icons/Label.d.ts +5 -0
  239. package/icons/Label.js +24 -0
  240. package/icons/Loan.d.ts +5 -0
  241. package/icons/Loan.js +48 -0
  242. package/icons/Location.d.ts +5 -0
  243. package/icons/Location.js +25 -0
  244. package/icons/LocationOutline.d.ts +5 -0
  245. package/icons/LocationOutline.js +27 -0
  246. package/icons/MailOutline.d.ts +5 -0
  247. package/icons/MailOutline.js +32 -0
  248. package/icons/Medal.d.ts +5 -0
  249. package/icons/Medal.js +21 -0
  250. package/icons/Msg.d.ts +5 -0
  251. package/icons/Msg.js +42 -0
  252. package/icons/PageIndicator.d.ts +5 -0
  253. package/icons/PageIndicator.js +25 -0
  254. package/icons/PaymentInfo.d.ts +5 -0
  255. package/icons/PaymentInfo.js +42 -0
  256. package/icons/PercentMsg.d.ts +5 -0
  257. package/icons/PercentMsg.js +51 -0
  258. package/icons/PhoneOutline.d.ts +5 -0
  259. package/icons/PhoneOutline.js +21 -0
  260. package/icons/Profile.d.ts +5 -0
  261. package/icons/Profile.js +29 -0
  262. package/icons/Search.d.ts +5 -0
  263. package/icons/Search.js +21 -0
  264. package/icons/Selected.d.ts +5 -0
  265. package/icons/Selected.js +34 -0
  266. package/icons/SellFaster.d.ts +5 -0
  267. package/icons/SellFaster.js +32 -0
  268. package/icons/Shop.d.ts +5 -0
  269. package/icons/Shop.js +22 -0
  270. package/icons/ShopFree.d.ts +5 -0
  271. package/icons/ShopFree.js +49 -0
  272. package/icons/ShopOutline.d.ts +5 -0
  273. package/icons/ShopOutline.js +24 -0
  274. package/icons/SpeechBubbles.d.ts +5 -0
  275. package/icons/SpeechBubbles.js +25 -0
  276. package/icons/SpeechBubblesOutline.d.ts +5 -0
  277. package/icons/SpeechBubblesOutline.js +21 -0
  278. package/icons/Star.d.ts +5 -0
  279. package/icons/Star.js +22 -0
  280. package/icons/StarOutline.d.ts +5 -0
  281. package/icons/StarOutline.js +23 -0
  282. package/icons/Stats.d.ts +5 -0
  283. package/icons/Stats.js +25 -0
  284. package/icons/Sticky.d.ts +5 -0
  285. package/icons/Sticky.js +29 -0
  286. package/icons/Suitcase.d.ts +5 -0
  287. package/icons/Suitcase.js +25 -0
  288. package/icons/Tot.d.ts +5 -0
  289. package/icons/Tot.js +21 -0
  290. package/icons/UnhideOutline.d.ts +5 -0
  291. package/icons/UnhideOutline.js +25 -0
  292. package/icons/User.d.ts +5 -0
  293. package/icons/User.js +138 -0
  294. package/icons/UserOutline.d.ts +5 -0
  295. package/icons/UserOutline.js +22 -0
  296. package/icons/Users.d.ts +5 -0
  297. package/icons/Users.js +44 -0
  298. package/icons/Verification.d.ts +5 -0
  299. package/icons/Verification.js +22 -0
  300. package/icons/VerifiedHouse.d.ts +5 -0
  301. package/icons/VerifiedHouse.js +30 -0
  302. package/icons/ViewMore.d.ts +5 -0
  303. package/icons/ViewMore.js +25 -0
  304. package/icons/Wallet.d.ts +5 -0
  305. package/icons/Wallet.js +35 -0
  306. package/icons/Warning.d.ts +5 -0
  307. package/icons/Warning.js +33 -0
  308. package/icons/index.d.ts +88 -0
  309. package/icons/index.js +88 -0
  310. package/index.d.ts +25 -0
  311. package/index.js +25 -0
  312. package/index.spec.js +50 -0
  313. package/package.json +178 -0
  314. package/patterns/Dialog/Dialog.d.ts +32 -0
  315. package/patterns/Dialog/Dialog.js +130 -0
  316. package/patterns/Dialog/Dialog.stories.js +388 -0
  317. package/patterns/Dialog/Dialog.styles.d.ts +14 -0
  318. package/patterns/Dialog/Dialog.styles.js +194 -0
  319. package/patterns/InputGroup/InputGroup.d.ts +18 -0
  320. package/patterns/InputGroup/InputGroup.js +52 -0
  321. package/patterns/InputGroup/InputGroup.spec.js +58 -0
  322. package/patterns/InputGroup/InputGroup.stories.js +203 -0
  323. package/patterns/InputGroup/InputGroup.styles.d.ts +6 -0
  324. package/patterns/InputGroup/InputGroup.styles.js +83 -0
  325. package/patterns/Popup/Popup.d.ts +41 -0
  326. package/patterns/Popup/Popup.js +175 -0
  327. package/patterns/Popup/Popup.spec.js +74 -0
  328. package/patterns/Popup/Popup.stories.js +548 -0
  329. package/patterns/Popup/Popup.styles.d.ts +20 -0
  330. package/patterns/Popup/Popup.styles.js +230 -0
  331. package/patterns/SearchInput/SearchInput.d.ts +66 -0
  332. package/patterns/SearchInput/SearchInput.js +355 -0
  333. package/patterns/SearchInput/SearchInput.stories.js +505 -0
  334. package/patterns/SearchInput/SearchInput.styles.d.ts +2 -0
  335. package/patterns/SearchInput/SearchInput.styles.js +49 -0
  336. package/patterns/TagCloud/TagCloud.d.ts +37 -0
  337. package/patterns/TagCloud/TagCloud.js +120 -0
  338. package/patterns/TagCloud/TagCloud.spec.js +163 -0
  339. package/patterns/TagCloud/TagCloud.stories.js +364 -0
  340. package/patterns/TagCloud/TagCloud.styles.d.ts +10 -0
  341. package/patterns/TagCloud/TagCloud.styles.js +39 -0
  342. package/patterns/TagCloud/TagCloudMultiActive.d.ts +42 -0
  343. package/patterns/TagCloud/TagCloudMultiActive.js +116 -0
  344. package/patterns/TagCloud/TagCloudSingleActive.d.ts +29 -0
  345. package/patterns/TagCloud/TagCloudSingleActive.js +64 -0
  346. package/patterns/index.d.ts +5 -0
  347. package/patterns/index.js +5 -0
  348. package/patterns/patterns-index.spec.js +25 -0
  349. package/scripts/create-badge-svg.d.ts +2 -0
  350. package/scripts/template-icons-index.d.ts +2 -0
  351. package/scripts/template-svg-icon.d.ts +16 -0
  352. package/shared/global.d.ts +12 -0
  353. package/shared/global.js +17 -0
  354. package/storybook/components/Bar.d.ts +10 -0
  355. package/storybook/components/BorderRadius.d.ts +12 -0
  356. package/storybook/components/BorderWidth.d.ts +10 -0
  357. package/storybook/components/Gradient.d.ts +16 -0
  358. package/storybook/components/GuideImage.d.ts +22 -0
  359. package/storybook/components/Icons.d.ts +2 -0
  360. package/storybook/components/Member.d.ts +26 -0
  361. package/storybook/components/Shadow.d.ts +10 -0
  362. package/storybook/components/Square.d.ts +10 -0
  363. package/storybook/components/Swatch.d.ts +10 -0
  364. package/storybook/components/Transition.d.ts +10 -0
  365. package/storybook/components/Type.d.ts +42 -0
  366. package/storybook/main.d.ts +3 -0
  367. package/storybook/manager.d.ts +1 -0
  368. package/storybook/preview.d.ts +76 -0
  369. package/storybook/theme-clad.d.ts +2 -0
  370. package/storybook/utils/jsonStringify.d.ts +8 -0
  371. package/storybook/utils/loadFontsForStorybook.d.ts +1 -0
  372. package/theme/__snapshots__/chotot.spec.js.snap +262 -0
  373. package/theme/__snapshots__/mudah.spec.js.snap +161 -0
  374. package/theme/chotot.d.ts +555 -0
  375. package/theme/chotot.js +937 -0
  376. package/theme/chotot.spec.js +72 -0
  377. package/theme/index.d.ts +2 -0
  378. package/theme/index.js +7 -0
  379. package/theme/mudah.d.ts +214 -0
  380. package/theme/mudah.js +333 -0
  381. package/theme/mudah.spec.js +9 -0
  382. package/types.d.ts +14 -0
  383. package/utils/_normalizeArrowKey.d.ts +9 -0
  384. package/utils/_normalizeArrowKey.js +17 -0
  385. package/utils/_simulateChangeEvent.d.ts +9 -0
  386. package/utils/_simulateChangeEvent.js +34 -0
  387. package/utils/_simulateChangeEvent.spec.js +54 -0
  388. package/utils/cleanHtml.d.ts +8 -0
  389. package/utils/cleanHtml.js +14 -0
  390. package/utils/cleanHtml.spec.js +45 -0
  391. package/utils/createUniversalPortal.d.ts +10 -0
  392. package/utils/createUniversalPortal.js +22 -0
  393. package/utils/createUniversalPortal.server.spec.js +13 -0
  394. package/utils/createUniversalPortal.spec.js +13 -0
  395. package/utils/getBreakpointsValues.d.ts +10 -0
  396. package/utils/getBreakpointsValues.js +24 -0
  397. package/utils/getBreakpointsValues.test.js +89 -0
  398. package/utils/index.d.ts +5 -0
  399. package/utils/index.js +5 -0
  400. package/utils/noop.d.ts +8 -0
  401. package/utils/noop.js +9 -0
  402. package/utils/noop.spec.js +12 -0
  403. package/utils/sx.d.ts +3770 -0
  404. package/utils/sx.js +95 -0
  405. package/utils/sx.spec.js +53 -0
@@ -0,0 +1,369 @@
1
+ import theme from '@clad-ui/theme';
2
+ import React, { useState, useRef, useEffect, useCallback } from 'react';
3
+ import PropTypes from 'prop-types';
4
+ import { cx } from 'linaria';
5
+ import formatValue from './utils/_formatValue';
6
+ import cleanValue from './utils/_cleanValue';
7
+
8
+ import inputClasses, {
9
+ Container,
10
+ inputFocusClasses,
11
+ InputWrapper,
12
+ Paragraph,
13
+ SuffixWrapper,
14
+ } from './CurrencyInput.styles.js';
15
+
16
+ const { sizes } = theme;
17
+
18
+ /**
19
+ * CurrencyInput component allows users to input money amount with numeric keyboard,
20
+ * automatic thousand separators, currency indicator
21
+ *
22
+ * `🧪Beta`
23
+ *
24
+ * @type {React.FC<{
25
+ * id?: string,
26
+ * name?: string,
27
+ * label?: string,
28
+ * value?: string | number,
29
+ * onChange?: Function,
30
+ * onFormatChange?: Function,
31
+ * onBlur?: Function,
32
+ * onFocus?: Function,
33
+ * disabled?: boolean,
34
+ * required?: boolean,
35
+ * status?: string,
36
+ * helptext?: string,
37
+ * inputRef?: any,
38
+ * width?: string,
39
+ * suffix?: string,
40
+ * allowNegativeValue?: boolean,
41
+ * separator?: string,
42
+ * decimalSeparator?: string,
43
+ * maxDigits?: number,
44
+ * decimalsLimit?: number,
45
+ * }>}
46
+ */
47
+ const CurrencyInput = ({
48
+ id,
49
+ name,
50
+ label,
51
+ value,
52
+ onChange,
53
+ onFormatChange,
54
+ onBlur,
55
+ onFocus,
56
+ disabled,
57
+ required,
58
+ status,
59
+ helptext,
60
+ inputRef,
61
+ width,
62
+ suffix,
63
+ allowNegativeValue,
64
+ separator,
65
+ decimalSeparator,
66
+ maxDigits,
67
+ decimalsLimit,
68
+ }) => {
69
+
70
+ const formatValueOptions = {
71
+ decimalSeparator,
72
+ separator,
73
+ suffix,
74
+ };
75
+
76
+ const formattedStateValue =
77
+ value !== undefined ? formatValue({ ...formatValueOptions, value: String(value) }) : '';
78
+
79
+ const cleanValueOptions = {
80
+ decimalSeparator,
81
+ separator,
82
+ decimalsLimit,
83
+ allowNegativeValue,
84
+ };
85
+
86
+ const [toggleSuffix, setToggleSuffix] = useState(false);
87
+ const [stateValue, setStateValue] = useState(formattedStateValue);
88
+ const [dirty, setDirty] = useState(false);
89
+ const [cursor, setCursor] = useState(0);
90
+ const defaultInputRef = useRef();
91
+ const ref = inputRef || defaultInputRef;
92
+
93
+ // Prevent cursor jumping if editing value
94
+ useEffect(() => {
95
+ if (dirty && stateValue !== '-' && ref && typeof ref === 'object' && ref.current) {
96
+ ref.current.setSelectionRange(cursor, cursor);
97
+ }
98
+ }, [stateValue, cursor, ref, dirty]);
99
+
100
+ const className = cx(
101
+ inputClasses,
102
+ inputFocusClasses,
103
+ value && 'hasValue',
104
+ !label && 'noLabel',
105
+ required && 'required'
106
+ );
107
+
108
+ const isValidWithMaxDigits = useCallback((valueInput) => {
109
+ let valueToComapre = valueInput;
110
+
111
+ if (allowNegativeValue) {
112
+ valueToComapre = valueToComapre.replace('-', '');
113
+ }
114
+
115
+ if (decimalsLimit > 0) {
116
+ valueToComapre = valueToComapre.replace(decimalSeparator, '');
117
+ }
118
+
119
+ return valueToComapre.length <= maxDigits;
120
+ }, [allowNegativeValue, decimalsLimit, decimalSeparator, maxDigits]);
121
+
122
+ const processFormatChange = (inputValue, selectionStart, formatChange) => {
123
+ setDirty(true);
124
+ const valueOnly = cleanValue({ value: inputValue, ...cleanValueOptions });
125
+
126
+ if (valueOnly === '') {
127
+ if (formatChange) {
128
+ formatChange(undefined);
129
+ }
130
+ setStateValue('');
131
+ return;
132
+ }
133
+
134
+ if (!isValidWithMaxDigits(valueOnly)) {
135
+ return;
136
+ }
137
+
138
+ if (valueOnly === '-' || valueOnly === decimalSeparator) {
139
+ if (formatChange) {
140
+ formatChange(undefined);
141
+ }
142
+ setStateValue(inputValue);
143
+ return;
144
+ }
145
+
146
+ const formattedValue = formatValue({ value: valueOnly, ...formatValueOptions });
147
+
148
+ if (selectionStart !== undefined && selectionStart !== null) {
149
+ // Prevent cursor jumping
150
+ const newCursor = selectionStart + (formattedValue.length - inputValue.length) || 1;
151
+ setCursor(newCursor);
152
+ }
153
+
154
+ setStateValue(formattedValue);
155
+
156
+ if (formatChange) {
157
+ let changedValue = formattedValue;
158
+ if (+valueOnly === 0) {
159
+ changedValue = '0';
160
+ }
161
+ formatChange(changedValue);
162
+ }
163
+ };
164
+
165
+ const handleChange = useCallback((e) => {
166
+ const {
167
+ target: { value: targetValue, selectionStart },
168
+ } = e;
169
+
170
+ processFormatChange(targetValue, selectionStart, onFormatChange);
171
+
172
+ if (onChange) {
173
+ let valueOnly = cleanValue({ value: targetValue, ...cleanValueOptions });
174
+
175
+ if (isValidWithMaxDigits(valueOnly)) {
176
+ if (decimalSeparator && stateValue.includes(decimalSeparator)) {
177
+ valueOnly = valueOnly.replace(decimalSeparator, '.');
178
+ }
179
+ onChange({ ...e, target: { value: valueOnly } });
180
+ }
181
+ }
182
+ }, [stateValue, onFormatChange, onChange]);
183
+
184
+ const handleBlur = useCallback((e) => {
185
+ if (!stateValue) {
186
+ setToggleSuffix(false);
187
+ }
188
+ const {
189
+ target: { value: targetValue },
190
+ } = e;
191
+
192
+ const valueOnly = cleanValue({ value: targetValue, ...cleanValueOptions });
193
+
194
+ if (valueOnly === '-' || !valueOnly) {
195
+ setStateValue('');
196
+ if (onBlur) {
197
+ onBlur(e);
198
+ }
199
+ return;
200
+ }
201
+ const formattedValue = formatValue({
202
+ ...formatValueOptions,
203
+ value: valueOnly,
204
+ });
205
+
206
+ setStateValue(formattedValue);
207
+
208
+ onBlur(e);
209
+ }, [stateValue, onBlur]);
210
+
211
+ const handleFocus = useCallback((e) => {
212
+ setToggleSuffix(true);
213
+ onFocus(e);
214
+ }, [onFocus]);
215
+
216
+ const formattedPropsValue =
217
+ value !== undefined
218
+ ? formatValue({
219
+ ...formatValueOptions,
220
+ value: String(value),
221
+ })
222
+ : undefined;
223
+
224
+ const attrs = {
225
+ className,
226
+ onChange: (/** @type {Object} */ e) => handleChange(e),
227
+ onBlur: (/** @type {Object} */ e) => handleBlur(e),
228
+ onFocus: (/** @type {Object} */ e) => handleFocus(e),
229
+ value:
230
+ formattedPropsValue !== undefined && stateValue !== '-' && stateValue !== decimalSeparator
231
+ ? formattedPropsValue
232
+ : stateValue,
233
+ };
234
+
235
+ if (id) attrs.id = id;
236
+ if (name) attrs.name = name;
237
+ if (disabled) attrs.disabled = 'disabled';
238
+
239
+ // The clear/Show-password/Status icons will not be able to displayed at the same time.
240
+ const isWithError = status === 'error';
241
+
242
+ const containerWidth = sizes[width] || 'initial';
243
+ const wrapperClassname = cx(disabled && 'disabled', isWithError && 'error');
244
+
245
+ return (
246
+ <Container containerWidth={containerWidth}>
247
+ <InputWrapper className={wrapperClassname}>
248
+ <input type="text" inputMode="decimal" {...attrs} ref={ref} />
249
+ {label && <label htmlFor={id}>{label}</label>}
250
+ {suffix && (toggleSuffix || value) && (
251
+ <SuffixWrapper>
252
+ <span>{suffix}</span>
253
+ </SuffixWrapper>
254
+ )}
255
+ </InputWrapper>
256
+ <Paragraph>{helptext}</Paragraph>
257
+ </Container>
258
+ );
259
+ };
260
+
261
+ const keysOfSizes = Object.keys(sizes);
262
+
263
+ CurrencyInput.propTypes = {
264
+ /**
265
+ * Input name
266
+ */
267
+ name: PropTypes.string,
268
+ /**
269
+ * Input label
270
+ */
271
+ label: PropTypes.string,
272
+ /**
273
+ * Input ref (implement with useRef() hooks)
274
+ */
275
+ inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.any]),
276
+ /**
277
+ * The input content value
278
+ */
279
+ value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
280
+ /**
281
+ * Callback when user input. Return event value with numeric value in string. eg: 1200.25
282
+ */
283
+ onChange: PropTypes.func,
284
+ /**
285
+ * Callback when user input. Return formatted value. eg: 1.200,25
286
+ */
287
+ onFormatChange: PropTypes.func,
288
+ /**
289
+ * Callback when user blur
290
+ */
291
+ onBlur: PropTypes.func,
292
+ /**
293
+ * Callback when user focus
294
+ */
295
+ onFocus: PropTypes.func,
296
+ /**
297
+ * Disabled input
298
+ */
299
+ disabled: PropTypes.bool,
300
+ /**
301
+ * Whether this input is required or not
302
+ */
303
+ required: PropTypes.bool,
304
+ /**
305
+ * The id of input
306
+ */
307
+ id: PropTypes.string,
308
+ /**
309
+ * Input status (useful when implemented with form validation)
310
+ */
311
+ status: PropTypes.oneOf(['error', 'default']),
312
+ /**
313
+ * Promp a helptext below input (useful when implemented with form validation)
314
+ */
315
+ helptext: PropTypes.string,
316
+ /**
317
+ * Input width
318
+ */
319
+ width: PropTypes.oneOf(keysOfSizes),
320
+ /**
321
+ * Suffix. eg: đ, $ or £,...
322
+ */
323
+ suffix: PropTypes.string,
324
+ /**
325
+ * Separator between thousand, million and billion
326
+ */
327
+ separator: PropTypes.string,
328
+ /**
329
+ * Separator between integer part and fractional part of value
330
+ */
331
+ decimalSeparator: PropTypes.string,
332
+ /**
333
+ * Maximum digits the user can enter.eg: 123,456 (maxDigits = 6)
334
+ */
335
+ maxDigits: PropTypes.number,
336
+ /**
337
+ * Limit length of decimals allowed. Not allow decimals if limit = 0. eg: 123,2222 (decimalsLimit = 4)
338
+ */
339
+ decimalsLimit: PropTypes.number,
340
+ /**
341
+ * Allow negative value
342
+ */
343
+ allowNegativeValue: PropTypes.bool,
344
+ };
345
+
346
+ CurrencyInput.defaultProps = {
347
+ name: '',
348
+ label: '',
349
+ inputRef: null,
350
+ value: '',
351
+ onChange: () => {},
352
+ onFormatChange: () => {},
353
+ onBlur: () => {},
354
+ onFocus: () => {},
355
+ disabled: false,
356
+ required: false,
357
+ id: '',
358
+ status: 'default',
359
+ helptext: '',
360
+ width: 'full',
361
+ suffix: 'đ',
362
+ allowNegativeValue: false,
363
+ separator: '.',
364
+ decimalSeparator: ',',
365
+ maxDigits: 12,
366
+ decimalsLimit: 0,
367
+ };
368
+
369
+ export default CurrencyInput;
@@ -0,0 +1,151 @@
1
+ import React from 'react';
2
+ import { render, fireEvent, cleanup } from '@testing-library/react';
3
+ import '@testing-library/jest-dom';
4
+ import CurrencyInput from './CurrencyInput';
5
+
6
+ afterEach(cleanup);
7
+
8
+ describe('test Currency Input component', () => {
9
+ it('should Currency Input value change properly', () => {
10
+ let value = '';
11
+ const onChange = jest.fn((e) => {
12
+ value = e.target.value;
13
+ });
14
+ const { getByLabelText, rerender } = render(
15
+ <CurrencyInput id="test" label="input test" value={value} onChange={onChange} />
16
+ );
17
+ const input = getByLabelText('input test');
18
+ expect(input.value).toBe('');
19
+
20
+ fireEvent.change(input, { target: { value: '2100000' } });
21
+ expect(onChange).toBeCalledTimes(1);
22
+ rerender(<CurrencyInput id="test" label="input test" value={value} onChange={onChange} />);
23
+ expect(input.value).toBe('2.100.000');
24
+ });
25
+
26
+ it('should Currency Input separator change properly', () => {
27
+ let value = '';
28
+ const onChange = jest.fn((e) => {
29
+ value = e.target.value;
30
+ });
31
+ const { getByLabelText, rerender } = render(
32
+ <CurrencyInput
33
+ separator=","
34
+ decimalSeparator="."
35
+ decimalsLimit={3}
36
+ id="test"
37
+ label="input test"
38
+ value={value}
39
+ onChange={onChange}
40
+ />
41
+ );
42
+ const input = getByLabelText('input test');
43
+ expect(input.value).toBe('');
44
+
45
+ fireEvent.change(input, { target: { value: '23000000.50' } });
46
+ expect(onChange).toBeCalledTimes(1);
47
+ rerender(
48
+ <CurrencyInput
49
+ separator=","
50
+ decimalSeparator="."
51
+ decimalsLimit={3}
52
+ id="test"
53
+ label="input test"
54
+ value={value}
55
+ onChange={onChange}
56
+ />
57
+ );
58
+ expect(input.value).toBe('23,000,000.50');
59
+ });
60
+
61
+ it('should Currency Input limit with max length properly', () => {
62
+ let value = '';
63
+ const onChange = jest.fn((e) => {
64
+ value = e.target.value;
65
+ });
66
+ const { getByLabelText, rerender } = render(
67
+ <CurrencyInput id="test" label="input test" value={value} maxDigits={6} onChange={onChange} />
68
+ );
69
+ const input = getByLabelText('input test');
70
+ expect(input.value).toBe('');
71
+
72
+ fireEvent.change(input, { target: { value: '230000' } });
73
+ rerender(
74
+ <CurrencyInput
75
+ separator=","
76
+ id="test"
77
+ label="input test"
78
+ maxDigits={6}
79
+ value={value}
80
+ onChange={onChange}
81
+ />
82
+ );
83
+ expect(input.value).toBe('230,000');
84
+ });
85
+
86
+ it('should Currency Input change negative value properly', () => {
87
+ let value = '';
88
+ const onChange = jest.fn((e) => {
89
+ value = e.target.value;
90
+ });
91
+ const { getByLabelText, rerender } = render(
92
+ <CurrencyInput
93
+ id="test"
94
+ allowNegativeValue
95
+ label="input test"
96
+ value={value}
97
+ onChange={onChange}
98
+ />
99
+ );
100
+ const input = getByLabelText('input test');
101
+ expect(input.value).toBe('');
102
+
103
+ fireEvent.change(input, { target: { value: '-230000' } });
104
+ rerender(
105
+ <CurrencyInput
106
+ allowNegativeValue
107
+ separator=","
108
+ id="test"
109
+ label="input test"
110
+ value={value}
111
+ onChange={onChange}
112
+ />
113
+ );
114
+ expect(input.value).toBe('-230,000');
115
+ });
116
+
117
+ it('should Currency Input change decimal part properly', () => {
118
+ let value = '';
119
+ const onChange = jest.fn((e) => {
120
+ value = e.target.value;
121
+ });
122
+ const { getByLabelText, rerender } = render(
123
+ <CurrencyInput
124
+ decimalsLimit={3}
125
+ id="test"
126
+ label="input test"
127
+ value={value}
128
+ onChange={onChange}
129
+ />
130
+ );
131
+ const input = getByLabelText('input test');
132
+ expect(input.value).toBe('');
133
+
134
+ fireEvent.change(input, { target: { value: '23500,45' } });
135
+ rerender(
136
+ <CurrencyInput
137
+ decimalsLimit={3}
138
+ id="test"
139
+ label="input test"
140
+ value={value}
141
+ onChange={onChange}
142
+ />
143
+ );
144
+ expect(input.value).toBe('23.500,45');
145
+ });
146
+
147
+ it('should Currency Input border change on error status', () => {
148
+ const { container } = render(<CurrencyInput status="error" />);
149
+ expect(container.querySelector('div > div > div')).toHaveClass('error');
150
+ });
151
+ });
@@ -0,0 +1,202 @@
1
+ import React, { useState, useRef, useCallback } from 'react';
2
+ import theme from '@clad-ui/theme';
3
+ import debounce from 'lodash/debounce';
4
+ import CurrencyInput from './CurrencyInput';
5
+ import Grid from '../Grid/Grid';
6
+ import Col from '../Grid/Col';
7
+
8
+ const { sizes } = theme;
9
+
10
+ const currencyInputStories = {
11
+ title: 'Components/Inputs/Currency Input',
12
+ component: CurrencyInput,
13
+ parameters: {
14
+ docs: {
15
+ description: {
16
+ component: `CurrencyInput component allows users to input money amount with numeric keyboard, automatic thousand separators, currency indicator
17
+
18
+ \`🧪 Beta\` [\`💎 Sketch Specs\`](https://www.sketch.com/s/061b5b52-f0d0-439f-9fde-2bfbcfaaf97e/a/Vr0G2dw)
19
+ `,
20
+ },
21
+ },
22
+ },
23
+ argTypes: {
24
+ label: {
25
+ defaultValue: 'Amount (VND)',
26
+ },
27
+ helptext: {
28
+ defaultValue: 'Please enter whole amount',
29
+ },
30
+ disabled: {},
31
+ required: {},
32
+ status: {},
33
+ width: { options: Object.keys(sizes) },
34
+ suffix: {},
35
+ allowNegativeValue: {},
36
+ decimalsLimit: {},
37
+ maxDigits: {},
38
+ id: { control: null },
39
+ name: { control: null },
40
+ value: { control: null },
41
+ onChange: { control: null },
42
+ onFormatChange: { control: null },
43
+ onBlur: { control: null },
44
+ onFocus: { control: null },
45
+ inputRef: { control: null },
46
+ },
47
+ };
48
+
49
+ export default currencyInputStories;
50
+
51
+ // top-most story will have the props breakdown
52
+ export const Overview = (args) => {
53
+ const [value, setValue] = useState('');
54
+ return (
55
+ <>
56
+ <Grid rowCols={[1, 2]}>
57
+ <Col>
58
+ <CurrencyInput {...args} value={value} onChange={(e) => setValue(e.target.value)} />
59
+ </Col>
60
+ </Grid>
61
+ <p>Input value: {value || 'Please enter something.'}</p>
62
+ </>
63
+ );
64
+ };
65
+
66
+ export const ValueBinding = () => {
67
+ const [value, setValue] = useState('');
68
+ const [formatValue, setFormatValue] = useState('');
69
+
70
+ return (
71
+ <>
72
+ <Grid rowCols={[1, 2]}>
73
+ <Col>
74
+ <CurrencyInput
75
+ allowNegativeValue
76
+ value={value}
77
+ onChange={(e) => setValue(e.target.value)}
78
+ onFormatChange={(v) => setFormatValue(v)}
79
+ label="Amount (VND)"
80
+ />
81
+ </Col>
82
+ </Grid>
83
+ <p>
84
+ Formatted Value with <b>onFormatChange</b>: {formatValue || 'Please enter something.'}
85
+ </p>
86
+ <p>
87
+ Event Value with <b>onChange (e.target.value)</b>: {value || 'Please enter something.'}
88
+ </p>
89
+ </>
90
+ );
91
+ };
92
+
93
+ ValueBinding.parameters = {
94
+ docs: {
95
+ description: {
96
+ story: 'Currently, Input only support controlled HTML &lt;input /&gt;',
97
+ },
98
+ },
99
+ };
100
+
101
+ export const Required = () => {
102
+ const [value, setValue] = useState('');
103
+ return (
104
+ <Grid rowCols={[1, 2]}>
105
+ <Col>
106
+ <CurrencyInput
107
+ label="Required input"
108
+ required
109
+ onChange={(e) => setValue(e.target.value)}
110
+ value={value}
111
+ />
112
+ </Col>
113
+ </Grid>
114
+ );
115
+ };
116
+
117
+ Required.parameters = {
118
+ docs: {
119
+ description: {
120
+ story:
121
+ 'Enable `required` will let Input display a red * on the label but validation is not forced for now',
122
+ },
123
+ },
124
+ };
125
+
126
+ export const ErrorStatus = () => {
127
+ const [value, setValue] = useState('');
128
+ const status = !value ? 'error' : 'default';
129
+ return (
130
+ <Grid rowCols={[1, 2]}>
131
+ <Col>
132
+ <CurrencyInput
133
+ status={status}
134
+ label="Required input"
135
+ helptext="is required"
136
+ value={value}
137
+ onChange={(e) => setValue(e.target.value)}
138
+ required
139
+ />
140
+ </Col>
141
+ </Grid>
142
+ );
143
+ };
144
+
145
+ ErrorStatus.parameters = {
146
+ docs: {
147
+ description: {
148
+ story:
149
+ 'Set `status="error"` will let Input has red borders and helpText, indicate error status.',
150
+ },
151
+ },
152
+ };
153
+
154
+ export const Disabled = () => (
155
+ <Grid rowCols={[1, 2]}>
156
+ <Col>
157
+ <CurrencyInput allowNegativeValue value={-11111.222} label="Read only" disabled />
158
+ </Col>
159
+ </Grid>
160
+ );
161
+
162
+ Disabled.parameters = {
163
+ docs: {
164
+ description: {
165
+ story:
166
+ 'Enable `disabled` will set attribute `disabled` to the HTML input. Effectively, disabled input is read-only.',
167
+ },
168
+ },
169
+ };
170
+
171
+ export const DebounceExample = () => {
172
+ const inputRef = useRef(null);
173
+ const [text, setText] = useState('');
174
+
175
+ const handleFetch = useCallback(
176
+ debounce(() => {
177
+ const { value } = inputRef.current;
178
+ // eslint-disable-next-line no-alert
179
+ if (value) alert('Searching for: ' + value);
180
+ }, 1000),
181
+ []
182
+ );
183
+ const handleInputChange = (e) => {
184
+ setText(e.target.value);
185
+ handleFetch();
186
+ };
187
+
188
+ return (
189
+ <Grid rowCols={[1, 2]}>
190
+ <Col>
191
+ <CurrencyInput
192
+ value={text}
193
+ onChange={handleInputChange}
194
+ label="Search input example"
195
+ inputRef={inputRef}
196
+ />
197
+ </Col>
198
+ </Grid>
199
+ );
200
+ };
201
+
202
+ DebounceExample.storyName = 'Ex: Debounce onChange';