@dhis2-ui/select 10.16.2 → 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.
- package/package.json +17 -16
- package/src/index.js +9 -0
- package/src/locales/ar/translations.json +7 -0
- package/src/locales/ar_IQ/translations.json +7 -0
- package/src/locales/ckb/translations.json +7 -0
- package/src/locales/cs/translations.json +7 -0
- package/src/locales/da/translations.json +7 -0
- package/src/locales/en/translations.json +11 -0
- package/src/locales/en_US/translations.json +9 -0
- package/src/locales/es/translations.json +7 -0
- package/src/locales/es_419/translations.json +7 -0
- package/src/locales/fr/translations.json +7 -0
- package/src/locales/hi_IN/translations.json +7 -0
- package/src/locales/id/translations.json +7 -0
- package/src/locales/index.js +80 -0
- package/src/locales/km/translations.json +7 -0
- package/src/locales/lo/translations.json +7 -0
- package/src/locales/my/translations.json +7 -0
- package/src/locales/nb/translations.json +7 -0
- package/src/locales/nl/translations.json +7 -0
- package/src/locales/prs/translations.json +7 -0
- package/src/locales/ps/translations.json +7 -0
- package/src/locales/pt/translations.json +7 -0
- package/src/locales/pt_BR/translations.json +7 -0
- package/src/locales/ru/translations.json +7 -0
- package/src/locales/sv/translations.json +7 -0
- package/src/locales/tet/translations.json +7 -0
- package/src/locales/tg/translations.json +7 -0
- package/src/locales/uk/translations.json +7 -0
- package/src/locales/ur/translations.json +7 -0
- package/src/locales/uz_Latn/translations.json +7 -0
- package/src/locales/uz_UZ_Cyrl/translations.json +7 -0
- package/src/locales/uz_UZ_Latn/translations.json +7 -0
- package/src/locales/vi/translations.json +7 -0
- package/src/locales/zh/translations.json +9 -0
- package/src/locales/zh_CN/translations.json +7 -0
- package/src/multi-select/__tests__/multi-select.test.js +23 -0
- package/src/multi-select/__tests__/selection-list.test.js +102 -0
- package/src/multi-select/features/accepts_blur_cb/index.js +14 -0
- package/src/multi-select/features/accepts_blur_cb.feature +8 -0
- package/src/multi-select/features/accepts_focus_cb/index.js +14 -0
- package/src/multi-select/features/accepts_focus_cb.feature +6 -0
- package/src/multi-select/features/accepts_initial_focus/index.js +5 -0
- package/src/multi-select/features/accepts_initial_focus.feature +5 -0
- package/src/multi-select/features/accepts_loading/index.js +22 -0
- package/src/multi-select/features/accepts_loading.feature +14 -0
- package/src/multi-select/features/accepts_max_height/index.js +31 -0
- package/src/multi-select/features/accepts_max_height.feature +11 -0
- package/src/multi-select/features/accepts_placeholder/index.js +25 -0
- package/src/multi-select/features/accepts_placeholder.feature +10 -0
- package/src/multi-select/features/accepts_prefix/index.js +19 -0
- package/src/multi-select/features/accepts_prefix.feature +10 -0
- package/src/multi-select/features/allows_invalid_options/index.js +27 -0
- package/src/multi-select/features/allows_invalid_options.feature +12 -0
- package/src/multi-select/features/allows_selecting/index.js +81 -0
- package/src/multi-select/features/allows_selecting.feature +44 -0
- package/src/multi-select/features/can_be_cleared/index.js +22 -0
- package/src/multi-select/features/can_be_cleared.feature +6 -0
- package/src/multi-select/features/can_be_disabled/index.js +36 -0
- package/src/multi-select/features/can_be_disabled.feature +32 -0
- package/src/multi-select/features/can_be_empty/index.js +28 -0
- package/src/multi-select/features/can_be_empty.feature +16 -0
- package/src/multi-select/features/can_be_filtered/index.js +23 -0
- package/src/multi-select/features/can_be_filtered.feature +14 -0
- package/src/multi-select/features/can_be_opened_and_closed/index.js +32 -0
- package/src/multi-select/features/can_be_opened_and_closed.feature +41 -0
- package/src/multi-select/features/common/index.js +49 -0
- package/src/multi-select/features/duplicate_option_values/index.js +21 -0
- package/src/multi-select/features/duplicate_option_values.feature +7 -0
- package/src/multi-select/features/menu_width_matches_input/index.js +53 -0
- package/src/multi-select/features/menu_width_matches_input.feature +11 -0
- package/src/multi-select/features/position/index.js +160 -0
- package/src/multi-select/features/position.feature +46 -0
- package/src/multi-select/features/shows_selections/index.js +18 -0
- package/src/multi-select/features/shows_selections.feature +9 -0
- package/src/multi-select/filterable-menu.js +43 -0
- package/src/multi-select/index.js +1 -0
- package/src/multi-select/input.js +129 -0
- package/src/multi-select/menu.js +91 -0
- package/src/multi-select/multi-select.e2e.stories.js +407 -0
- package/src/multi-select/multi-select.js +148 -0
- package/src/multi-select/multi-select.prod.stories.js +408 -0
- package/src/multi-select/selection-list.js +79 -0
- package/src/multi-select-field/__tests__/multi-select-field.test.js +29 -0
- package/src/multi-select-field/features/accepts_help_text/index.js +9 -0
- package/src/multi-select-field/features/accepts_help_text.feature +5 -0
- package/src/multi-select-field/features/accepts_label/index.js +9 -0
- package/src/multi-select-field/features/accepts_label.feature +5 -0
- package/src/multi-select-field/features/accepts_validation_text/index.js +9 -0
- package/src/multi-select-field/features/accepts_validation_text.feature +5 -0
- package/src/multi-select-field/features/can_be_required/index.js +11 -0
- package/src/multi-select-field/features/can_be_required.feature +5 -0
- package/src/multi-select-field/features/has_default_clear_text/index.js +16 -0
- package/src/multi-select-field/features/has_default_clear_text.feature +6 -0
- package/src/multi-select-field/features/has_default_empty_text/index.js +13 -0
- package/src/multi-select-field/features/has_default_empty_text.feature +6 -0
- package/src/multi-select-field/features/has_default_filter_nomatch_text/index.js +17 -0
- package/src/multi-select-field/features/has_default_filter_nomatch_text.feature +7 -0
- package/src/multi-select-field/features/has_default_filter_placeholder/index.js +15 -0
- package/src/multi-select-field/features/has_default_filter_placeholder.feature +6 -0
- package/src/multi-select-field/features/has_default_loading_text/index.js +13 -0
- package/src/multi-select-field/features/has_default_loading_text.feature +6 -0
- package/src/multi-select-field/index.js +1 -0
- package/src/multi-select-field/multi-select-field.e2e.stories.js +45 -0
- package/src/multi-select-field/multi-select-field.js +178 -0
- package/src/multi-select-field/multi-select-field.prod.stories.js +137 -0
- package/src/multi-select-option/index.js +1 -0
- package/src/multi-select-option/multi-select-option.js +70 -0
- package/src/select/__tests__/select.test.js +31 -0
- package/src/select/debounce/debounce.js +30 -0
- package/src/select/debounce/debounce.spec.js +35 -0
- package/src/select/empty.js +28 -0
- package/src/select/filter-input.js +42 -0
- package/src/select/filterable-menu.js +106 -0
- package/src/select/index.js +14 -0
- package/src/select/input-clear-button.js +66 -0
- package/src/select/input-placeholder.js +30 -0
- package/src/select/input-prefix.js +32 -0
- package/src/select/input-wrapper.js +114 -0
- package/src/select/loading.js +33 -0
- package/src/select/menu-wrapper.js +52 -0
- package/src/select/no-match.js +28 -0
- package/src/select/option-helpers.js +34 -0
- package/src/select/select.js +256 -0
- package/src/simple-single-select/__stories__/DefaultPosition.js +37 -0
- package/src/simple-single-select/__stories__/Dense.js +17 -0
- package/src/simple-single-select/__stories__/Empty.js +15 -0
- package/src/simple-single-select/__stories__/EmptyWithEmptyNode.js +27 -0
- package/src/simple-single-select/__stories__/EmptyWithEmptyText.js +16 -0
- package/src/simple-single-select/__stories__/FlippedPosition.js +36 -0
- package/src/simple-single-select/__stories__/InfiniteLoading.js +65 -0
- package/src/simple-single-select/__stories__/Loading.js +15 -0
- package/src/simple-single-select/__stories__/ShiftedIntoView.js +35 -0
- package/src/simple-single-select/__stories__/WithClearButton.js +26 -0
- package/src/simple-single-select/__stories__/WithCustomLowMaxHeight.js +17 -0
- package/src/simple-single-select/__stories__/WithCustomOptions.js +78 -0
- package/src/simple-single-select/__stories__/WithDisabledOption.js +25 -0
- package/src/simple-single-select/__stories__/WithFilterField.js +30 -0
- package/src/simple-single-select/__stories__/WithInitialFocus.js +29 -0
- package/src/simple-single-select/__stories__/WithManyOptions.js +19 -0
- package/src/simple-single-select/__stories__/WithMultipleSelectsAndScrollableParent.js +65 -0
- package/src/simple-single-select/__stories__/WithNoMatchText.js +25 -0
- package/src/simple-single-select/__stories__/WithOnBlur.js +22 -0
- package/src/simple-single-select/__stories__/WithOnFocus.js +22 -0
- package/src/simple-single-select/__stories__/WithOptionsAndDisabled.js +17 -0
- package/src/simple-single-select/__stories__/WithOptionsAndLoading.js +23 -0
- package/src/simple-single-select/__stories__/WithOptionsAndLoadingText.js +24 -0
- package/src/simple-single-select/__stories__/WithPlaceholder.js +18 -0
- package/src/simple-single-select/__stories__/WithPlaceholderAndSelection.js +20 -0
- package/src/simple-single-select/__stories__/WithPrefix.js +17 -0
- package/src/simple-single-select/__stories__/WithPrefixAndSelection.js +20 -0
- package/src/simple-single-select/__stories__/WithRTL.js +29 -0
- package/src/simple-single-select/__stories__/WithScrollingParent.js +51 -0
- package/src/simple-single-select/__stories__/WithSelection.js +19 -0
- package/src/simple-single-select/__stories__/WithSelectionAndDisabled.js +20 -0
- package/src/simple-single-select/__stories__/WithServerSideFilteringDemo.js +245 -0
- package/src/simple-single-select/__stories__/WithSimpleFilteringDemo.js +50 -0
- package/src/simple-single-select/__stories__/WithoutOptionsAndLoading.js +16 -0
- package/src/simple-single-select/__stories__/WithoutSelection.js +17 -0
- package/src/simple-single-select/__stories__/options.js +96 -0
- package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/CustomDataProvider.js +70 -0
- package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/dataElements.js +4271 -0
- package/src/simple-single-select/__stories__/simple-single-select-server-side-filtering/index.js +1 -0
- package/src/simple-single-select/features/keyboard-interactions.test.e2e.js +581 -0
- package/src/simple-single-select/features/menu-positioning.test.e2e.js +187 -0
- package/src/simple-single-select/index.js +2 -0
- package/src/simple-single-select/is-option-hidden.js +21 -0
- package/src/simple-single-select/menu/empty.js +27 -0
- package/src/simple-single-select/menu/filter.js +61 -0
- package/src/simple-single-select/menu/index.js +1 -0
- package/src/simple-single-select/menu/loading.js +38 -0
- package/src/simple-single-select/menu/menu.js +171 -0
- package/src/simple-single-select/menu/no-match.js +28 -0
- package/src/simple-single-select/menu/option.js +161 -0
- package/src/simple-single-select/menu/options-list.js +107 -0
- package/src/simple-single-select/selected-value/clear-button.js +71 -0
- package/src/simple-single-select/selected-value/container.js +146 -0
- package/src/simple-single-select/selected-value/index.js +1 -0
- package/src/simple-single-select/selected-value/placeholder.js +24 -0
- package/src/simple-single-select/selected-value/prefix.js +26 -0
- package/src/simple-single-select/selected-value/selected-value.js +174 -0
- package/src/simple-single-select/shared-prop-types.js +8 -0
- package/src/simple-single-select/simple-single-select.e2e.stories.js +216 -0
- package/src/simple-single-select/simple-single-select.js +389 -0
- package/src/simple-single-select/simple-single-select.prod.stories.js +41 -0
- package/src/simple-single-select/simple-single-select.test.js +1136 -0
- package/src/simple-single-select/use-handle-key-press/index.js +2 -0
- package/src/simple-single-select/use-handle-key-press/use-focus-option.js +24 -0
- package/src/simple-single-select/use-handle-key-press/use-handle-key-press-on-combobox.js +259 -0
- package/src/simple-single-select/use-handle-key-press/use-handle-key-press-on-filter-input.js +147 -0
- package/src/simple-single-select/use-handle-key-press/use-handle-typing.js +79 -0
- package/src/simple-single-select/use-handle-key-press/use-highlight-first-option-on-previous-page.js +62 -0
- package/src/simple-single-select/use-handle-key-press/use-highlight-first-visible-option.js +38 -0
- package/src/simple-single-select/use-handle-key-press/use-highlight-last-option-on-next-page.js +60 -0
- package/src/simple-single-select/use-handle-key-press/use-highlight-last-visible-option.js +52 -0
- package/src/simple-single-select/use-handle-key-press/use-page-down.js +50 -0
- package/src/simple-single-select/use-handle-key-press/use-page-up.js +50 -0
- package/src/simple-single-select/use-handle-key-press/utils.js +23 -0
- package/src/simple-single-select-field/index.js +1 -0
- package/src/simple-single-select-field/simple-single-select-field.js +198 -0
- package/src/simple-single-select-field/simple-single-select-field.prod.stories.js +188 -0
- package/src/simple-single-select-field/simple-single-select-field.test.js +144 -0
- package/src/single-select/__tests__/selection.test.js +102 -0
- package/src/single-select/__tests__/single-select.test.js +23 -0
- package/src/single-select/features/accepts_blur_cb/index.js +14 -0
- package/src/single-select/features/accepts_blur_cb.feature +8 -0
- package/src/single-select/features/accepts_focus_cb/index.js +14 -0
- package/src/single-select/features/accepts_focus_cb.feature +6 -0
- package/src/single-select/features/accepts_initial_focus/index.js +5 -0
- package/src/single-select/features/accepts_initial_focus.feature +5 -0
- package/src/single-select/features/accepts_loading/index.js +22 -0
- package/src/single-select/features/accepts_loading.feature +14 -0
- package/src/single-select/features/accepts_max_height/index.js +31 -0
- package/src/single-select/features/accepts_max_height.feature +11 -0
- package/src/single-select/features/accepts_placeholder/index.js +25 -0
- package/src/single-select/features/accepts_placeholder.feature +10 -0
- package/src/single-select/features/accepts_prefix/index.js +19 -0
- package/src/single-select/features/accepts_prefix.feature +10 -0
- package/src/single-select/features/allows_invalid_options/index.js +28 -0
- package/src/single-select/features/allows_invalid_options.feature +12 -0
- package/src/single-select/features/allows_selecting/index.js +38 -0
- package/src/single-select/features/allows_selecting.feature +21 -0
- package/src/single-select/features/can_be_cleared/index.js +22 -0
- package/src/single-select/features/can_be_cleared.feature +6 -0
- package/src/single-select/features/can_be_disabled/index.js +39 -0
- package/src/single-select/features/can_be_disabled.feature +32 -0
- package/src/single-select/features/can_be_empty/index.js +28 -0
- package/src/single-select/features/can_be_empty.feature +16 -0
- package/src/single-select/features/can_be_filtered/index.js +23 -0
- package/src/single-select/features/can_be_filtered.feature +14 -0
- package/src/single-select/features/can_be_opened_and_closed/index.js +32 -0
- package/src/single-select/features/can_be_opened_and_closed.feature +41 -0
- package/src/single-select/features/common/index.js +44 -0
- package/src/single-select/features/duplicate_option_values/index.js +26 -0
- package/src/single-select/features/duplicate_option_values.feature +7 -0
- package/src/single-select/features/menu_width_matches_input/index.js +53 -0
- package/src/single-select/features/menu_width_matches_input.feature +11 -0
- package/src/single-select/features/position/index.js +111 -0
- package/src/single-select/features/position.feature +26 -0
- package/src/single-select/features/shows_selections/index.js +9 -0
- package/src/single-select/features/shows_selections.feature +5 -0
- package/src/single-select/filterable-menu.js +41 -0
- package/src/single-select/index.js +1 -0
- package/src/single-select/input.js +101 -0
- package/src/single-select/menu.js +75 -0
- package/src/single-select/selection.js +60 -0
- package/src/single-select/single-select.e2e.stories.js +376 -0
- package/src/single-select/single-select.js +146 -0
- package/src/single-select/single-select.prod.stories.js +358 -0
- package/src/single-select-field/__tests__/single-select-field.test.js +29 -0
- package/src/single-select-field/features/accepts_help_text/index.js +9 -0
- package/src/single-select-field/features/accepts_help_text.feature +5 -0
- package/src/single-select-field/features/accepts_label/index.js +9 -0
- package/src/single-select-field/features/accepts_label.feature +5 -0
- package/src/single-select-field/features/accepts_validation_text/index.js +9 -0
- package/src/single-select-field/features/accepts_validation_text.feature +5 -0
- package/src/single-select-field/features/can_be_required/index.js +11 -0
- package/src/single-select-field/features/can_be_required.feature +5 -0
- package/src/single-select-field/features/has_default_clear_text/index.js +16 -0
- package/src/single-select-field/features/has_default_clear_text.feature +6 -0
- package/src/single-select-field/features/has_default_empty_text/index.js +13 -0
- package/src/single-select-field/features/has_default_empty_text.feature +6 -0
- package/src/single-select-field/features/has_default_filter_nomatch_text/index.js +17 -0
- package/src/single-select-field/features/has_default_filter_nomatch_text.feature +7 -0
- package/src/single-select-field/features/has_default_filter_placeholder/index.js +15 -0
- package/src/single-select-field/features/has_default_filter_placeholder.feature +6 -0
- package/src/single-select-field/features/has_default_loading_text/index.js +13 -0
- package/src/single-select-field/features/has_default_loading_text.feature +6 -0
- package/src/single-select-field/index.js +1 -0
- package/src/single-select-field/single-select-field.e2e.stories.js +45 -0
- package/src/single-select-field/single-select-field.js +178 -0
- package/src/single-select-field/single-select-field.prod.stories.js +146 -0
- package/src/single-select-option/index.js +1 -0
- package/src/single-select-option/single-select-option.js +84 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useCallback } from 'react'
|
|
2
|
+
|
|
3
|
+
export function useHighlightLastVisibleOption({
|
|
4
|
+
options,
|
|
5
|
+
focussedOptionIndex,
|
|
6
|
+
setFocussedOptionIndex,
|
|
7
|
+
}) {
|
|
8
|
+
return useCallback(
|
|
9
|
+
(highestVisibleIndex) => {
|
|
10
|
+
if (!options[highestVisibleIndex].disabled) {
|
|
11
|
+
setFocussedOptionIndex(highestVisibleIndex)
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Last option on page is disabled, find next option that's not disabled
|
|
16
|
+
const followingEnabledOptionIndex = options
|
|
17
|
+
.slice(highestVisibleIndex)
|
|
18
|
+
.findIndex((option) => !option.disabled)
|
|
19
|
+
|
|
20
|
+
if (followingEnabledOptionIndex >= 0) {
|
|
21
|
+
// We need to add the highest visible index because the index is lower due to slicing the array
|
|
22
|
+
setFocussedOptionIndex(
|
|
23
|
+
followingEnabledOptionIndex + highestVisibleIndex
|
|
24
|
+
)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// No following enabled option, trying to find the closest previous sibling of the last option on the current page
|
|
29
|
+
const closestToEndOfPageEnabledOptionIndex = options
|
|
30
|
+
.slice(
|
|
31
|
+
// We don't include the currently highlighted option
|
|
32
|
+
focussedOptionIndex + 1,
|
|
33
|
+
highestVisibleIndex
|
|
34
|
+
)
|
|
35
|
+
.findLastIndex((option) => !option.disabled)
|
|
36
|
+
|
|
37
|
+
if (closestToEndOfPageEnabledOptionIndex >= 0) {
|
|
38
|
+
setFocussedOptionIndex(
|
|
39
|
+
closestToEndOfPageEnabledOptionIndex +
|
|
40
|
+
// We need to add the focused index and 1 because the index is lower due to slicing the array
|
|
41
|
+
focussedOptionIndex +
|
|
42
|
+
1
|
|
43
|
+
)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The currently highlighted option is the last enabled option
|
|
48
|
+
return
|
|
49
|
+
},
|
|
50
|
+
[options, focussedOptionIndex, setFocussedOptionIndex]
|
|
51
|
+
)
|
|
52
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useCallback } from 'react'
|
|
2
|
+
import { isOptionHidden } from '../is-option-hidden.js'
|
|
3
|
+
import { useHighlightLastOptionOnNextPage } from './use-highlight-last-option-on-next-page.js'
|
|
4
|
+
import { useHighlightLastVisibleOption } from './use-highlight-last-visible-option.js'
|
|
5
|
+
|
|
6
|
+
export function usePageDown({
|
|
7
|
+
options,
|
|
8
|
+
focussedOptionIndex,
|
|
9
|
+
setFocussedOptionIndex,
|
|
10
|
+
listBoxRef,
|
|
11
|
+
}) {
|
|
12
|
+
const highlightLastVisibleOption = useHighlightLastVisibleOption({
|
|
13
|
+
options,
|
|
14
|
+
focussedOptionIndex,
|
|
15
|
+
setFocussedOptionIndex,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const highlightLastOptionOnNextPage = useHighlightLastOptionOnNextPage({
|
|
19
|
+
options,
|
|
20
|
+
focussedOptionIndex,
|
|
21
|
+
setFocussedOptionIndex,
|
|
22
|
+
listBoxRef,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return useCallback(() => {
|
|
26
|
+
const listBoxParent = listBoxRef.current.parentNode
|
|
27
|
+
const options = Array.from(listBoxRef.current.childNodes)
|
|
28
|
+
const highestVisibleIndex = options.findLastIndex(
|
|
29
|
+
(option) => !isOptionHidden(option, listBoxParent)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if (highestVisibleIndex > focussedOptionIndex) {
|
|
33
|
+
highlightLastVisibleOption(highestVisibleIndex)
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (highestVisibleIndex > -1) {
|
|
38
|
+
highlightLastOptionOnNextPage()
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// No visible option (e.g. when menu is empty)
|
|
43
|
+
return
|
|
44
|
+
}, [
|
|
45
|
+
focussedOptionIndex,
|
|
46
|
+
listBoxRef,
|
|
47
|
+
highlightLastVisibleOption,
|
|
48
|
+
highlightLastOptionOnNextPage,
|
|
49
|
+
])
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useCallback } from 'react'
|
|
2
|
+
import { isOptionHidden } from '../is-option-hidden.js'
|
|
3
|
+
import { useHighlightFirstOptionOnPreviousPage } from './use-highlight-first-option-on-previous-page.js'
|
|
4
|
+
import { useHighlightFirstVisibleOption } from './use-highlight-first-visible-option.js'
|
|
5
|
+
|
|
6
|
+
export function usePageUp({
|
|
7
|
+
options,
|
|
8
|
+
listBoxRef,
|
|
9
|
+
focussedOptionIndex,
|
|
10
|
+
setFocussedOptionIndex,
|
|
11
|
+
}) {
|
|
12
|
+
const highlightFirstVisibleOption = useHighlightFirstVisibleOption({
|
|
13
|
+
options,
|
|
14
|
+
focussedOptionIndex,
|
|
15
|
+
setFocussedOptionIndex,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const highlightFirstOptionOnPreviousPage =
|
|
19
|
+
useHighlightFirstOptionOnPreviousPage({
|
|
20
|
+
options,
|
|
21
|
+
focussedOptionIndex,
|
|
22
|
+
setFocussedOptionIndex,
|
|
23
|
+
listBoxRef,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return useCallback(() => {
|
|
27
|
+
const listBoxParent = listBoxRef.current.parentNode
|
|
28
|
+
const optionElements = Array.from(listBoxRef.current.childNodes)
|
|
29
|
+
const lowestVisibleIndex = optionElements.findIndex(
|
|
30
|
+
(optionElement) => !isOptionHidden(optionElement, listBoxParent)
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
// No visible option (e.g. when menu is empty)
|
|
34
|
+
if (lowestVisibleIndex === -1) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (lowestVisibleIndex < focussedOptionIndex) {
|
|
39
|
+
highlightFirstVisibleOption(lowestVisibleIndex)
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
highlightFirstOptionOnPreviousPage()
|
|
44
|
+
}, [
|
|
45
|
+
focussedOptionIndex,
|
|
46
|
+
listBoxRef,
|
|
47
|
+
highlightFirstOptionOnPreviousPage,
|
|
48
|
+
highlightFirstVisibleOption,
|
|
49
|
+
])
|
|
50
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function isEnabled({ disabled }) {
|
|
2
|
+
return !disabled
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function findNextOptionIndex({ options, activeIndex }) {
|
|
6
|
+
const startIndex = activeIndex + 1
|
|
7
|
+
const optionsToSearch = options.slice(startIndex)
|
|
8
|
+
|
|
9
|
+
// Need to add back the count we removed by slicing the options array
|
|
10
|
+
return startIndex + optionsToSearch.findIndex(isEnabled)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function findPrevOptionIndex({ options, activeIndex }) {
|
|
14
|
+
return options.slice(0, activeIndex).findLastIndex(isEnabled)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function findFirstOptionIndex({ options }) {
|
|
18
|
+
return options.findIndex(isEnabled)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function findLastOptionIndex({ options }) {
|
|
22
|
+
return options.findLastIndex(isEnabled)
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SimpleSingleSelectField } from './simple-single-select-field.js'
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { requiredIf } from '@dhis2/prop-types'
|
|
2
|
+
import { sharedPropTypes } from '@dhis2/ui-constants'
|
|
3
|
+
import { Field } from '@dhis2-ui/field'
|
|
4
|
+
import PropTypes from 'prop-types'
|
|
5
|
+
import React from 'react'
|
|
6
|
+
import {
|
|
7
|
+
SimpleSingleSelect,
|
|
8
|
+
optionProp,
|
|
9
|
+
} from '../simple-single-select/index.js'
|
|
10
|
+
|
|
11
|
+
export function SimpleSingleSelectField(props) {
|
|
12
|
+
const {
|
|
13
|
+
className,
|
|
14
|
+
dataTest,
|
|
15
|
+
disabled,
|
|
16
|
+
error,
|
|
17
|
+
helpText,
|
|
18
|
+
label,
|
|
19
|
+
name,
|
|
20
|
+
onChange,
|
|
21
|
+
options,
|
|
22
|
+
required,
|
|
23
|
+
valid,
|
|
24
|
+
validationText,
|
|
25
|
+
value,
|
|
26
|
+
valueLabel,
|
|
27
|
+
warning,
|
|
28
|
+
} = props
|
|
29
|
+
const labelId = `${name}-label`
|
|
30
|
+
|
|
31
|
+
const selectedOption = options.find((opt) => opt.value === value)
|
|
32
|
+
const selected =
|
|
33
|
+
value != null && value !== ''
|
|
34
|
+
? {
|
|
35
|
+
value,
|
|
36
|
+
label: selectedOption?.label ?? valueLabel ?? '',
|
|
37
|
+
}
|
|
38
|
+
: undefined
|
|
39
|
+
|
|
40
|
+
const handleChange = (option) => {
|
|
41
|
+
onChange(option?.value ?? '')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const handleClear = () => {
|
|
45
|
+
onChange('')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Field
|
|
50
|
+
className={className}
|
|
51
|
+
dataTest={dataTest}
|
|
52
|
+
disabled={disabled}
|
|
53
|
+
required={required}
|
|
54
|
+
labelId={labelId}
|
|
55
|
+
label={label}
|
|
56
|
+
helpText={helpText}
|
|
57
|
+
validationText={validationText}
|
|
58
|
+
error={error}
|
|
59
|
+
warning={warning}
|
|
60
|
+
valid={valid}
|
|
61
|
+
>
|
|
62
|
+
<SimpleSingleSelect
|
|
63
|
+
{...props}
|
|
64
|
+
labelledBy={labelId}
|
|
65
|
+
selected={selected}
|
|
66
|
+
onChange={handleChange}
|
|
67
|
+
onClear={handleClear}
|
|
68
|
+
/>
|
|
69
|
+
</Field>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
SimpleSingleSelectField.propTypes = {
|
|
74
|
+
/** Label displayed above the input **/
|
|
75
|
+
label: PropTypes.string.isRequired,
|
|
76
|
+
|
|
77
|
+
/** necessary for IDs that are required for accessibility **/
|
|
78
|
+
name: PropTypes.string.isRequired,
|
|
79
|
+
|
|
80
|
+
/** An array of options **/
|
|
81
|
+
options: PropTypes.arrayOf(optionProp).isRequired,
|
|
82
|
+
|
|
83
|
+
/** A callback that will be called with the new value or an empty string **/
|
|
84
|
+
onChange: PropTypes.func.isRequired,
|
|
85
|
+
|
|
86
|
+
/** Will focus the select initially **/
|
|
87
|
+
autoFocus: PropTypes.bool,
|
|
88
|
+
|
|
89
|
+
/** Additional class names that will be applied to the root element **/
|
|
90
|
+
className: PropTypes.string,
|
|
91
|
+
|
|
92
|
+
/** This will allow us to put an aria-label on the clear button **/
|
|
93
|
+
clearText: requiredIf((props) => props.clearable, PropTypes.string),
|
|
94
|
+
|
|
95
|
+
/** Whether a clear button should be displayed or not **/
|
|
96
|
+
clearable: PropTypes.bool,
|
|
97
|
+
|
|
98
|
+
/** A value for a `data-test` attribute on the root element **/
|
|
99
|
+
dataTest: PropTypes.string,
|
|
100
|
+
|
|
101
|
+
/** Renders a select with lower height **/
|
|
102
|
+
dense: PropTypes.bool,
|
|
103
|
+
|
|
104
|
+
/** Disables all interactions with the select (except focussing) **/
|
|
105
|
+
disabled: PropTypes.bool,
|
|
106
|
+
|
|
107
|
+
/** Text or component to display when there are no options **/
|
|
108
|
+
empty: PropTypes.node,
|
|
109
|
+
|
|
110
|
+
/** Applies 'error' appearance for validation feedback. Mutually exclusive with `warning` and `valid` props **/
|
|
111
|
+
error: sharedPropTypes.statusPropType,
|
|
112
|
+
|
|
113
|
+
/** Help text that will be displayed below the input **/
|
|
114
|
+
filterHelpText: PropTypes.string,
|
|
115
|
+
|
|
116
|
+
/** Value will be used as aria-label attribute on the filter input **/
|
|
117
|
+
filterLabel: PropTypes.string,
|
|
118
|
+
|
|
119
|
+
/** Placeholder for the filter input **/
|
|
120
|
+
filterPlaceholder: PropTypes.string,
|
|
121
|
+
|
|
122
|
+
/** Value of the filter input **/
|
|
123
|
+
filterValue: PropTypes.string,
|
|
124
|
+
|
|
125
|
+
/** Whether the select should display a filter input **/
|
|
126
|
+
filterable: PropTypes.bool,
|
|
127
|
+
|
|
128
|
+
/** Help text, displayed below the input **/
|
|
129
|
+
helpText: PropTypes.string,
|
|
130
|
+
|
|
131
|
+
/** Will show a loading indicator at the end of the options-list **/
|
|
132
|
+
loading: PropTypes.bool,
|
|
133
|
+
|
|
134
|
+
/** Text that will be displayed next to the loading indicator **/
|
|
135
|
+
menuLoadingText: PropTypes.string,
|
|
136
|
+
|
|
137
|
+
/** Allows to modify the max height of the menu **/
|
|
138
|
+
menuMaxHeight: PropTypes.string,
|
|
139
|
+
|
|
140
|
+
/** String that will be displayed when the select is being filtered but the options array is empty **/
|
|
141
|
+
noMatchText: requiredIf((props) => props.filterable, PropTypes.string),
|
|
142
|
+
|
|
143
|
+
/** Allows to override what's rendered inside the `button[role="option"]`.
|
|
144
|
+
* Can be overriden on an individual option basis **/
|
|
145
|
+
optionComponent: PropTypes.elementType,
|
|
146
|
+
|
|
147
|
+
/** For a11y: How aggressively the user should be updated about changes in options **/
|
|
148
|
+
optionUpdateStrategy: PropTypes.oneOf(['off', 'polite', 'assertive']),
|
|
149
|
+
|
|
150
|
+
/** String to show when there's no value and no valueLabel **/
|
|
151
|
+
placeholder: PropTypes.string,
|
|
152
|
+
|
|
153
|
+
/** String that will be displayed before the label of the selected option **/
|
|
154
|
+
prefix: PropTypes.string,
|
|
155
|
+
|
|
156
|
+
/** Whether a value is required or not **/
|
|
157
|
+
required: PropTypes.bool,
|
|
158
|
+
|
|
159
|
+
/** Standard HTML tab-index attribute that will be put on the combobox's root element **/
|
|
160
|
+
tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
161
|
+
|
|
162
|
+
/** Applies 'valid' appearance for validation feedback. Mutually exclusive with `warning` and `valid` props **/
|
|
163
|
+
valid: sharedPropTypes.statusPropType,
|
|
164
|
+
|
|
165
|
+
/** Text shown below input when `props.error` is true **/
|
|
166
|
+
validationText: PropTypes.string,
|
|
167
|
+
|
|
168
|
+
/** As of now, this component does not support being uncontrolled **/
|
|
169
|
+
value: PropTypes.string,
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* When the option is not in the options list (e.g. not loaded or list is
|
|
173
|
+
* filtered), but a selected value needs to be displayed, then this prop can
|
|
174
|
+
* be used to supply the text to be shown.
|
|
175
|
+
**/
|
|
176
|
+
valueLabel: requiredIf((props) => {
|
|
177
|
+
if (props.options.find(({ value }) => props.value === value)) {
|
|
178
|
+
return false
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return props.value
|
|
182
|
+
}, PropTypes.string),
|
|
183
|
+
|
|
184
|
+
/** Applies 'warning' appearance for validation feedback. Mutually exclusive with `warning` and `valid` props **/
|
|
185
|
+
warning: sharedPropTypes.statusPropType,
|
|
186
|
+
|
|
187
|
+
/** Will be called when the combobox is loses focus **/
|
|
188
|
+
onBlur: PropTypes.func,
|
|
189
|
+
|
|
190
|
+
/** Will be called when the last option is scrolled into the visible area **/
|
|
191
|
+
onEndReached: PropTypes.func,
|
|
192
|
+
|
|
193
|
+
/** Will be called when the filter value changes **/
|
|
194
|
+
onFilterChange: PropTypes.func,
|
|
195
|
+
|
|
196
|
+
/** Will be called when the combobox is being focused **/
|
|
197
|
+
onFocus: PropTypes.func,
|
|
198
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import { SimpleSingleSelectField } from './index.js'
|
|
3
|
+
|
|
4
|
+
const description = `
|
|
5
|
+
\`SimpleSingleSelectField\` is a wrapper around a \`SimpleSingleSelect\` component that adds a label, help text, validation text, and other features.
|
|
6
|
+
|
|
7
|
+
See the SimpleSingleSelect for more information, and read more at [Design System: Select](https://github.com/dhis2/design-system/blob/master/molecules/select.md).
|
|
8
|
+
|
|
9
|
+
\`\`\`js
|
|
10
|
+
import { SimpleSingleSelectField, SingleSelectOption } from '@dhis2/ui'
|
|
11
|
+
\`\`\`
|
|
12
|
+
|
|
13
|
+
_**Note**: The dropdowns in some of the following stories won't appear correctly on this page. View these demos in the 'Canvas' tab._
|
|
14
|
+
`
|
|
15
|
+
|
|
16
|
+
const options = [
|
|
17
|
+
{ value: '1', label: 'one' },
|
|
18
|
+
{ value: '2', label: 'two' },
|
|
19
|
+
{ value: '3', label: 'three' },
|
|
20
|
+
{ value: '4', label: 'four' },
|
|
21
|
+
{ value: '5', label: 'five' },
|
|
22
|
+
{ value: '6', label: 'six' },
|
|
23
|
+
{ value: '7', label: 'seven' },
|
|
24
|
+
{ value: '8', label: 'eight' },
|
|
25
|
+
{ value: '9', label: 'nine' },
|
|
26
|
+
{ value: '10', label: 'ten' },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
export default {
|
|
30
|
+
title: 'SimpleSingleSelectField',
|
|
31
|
+
component: SimpleSingleSelectField,
|
|
32
|
+
parameters: { docs: { description: { component: description } } },
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const Default = () => {
|
|
36
|
+
const [value, setValue] = useState('3')
|
|
37
|
+
const valueLabel = value
|
|
38
|
+
? options.find((option) => option.value === value)?.label
|
|
39
|
+
: ''
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<SimpleSingleSelectField
|
|
43
|
+
name="simple"
|
|
44
|
+
label="This is the label"
|
|
45
|
+
value={value}
|
|
46
|
+
valueLabel={valueLabel}
|
|
47
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
48
|
+
options={options}
|
|
49
|
+
/>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const WithHelpText = () => {
|
|
54
|
+
const [value, setValue] = useState('3')
|
|
55
|
+
const valueLabel = value
|
|
56
|
+
? options.find((option) => option.value === value)?.label
|
|
57
|
+
: ''
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<SimpleSingleSelectField
|
|
61
|
+
name="simple"
|
|
62
|
+
label="This is the label"
|
|
63
|
+
helpText="This is the help text"
|
|
64
|
+
value={value}
|
|
65
|
+
valueLabel={valueLabel}
|
|
66
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
67
|
+
options={options}
|
|
68
|
+
/>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const StatusValid = () => {
|
|
73
|
+
const [value, setValue] = useState('3')
|
|
74
|
+
const valueLabel = value
|
|
75
|
+
? options.find((option) => option.value === value)?.label
|
|
76
|
+
: ''
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<SimpleSingleSelectField
|
|
80
|
+
valid
|
|
81
|
+
name="simple"
|
|
82
|
+
label="This is the label"
|
|
83
|
+
value={value}
|
|
84
|
+
valueLabel={valueLabel}
|
|
85
|
+
validationText="Totally valid!"
|
|
86
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
87
|
+
options={options}
|
|
88
|
+
/>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const StatusWarning = () => {
|
|
93
|
+
const [value, setValue] = useState('3')
|
|
94
|
+
const valueLabel = value
|
|
95
|
+
? options.find((option) => option.value === value)?.label
|
|
96
|
+
: ''
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<SimpleSingleSelectField
|
|
100
|
+
warning
|
|
101
|
+
name="simple"
|
|
102
|
+
label="This is the label"
|
|
103
|
+
value={value}
|
|
104
|
+
valueLabel={valueLabel}
|
|
105
|
+
validationText="Hm, not quite, I warn thee!"
|
|
106
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
107
|
+
options={options}
|
|
108
|
+
/>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const StatusError = () => {
|
|
113
|
+
const [value, setValue] = useState('3')
|
|
114
|
+
const valueLabel = value
|
|
115
|
+
? options.find((option) => option.value === value)?.label
|
|
116
|
+
: ''
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<SimpleSingleSelectField
|
|
120
|
+
error
|
|
121
|
+
name="simple"
|
|
122
|
+
label="This is the label"
|
|
123
|
+
value={value}
|
|
124
|
+
valueLabel={valueLabel}
|
|
125
|
+
validationText="That value is wrong. Sorry!"
|
|
126
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
127
|
+
options={options}
|
|
128
|
+
/>
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const StatusDisabled = () => {
|
|
133
|
+
const [value, setValue] = useState('3')
|
|
134
|
+
const valueLabel = value
|
|
135
|
+
? options.find((option) => option.value === value)?.label
|
|
136
|
+
: ''
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<SimpleSingleSelectField
|
|
140
|
+
disabled
|
|
141
|
+
name="simple"
|
|
142
|
+
label="This is the label"
|
|
143
|
+
value={value}
|
|
144
|
+
valueLabel={valueLabel}
|
|
145
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
146
|
+
options={options}
|
|
147
|
+
/>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const Required = () => {
|
|
152
|
+
const [value, setValue] = useState('3')
|
|
153
|
+
const valueLabel = value
|
|
154
|
+
? options.find((option) => option.value === value)?.label
|
|
155
|
+
: ''
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<SimpleSingleSelectField
|
|
159
|
+
required
|
|
160
|
+
name="simple"
|
|
161
|
+
label="This is the label"
|
|
162
|
+
value={value}
|
|
163
|
+
valueLabel={valueLabel}
|
|
164
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
165
|
+
options={options}
|
|
166
|
+
/>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const InputWidth = () => {
|
|
171
|
+
const [value, setValue] = useState('3')
|
|
172
|
+
const valueLabel = value
|
|
173
|
+
? options.find((option) => option.value === value)?.label
|
|
174
|
+
: ''
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<div style={{ width: 200 }}>
|
|
178
|
+
<SimpleSingleSelectField
|
|
179
|
+
name="simple"
|
|
180
|
+
label="A very long label indeed, well at least longer than the input field to show how it looks and works and stuff"
|
|
181
|
+
value={value}
|
|
182
|
+
valueLabel={valueLabel}
|
|
183
|
+
onChange={(nextValue) => setValue(nextValue)}
|
|
184
|
+
options={options}
|
|
185
|
+
/>
|
|
186
|
+
</div>
|
|
187
|
+
)
|
|
188
|
+
}
|