@navikt/ds-react 6.1.1 → 6.3.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 (438) hide show
  1. package/cjs/accordion/Accordion.d.ts +4 -4
  2. package/cjs/alert/Alert.d.ts +12 -10
  3. package/cjs/alert/Alert.js.map +1 -1
  4. package/cjs/button/Button.d.ts +9 -8
  5. package/cjs/button/Button.js.map +1 -1
  6. package/cjs/chat/Chat.d.ts +6 -6
  7. package/cjs/chips/Chips.d.ts +2 -2
  8. package/cjs/copybutton/CopyButton.d.ts +14 -12
  9. package/cjs/copybutton/CopyButton.js.map +1 -1
  10. package/cjs/date/datepicker/types.d.ts +7 -6
  11. package/cjs/date/hooks/useDatepicker.d.ts +4 -3
  12. package/cjs/date/hooks/useDatepicker.js.map +1 -1
  13. package/cjs/date/hooks/useMonthPicker.d.ts +4 -3
  14. package/cjs/date/hooks/useMonthPicker.js.map +1 -1
  15. package/cjs/date/monthpicker/types.d.ts +2 -1
  16. package/cjs/date/parts/DateInput.d.ts +1 -1
  17. package/cjs/expansion-card/ExpansionCardTitle.d.ts +1 -1
  18. package/cjs/form/checkbox/CheckboxGroup.d.ts +3 -3
  19. package/cjs/form/checkbox/types.d.ts +4 -4
  20. package/cjs/form/combobox/ComboboxProvider.js +5 -1
  21. package/cjs/form/combobox/ComboboxProvider.js.map +1 -1
  22. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +14 -12
  23. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  24. package/cjs/form/combobox/FilteredOptions/filtered-options-util.d.ts +3 -3
  25. package/cjs/form/combobox/FilteredOptions/filtered-options-util.js +1 -3
  26. package/cjs/form/combobox/FilteredOptions/filtered-options-util.js.map +1 -1
  27. package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.d.ts +8 -5
  28. package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js +8 -13
  29. package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
  30. package/cjs/form/combobox/Input/Input.js +9 -7
  31. package/cjs/form/combobox/Input/Input.js.map +1 -1
  32. package/cjs/form/combobox/SelectedOptions/SelectedOptions.d.ts +2 -1
  33. package/cjs/form/combobox/SelectedOptions/SelectedOptions.js +3 -3
  34. package/cjs/form/combobox/SelectedOptions/SelectedOptions.js.map +1 -1
  35. package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.d.ts +10 -7
  36. package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.js +6 -8
  37. package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.js.map +1 -1
  38. package/cjs/form/combobox/combobox-utils.d.ts +10 -0
  39. package/cjs/form/combobox/combobox-utils.js +27 -0
  40. package/cjs/form/combobox/combobox-utils.js.map +1 -0
  41. package/cjs/form/combobox/customOptionsContext.d.ts +5 -4
  42. package/cjs/form/combobox/customOptionsContext.js +1 -1
  43. package/cjs/form/combobox/customOptionsContext.js.map +1 -1
  44. package/cjs/form/combobox/types.d.ts +33 -22
  45. package/cjs/form/error-summary/ErrorSummary.d.ts +5 -5
  46. package/cjs/form/file-upload/FileUpload.context.d.ts +8 -0
  47. package/cjs/form/file-upload/FileUpload.context.js +7 -0
  48. package/cjs/form/file-upload/FileUpload.context.js.map +1 -0
  49. package/cjs/form/file-upload/FileUpload.d.ts +118 -0
  50. package/cjs/form/file-upload/FileUpload.js +73 -0
  51. package/cjs/form/file-upload/FileUpload.js.map +1 -0
  52. package/cjs/form/file-upload/FileUpload.types.d.ts +55 -0
  53. package/cjs/form/file-upload/FileUpload.types.js +8 -0
  54. package/cjs/form/file-upload/FileUpload.types.js.map +1 -0
  55. package/cjs/form/file-upload/i18n/get.d.ts +2 -0
  56. package/cjs/form/file-upload/i18n/get.js +38 -0
  57. package/cjs/form/file-upload/i18n/get.js.map +1 -0
  58. package/cjs/form/file-upload/i18n/i18n.context.d.ts +11 -0
  59. package/cjs/form/file-upload/i18n/i18n.context.js +39 -0
  60. package/cjs/form/file-upload/i18n/i18n.context.js.map +1 -0
  61. package/cjs/form/file-upload/i18n/i18n.types.d.ts +13 -0
  62. package/cjs/form/file-upload/i18n/i18n.types.js +3 -0
  63. package/cjs/form/file-upload/i18n/i18n.types.js.map +1 -0
  64. package/cjs/form/file-upload/i18n/locales/nb.json +20 -0
  65. package/cjs/form/file-upload/i18n/merge.d.ts +2 -0
  66. package/cjs/form/file-upload/i18n/merge.js +29 -0
  67. package/cjs/form/file-upload/i18n/merge.js.map +1 -0
  68. package/cjs/form/file-upload/index.d.ts +7 -0
  69. package/cjs/form/file-upload/index.js +16 -0
  70. package/cjs/form/file-upload/index.js.map +1 -0
  71. package/cjs/form/file-upload/parts/Trigger.d.ts +7 -0
  72. package/cjs/form/file-upload/parts/Trigger.js +43 -0
  73. package/cjs/form/file-upload/parts/Trigger.js.map +1 -0
  74. package/cjs/form/file-upload/parts/dropzone/Dropzone.d.ts +4 -0
  75. package/cjs/form/file-upload/parts/dropzone/Dropzone.js +106 -0
  76. package/cjs/form/file-upload/parts/dropzone/Dropzone.js.map +1 -0
  77. package/cjs/form/file-upload/parts/dropzone/dropzone.types.d.ts +18 -0
  78. package/cjs/form/file-upload/parts/dropzone/dropzone.types.js +3 -0
  79. package/cjs/form/file-upload/parts/dropzone/dropzone.types.js.map +1 -0
  80. package/cjs/form/file-upload/parts/dropzone/useDropzone.d.ts +13 -0
  81. package/cjs/form/file-upload/parts/dropzone/useDropzone.js +34 -0
  82. package/cjs/form/file-upload/parts/dropzone/useDropzone.js.map +1 -0
  83. package/cjs/form/file-upload/parts/item/Item.d.ts +55 -0
  84. package/cjs/form/file-upload/parts/item/Item.js +79 -0
  85. package/cjs/form/file-upload/parts/item/Item.js.map +1 -0
  86. package/cjs/form/file-upload/parts/item/Item.types.d.ts +5 -0
  87. package/cjs/form/file-upload/parts/item/Item.types.js +3 -0
  88. package/cjs/form/file-upload/parts/item/Item.types.js.map +1 -0
  89. package/cjs/form/file-upload/parts/item/ItemButton.d.ts +12 -0
  90. package/cjs/form/file-upload/parts/item/ItemButton.js +22 -0
  91. package/cjs/form/file-upload/parts/item/ItemButton.js.map +1 -0
  92. package/cjs/form/file-upload/parts/item/ItemIcon.d.ts +9 -0
  93. package/cjs/form/file-upload/parts/item/ItemIcon.js +51 -0
  94. package/cjs/form/file-upload/parts/item/ItemIcon.js.map +1 -0
  95. package/cjs/form/file-upload/parts/item/ItemName.d.ts +9 -0
  96. package/cjs/form/file-upload/parts/item/ItemName.js +32 -0
  97. package/cjs/form/file-upload/parts/item/ItemName.js.map +1 -0
  98. package/cjs/form/file-upload/parts/item/utils/download-file.d.ts +1 -0
  99. package/cjs/form/file-upload/parts/item/utils/download-file.js +13 -0
  100. package/cjs/form/file-upload/parts/item/utils/download-file.js.map +1 -0
  101. package/cjs/form/file-upload/parts/item/utils/file-type-checker.d.ts +2 -0
  102. package/cjs/form/file-upload/parts/item/utils/file-type-checker.js +6 -0
  103. package/cjs/form/file-upload/parts/item/utils/file-type-checker.js.map +1 -0
  104. package/cjs/form/file-upload/parts/item/utils/format-file-size.d.ts +2 -0
  105. package/cjs/form/file-upload/parts/item/utils/format-file-size.js +24 -0
  106. package/cjs/form/file-upload/parts/item/utils/format-file-size.js.map +1 -0
  107. package/cjs/form/file-upload/useFileUpload.d.ts +12 -0
  108. package/cjs/form/file-upload/useFileUpload.js +33 -0
  109. package/cjs/form/file-upload/useFileUpload.js.map +1 -0
  110. package/cjs/form/file-upload/utils/is-accepted-file-type.d.ts +1 -0
  111. package/cjs/form/file-upload/utils/is-accepted-file-type.js +26 -0
  112. package/cjs/form/file-upload/utils/is-accepted-file-type.js.map +1 -0
  113. package/cjs/form/file-upload/utils/is-accepted-size.d.ts +1 -0
  114. package/cjs/form/file-upload/utils/is-accepted-size.js +11 -0
  115. package/cjs/form/file-upload/utils/is-accepted-size.js.map +1 -0
  116. package/cjs/form/file-upload/utils/validate-files.d.ts +8 -0
  117. package/cjs/form/file-upload/utils/validate-files.js +48 -0
  118. package/cjs/form/file-upload/utils/validate-files.js.map +1 -0
  119. package/cjs/form/radio/RadioGroup.d.ts +1 -1
  120. package/cjs/form/search/Search.d.ts +3 -2
  121. package/cjs/form/search/Search.js.map +1 -1
  122. package/cjs/form/select/Select.d.ts +9 -9
  123. package/cjs/form/switch/Switch.d.ts +5 -5
  124. package/cjs/form/textarea/Textarea.d.ts +5 -4
  125. package/cjs/form/textarea/Textarea.js.map +1 -1
  126. package/cjs/form/useFormField.d.ts +10 -8
  127. package/cjs/form/useFormField.js.map +1 -1
  128. package/cjs/index.d.ts +1 -0
  129. package/cjs/index.js +3 -1
  130. package/cjs/index.js.map +1 -1
  131. package/cjs/layout/bleed/Bleed.d.ts +9 -1
  132. package/cjs/layout/bleed/Bleed.js.map +1 -1
  133. package/cjs/layout/box/Box.d.ts +5 -5
  134. package/cjs/layout/grid/HGrid.d.ts +1 -3
  135. package/cjs/layout/grid/HGrid.js.map +1 -1
  136. package/cjs/layout/page/Page.d.ts +2 -2
  137. package/cjs/layout/responsive/Responsive.d.ts +2 -4
  138. package/cjs/layout/responsive/Responsive.js.map +1 -1
  139. package/cjs/layout/stack/Stack.d.ts +18 -4
  140. package/cjs/layout/stack/Stack.js.map +1 -1
  141. package/cjs/list/types.d.ts +1 -1
  142. package/cjs/loader/Loader.d.ts +0 -6
  143. package/cjs/loader/Loader.js.map +1 -1
  144. package/cjs/modal/Modal.js +2 -2
  145. package/cjs/modal/types.d.ts +8 -8
  146. package/cjs/pagination/Pagination.d.ts +3 -2
  147. package/cjs/pagination/Pagination.js.map +1 -1
  148. package/cjs/pagination/PaginationItem.d.ts +1 -1
  149. package/cjs/popover/Popover.d.ts +2 -1
  150. package/cjs/popover/Popover.js.map +1 -1
  151. package/cjs/read-more/ReadMore.d.ts +7 -7
  152. package/cjs/stepper/Step.d.ts +4 -4
  153. package/cjs/stepper/Stepper.d.ts +7 -5
  154. package/cjs/stepper/Stepper.js.map +1 -1
  155. package/cjs/table/AnimateHeight.d.ts +1 -1
  156. package/cjs/table/DataCell.d.ts +1 -3
  157. package/cjs/table/DataCell.js.map +1 -1
  158. package/cjs/tabs/TabList.d.ts +1 -1
  159. package/cjs/tabs/Tabs.d.ts +7 -7
  160. package/cjs/timeline/Timeline.d.ts +6 -3
  161. package/cjs/timeline/Timeline.js +2 -1
  162. package/cjs/timeline/Timeline.js.map +1 -1
  163. package/cjs/timeline/period/index.d.ts +3 -2
  164. package/cjs/timeline/period/index.js.map +1 -1
  165. package/cjs/tooltip/Tooltip.d.ts +19 -12
  166. package/cjs/tooltip/Tooltip.js.map +1 -1
  167. package/cjs/util/create-context.d.ts +1 -0
  168. package/cjs/util/create-context.js.map +1 -1
  169. package/cjs/util/hooks/useEventListener.d.ts +1 -1
  170. package/cjs/util/hooks/useMergeRefs.d.ts +1 -1
  171. package/esm/accordion/Accordion.d.ts +4 -4
  172. package/esm/alert/Alert.d.ts +12 -10
  173. package/esm/alert/Alert.js.map +1 -1
  174. package/esm/button/Button.d.ts +9 -8
  175. package/esm/button/Button.js.map +1 -1
  176. package/esm/chat/Chat.d.ts +6 -6
  177. package/esm/chips/Chips.d.ts +2 -2
  178. package/esm/copybutton/CopyButton.d.ts +14 -12
  179. package/esm/copybutton/CopyButton.js.map +1 -1
  180. package/esm/date/datepicker/types.d.ts +7 -6
  181. package/esm/date/hooks/useDatepicker.d.ts +4 -3
  182. package/esm/date/hooks/useDatepicker.js.map +1 -1
  183. package/esm/date/hooks/useMonthPicker.d.ts +4 -3
  184. package/esm/date/hooks/useMonthPicker.js.map +1 -1
  185. package/esm/date/monthpicker/types.d.ts +2 -1
  186. package/esm/date/parts/DateInput.d.ts +1 -1
  187. package/esm/expansion-card/ExpansionCardTitle.d.ts +1 -1
  188. package/esm/form/checkbox/CheckboxGroup.d.ts +3 -3
  189. package/esm/form/checkbox/types.d.ts +4 -4
  190. package/esm/form/combobox/ComboboxProvider.js +5 -1
  191. package/esm/form/combobox/ComboboxProvider.js.map +1 -1
  192. package/esm/form/combobox/FilteredOptions/FilteredOptions.js +14 -12
  193. package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  194. package/esm/form/combobox/FilteredOptions/filtered-options-util.d.ts +3 -3
  195. package/esm/form/combobox/FilteredOptions/filtered-options-util.js +1 -3
  196. package/esm/form/combobox/FilteredOptions/filtered-options-util.js.map +1 -1
  197. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.d.ts +8 -5
  198. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js +8 -13
  199. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
  200. package/esm/form/combobox/Input/Input.js +9 -7
  201. package/esm/form/combobox/Input/Input.js.map +1 -1
  202. package/esm/form/combobox/SelectedOptions/SelectedOptions.d.ts +2 -1
  203. package/esm/form/combobox/SelectedOptions/SelectedOptions.js +3 -3
  204. package/esm/form/combobox/SelectedOptions/SelectedOptions.js.map +1 -1
  205. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.d.ts +10 -7
  206. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js +6 -8
  207. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js.map +1 -1
  208. package/esm/form/combobox/combobox-utils.d.ts +10 -0
  209. package/esm/form/combobox/combobox-utils.js +22 -0
  210. package/esm/form/combobox/combobox-utils.js.map +1 -0
  211. package/esm/form/combobox/customOptionsContext.d.ts +5 -4
  212. package/esm/form/combobox/customOptionsContext.js +1 -1
  213. package/esm/form/combobox/customOptionsContext.js.map +1 -1
  214. package/esm/form/combobox/types.d.ts +33 -22
  215. package/esm/form/error-summary/ErrorSummary.d.ts +5 -5
  216. package/esm/form/file-upload/FileUpload.context.d.ts +8 -0
  217. package/esm/form/file-upload/FileUpload.context.js +3 -0
  218. package/esm/form/file-upload/FileUpload.context.js.map +1 -0
  219. package/esm/form/file-upload/FileUpload.d.ts +118 -0
  220. package/esm/form/file-upload/FileUpload.js +44 -0
  221. package/esm/form/file-upload/FileUpload.js.map +1 -0
  222. package/esm/form/file-upload/FileUpload.types.d.ts +55 -0
  223. package/esm/form/file-upload/FileUpload.types.js +5 -0
  224. package/esm/form/file-upload/FileUpload.types.js.map +1 -0
  225. package/esm/form/file-upload/i18n/get.d.ts +2 -0
  226. package/esm/form/file-upload/i18n/get.js +34 -0
  227. package/esm/form/file-upload/i18n/get.js.map +1 -0
  228. package/esm/form/file-upload/i18n/i18n.context.d.ts +11 -0
  229. package/esm/form/file-upload/i18n/i18n.context.js +32 -0
  230. package/esm/form/file-upload/i18n/i18n.context.js.map +1 -0
  231. package/esm/form/file-upload/i18n/i18n.types.d.ts +13 -0
  232. package/esm/form/file-upload/i18n/i18n.types.js +2 -0
  233. package/esm/form/file-upload/i18n/i18n.types.js.map +1 -0
  234. package/esm/form/file-upload/i18n/locales/nb.json +20 -0
  235. package/esm/form/file-upload/i18n/merge.d.ts +2 -0
  236. package/esm/form/file-upload/i18n/merge.js +25 -0
  237. package/esm/form/file-upload/i18n/merge.js.map +1 -0
  238. package/esm/form/file-upload/index.d.ts +7 -0
  239. package/esm/form/file-upload/index.js +6 -0
  240. package/esm/form/file-upload/index.js.map +1 -0
  241. package/esm/form/file-upload/parts/Trigger.d.ts +7 -0
  242. package/esm/form/file-upload/parts/Trigger.js +18 -0
  243. package/esm/form/file-upload/parts/Trigger.js.map +1 -0
  244. package/esm/form/file-upload/parts/dropzone/Dropzone.d.ts +4 -0
  245. package/esm/form/file-upload/parts/dropzone/Dropzone.js +78 -0
  246. package/esm/form/file-upload/parts/dropzone/Dropzone.js.map +1 -0
  247. package/esm/form/file-upload/parts/dropzone/dropzone.types.d.ts +18 -0
  248. package/esm/form/file-upload/parts/dropzone/dropzone.types.js +2 -0
  249. package/esm/form/file-upload/parts/dropzone/dropzone.types.js.map +1 -0
  250. package/esm/form/file-upload/parts/dropzone/useDropzone.d.ts +13 -0
  251. package/esm/form/file-upload/parts/dropzone/useDropzone.js +30 -0
  252. package/esm/form/file-upload/parts/dropzone/useDropzone.js.map +1 -0
  253. package/esm/form/file-upload/parts/item/Item.d.ts +55 -0
  254. package/esm/form/file-upload/parts/item/Item.js +50 -0
  255. package/esm/form/file-upload/parts/item/Item.js.map +1 -0
  256. package/esm/form/file-upload/parts/item/Item.types.d.ts +5 -0
  257. package/esm/form/file-upload/parts/item/Item.types.js +2 -0
  258. package/esm/form/file-upload/parts/item/Item.types.js.map +1 -0
  259. package/esm/form/file-upload/parts/item/ItemButton.d.ts +12 -0
  260. package/esm/form/file-upload/parts/item/ItemButton.js +17 -0
  261. package/esm/form/file-upload/parts/item/ItemButton.js.map +1 -0
  262. package/esm/form/file-upload/parts/item/ItemIcon.d.ts +9 -0
  263. package/esm/form/file-upload/parts/item/ItemIcon.js +46 -0
  264. package/esm/form/file-upload/parts/item/ItemIcon.js.map +1 -0
  265. package/esm/form/file-upload/parts/item/ItemName.d.ts +9 -0
  266. package/esm/form/file-upload/parts/item/ItemName.js +27 -0
  267. package/esm/form/file-upload/parts/item/ItemName.js.map +1 -0
  268. package/esm/form/file-upload/parts/item/utils/download-file.d.ts +1 -0
  269. package/esm/form/file-upload/parts/item/utils/download-file.js +9 -0
  270. package/esm/form/file-upload/parts/item/utils/download-file.js.map +1 -0
  271. package/esm/form/file-upload/parts/item/utils/file-type-checker.d.ts +2 -0
  272. package/esm/form/file-upload/parts/item/utils/file-type-checker.js +2 -0
  273. package/esm/form/file-upload/parts/item/utils/file-type-checker.js.map +1 -0
  274. package/esm/form/file-upload/parts/item/utils/format-file-size.d.ts +2 -0
  275. package/esm/form/file-upload/parts/item/utils/format-file-size.js +20 -0
  276. package/esm/form/file-upload/parts/item/utils/format-file-size.js.map +1 -0
  277. package/esm/form/file-upload/useFileUpload.d.ts +12 -0
  278. package/esm/form/file-upload/useFileUpload.js +29 -0
  279. package/esm/form/file-upload/useFileUpload.js.map +1 -0
  280. package/esm/form/file-upload/utils/is-accepted-file-type.d.ts +1 -0
  281. package/esm/form/file-upload/utils/is-accepted-file-type.js +22 -0
  282. package/esm/form/file-upload/utils/is-accepted-file-type.js.map +1 -0
  283. package/esm/form/file-upload/utils/is-accepted-size.d.ts +1 -0
  284. package/esm/form/file-upload/utils/is-accepted-size.js +7 -0
  285. package/esm/form/file-upload/utils/is-accepted-size.js.map +1 -0
  286. package/esm/form/file-upload/utils/validate-files.d.ts +8 -0
  287. package/esm/form/file-upload/utils/validate-files.js +44 -0
  288. package/esm/form/file-upload/utils/validate-files.js.map +1 -0
  289. package/esm/form/radio/RadioGroup.d.ts +1 -1
  290. package/esm/form/search/Search.d.ts +3 -2
  291. package/esm/form/search/Search.js.map +1 -1
  292. package/esm/form/select/Select.d.ts +9 -9
  293. package/esm/form/switch/Switch.d.ts +5 -5
  294. package/esm/form/textarea/Textarea.d.ts +5 -4
  295. package/esm/form/textarea/Textarea.js.map +1 -1
  296. package/esm/form/useFormField.d.ts +10 -8
  297. package/esm/form/useFormField.js.map +1 -1
  298. package/esm/index.d.ts +1 -0
  299. package/esm/index.js +1 -0
  300. package/esm/index.js.map +1 -1
  301. package/esm/layout/bleed/Bleed.d.ts +9 -1
  302. package/esm/layout/bleed/Bleed.js.map +1 -1
  303. package/esm/layout/box/Box.d.ts +5 -5
  304. package/esm/layout/grid/HGrid.d.ts +1 -3
  305. package/esm/layout/grid/HGrid.js.map +1 -1
  306. package/esm/layout/page/Page.d.ts +2 -2
  307. package/esm/layout/responsive/Responsive.d.ts +2 -4
  308. package/esm/layout/responsive/Responsive.js.map +1 -1
  309. package/esm/layout/stack/Stack.d.ts +18 -4
  310. package/esm/layout/stack/Stack.js.map +1 -1
  311. package/esm/list/types.d.ts +1 -1
  312. package/esm/loader/Loader.d.ts +0 -6
  313. package/esm/loader/Loader.js.map +1 -1
  314. package/esm/modal/Modal.js +2 -2
  315. package/esm/modal/types.d.ts +8 -8
  316. package/esm/pagination/Pagination.d.ts +3 -2
  317. package/esm/pagination/Pagination.js.map +1 -1
  318. package/esm/pagination/PaginationItem.d.ts +1 -1
  319. package/esm/popover/Popover.d.ts +2 -1
  320. package/esm/popover/Popover.js.map +1 -1
  321. package/esm/read-more/ReadMore.d.ts +7 -7
  322. package/esm/stepper/Step.d.ts +4 -4
  323. package/esm/stepper/Stepper.d.ts +7 -5
  324. package/esm/stepper/Stepper.js.map +1 -1
  325. package/esm/table/AnimateHeight.d.ts +1 -1
  326. package/esm/table/DataCell.d.ts +1 -3
  327. package/esm/table/DataCell.js.map +1 -1
  328. package/esm/tabs/TabList.d.ts +1 -1
  329. package/esm/tabs/Tabs.d.ts +7 -7
  330. package/esm/timeline/Timeline.d.ts +6 -3
  331. package/esm/timeline/Timeline.js +2 -1
  332. package/esm/timeline/Timeline.js.map +1 -1
  333. package/esm/timeline/period/index.d.ts +3 -2
  334. package/esm/timeline/period/index.js.map +1 -1
  335. package/esm/tooltip/Tooltip.d.ts +19 -12
  336. package/esm/tooltip/Tooltip.js.map +1 -1
  337. package/esm/util/create-context.d.ts +1 -0
  338. package/esm/util/create-context.js.map +1 -1
  339. package/esm/util/hooks/useEventListener.d.ts +1 -1
  340. package/esm/util/hooks/useMergeRefs.d.ts +1 -1
  341. package/package.json +13 -3
  342. package/src/accordion/Accordion.tsx +4 -4
  343. package/src/accordion/accordion.stories.tsx +0 -2
  344. package/src/alert/Alert.tsx +12 -10
  345. package/src/button/Button.tsx +9 -8
  346. package/src/chat/Chat.tsx +6 -6
  347. package/src/chips/Chips.tsx +2 -2
  348. package/src/copybutton/CopyButton.tsx +14 -12
  349. package/src/date/datepicker/datepicker.stories.tsx +0 -1
  350. package/src/date/datepicker/types.ts +7 -6
  351. package/src/date/hooks/useDatepicker.tsx +4 -3
  352. package/src/date/hooks/useMonthPicker.tsx +4 -3
  353. package/src/date/monthpicker/types.ts +2 -1
  354. package/src/date/parts/DateInput.tsx +1 -1
  355. package/src/expansion-card/ExpansionCardTitle.tsx +1 -1
  356. package/src/form/checkbox/CheckboxGroup.tsx +3 -3
  357. package/src/form/checkbox/types.ts +4 -4
  358. package/src/form/combobox/ComboboxProvider.tsx +7 -3
  359. package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +22 -15
  360. package/src/form/combobox/FilteredOptions/filtered-options-util.ts +5 -10
  361. package/src/form/combobox/FilteredOptions/filteredOptionsContext.tsx +19 -29
  362. package/src/form/combobox/Input/Input.tsx +14 -8
  363. package/src/form/combobox/SelectedOptions/SelectedOptions.tsx +8 -5
  364. package/src/form/combobox/SelectedOptions/selectedOptionsContext.tsx +24 -25
  365. package/src/form/combobox/combobox-utils.test.ts +67 -0
  366. package/src/form/combobox/combobox-utils.ts +32 -0
  367. package/src/form/combobox/combobox.stories.tsx +67 -32
  368. package/src/form/combobox/combobox.test.tsx +32 -1
  369. package/src/form/combobox/customOptionsContext.tsx +9 -8
  370. package/src/form/combobox/types.ts +34 -22
  371. package/src/form/error-summary/ErrorSummary.tsx +5 -5
  372. package/src/form/file-upload/FileUpload.context.tsx +9 -0
  373. package/src/form/file-upload/FileUpload.tsx +142 -0
  374. package/src/form/file-upload/FileUpload.types.ts +57 -0
  375. package/src/form/file-upload/file-upload-dropzone.stories.tsx +123 -0
  376. package/src/form/file-upload/file-upload-item.stories.tsx +136 -0
  377. package/src/form/file-upload/file-upload.stories.tsx +236 -0
  378. package/src/form/file-upload/i18n/get.ts +48 -0
  379. package/src/form/file-upload/i18n/i18n.context.test.tsx +92 -0
  380. package/src/form/file-upload/i18n/i18n.context.ts +67 -0
  381. package/src/form/file-upload/i18n/i18n.types.ts +20 -0
  382. package/src/form/file-upload/i18n/locales/nb.json +20 -0
  383. package/src/form/file-upload/i18n/merge.ts +35 -0
  384. package/src/form/file-upload/index.ts +21 -0
  385. package/src/form/file-upload/parts/Trigger.tsx +48 -0
  386. package/src/form/file-upload/parts/dropzone/Dropzone.tsx +180 -0
  387. package/src/form/file-upload/parts/dropzone/dropzone.types.ts +22 -0
  388. package/src/form/file-upload/parts/dropzone/useDropzone.ts +43 -0
  389. package/src/form/file-upload/parts/item/Item.tsx +165 -0
  390. package/src/form/file-upload/parts/item/Item.types.ts +6 -0
  391. package/src/form/file-upload/parts/item/ItemButton.tsx +52 -0
  392. package/src/form/file-upload/parts/item/ItemIcon.tsx +74 -0
  393. package/src/form/file-upload/parts/item/ItemName.tsx +58 -0
  394. package/src/form/file-upload/parts/item/utils/download-file.ts +9 -0
  395. package/src/form/file-upload/parts/item/utils/file-type-checker.ts +4 -0
  396. package/src/form/file-upload/parts/item/utils/format-file-size.test.ts +76 -0
  397. package/src/form/file-upload/parts/item/utils/format-file-size.ts +25 -0
  398. package/src/form/file-upload/useFileUpload.ts +54 -0
  399. package/src/form/file-upload/utils/is-accepted-file-type.test.ts +69 -0
  400. package/src/form/file-upload/utils/is-accepted-file-type.ts +25 -0
  401. package/src/form/file-upload/utils/is-accepted-size.test.ts +26 -0
  402. package/src/form/file-upload/utils/is-accepted-size.ts +7 -0
  403. package/src/form/file-upload/utils/validate-files.test.ts +132 -0
  404. package/src/form/file-upload/utils/validate-files.ts +62 -0
  405. package/src/form/radio/RadioGroup.tsx +1 -1
  406. package/src/form/search/Search.tsx +3 -2
  407. package/src/form/select/Select.tsx +9 -9
  408. package/src/form/select/select.stories.tsx +32 -37
  409. package/src/form/switch/Switch.tsx +5 -5
  410. package/src/form/textarea/Textarea.tsx +5 -4
  411. package/src/form/useFormField.ts +10 -8
  412. package/src/index.ts +14 -0
  413. package/src/internal-header/header.stories.tsx +8 -5
  414. package/src/layout/bleed/Bleed.tsx +9 -1
  415. package/src/layout/box/Box.tsx +5 -5
  416. package/src/layout/grid/HGrid.tsx +1 -3
  417. package/src/layout/page/Page.tsx +2 -2
  418. package/src/layout/responsive/Responsive.tsx +2 -4
  419. package/src/layout/stack/Stack.tsx +18 -4
  420. package/src/list/types.ts +1 -1
  421. package/src/loader/Loader.tsx +0 -6
  422. package/src/modal/Modal.tsx +2 -2
  423. package/src/modal/types.ts +8 -8
  424. package/src/pagination/Pagination.tsx +3 -2
  425. package/src/pagination/PaginationItem.tsx +1 -1
  426. package/src/popover/Popover.tsx +2 -1
  427. package/src/read-more/ReadMore.tsx +7 -7
  428. package/src/stepper/Step.tsx +4 -4
  429. package/src/stepper/Stepper.tsx +7 -5
  430. package/src/table/AnimateHeight.tsx +1 -1
  431. package/src/table/DataCell.tsx +1 -6
  432. package/src/tabs/TabList.tsx +1 -1
  433. package/src/tabs/Tabs.tsx +7 -7
  434. package/src/timeline/Timeline.tsx +6 -3
  435. package/src/timeline/period/index.tsx +3 -2
  436. package/src/tooltip/Tooltip.tsx +19 -12
  437. package/src/util/create-context.tsx +1 -0
  438. package/src/util/hooks/useMergeRefs.ts +1 -1
@@ -0,0 +1,165 @@
1
+ import cl from "clsx";
2
+ import React, { MouseEvent, forwardRef } from "react";
3
+ import { ExclamationmarkTriangleIcon } from "@navikt/aksel-icons";
4
+ import { BodyShort } from "../../../../typography";
5
+ import { OverridableComponent } from "../../../../util";
6
+ import { useFileUploadTranslation } from "../../FileUpload.context";
7
+ import { useI18n } from "../../i18n/i18n.context";
8
+ import { ComponentTranslation } from "../../i18n/i18n.types";
9
+ import { FileItem } from "./Item.types";
10
+ import ItemButton from "./ItemButton";
11
+ import ItemIcon from "./ItemIcon";
12
+ import ItemName from "./ItemName";
13
+ import { formatFileSize } from "./utils/format-file-size";
14
+
15
+ export interface FileItemBaseProps {
16
+ /**
17
+ * Overrides html-tag
18
+ * @default "div"
19
+ */
20
+ as?: "div" | "li";
21
+ /**
22
+ * Either a native File or file metadata.
23
+ */
24
+ file: FileItem;
25
+ /**
26
+ * onClick on the file name.
27
+ * @note If this and `href` is not set and the `file` prop is a native file, onClick will download the file.
28
+ */
29
+ onFileClick?: (event: MouseEvent<HTMLAnchorElement>) => void;
30
+ /**
31
+ * href on the file name.
32
+ * @note If this and `onFileClick` is not set and the `file` prop is a native file, onClick will download the file.
33
+ */
34
+ href?: string;
35
+ /**
36
+ * Error message relating to the item.
37
+ */
38
+ error?: string;
39
+ /**
40
+ * Status "downloading" and "uploading" displays a loading indicator.
41
+ * @default "idle"
42
+ */
43
+ status?: "downloading" | "uploading" | "idle";
44
+ /**
45
+ * i18n-API for customizing texts and labels
46
+ */
47
+ translations?: ComponentTranslation<"FileUpload">["item"];
48
+ onRetry?: (event: MouseEvent<HTMLButtonElement>) => void;
49
+ onDelete?: (event: MouseEvent<HTMLButtonElement>) => void;
50
+ }
51
+
52
+ type FileItemActionDelete = {
53
+ onDelete: (event: MouseEvent<HTMLButtonElement>) => void;
54
+ itemAction: "delete";
55
+ };
56
+
57
+ type FileItemActionRetry = {
58
+ onRetry: (event: MouseEvent<HTMLButtonElement>) => void;
59
+ itemAction: "retry";
60
+ };
61
+
62
+ type FileItemActionNone = {
63
+ itemAction?: "none";
64
+ };
65
+
66
+ type FileItemConditionalProps =
67
+ | FileItemActionDelete
68
+ | FileItemActionRetry
69
+ | FileItemActionNone;
70
+
71
+ export type FileUploadItemProps = FileItemBaseProps &
72
+ FileItemConditionalProps &
73
+ React.HTMLAttributes<HTMLDivElement>;
74
+
75
+ export const Item: OverridableComponent<FileUploadItemProps, HTMLDivElement> =
76
+ forwardRef(
77
+ (
78
+ {
79
+ as: Component = "div",
80
+ file,
81
+ status = "idle",
82
+ onDelete,
83
+ onRetry,
84
+ error,
85
+ className,
86
+ href,
87
+ onFileClick,
88
+ itemAction = "delete",
89
+ translations,
90
+ ...rest
91
+ },
92
+ ref,
93
+ ) => {
94
+ const context = useFileUploadTranslation(false);
95
+ const translate = useI18n(
96
+ "FileUpload",
97
+ { item: translations },
98
+ context?.translations,
99
+ );
100
+
101
+ const showError = !!error && status === "idle";
102
+
103
+ function getStatusText() {
104
+ if (status === "uploading") {
105
+ return translate("item.uploading");
106
+ }
107
+ if (status === "downloading") {
108
+ return translate("item.downloading");
109
+ }
110
+ return formatFileSize(file);
111
+ }
112
+
113
+ return (
114
+ <Component
115
+ ref={ref}
116
+ {...rest}
117
+ className={cl("navds-file-item", className, {
118
+ "navds-file-item--error": showError,
119
+ })}
120
+ >
121
+ <div className="navds-file-item__inner">
122
+ <ItemIcon
123
+ isLoading={status !== "idle"}
124
+ file={file}
125
+ showError={showError}
126
+ />
127
+ <div className="navds-file-item__file-info">
128
+ <ItemName file={file} href={href} onClick={onFileClick} />
129
+ <BodyShort as="div" size="small">
130
+ {getStatusText()}
131
+ </BodyShort>
132
+ <div
133
+ className="navds-file-item__error"
134
+ aria-relevant="additions removals"
135
+ aria-live="polite"
136
+ >
137
+ {showError && (
138
+ <BodyShort
139
+ size="small"
140
+ className="navds-file-item__error-content"
141
+ >
142
+ <ExclamationmarkTriangleIcon aria-hidden />
143
+ {error}
144
+ </BodyShort>
145
+ )}
146
+ </div>
147
+ </div>
148
+
149
+ {status === "idle" && (
150
+ <ItemButton
151
+ file={file}
152
+ onRetry={onRetry}
153
+ onDelete={onDelete}
154
+ action={itemAction}
155
+ retryTitle={translate("item.retryButtonTitle")}
156
+ deleteTitle={translate("item.deleteButtonTitle")}
157
+ />
158
+ )}
159
+ </div>
160
+ </Component>
161
+ );
162
+ },
163
+ );
164
+
165
+ export default Item;
@@ -0,0 +1,6 @@
1
+ export interface FileMetadata {
2
+ name: string;
3
+ size?: number;
4
+ }
5
+
6
+ export type FileItem = FileMetadata | File;
@@ -0,0 +1,52 @@
1
+ import React from "react";
2
+ import { ArrowsCirclepathIcon, TrashIcon } from "@navikt/aksel-icons";
3
+ import { Button } from "../../../../button";
4
+ import { FileItem } from "./Item.types";
5
+
6
+ interface Props {
7
+ file: FileItem;
8
+ onRetry?: (event: React.MouseEvent<HTMLButtonElement>) => void;
9
+ onDelete?: (event: React.MouseEvent<HTMLButtonElement>) => void;
10
+ action: "delete" | "retry" | "none";
11
+ retryTitle: string;
12
+ deleteTitle: string;
13
+ }
14
+
15
+ const ItemButton = ({
16
+ onRetry,
17
+ onDelete,
18
+ action,
19
+ retryTitle,
20
+ deleteTitle,
21
+ }: Props) => {
22
+ if (action === "none") {
23
+ return null;
24
+ }
25
+
26
+ if (onRetry && action === "retry") {
27
+ return (
28
+ <Button
29
+ className="navds-file-item__button"
30
+ type="button"
31
+ variant="tertiary-neutral"
32
+ onClick={onRetry}
33
+ icon={<ArrowsCirclepathIcon title={retryTitle} />}
34
+ />
35
+ );
36
+ }
37
+ if (onDelete && action === "delete") {
38
+ return (
39
+ <Button
40
+ className="navds-file-item__button"
41
+ type="button"
42
+ variant="tertiary-neutral"
43
+ onClick={onDelete}
44
+ icon={<TrashIcon title={deleteTitle} />}
45
+ />
46
+ );
47
+ }
48
+
49
+ return null;
50
+ };
51
+
52
+ export default ItemButton;
@@ -0,0 +1,74 @@
1
+ import React from "react";
2
+ import {
3
+ FileCsvIcon,
4
+ FileExcelIcon,
5
+ FileIcon,
6
+ FileImageIcon,
7
+ FilePdfIcon,
8
+ FileTextIcon,
9
+ FileWordIcon,
10
+ FileXMarkIcon,
11
+ } from "@navikt/aksel-icons";
12
+ import { Loader } from "../../../../loader";
13
+ import { FileItem } from "./Item.types";
14
+
15
+ interface ItemIconProps {
16
+ isLoading?: boolean;
17
+ file: FileItem;
18
+ showError: boolean;
19
+ }
20
+
21
+ const iconProps = {
22
+ fontSize: "2rem",
23
+ "aria-hidden": true,
24
+ };
25
+
26
+ function ItemIcon({ isLoading, file, showError }: ItemIconProps) {
27
+ if (isLoading) {
28
+ return (
29
+ <div className="navds-file-item__icon navds-file-item__icon--loading">
30
+ <Loader size="large" />
31
+ </div>
32
+ );
33
+ } else if (showError) {
34
+ return (
35
+ <div className="navds-file-item__icon">
36
+ <FileXMarkIcon {...iconProps} />
37
+ </div>
38
+ );
39
+ }
40
+ return (
41
+ <div className="navds-file-item__icon">
42
+ <Icon file={file} />
43
+ </div>
44
+ );
45
+ }
46
+
47
+ function Icon({ file }: { file: FileItem }) {
48
+ const extension = file.name.substring(file.name.lastIndexOf(".") + 1);
49
+
50
+ switch (extension) {
51
+ case "jpg":
52
+ case "jpeg":
53
+ case "png":
54
+ case "gif":
55
+ case "webp":
56
+ return <FileImageIcon {...iconProps} />;
57
+ case "pdf":
58
+ return <FilePdfIcon {...iconProps} />;
59
+ case "txt":
60
+ return <FileTextIcon {...iconProps} />;
61
+ case "csv":
62
+ return <FileCsvIcon {...iconProps} />;
63
+ case "xls":
64
+ case "xlsx":
65
+ return <FileExcelIcon {...iconProps} />;
66
+ case "doc":
67
+ case "docx":
68
+ return <FileWordIcon {...iconProps} />;
69
+ default:
70
+ return <FileIcon {...iconProps} />;
71
+ }
72
+ }
73
+
74
+ export default ItemIcon;
@@ -0,0 +1,58 @@
1
+ import React from "react";
2
+ import { Link } from "../../../../link";
3
+ import { FileItem } from "./Item.types";
4
+ import { downloadFile } from "./utils/download-file";
5
+ import { isNativeFile } from "./utils/file-type-checker";
6
+
7
+ interface Props {
8
+ file: FileItem;
9
+ href?: string;
10
+ onClick?: (event: React.MouseEvent<HTMLAnchorElement>) => void;
11
+ }
12
+
13
+ const ItemName = ({ file, href, onClick }: Props) => {
14
+ if (onClick && href) {
15
+ return (
16
+ <Link href={href} onClick={onClick}>
17
+ {file.name}
18
+ </Link>
19
+ );
20
+ }
21
+
22
+ if (onClick) {
23
+ return (
24
+ <Link
25
+ href="#"
26
+ onClick={(event) => {
27
+ event.preventDefault();
28
+ onClick(event);
29
+ }}
30
+ >
31
+ {file.name}
32
+ </Link>
33
+ );
34
+ }
35
+
36
+ if (href) {
37
+ return <Link href={href}>{file.name}</Link>;
38
+ }
39
+
40
+ if (isNativeFile(file)) {
41
+ return (
42
+ <Link
43
+ href="#"
44
+ download={file.name}
45
+ onClick={(event) => {
46
+ event.preventDefault();
47
+ downloadFile(file);
48
+ }}
49
+ >
50
+ {file.name}
51
+ </Link>
52
+ );
53
+ }
54
+
55
+ return <span>{file.name}</span>;
56
+ };
57
+
58
+ export default ItemName;
@@ -0,0 +1,9 @@
1
+ export const downloadFile = (file: File): void => {
2
+ const a = document.createElement("a");
3
+ const url = URL.createObjectURL(file);
4
+ a.href = url;
5
+ a.download = file.name;
6
+ a.click();
7
+
8
+ URL.revokeObjectURL(url);
9
+ };
@@ -0,0 +1,4 @@
1
+ import { FileItem } from "../Item.types";
2
+
3
+ export const isNativeFile = (fileItem: FileItem): fileItem is File =>
4
+ "lastModified" in fileItem;
@@ -0,0 +1,76 @@
1
+ import { formatFileSize } from "./format-file-size";
2
+
3
+ describe("format-file-size", () => {
4
+ describe("with native File", () => {
5
+ it('returns "0,01 MB" when file size is less than 0,01 MB', () => {
6
+ const file = new File(["abc"], "file.txt");
7
+
8
+ expect(formatFileSize(file)).toBe("0,01 MB");
9
+ });
10
+
11
+ it('returns "0,29 MB" when file size is 0,29 MB', () => {
12
+ const file = new File(["abc".repeat(99_999)], "file.txt");
13
+
14
+ expect(formatFileSize(file)).toBe("0,29 MB");
15
+ });
16
+
17
+ it('returns "> 500 MB" when file size is more than 500 MB', () => {
18
+ const file = createLargeMockFile(600_000_000);
19
+
20
+ expect(formatFileSize(file)).toBe("> 500 MB");
21
+ });
22
+ });
23
+
24
+ describe("with MetadataFile", () => {
25
+ it("returns null when file size is undefined", () => {
26
+ const file = {
27
+ name: "myfile.txt",
28
+ };
29
+
30
+ expect(formatFileSize(file)).toBeNull();
31
+ });
32
+ it('returns "0,01 MB" when file size is less than 0,01 MB', () => {
33
+ const file = {
34
+ name: "myfile.txt",
35
+ size: 1,
36
+ };
37
+
38
+ expect(formatFileSize(file)).toBe("0,01 MB");
39
+ });
40
+
41
+ it('returns "0,96 MB" when file size is 0,96 MB', () => {
42
+ const file = {
43
+ name: "myfile.txt",
44
+ size: 999_999,
45
+ };
46
+
47
+ expect(formatFileSize(file)).toBe("0,96 MB");
48
+ });
49
+
50
+ it('returns "> 500 MB" when file size is more than 500 MB', () => {
51
+ const file = {
52
+ name: "myfile.txt",
53
+ size: 600_000_000,
54
+ };
55
+
56
+ expect(formatFileSize(file)).toBe("> 500 MB");
57
+ });
58
+ });
59
+ });
60
+
61
+ function createLargeMockFile(sizeInBytes: number): File {
62
+ const chunkSize = 1024 * 1024; // 1MB chunk size
63
+ const chunks: Uint8Array[] = [];
64
+
65
+ for (let i = 0; i < sizeInBytes; i += chunkSize) {
66
+ const size = Math.min(chunkSize, sizeInBytes - i);
67
+ const chunk = new Uint8Array(size);
68
+ chunk.fill("a".charCodeAt(0));
69
+ chunks.push(chunk);
70
+ }
71
+
72
+ const blob = new Blob(chunks, { type: "text/plain" });
73
+ return new File([blob], "largeMockFile.txt", {
74
+ type: "text/plain",
75
+ });
76
+ }
@@ -0,0 +1,25 @@
1
+ import { FileItem } from "../Item.types";
2
+
3
+ const MAX_MEGA_BYTES = 500;
4
+
5
+ export function formatFileSize(file: FileItem): string | null {
6
+ if (!file.size) {
7
+ return null;
8
+ }
9
+ const megaBytes = file.size / (1024 * 1024);
10
+
11
+ if (megaBytes <= MAX_MEGA_BYTES) {
12
+ return formatter.format(megaBytes);
13
+ }
14
+
15
+ return `> ${MAX_MEGA_BYTES} MB`;
16
+ }
17
+
18
+ const formatter = new Intl.NumberFormat("nb-NO", {
19
+ style: "unit",
20
+ unit: "megabyte",
21
+ minimumFractionDigits: 2,
22
+ maximumFractionDigits: 2,
23
+ // @ts-expect-error - Looks like roundingMode hasn't been added to TypeScript yet
24
+ roundingMode: "ceil",
25
+ });
@@ -0,0 +1,54 @@
1
+ import { useRef } from "react";
2
+ import { useMergeRefs } from "../../util/hooks";
3
+ import { FileUploadBaseProps } from "./FileUpload.types";
4
+ import { validateFiles } from "./utils/validate-files";
5
+
6
+ export interface UseFileUploadProps
7
+ extends Omit<FileUploadBaseProps, "fileLimit"> {
8
+ ref: React.ForwardedRef<HTMLInputElement>;
9
+ disabled?: boolean;
10
+ }
11
+
12
+ export const useFileUpload = ({
13
+ ref,
14
+ accept,
15
+ onSelect,
16
+ validator,
17
+ maxSizeInBytes,
18
+ disabled,
19
+ }: UseFileUploadProps) => {
20
+ const inputRef = useRef<HTMLInputElement | null>(null);
21
+ const mergedRef = useMergeRefs(inputRef, ref);
22
+
23
+ const upload = (fileList: FileList) => {
24
+ const { files, partitionedFiles } = validateFiles(
25
+ Array.from(fileList),
26
+ accept,
27
+ validator,
28
+ maxSizeInBytes,
29
+ );
30
+
31
+ onSelect(files, partitionedFiles);
32
+ };
33
+
34
+ const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
35
+ const fileList = event.target.files;
36
+ if (!fileList) {
37
+ return;
38
+ }
39
+
40
+ if (!disabled) {
41
+ upload(fileList);
42
+ }
43
+
44
+ // Resets the value to make it is possible to upload the same file several consecutive times
45
+ event.target.value = "";
46
+ };
47
+
48
+ return {
49
+ upload,
50
+ onChange,
51
+ inputRef,
52
+ mergedRef,
53
+ };
54
+ };
@@ -0,0 +1,69 @@
1
+ import { isAcceptedFileType } from "./is-accepted-file-type";
2
+
3
+ const txtFile = () =>
4
+ new File(["foo"], "foo.txt", {
5
+ type: "text/plain",
6
+ });
7
+
8
+ describe("isAcceptedFileType", () => {
9
+ test("returns true when accept is undefined", () => {
10
+ expect(isAcceptedFileType(txtFile(), undefined)).toBe(true);
11
+ });
12
+
13
+ test("returns true when accept is empty string", () => {
14
+ expect(isAcceptedFileType(txtFile(), "")).toBe(true);
15
+ });
16
+
17
+ test("returns true when file matches accepted extensions", () => {
18
+ expect(isAcceptedFileType(txtFile(), ".txt, .pdf")).toBe(true);
19
+ });
20
+
21
+ test("works as expected when there is no space after comma in accept", () => {
22
+ expect(isAcceptedFileType(txtFile(), ".txt,.pdf")).toBe(true);
23
+ });
24
+
25
+ test("returns false when file does not match the accepted extensions", () => {
26
+ expect(isAcceptedFileType(txtFile(), ".xlsx, .pdf")).toBe(false);
27
+ });
28
+
29
+ test("returns true when file matches the accepted exact mime types", () => {
30
+ expect(isAcceptedFileType(txtFile(), "application/pdf, text/plain")).toBe(
31
+ true,
32
+ );
33
+ });
34
+
35
+ test("returns false when file does not the accepted exact mime types", () => {
36
+ expect(isAcceptedFileType(txtFile(), "application/pdf, text/csv")).toBe(
37
+ false,
38
+ );
39
+ });
40
+
41
+ test("returns true when file matches the accepted wildcard mime types", () => {
42
+ expect(isAcceptedFileType(txtFile(), "application/*, text/*")).toBe(true);
43
+ });
44
+
45
+ test("returns false when file does not the accepted wildcard mime types", () => {
46
+ expect(isAcceptedFileType(txtFile(), "application/*, image/*")).toBe(false);
47
+ });
48
+
49
+ test("returns true if matches extension, but not exact or wildcard mime type", () => {
50
+ expect(
51
+ isAcceptedFileType(txtFile(), "application/*, image/*, .txt, audio/mpeg"),
52
+ ).toBe(true);
53
+ });
54
+
55
+ test("returns true if matches exact mime type, but not extension or wildcard mime type", () => {
56
+ expect(
57
+ isAcceptedFileType(
58
+ txtFile(),
59
+ "application/*, text/plain, .jpg, audio/mpeg",
60
+ ),
61
+ ).toBe(true);
62
+ });
63
+
64
+ test("returns true if matches wildcard mime type, but not extension or exact mime type", () => {
65
+ expect(
66
+ isAcceptedFileType(txtFile(), "application/*, text/*, .jpg, audio/mpeg"),
67
+ ).toBe(true);
68
+ });
69
+ });
@@ -0,0 +1,25 @@
1
+ export function isAcceptedFileType(
2
+ file: File,
3
+ accept: string | undefined,
4
+ ): boolean {
5
+ if (!accept) {
6
+ return true;
7
+ }
8
+ const mimeType = file.type;
9
+ const acceptedTypes = accept.split(",");
10
+
11
+ return acceptedTypes.some((type) => {
12
+ const validType = type.trim();
13
+ const isExtensionType = validType.startsWith(".");
14
+ const isWildcardMimeType = validType.endsWith("/*");
15
+
16
+ if (isExtensionType) {
17
+ return file.name.toLowerCase().endsWith(validType.toLowerCase());
18
+ } else if (isWildcardMimeType) {
19
+ const baseMimeType = mimeType.replace(/\/.*$/, "");
20
+ const baseValidType = validType.replace(/\/.*$/, "");
21
+ return baseMimeType === baseValidType;
22
+ }
23
+ return mimeType === validType;
24
+ });
25
+ }
@@ -0,0 +1,26 @@
1
+ import { isAcceptedSize } from "./is-accepted-size";
2
+
3
+ describe("isAcceptedSize", () => {
4
+ it("should return true if maxSize is less than or equal to 0", () => {
5
+ const file = new File([new Array(5000).join("a")], "filename.txt", {
6
+ type: "text/plain",
7
+ });
8
+ expect(isAcceptedSize(file, -1)).toBe(true);
9
+ expect(isAcceptedSize(file, 0)).toBe(true);
10
+ });
11
+
12
+ it("should return true if file size is less than or equal to maxSize", () => {
13
+ const file = new File([new Array(5000).join("a")], "filename.txt", {
14
+ type: "text/plain",
15
+ });
16
+ expect(isAcceptedSize(file, 5000)).toBe(true);
17
+ expect(isAcceptedSize(file, 6000)).toBe(true);
18
+ });
19
+
20
+ it("should return false if file size is greater than maxSize", () => {
21
+ const file = new File([new Array(5000).join("a")], "filename.txt", {
22
+ type: "text/plain",
23
+ });
24
+ expect(isAcceptedSize(file, 4000)).toBe(false);
25
+ });
26
+ });
@@ -0,0 +1,7 @@
1
+ export function isAcceptedSize(file: File, maxSize: number = -1): boolean {
2
+ if (maxSize <= 0 || file.size <= maxSize) {
3
+ return true;
4
+ }
5
+
6
+ return false;
7
+ }