@dhis2-ui/select 10.16.1 → 10.16.3-alpha.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 (278) hide show
  1. package/build/cjs/select/select.js +5 -2
  2. package/build/cjs/simple-single-select/use-handle-key-press/use-handle-key-press-on-filter-input.js +2 -2
  3. package/build/es/select/select.js +5 -2
  4. package/build/es/simple-single-select/use-handle-key-press/use-handle-key-press-on-filter-input.js +2 -2
  5. package/package.json +17 -16
  6. package/src/index.js +9 -0
  7. package/src/locales/ar/translations.json +7 -0
  8. package/src/locales/ar_IQ/translations.json +7 -0
  9. package/src/locales/ckb/translations.json +7 -0
  10. package/src/locales/cs/translations.json +7 -0
  11. package/src/locales/da/translations.json +7 -0
  12. package/src/locales/en/translations.json +11 -0
  13. package/src/locales/en_US/translations.json +9 -0
  14. package/src/locales/es/translations.json +7 -0
  15. package/src/locales/es_419/translations.json +7 -0
  16. package/src/locales/fr/translations.json +7 -0
  17. package/src/locales/hi_IN/translations.json +7 -0
  18. package/src/locales/id/translations.json +7 -0
  19. package/src/locales/index.js +80 -0
  20. package/src/locales/km/translations.json +7 -0
  21. package/src/locales/lo/translations.json +7 -0
  22. package/src/locales/my/translations.json +7 -0
  23. package/src/locales/nb/translations.json +7 -0
  24. package/src/locales/nl/translations.json +7 -0
  25. package/src/locales/prs/translations.json +7 -0
  26. package/src/locales/ps/translations.json +7 -0
  27. package/src/locales/pt/translations.json +7 -0
  28. package/src/locales/pt_BR/translations.json +7 -0
  29. package/src/locales/ru/translations.json +7 -0
  30. package/src/locales/sv/translations.json +7 -0
  31. package/src/locales/tet/translations.json +7 -0
  32. package/src/locales/tg/translations.json +7 -0
  33. package/src/locales/uk/translations.json +7 -0
  34. package/src/locales/ur/translations.json +7 -0
  35. package/src/locales/uz_Latn/translations.json +7 -0
  36. package/src/locales/uz_UZ_Cyrl/translations.json +7 -0
  37. package/src/locales/uz_UZ_Latn/translations.json +7 -0
  38. package/src/locales/vi/translations.json +7 -0
  39. package/src/locales/zh/translations.json +9 -0
  40. package/src/locales/zh_CN/translations.json +7 -0
  41. package/src/multi-select/__tests__/multi-select.test.js +23 -0
  42. package/src/multi-select/__tests__/selection-list.test.js +102 -0
  43. package/src/multi-select/features/accepts_blur_cb/index.js +14 -0
  44. package/src/multi-select/features/accepts_blur_cb.feature +8 -0
  45. package/src/multi-select/features/accepts_focus_cb/index.js +14 -0
  46. package/src/multi-select/features/accepts_focus_cb.feature +6 -0
  47. package/src/multi-select/features/accepts_initial_focus/index.js +5 -0
  48. package/src/multi-select/features/accepts_initial_focus.feature +5 -0
  49. package/src/multi-select/features/accepts_loading/index.js +22 -0
  50. package/src/multi-select/features/accepts_loading.feature +14 -0
  51. package/src/multi-select/features/accepts_max_height/index.js +31 -0
  52. package/src/multi-select/features/accepts_max_height.feature +11 -0
  53. package/src/multi-select/features/accepts_placeholder/index.js +25 -0
  54. package/src/multi-select/features/accepts_placeholder.feature +10 -0
  55. package/src/multi-select/features/accepts_prefix/index.js +19 -0
  56. package/src/multi-select/features/accepts_prefix.feature +10 -0
  57. package/src/multi-select/features/allows_invalid_options/index.js +27 -0
  58. package/src/multi-select/features/allows_invalid_options.feature +12 -0
  59. package/src/multi-select/features/allows_selecting/index.js +81 -0
  60. package/src/multi-select/features/allows_selecting.feature +44 -0
  61. package/src/multi-select/features/can_be_cleared/index.js +22 -0
  62. package/src/multi-select/features/can_be_cleared.feature +6 -0
  63. package/src/multi-select/features/can_be_disabled/index.js +36 -0
  64. package/src/multi-select/features/can_be_disabled.feature +32 -0
  65. package/src/multi-select/features/can_be_empty/index.js +28 -0
  66. package/src/multi-select/features/can_be_empty.feature +16 -0
  67. package/src/multi-select/features/can_be_filtered/index.js +23 -0
  68. package/src/multi-select/features/can_be_filtered.feature +14 -0
  69. package/src/multi-select/features/can_be_opened_and_closed/index.js +32 -0
  70. package/src/multi-select/features/can_be_opened_and_closed.feature +41 -0
  71. package/src/multi-select/features/common/index.js +49 -0
  72. package/src/multi-select/features/duplicate_option_values/index.js +21 -0
  73. package/src/multi-select/features/duplicate_option_values.feature +7 -0
  74. package/src/multi-select/features/menu_width_matches_input/index.js +53 -0
  75. package/src/multi-select/features/menu_width_matches_input.feature +11 -0
  76. package/src/multi-select/features/position/index.js +160 -0
  77. package/src/multi-select/features/position.feature +46 -0
  78. package/src/multi-select/features/shows_selections/index.js +18 -0
  79. package/src/multi-select/features/shows_selections.feature +9 -0
  80. package/src/multi-select/filterable-menu.js +43 -0
  81. package/src/multi-select/index.js +1 -0
  82. package/src/multi-select/input.js +129 -0
  83. package/src/multi-select/menu.js +91 -0
  84. package/src/multi-select/multi-select.e2e.stories.js +407 -0
  85. package/src/multi-select/multi-select.js +148 -0
  86. package/src/multi-select/multi-select.prod.stories.js +408 -0
  87. package/src/multi-select/selection-list.js +79 -0
  88. package/src/multi-select-field/__tests__/multi-select-field.test.js +29 -0
  89. package/src/multi-select-field/features/accepts_help_text/index.js +9 -0
  90. package/src/multi-select-field/features/accepts_help_text.feature +5 -0
  91. package/src/multi-select-field/features/accepts_label/index.js +9 -0
  92. package/src/multi-select-field/features/accepts_label.feature +5 -0
  93. package/src/multi-select-field/features/accepts_validation_text/index.js +9 -0
  94. package/src/multi-select-field/features/accepts_validation_text.feature +5 -0
  95. package/src/multi-select-field/features/can_be_required/index.js +11 -0
  96. package/src/multi-select-field/features/can_be_required.feature +5 -0
  97. package/src/multi-select-field/features/has_default_clear_text/index.js +16 -0
  98. package/src/multi-select-field/features/has_default_clear_text.feature +6 -0
  99. package/src/multi-select-field/features/has_default_empty_text/index.js +13 -0
  100. package/src/multi-select-field/features/has_default_empty_text.feature +6 -0
  101. package/src/multi-select-field/features/has_default_filter_nomatch_text/index.js +17 -0
  102. package/src/multi-select-field/features/has_default_filter_nomatch_text.feature +7 -0
  103. package/src/multi-select-field/features/has_default_filter_placeholder/index.js +15 -0
  104. package/src/multi-select-field/features/has_default_filter_placeholder.feature +6 -0
  105. package/src/multi-select-field/features/has_default_loading_text/index.js +13 -0
  106. package/src/multi-select-field/features/has_default_loading_text.feature +6 -0
  107. package/src/multi-select-field/index.js +1 -0
  108. package/src/multi-select-field/multi-select-field.e2e.stories.js +45 -0
  109. package/src/multi-select-field/multi-select-field.js +178 -0
  110. package/src/multi-select-field/multi-select-field.prod.stories.js +137 -0
  111. package/src/multi-select-option/index.js +1 -0
  112. package/src/multi-select-option/multi-select-option.js +70 -0
  113. package/src/select/__tests__/select.test.js +31 -0
  114. package/src/select/debounce/debounce.js +30 -0
  115. package/src/select/debounce/debounce.spec.js +35 -0
  116. package/src/select/empty.js +28 -0
  117. package/src/select/filter-input.js +42 -0
  118. package/src/select/filterable-menu.js +106 -0
  119. package/src/select/index.js +14 -0
  120. package/src/select/input-clear-button.js +66 -0
  121. package/src/select/input-placeholder.js +30 -0
  122. package/src/select/input-prefix.js +32 -0
  123. package/src/select/input-wrapper.js +114 -0
  124. package/src/select/loading.js +33 -0
  125. package/src/select/menu-wrapper.js +52 -0
  126. package/src/select/no-match.js +28 -0
  127. package/src/select/option-helpers.js +34 -0
  128. package/src/select/select.js +256 -0
  129. package/src/simple-single-select/__stories__/DefaultPosition.js +37 -0
  130. package/src/simple-single-select/__stories__/Dense.js +17 -0
  131. package/src/simple-single-select/__stories__/Empty.js +15 -0
  132. package/src/simple-single-select/__stories__/EmptyWithEmptyNode.js +27 -0
  133. package/src/simple-single-select/__stories__/EmptyWithEmptyText.js +16 -0
  134. package/src/simple-single-select/__stories__/FlippedPosition.js +36 -0
  135. package/src/simple-single-select/__stories__/InfiniteLoading.js +65 -0
  136. package/src/simple-single-select/__stories__/Loading.js +15 -0
  137. package/src/simple-single-select/__stories__/ShiftedIntoView.js +35 -0
  138. package/src/simple-single-select/__stories__/WithClearButton.js +26 -0
  139. package/src/simple-single-select/__stories__/WithCustomLowMaxHeight.js +17 -0
  140. package/src/simple-single-select/__stories__/WithCustomOptions.js +78 -0
  141. package/src/simple-single-select/__stories__/WithDisabledOption.js +25 -0
  142. package/src/simple-single-select/__stories__/WithFilterField.js +30 -0
  143. package/src/simple-single-select/__stories__/WithInitialFocus.js +29 -0
  144. package/src/simple-single-select/__stories__/WithManyOptions.js +19 -0
  145. package/src/simple-single-select/__stories__/WithMultipleSelectsAndScrollableParent.js +65 -0
  146. package/src/simple-single-select/__stories__/WithNoMatchText.js +25 -0
  147. package/src/simple-single-select/__stories__/WithOnBlur.js +22 -0
  148. package/src/simple-single-select/__stories__/WithOnFocus.js +22 -0
  149. package/src/simple-single-select/__stories__/WithOptionsAndDisabled.js +17 -0
  150. package/src/simple-single-select/__stories__/WithOptionsAndLoading.js +23 -0
  151. package/src/simple-single-select/__stories__/WithOptionsAndLoadingText.js +24 -0
  152. package/src/simple-single-select/__stories__/WithPlaceholder.js +18 -0
  153. package/src/simple-single-select/__stories__/WithPlaceholderAndSelection.js +20 -0
  154. package/src/simple-single-select/__stories__/WithPrefix.js +17 -0
  155. package/src/simple-single-select/__stories__/WithPrefixAndSelection.js +20 -0
  156. package/src/simple-single-select/__stories__/WithRTL.js +29 -0
  157. package/src/simple-single-select/__stories__/WithScrollingParent.js +51 -0
  158. package/src/simple-single-select/__stories__/WithSelection.js +19 -0
  159. package/src/simple-single-select/__stories__/WithSelectionAndDisabled.js +20 -0
  160. package/src/simple-single-select/__stories__/WithServerSideFilteringDemo.js +245 -0
  161. package/src/simple-single-select/__stories__/WithSimpleFilteringDemo.js +50 -0
  162. package/src/simple-single-select/__stories__/WithoutOptionsAndLoading.js +16 -0
  163. package/src/simple-single-select/__stories__/WithoutSelection.js +17 -0
  164. package/src/simple-single-select/__stories__/options.js +96 -0
  165. package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/CustomDataProvider.js +70 -0
  166. package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/dataElements.js +4271 -0
  167. package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/index.js +1 -0
  168. package/src/simple-single-select/features/keyboard-interactions.test.e2e.js +581 -0
  169. package/src/simple-single-select/features/menu-positioning.test.e2e.js +187 -0
  170. package/src/simple-single-select/index.js +2 -0
  171. package/src/simple-single-select/is-option-hidden.js +21 -0
  172. package/src/simple-single-select/menu/empty.js +27 -0
  173. package/src/simple-single-select/menu/filter.js +61 -0
  174. package/src/simple-single-select/menu/index.js +1 -0
  175. package/src/simple-single-select/menu/loading.js +38 -0
  176. package/src/simple-single-select/menu/menu.js +171 -0
  177. package/src/simple-single-select/menu/no-match.js +28 -0
  178. package/src/simple-single-select/menu/option.js +161 -0
  179. package/src/simple-single-select/menu/options-list.js +107 -0
  180. package/src/simple-single-select/selected-value/clear-button.js +71 -0
  181. package/src/simple-single-select/selected-value/container.js +146 -0
  182. package/src/simple-single-select/selected-value/index.js +1 -0
  183. package/src/simple-single-select/selected-value/placeholder.js +24 -0
  184. package/src/simple-single-select/selected-value/prefix.js +26 -0
  185. package/src/simple-single-select/selected-value/selected-value.js +174 -0
  186. package/src/simple-single-select/shared-prop-types.js +8 -0
  187. package/src/simple-single-select/simple-single-select.e2e.stories.js +216 -0
  188. package/src/simple-single-select/simple-single-select.js +389 -0
  189. package/src/simple-single-select/simple-single-select.prod.stories.js +41 -0
  190. package/src/simple-single-select/simple-single-select.test.js +1136 -0
  191. package/src/simple-single-select/use-handle-key-press/index.js +2 -0
  192. package/src/simple-single-select/use-handle-key-press/use-focus-option.js +24 -0
  193. package/src/simple-single-select/use-handle-key-press/use-handle-key-press-on-combobox.js +259 -0
  194. package/src/simple-single-select/use-handle-key-press/use-handle-key-press-on-filter-input.js +147 -0
  195. package/src/simple-single-select/use-handle-key-press/use-handle-typing.js +79 -0
  196. package/src/simple-single-select/use-handle-key-press/use-highlight-first-option-on-previous-page.js +62 -0
  197. package/src/simple-single-select/use-handle-key-press/use-highlight-first-visible-option.js +38 -0
  198. package/src/simple-single-select/use-handle-key-press/use-highlight-last-option-on-next-page.js +60 -0
  199. package/src/simple-single-select/use-handle-key-press/use-highlight-last-visible-option.js +52 -0
  200. package/src/simple-single-select/use-handle-key-press/use-page-down.js +50 -0
  201. package/src/simple-single-select/use-handle-key-press/use-page-up.js +50 -0
  202. package/src/simple-single-select/use-handle-key-press/utils.js +23 -0
  203. package/src/simple-single-select-field/index.js +1 -0
  204. package/src/simple-single-select-field/simple-single-select-field.js +198 -0
  205. package/src/simple-single-select-field/simple-single-select-field.prod.stories.js +188 -0
  206. package/src/simple-single-select-field/simple-single-select-field.test.js +144 -0
  207. package/src/single-select/__tests__/selection.test.js +102 -0
  208. package/src/single-select/__tests__/single-select.test.js +23 -0
  209. package/src/single-select/features/accepts_blur_cb/index.js +14 -0
  210. package/src/single-select/features/accepts_blur_cb.feature +8 -0
  211. package/src/single-select/features/accepts_focus_cb/index.js +14 -0
  212. package/src/single-select/features/accepts_focus_cb.feature +6 -0
  213. package/src/single-select/features/accepts_initial_focus/index.js +5 -0
  214. package/src/single-select/features/accepts_initial_focus.feature +5 -0
  215. package/src/single-select/features/accepts_loading/index.js +22 -0
  216. package/src/single-select/features/accepts_loading.feature +14 -0
  217. package/src/single-select/features/accepts_max_height/index.js +31 -0
  218. package/src/single-select/features/accepts_max_height.feature +11 -0
  219. package/src/single-select/features/accepts_placeholder/index.js +25 -0
  220. package/src/single-select/features/accepts_placeholder.feature +10 -0
  221. package/src/single-select/features/accepts_prefix/index.js +19 -0
  222. package/src/single-select/features/accepts_prefix.feature +10 -0
  223. package/src/single-select/features/allows_invalid_options/index.js +28 -0
  224. package/src/single-select/features/allows_invalid_options.feature +12 -0
  225. package/src/single-select/features/allows_selecting/index.js +38 -0
  226. package/src/single-select/features/allows_selecting.feature +21 -0
  227. package/src/single-select/features/can_be_cleared/index.js +22 -0
  228. package/src/single-select/features/can_be_cleared.feature +6 -0
  229. package/src/single-select/features/can_be_disabled/index.js +39 -0
  230. package/src/single-select/features/can_be_disabled.feature +32 -0
  231. package/src/single-select/features/can_be_empty/index.js +28 -0
  232. package/src/single-select/features/can_be_empty.feature +16 -0
  233. package/src/single-select/features/can_be_filtered/index.js +23 -0
  234. package/src/single-select/features/can_be_filtered.feature +14 -0
  235. package/src/single-select/features/can_be_opened_and_closed/index.js +32 -0
  236. package/src/single-select/features/can_be_opened_and_closed.feature +41 -0
  237. package/src/single-select/features/common/index.js +44 -0
  238. package/src/single-select/features/duplicate_option_values/index.js +26 -0
  239. package/src/single-select/features/duplicate_option_values.feature +7 -0
  240. package/src/single-select/features/menu_width_matches_input/index.js +53 -0
  241. package/src/single-select/features/menu_width_matches_input.feature +11 -0
  242. package/src/single-select/features/position/index.js +111 -0
  243. package/src/single-select/features/position.feature +26 -0
  244. package/src/single-select/features/shows_selections/index.js +9 -0
  245. package/src/single-select/features/shows_selections.feature +5 -0
  246. package/src/single-select/filterable-menu.js +41 -0
  247. package/src/single-select/index.js +1 -0
  248. package/src/single-select/input.js +101 -0
  249. package/src/single-select/menu.js +75 -0
  250. package/src/single-select/selection.js +60 -0
  251. package/src/single-select/single-select.e2e.stories.js +376 -0
  252. package/src/single-select/single-select.js +146 -0
  253. package/src/single-select/single-select.prod.stories.js +358 -0
  254. package/src/single-select-field/__tests__/single-select-field.test.js +29 -0
  255. package/src/single-select-field/features/accepts_help_text/index.js +9 -0
  256. package/src/single-select-field/features/accepts_help_text.feature +5 -0
  257. package/src/single-select-field/features/accepts_label/index.js +9 -0
  258. package/src/single-select-field/features/accepts_label.feature +5 -0
  259. package/src/single-select-field/features/accepts_validation_text/index.js +9 -0
  260. package/src/single-select-field/features/accepts_validation_text.feature +5 -0
  261. package/src/single-select-field/features/can_be_required/index.js +11 -0
  262. package/src/single-select-field/features/can_be_required.feature +5 -0
  263. package/src/single-select-field/features/has_default_clear_text/index.js +16 -0
  264. package/src/single-select-field/features/has_default_clear_text.feature +6 -0
  265. package/src/single-select-field/features/has_default_empty_text/index.js +13 -0
  266. package/src/single-select-field/features/has_default_empty_text.feature +6 -0
  267. package/src/single-select-field/features/has_default_filter_nomatch_text/index.js +17 -0
  268. package/src/single-select-field/features/has_default_filter_nomatch_text.feature +7 -0
  269. package/src/single-select-field/features/has_default_filter_placeholder/index.js +15 -0
  270. package/src/single-select-field/features/has_default_filter_placeholder.feature +6 -0
  271. package/src/single-select-field/features/has_default_loading_text/index.js +13 -0
  272. package/src/single-select-field/features/has_default_loading_text.feature +6 -0
  273. package/src/single-select-field/index.js +1 -0
  274. package/src/single-select-field/single-select-field.e2e.stories.js +45 -0
  275. package/src/single-select-field/single-select-field.js +178 -0
  276. package/src/single-select-field/single-select-field.prod.stories.js +146 -0
  277. package/src/single-select-option/index.js +1 -0
  278. package/src/single-select-option/single-select-option.js +84 -0
@@ -0,0 +1,256 @@
1
+ import { sharedPropTypes } from '@dhis2/ui-constants'
2
+ import PropTypes from 'prop-types'
3
+ import React, { Component } from 'react'
4
+ import { debounce } from './debounce/debounce.js'
5
+ import { InputWrapper } from './input-wrapper.js'
6
+ import { MenuWrapper } from './menu-wrapper.js'
7
+
8
+ // Keycodes for the keypress event handlers
9
+ const TAB_KEY = 9
10
+ const ESCAPE_KEY = 27
11
+ const SPACE_KEY = 32
12
+ const UP_KEY = 38
13
+ const DOWN_KEY = 40
14
+
15
+ export class Select extends Component {
16
+ state = {
17
+ open: false,
18
+ menuWidth: 'auto',
19
+ }
20
+
21
+ static defaultProps = {
22
+ dataTest: 'dhis2-uicore-select',
23
+ }
24
+
25
+ selectRef = React.createRef()
26
+ inputRef = React.createRef()
27
+
28
+ componentDidMount() {
29
+ if (this.props.initialFocus) {
30
+ this.inputRef.current.focus()
31
+ }
32
+
33
+ this.setState({ menuWidth: this.getMenuWidth() })
34
+ window.addEventListener('resize', this.onResize)
35
+ }
36
+
37
+ componentWillUnmount() {
38
+ window.removeEventListener('resize', this.onResize)
39
+ }
40
+
41
+ /**
42
+ * We're debouncing this so it doesn't fire continually during a resize.
43
+ *
44
+ * Additionally we should use requestPostAnimationFrame to not trigger a forced
45
+ * layout, but that's just a proposal, and the added complexity of solving this
46
+ * in another manner does not seem worth it, considering the minor perf penalty.
47
+ *
48
+ * See: https://nolanlawson.com/2018/09/25/accurately-measuring-layout-on-the-web
49
+ */
50
+ onResize = debounce(() => {
51
+ const menuWidth = this.getMenuWidth()
52
+
53
+ if (this.state.menuWidth !== menuWidth) {
54
+ this.setState({ menuWidth })
55
+ }
56
+ }, 50)
57
+
58
+ getMenuWidth() {
59
+ const { offsetWidth } = this.inputRef.current
60
+ const { menuWidth } = this.state
61
+
62
+ if (offsetWidth && `${offsetWidth}px` !== menuWidth) {
63
+ return `${offsetWidth}px`
64
+ }
65
+ return menuWidth
66
+ }
67
+
68
+ handleFocusInput = () => {
69
+ this.inputRef.current.focus()
70
+ }
71
+
72
+ onFocus = (e) => {
73
+ const { onFocus, disabled, selected } = this.props
74
+
75
+ if (disabled || !onFocus) {
76
+ return
77
+ }
78
+
79
+ onFocus({ selected }, e)
80
+ }
81
+
82
+ handleOpen = () => {
83
+ this.setState({
84
+ open: true,
85
+ menuWidth: this.getMenuWidth(),
86
+ })
87
+ }
88
+
89
+ handleClose = () => {
90
+ this.setState({ open: false })
91
+ }
92
+
93
+ onToggle = (e) => {
94
+ if (this.props.disabled) {
95
+ return
96
+ }
97
+
98
+ e.stopPropagation()
99
+
100
+ this.state.open ? this.handleClose() : this.handleOpen()
101
+ }
102
+
103
+ onOutsideClick = (e) => {
104
+ const { onBlur, disabled, selected } = this.props
105
+
106
+ if (disabled) {
107
+ return
108
+ }
109
+
110
+ this.handleClose()
111
+
112
+ if (onBlur) {
113
+ onBlur({ selected }, e)
114
+ }
115
+ }
116
+
117
+ onKeyDown = (e) => {
118
+ const { onKeyDown, disabled, selected } = this.props
119
+
120
+ if (disabled) {
121
+ return
122
+ }
123
+
124
+ if (onKeyDown) {
125
+ onKeyDown({ selected }, e)
126
+ }
127
+
128
+ if (e.defaultPrevented) {
129
+ return
130
+ }
131
+
132
+ const { open } = this.state
133
+ const { keyCode } = e
134
+ const shouldOpen =
135
+ !open &&
136
+ (keyCode === SPACE_KEY ||
137
+ keyCode === UP_KEY ||
138
+ keyCode === DOWN_KEY)
139
+
140
+ /* Do not block event propagation when the Select is closed unless
141
+ * the key to open it is pressed, so that other components like
142
+ * the Modal can still respond to keypresses (i.e. ESC to close) */
143
+ if (open || shouldOpen) {
144
+ e.stopPropagation()
145
+ }
146
+
147
+ if (open && keyCode === ESCAPE_KEY) {
148
+ return this.handleClose()
149
+ }
150
+
151
+ if (open && keyCode === TAB_KEY) {
152
+ // Defer so Tab advances focus before the menu unmounts.
153
+ return requestAnimationFrame(this.handleClose)
154
+ }
155
+
156
+ if (shouldOpen) {
157
+ return this.handleOpen()
158
+ }
159
+ }
160
+
161
+ render() {
162
+ const { open, menuWidth } = this.state
163
+ const {
164
+ children,
165
+ className,
166
+ selected,
167
+ onChange,
168
+ tabIndex,
169
+ maxHeight,
170
+ error,
171
+ warning,
172
+ valid,
173
+ disabled,
174
+ dense,
175
+ dataTest,
176
+ } = this.props
177
+
178
+ // Create the input
179
+ const inputProps = {
180
+ selected,
181
+ onChange,
182
+ options: children,
183
+ disabled,
184
+ }
185
+ const input = React.cloneElement(this.props.input, inputProps)
186
+
187
+ // Create the menu
188
+ const menuProps = {
189
+ selected,
190
+ onChange,
191
+ options: children,
192
+ handleClose: this.handleClose,
193
+ handleFocusInput: this.handleFocusInput,
194
+ }
195
+ const menu = React.cloneElement(this.props.menu, menuProps)
196
+
197
+ return (
198
+ <div
199
+ className={className}
200
+ ref={this.selectRef}
201
+ onFocus={this.onFocus}
202
+ onKeyDown={this.onKeyDown}
203
+ data-test={dataTest}
204
+ >
205
+ <InputWrapper
206
+ onToggle={this.onToggle}
207
+ inputRef={this.inputRef}
208
+ tabIndex={disabled ? '-1' : tabIndex}
209
+ error={error}
210
+ warning={warning}
211
+ valid={valid}
212
+ disabled={disabled}
213
+ dense={dense}
214
+ dataTest={`${dataTest}-input`}
215
+ >
216
+ {input}
217
+ </InputWrapper>
218
+ {open && (
219
+ <MenuWrapper
220
+ onClick={this.onOutsideClick}
221
+ maxHeight={maxHeight}
222
+ selectRef={this.selectRef}
223
+ menuWidth={menuWidth}
224
+ dataTest={`${dataTest}-menu`}
225
+ >
226
+ {menu}
227
+ </MenuWrapper>
228
+ )}
229
+ </div>
230
+ )
231
+ }
232
+ }
233
+
234
+ Select.propTypes = {
235
+ input: PropTypes.element.isRequired,
236
+ menu: PropTypes.element.isRequired,
237
+ selected: PropTypes.oneOfType([
238
+ PropTypes.string,
239
+ PropTypes.arrayOf(PropTypes.string),
240
+ ]).isRequired,
241
+ children: PropTypes.node,
242
+ className: PropTypes.string,
243
+ dataTest: PropTypes.string,
244
+ dense: PropTypes.bool,
245
+ disabled: PropTypes.bool,
246
+ error: sharedPropTypes.statusPropType,
247
+ initialFocus: PropTypes.bool,
248
+ maxHeight: PropTypes.string,
249
+ tabIndex: PropTypes.string,
250
+ valid: sharedPropTypes.statusPropType,
251
+ warning: sharedPropTypes.statusPropType,
252
+ onBlur: PropTypes.func,
253
+ onChange: PropTypes.func,
254
+ onFocus: PropTypes.func,
255
+ onKeyDown: PropTypes.func,
256
+ }
@@ -0,0 +1,37 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ export const DefaultPosition = () => {
6
+ const [selected, setSelected] = useState({
7
+ label: 'ANC 1st visit',
8
+ value: 'anc_1st_visit',
9
+ })
10
+
11
+ return (
12
+ <div
13
+ style={{
14
+ position: 'relative',
15
+ // Accounting for the padding on the body
16
+ height: 'calc(100vh - 32px)',
17
+ }}
18
+ >
19
+ <div
20
+ style={{
21
+ position: 'absolute',
22
+ top: '50%',
23
+ left: 0,
24
+ transform: 'translateY(-50%)',
25
+ width: '100%',
26
+ }}
27
+ >
28
+ <SimpleSingleSelect
29
+ name="simple"
30
+ selected={selected}
31
+ onChange={setSelected}
32
+ options={fiveOptions}
33
+ />
34
+ </div>
35
+ </div>
36
+ )
37
+ }
@@ -0,0 +1,17 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ export const Dense = () => {
6
+ const [selected, setSelected] = useState(null)
7
+
8
+ return (
9
+ <SimpleSingleSelect
10
+ dense
11
+ name="simple"
12
+ selected={selected}
13
+ onChange={setSelected}
14
+ options={fiveOptions}
15
+ />
16
+ )
17
+ }
@@ -0,0 +1,15 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+
4
+ export const Empty = () => {
5
+ const [selected, setSelected] = useState(null)
6
+
7
+ return (
8
+ <SimpleSingleSelect
9
+ name="simple"
10
+ selected={selected}
11
+ onChange={setSelected}
12
+ options={[]}
13
+ />
14
+ )
15
+ }
@@ -0,0 +1,27 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+
4
+ export const EmptyWithEmptyNode = () => {
5
+ const [selected, setSelected] = useState(null)
6
+ const emptyNode = (
7
+ <div
8
+ style={{
9
+ color: 'red',
10
+ textAlign: 'center',
11
+ padding: '10px 0',
12
+ }}
13
+ >
14
+ Custom empty text
15
+ </div>
16
+ )
17
+
18
+ return (
19
+ <SimpleSingleSelect
20
+ name="simple"
21
+ selected={selected}
22
+ onChange={setSelected}
23
+ options={[]}
24
+ empty={emptyNode}
25
+ />
26
+ )
27
+ }
@@ -0,0 +1,16 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+
4
+ export const EmptyWithEmptyText = () => {
5
+ const [selected, setSelected] = useState(null)
6
+
7
+ return (
8
+ <SimpleSingleSelect
9
+ name="simple"
10
+ selected={selected}
11
+ onChange={setSelected}
12
+ options={[]}
13
+ empty="Custom empty text"
14
+ />
15
+ )
16
+ }
@@ -0,0 +1,36 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ export const FlippedPosition = () => {
6
+ const [selected, setSelected] = useState({
7
+ label: 'ANC 1st visit',
8
+ value: 'anc_1st_visit',
9
+ })
10
+
11
+ return (
12
+ <div
13
+ style={{
14
+ position: 'relative',
15
+ // Accounting for the padding on the body
16
+ height: 'calc(100vh - 32px)',
17
+ }}
18
+ >
19
+ <div
20
+ style={{
21
+ position: 'absolute',
22
+ bottom: 0,
23
+ left: 0,
24
+ width: '100%',
25
+ }}
26
+ >
27
+ <SimpleSingleSelect
28
+ name="simple"
29
+ selected={selected}
30
+ onChange={setSelected}
31
+ options={fiveOptions}
32
+ />
33
+ </div>
34
+ </div>
35
+ )
36
+ }
@@ -0,0 +1,65 @@
1
+ import React, { useCallback, useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { options } from './options.js'
4
+
5
+ // The select displays exactly 9 options per page,
6
+ // using 10 so the user has to scroll to see the effect
7
+ const CHUNK_SIZE = 10
8
+
9
+ const optionChunks = options.reduce((chunks, option, index) => {
10
+ if (index === 0) {
11
+ return [[option]]
12
+ }
13
+
14
+ const prevChunkIndex = chunks.length - 1
15
+ const prevChunk = chunks[prevChunkIndex]
16
+ const prevChunkLength = prevChunk.length
17
+
18
+ if (prevChunkLength < CHUNK_SIZE) {
19
+ return [...chunks.slice(0, prevChunkIndex), [...prevChunk, option]]
20
+ }
21
+
22
+ return [...chunks, [option]]
23
+ }, [])
24
+
25
+ export const InfiniteLoading = () => {
26
+ const [loading, setLoading] = useState(false)
27
+ const [curLoadedPage, setCurLoadedPage] = useState(0)
28
+ const [selected, setSelected] = useState(null)
29
+ const [loadedOptions, setLoadedOptions] = useState(optionChunks[0])
30
+
31
+ const loadNextOptions = useCallback(() => {
32
+ const nextPage = curLoadedPage + 1
33
+
34
+ if (
35
+ // We're already loading a page
36
+ loading ||
37
+ // No need to load anything when already loaded everything
38
+ nextPage >= optionChunks.length
39
+ ) {
40
+ return
41
+ }
42
+
43
+ setLoading(true)
44
+ setCurLoadedPage(nextPage)
45
+
46
+ // Fake network request to show loader a reasonable amount of time
47
+ setTimeout(() => {
48
+ const nextChunk = optionChunks[nextPage]
49
+ setLoadedOptions((prevOptions) => [...prevOptions, ...nextChunk])
50
+ setLoading(false)
51
+ }, 1500)
52
+ }, [curLoadedPage, loading])
53
+
54
+ return (
55
+ <SimpleSingleSelect
56
+ name="simple"
57
+ selected={selected}
58
+ loading={loading}
59
+ menuLoadingText="Foo bar baz"
60
+ onChange={setSelected}
61
+ options={loadedOptions}
62
+ onEndReached={loadNextOptions}
63
+ />
64
+ )
65
+ }
@@ -0,0 +1,15 @@
1
+ import React from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { options } from './options.js'
4
+
5
+ export const Loading = () => {
6
+ return (
7
+ <SimpleSingleSelect
8
+ loading
9
+ name="simple"
10
+ selected={null}
11
+ onChange={() => null}
12
+ options={options}
13
+ />
14
+ )
15
+ }
@@ -0,0 +1,35 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ export const ShiftedIntoView = () => {
6
+ const [selected, setSelected] = useState({
7
+ label: 'ANC 1st visit',
8
+ value: 'anc_1st_visit',
9
+ })
10
+
11
+ return (
12
+ <>
13
+ <SimpleSingleSelect
14
+ name="simple"
15
+ selected={selected}
16
+ onChange={setSelected}
17
+ options={fiveOptions}
18
+ />
19
+
20
+ <style jsx>{`
21
+ :global(html),
22
+ :global(body),
23
+ :global(#storybook-root) {
24
+ position: relative;
25
+ height: 300px !important;
26
+ max-height: 300px;
27
+ }
28
+
29
+ :global(#storybook-root) {
30
+ padding-top: 130px !important;
31
+ }
32
+ `}</style>
33
+ </>
34
+ )
35
+ }
@@ -0,0 +1,26 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ export const WithClearButton = () => {
6
+ const [selected, setSelected] = useState({
7
+ label: 'ANC 1st visit',
8
+ value: 'anc_1st_visit',
9
+ })
10
+
11
+ return (
12
+ <SimpleSingleSelect
13
+ clearable
14
+ name="simple"
15
+ onClear={() =>
16
+ setSelected({
17
+ label: '',
18
+ value: '',
19
+ })
20
+ }
21
+ selected={selected}
22
+ onChange={setSelected}
23
+ options={fiveOptions}
24
+ />
25
+ )
26
+ }
@@ -0,0 +1,17 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { options } from './options.js'
4
+
5
+ export const WithCustomLowMaxHeight = () => {
6
+ const [selected, setSelected] = useState(null)
7
+
8
+ return (
9
+ <SimpleSingleSelect
10
+ name="simple"
11
+ selected={selected}
12
+ onChange={setSelected}
13
+ options={options}
14
+ menuMaxHeight="100px"
15
+ />
16
+ )
17
+ }
@@ -0,0 +1,78 @@
1
+ import React, { useState, useMemo } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { options } from './options.js'
4
+
5
+ const CustomOption = ({ label }) => (
6
+ <span className="container">
7
+ <span className="label">{label}</span>
8
+
9
+ <span
10
+ className="button"
11
+ role="button"
12
+ onClick={(e) => {
13
+ e.stopPropagation()
14
+ alert('Custom "button" clicked!')
15
+ }}
16
+ >
17
+ x
18
+ </span>
19
+
20
+ <style jsx>{`
21
+ .container {
22
+ display: flex;
23
+ color: white;
24
+ cursor: pointer;
25
+ height: 30px;
26
+ }
27
+
28
+ .label {
29
+ flex-grow: 1;
30
+ background: green;
31
+ padding: 5px 10px;
32
+ }
33
+
34
+ .label:hover {
35
+ background: blue;
36
+ }
37
+
38
+ .button {
39
+ height: 30px;
40
+ width: 30px;
41
+ padding: 0;
42
+ border: 0;
43
+ background: none;
44
+ background: white;
45
+ cursor: pointer;
46
+ font-size: 20px;
47
+ display: flex;
48
+ align-items: center;
49
+ justify-content: center;
50
+ color: black;
51
+ }
52
+
53
+ .button:hover {
54
+ background: black;
55
+ color: white;
56
+ }
57
+ `}</style>
58
+ </span>
59
+ )
60
+
61
+ export const WithCustomOptions = () => {
62
+ const [selected, setSelected] = useState(null)
63
+ const optionsWithCustomStyle = useMemo(() => {
64
+ return options.slice(0, 3).map((option) => ({
65
+ ...option,
66
+ component: CustomOption,
67
+ }))
68
+ }, [])
69
+
70
+ return (
71
+ <SimpleSingleSelect
72
+ name="simple"
73
+ selected={selected}
74
+ onChange={setSelected}
75
+ options={optionsWithCustomStyle}
76
+ />
77
+ )
78
+ }
@@ -0,0 +1,25 @@
1
+ import React, { useState } from 'react'
2
+ import { SimpleSingleSelect } from '../simple-single-select.js'
3
+ import { fiveOptions } from './options.js'
4
+
5
+ const fiveOptionsWithFourthDisabled = [
6
+ ...fiveOptions.slice(0, 3),
7
+ { ...fiveOptions[3], disabled: true },
8
+ ...fiveOptions.slice(4),
9
+ ]
10
+
11
+ export const WithDisabledOption = () => {
12
+ const [selected, setSelected] = useState({
13
+ label: 'ANC 1st visit',
14
+ value: 'anc_1st_visit',
15
+ })
16
+
17
+ return (
18
+ <SimpleSingleSelect
19
+ name="simple"
20
+ selected={selected}
21
+ onChange={setSelected}
22
+ options={fiveOptionsWithFourthDisabled}
23
+ />
24
+ )
25
+ }