@transferwise/components 0.0.0-experimental-bcfa03a → 0.0.0-experimental-5cd0315
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/build/dateInput/DateInput.js +3 -6
- package/build/dateInput/DateInput.js.map +1 -1
- package/build/dateInput/DateInput.mjs +2 -5
- package/build/dateInput/DateInput.mjs.map +1 -1
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js +3 -5
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js.map +1 -1
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs +1 -3
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs.map +1 -1
- package/build/index.js +3 -5
- package/build/index.js.map +1 -1
- package/build/index.mjs +1 -3
- package/build/index.mjs.map +1 -1
- package/build/inputs/SelectInput.js +840 -0
- package/build/inputs/SelectInput.js.map +1 -0
- package/build/inputs/SelectInput.messages.js.map +1 -0
- package/build/inputs/SelectInput.messages.mjs.map +1 -0
- package/build/inputs/SelectInput.mjs +832 -0
- package/build/inputs/SelectInput.mjs.map +1 -0
- package/build/main.css +70 -65
- package/build/moneyInput/MoneyInput.js +2 -5
- package/build/moneyInput/MoneyInput.js.map +1 -1
- package/build/moneyInput/MoneyInput.mjs +1 -4
- package/build/moneyInput/MoneyInput.mjs.map +1 -1
- package/build/phoneNumberInput/PhoneNumberInput.js +2 -5
- package/build/phoneNumberInput/PhoneNumberInput.js.map +1 -1
- package/build/phoneNumberInput/PhoneNumberInput.mjs +1 -4
- package/build/phoneNumberInput/PhoneNumberInput.mjs.map +1 -1
- package/build/styles/inputs/{SelectInput/SelectInput.css → SelectInput.css} +70 -65
- package/build/styles/main.css +70 -65
- package/build/types/inputs/{SelectInput/SelectInput.types.d.ts → SelectInput.d.ts} +7 -10
- package/build/types/inputs/SelectInput.d.ts.map +1 -0
- package/build/types/inputs/SelectInput.messages.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/inputs/{SelectInput/SelectInput.css → SelectInput.css} +70 -65
- package/src/inputs/{SelectInput/SelectInput.docs.mdx → SelectInput.docs.mdx} +1 -0
- package/src/inputs/SelectInput.less +219 -0
- package/src/inputs/{SelectInput/SelectInput.story.tsx → SelectInput.story.tsx} +7 -7
- package/src/inputs/SelectInput.tsx +1209 -0
- package/src/main.css +70 -65
- package/src/main.less +1 -1
- package/build/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.js +0 -26
- package/build/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.js.map +0 -1
- package/build/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.mjs +0 -24
- package/build/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.mjs.map +0 -1
- package/build/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.js +0 -54
- package/build/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.js.map +0 -1
- package/build/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.mjs +0 -52
- package/build/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.mjs.map +0 -1
- package/build/inputs/SelectInput/OptionContent/SelectInputOptionContent.js +0 -41
- package/build/inputs/SelectInput/OptionContent/SelectInputOptionContent.js.map +0 -1
- package/build/inputs/SelectInput/OptionContent/SelectInputOptionContent.mjs +0 -38
- package/build/inputs/SelectInput/OptionContent/SelectInputOptionContent.mjs.map +0 -1
- package/build/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.js +0 -50
- package/build/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.js.map +0 -1
- package/build/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.mjs +0 -48
- package/build/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.mjs.map +0 -1
- package/build/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.js +0 -45
- package/build/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.js.map +0 -1
- package/build/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.mjs +0 -41
- package/build/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.mjs.map +0 -1
- package/build/inputs/SelectInput/Options/ItemView/SelectInputItemView.js +0 -50
- package/build/inputs/SelectInput/Options/ItemView/SelectInputItemView.js.map +0 -1
- package/build/inputs/SelectInput/Options/ItemView/SelectInputItemView.mjs +0 -48
- package/build/inputs/SelectInput/Options/ItemView/SelectInputItemView.mjs.map +0 -1
- package/build/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.js +0 -48
- package/build/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.js.map +0 -1
- package/build/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.mjs +0 -46
- package/build/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.mjs.map +0 -1
- package/build/inputs/SelectInput/Options/SelectInputOptions.js +0 -270
- package/build/inputs/SelectInput/Options/SelectInputOptions.js.map +0 -1
- package/build/inputs/SelectInput/Options/SelectInputOptions.mjs +0 -268
- package/build/inputs/SelectInput/Options/SelectInputOptions.mjs.map +0 -1
- package/build/inputs/SelectInput/SelectInput.constants.js +0 -6
- package/build/inputs/SelectInput/SelectInput.constants.js.map +0 -1
- package/build/inputs/SelectInput/SelectInput.constants.mjs +0 -4
- package/build/inputs/SelectInput/SelectInput.constants.mjs.map +0 -1
- package/build/inputs/SelectInput/SelectInput.helpers.js +0 -115
- package/build/inputs/SelectInput/SelectInput.helpers.js.map +0 -1
- package/build/inputs/SelectInput/SelectInput.helpers.mjs +0 -109
- package/build/inputs/SelectInput/SelectInput.helpers.mjs.map +0 -1
- package/build/inputs/SelectInput/SelectInput.js +0 -216
- package/build/inputs/SelectInput/SelectInput.js.map +0 -1
- package/build/inputs/SelectInput/SelectInput.messages.js.map +0 -1
- package/build/inputs/SelectInput/SelectInput.messages.mjs.map +0 -1
- package/build/inputs/SelectInput/SelectInput.mjs +0 -210
- package/build/inputs/SelectInput/SelectInput.mjs.map +0 -1
- package/build/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.js +0 -41
- package/build/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.js.map +0 -1
- package/build/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.mjs +0 -34
- package/build/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.mjs.map +0 -1
- package/build/styles/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.css +0 -17
- package/build/styles/inputs/SelectInput/OptionContent/SelectInputOptionContent.css +0 -37
- package/build/styles/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.css +0 -33
- package/build/styles/inputs/SelectInput/Options/ItemView/SelectInputItemView.css +0 -44
- package/build/styles/inputs/SelectInput/Options/SelectInputOptions.css +0 -125
- package/build/types/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.d.ts +0 -5
- package/build/types/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/DefaultTrigger/ClearButton/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/DefaultTrigger/ClearButton/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.d.ts +0 -9
- package/build/types/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/DefaultTrigger/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/DefaultTrigger/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/OptionContent/SelectInputOptionContent.d.ts +0 -9
- package/build/types/inputs/SelectInput/OptionContent/SelectInputOptionContent.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/OptionContent/index.d.ts +0 -3
- package/build/types/inputs/SelectInput/OptionContent/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.d.ts +0 -3
- package/build/types/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/GroupItemView/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/Options/GroupItemView/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.d.ts +0 -10
- package/build/types/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/ItemView/Option/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/Options/ItemView/Option/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/ItemView/SelectInputItemView.d.ts +0 -3
- package/build/types/inputs/SelectInput/Options/ItemView/SelectInputItemView.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/ItemView/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/Options/ItemView/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.d.ts +0 -6
- package/build/types/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/OptionsContainer/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/Options/OptionsContainer/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/SelectInputOptions.d.ts +0 -15
- package/build/types/inputs/SelectInput/Options/SelectInputOptions.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/Options/index.d.ts +0 -2
- package/build/types/inputs/SelectInput/Options/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/SelectInput.constants.d.ts +0 -2
- package/build/types/inputs/SelectInput/SelectInput.constants.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/SelectInput.d.ts +0 -3
- package/build/types/inputs/SelectInput/SelectInput.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/SelectInput.helpers.d.ts +0 -28
- package/build/types/inputs/SelectInput/SelectInput.helpers.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/SelectInput.messages.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/SelectInput.types.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.d.ts +0 -15
- package/build/types/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/TriggerButton/index.d.ts +0 -3
- package/build/types/inputs/SelectInput/TriggerButton/index.d.ts.map +0 -1
- package/build/types/inputs/SelectInput/index.d.ts +0 -5
- package/build/types/inputs/SelectInput/index.d.ts.map +0 -1
- package/src/inputs/SelectInput/DefaultTrigger/ClearButton/SelectInputClearButton.tsx +0 -25
- package/src/inputs/SelectInput/DefaultTrigger/ClearButton/index.ts +0 -1
- package/src/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.css +0 -17
- package/src/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.less +0 -15
- package/src/inputs/SelectInput/DefaultTrigger/SelectInputDefaultTrigger.tsx +0 -56
- package/src/inputs/SelectInput/DefaultTrigger/index.ts +0 -1
- package/src/inputs/SelectInput/OptionContent/SelectInputOptionContent.css +0 -37
- package/src/inputs/SelectInput/OptionContent/SelectInputOptionContent.less +0 -38
- package/src/inputs/SelectInput/OptionContent/SelectInputOptionContent.tsx +0 -67
- package/src/inputs/SelectInput/OptionContent/index.ts +0 -5
- package/src/inputs/SelectInput/Options/GroupItemView/SelectInputGroupItemView.tsx +0 -53
- package/src/inputs/SelectInput/Options/GroupItemView/index.ts +0 -1
- package/src/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.css +0 -33
- package/src/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.less +0 -32
- package/src/inputs/SelectInput/Options/ItemView/Option/SelectInputOption.tsx +0 -51
- package/src/inputs/SelectInput/Options/ItemView/Option/index.ts +0 -5
- package/src/inputs/SelectInput/Options/ItemView/SelectInputItemView.css +0 -44
- package/src/inputs/SelectInput/Options/ItemView/SelectInputItemView.less +0 -14
- package/src/inputs/SelectInput/Options/ItemView/SelectInputItemView.tsx +0 -37
- package/src/inputs/SelectInput/Options/ItemView/index.ts +0 -1
- package/src/inputs/SelectInput/Options/OptionsContainer/SelectInputOptionsContainer.tsx +0 -55
- package/src/inputs/SelectInput/Options/OptionsContainer/index.ts +0 -1
- package/src/inputs/SelectInput/Options/SelectInputOptions.css +0 -125
- package/src/inputs/SelectInput/Options/SelectInputOptions.less +0 -78
- package/src/inputs/SelectInput/Options/SelectInputOptions.tsx +0 -372
- package/src/inputs/SelectInput/Options/index.ts +0 -1
- package/src/inputs/SelectInput/SelectInput.constants.ts +0 -1
- package/src/inputs/SelectInput/SelectInput.helpers.ts +0 -152
- package/src/inputs/SelectInput/SelectInput.less +0 -40
- package/src/inputs/SelectInput/SelectInput.test.tsx +0 -606
- package/src/inputs/SelectInput/SelectInput.tsx +0 -247
- package/src/inputs/SelectInput/SelectInput.types.ts +0 -127
- package/src/inputs/SelectInput/TriggerButton/SelectInputTriggerButton.tsx +0 -39
- package/src/inputs/SelectInput/TriggerButton/index.ts +0 -5
- package/src/inputs/SelectInput/index.ts +0 -13
- /package/build/inputs/{SelectInput/SelectInput.messages.js → SelectInput.messages.js} +0 -0
- /package/build/inputs/{SelectInput/SelectInput.messages.mjs → SelectInput.messages.mjs} +0 -0
- /package/build/types/inputs/{SelectInput/SelectInput.messages.d.ts → SelectInput.messages.d.ts} +0 -0
- /package/src/inputs/{SelectInput/SelectInput.messages.ts → SelectInput.messages.ts} +0 -0
|
@@ -0,0 +1,832 @@
|
|
|
1
|
+
import { Listbox, ListboxOptions, ListboxButton, ListboxOption } from '@headlessui/react';
|
|
2
|
+
import { CrossCircle, ChevronDown, Cross, Check } from '@transferwise/icons';
|
|
3
|
+
import { clsx } from 'clsx';
|
|
4
|
+
import mergeProps from 'merge-props';
|
|
5
|
+
import { useState, useRef, useEffect, useDeferredValue, createContext, useContext, useMemo, useId, forwardRef } from 'react';
|
|
6
|
+
import { useIntl } from 'react-intl';
|
|
7
|
+
import { Virtualizer } from 'virtua';
|
|
8
|
+
import { useEffectEvent } from '../common/hooks/useEffectEvent.mjs';
|
|
9
|
+
import { useScreenSize } from '../common/hooks/useScreenSize.mjs';
|
|
10
|
+
import { PolymorphicWithOverrides } from '../common/polymorphicWithOverrides/PolymorphicWithOverrides.mjs';
|
|
11
|
+
import { Breakpoint } from '../common/propsValues/breakpoint.mjs';
|
|
12
|
+
import messages$1 from '../dateLookup/dateTrigger/DateTrigger.messages.mjs';
|
|
13
|
+
import { BottomSheet } from './_BottomSheet.mjs';
|
|
14
|
+
import { ButtonInput } from './_ButtonInput.mjs';
|
|
15
|
+
import { Popover } from './_Popover.mjs';
|
|
16
|
+
import { useInputAttributes } from './contexts.mjs';
|
|
17
|
+
import { InputGroup } from './InputGroup.mjs';
|
|
18
|
+
import { SearchInput } from './SearchInput.mjs';
|
|
19
|
+
import messages from './SelectInput.messages.mjs';
|
|
20
|
+
import Header from '../header/Header.mjs';
|
|
21
|
+
import Section from '../section/Section.mjs';
|
|
22
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
23
|
+
|
|
24
|
+
const MAX_ITEMS_WITHOUT_VIRTUALIZATION = 50;
|
|
25
|
+
function searchableString(value) {
|
|
26
|
+
return value.trim().replace(/\s+/gu, ' ')
|
|
27
|
+
// NFD converts an Å to A + ̊ (and other special characters)
|
|
28
|
+
.normalize('NFD')
|
|
29
|
+
// and then this replaces the ̊ with nothing (and other special characters)
|
|
30
|
+
.replace(/[\u0300-\u036f]/g, '').toLowerCase();
|
|
31
|
+
}
|
|
32
|
+
function inferSearchableStrings(value) {
|
|
33
|
+
if (typeof value === 'string') {
|
|
34
|
+
return [searchableString(value)];
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === 'object' && value != null) {
|
|
37
|
+
return Object.values(value).filter(innerValue => typeof innerValue === 'string').map(innerValue => searchableString(innerValue));
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
function dedupeSelectInputOptionItem(item, existingValues, compareValues) {
|
|
42
|
+
const isDuplicate = compareValues ? Array.from(existingValues).some(existingValue => compareValues(item.value, existingValue)) : existingValues.has(item.value);
|
|
43
|
+
if (!isDuplicate) {
|
|
44
|
+
existingValues.add(item.value);
|
|
45
|
+
return item;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
...item,
|
|
49
|
+
value: undefined
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Sets the `value` of duplicate option items to `undefined`, hiding them when
|
|
54
|
+
* rendered. Indexes are kept intact within groups to preserve the active item
|
|
55
|
+
* between filter changes when possible.
|
|
56
|
+
*/
|
|
57
|
+
function dedupeSelectInputItems(items, compareValues) {
|
|
58
|
+
const existingValues = new Set();
|
|
59
|
+
return items.map(item => {
|
|
60
|
+
switch (item.type) {
|
|
61
|
+
case 'option':
|
|
62
|
+
{
|
|
63
|
+
return dedupeSelectInputOptionItem(item, existingValues, compareValues);
|
|
64
|
+
}
|
|
65
|
+
case 'group':
|
|
66
|
+
{
|
|
67
|
+
return {
|
|
68
|
+
...item,
|
|
69
|
+
options: item.options.map(option => dedupeSelectInputOptionItem(option, existingValues, compareValues))
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return item;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function selectInputOptionItemIncludesNeedle(item, needle) {
|
|
77
|
+
return inferSearchableStrings(item.filterMatchers ?? item.value).some(haystack => haystack.includes(needle));
|
|
78
|
+
}
|
|
79
|
+
function filterSelectInputItems(items, predicate) {
|
|
80
|
+
return items.filter(item => {
|
|
81
|
+
switch (item.type) {
|
|
82
|
+
case 'option':
|
|
83
|
+
{
|
|
84
|
+
return predicate(item);
|
|
85
|
+
}
|
|
86
|
+
case 'group':
|
|
87
|
+
{
|
|
88
|
+
return item.options.some(option => predicate(option));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Flattens and sorts filtered options using the provided comparator.
|
|
96
|
+
* Extracts all options from groups, filters out undefined values (deduplicated items),
|
|
97
|
+
* sorts them, and returns as a flat list of option items.
|
|
98
|
+
*/
|
|
99
|
+
function sortSelectInputItems(items, compareFn, searchQuery) {
|
|
100
|
+
const flattenedOption = items.flatMap(item => {
|
|
101
|
+
if (item.type === 'option') {
|
|
102
|
+
return item.value !== undefined ? [item] : [];
|
|
103
|
+
}
|
|
104
|
+
if (item.type === 'group') {
|
|
105
|
+
return item.options.filter(option => option.value !== undefined);
|
|
106
|
+
}
|
|
107
|
+
return [];
|
|
108
|
+
});
|
|
109
|
+
// eslint-disable-next-line functional/immutable-data
|
|
110
|
+
return flattenedOption.sort((a, b) => compareFn(a, b, searchQuery));
|
|
111
|
+
}
|
|
112
|
+
const defaultRenderTrigger = ({
|
|
113
|
+
content,
|
|
114
|
+
placeholderShown,
|
|
115
|
+
clear,
|
|
116
|
+
disabled,
|
|
117
|
+
size,
|
|
118
|
+
className
|
|
119
|
+
}) => /*#__PURE__*/jsx(InputGroup, {
|
|
120
|
+
addonEnd: {
|
|
121
|
+
content: /*#__PURE__*/jsxs("span", {
|
|
122
|
+
className: clsx('np-select-input-addon-container', disabled && 'disabled'),
|
|
123
|
+
children: [clear != null && !placeholderShown ? /*#__PURE__*/jsxs(Fragment, {
|
|
124
|
+
children: [/*#__PURE__*/jsx(SelectInputClearButton, {
|
|
125
|
+
onClick: event => {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
clear();
|
|
128
|
+
}
|
|
129
|
+
}), /*#__PURE__*/jsx("span", {
|
|
130
|
+
className: "np-select-input-addon-separator"
|
|
131
|
+
})]
|
|
132
|
+
}) : null, /*#__PURE__*/jsx("span", {
|
|
133
|
+
className: "np-select-input-addon",
|
|
134
|
+
children: /*#__PURE__*/jsx(ChevronDown, {
|
|
135
|
+
size: 16
|
|
136
|
+
})
|
|
137
|
+
})]
|
|
138
|
+
}),
|
|
139
|
+
initialContentWidth: 24 + 4,
|
|
140
|
+
padding: 'sm'
|
|
141
|
+
},
|
|
142
|
+
disabled: disabled,
|
|
143
|
+
className: className,
|
|
144
|
+
children: /*#__PURE__*/jsx(SelectInputTriggerButton, {
|
|
145
|
+
as: ButtonInput,
|
|
146
|
+
size: size,
|
|
147
|
+
children: /*#__PURE__*/jsx("span", {
|
|
148
|
+
className: clsx('np-select-input-content', placeholderShown && 'np-select-input-placeholder'),
|
|
149
|
+
children: content
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
});
|
|
153
|
+
function SelectInputClearButton({
|
|
154
|
+
className,
|
|
155
|
+
onClick
|
|
156
|
+
}) {
|
|
157
|
+
const intl = useIntl();
|
|
158
|
+
return /*#__PURE__*/jsx("button", {
|
|
159
|
+
type: "button",
|
|
160
|
+
"aria-label": intl.formatMessage(messages$1.ariaLabel),
|
|
161
|
+
className: clsx(className, 'np-select-input-addon np-select-input-addon--interactive'),
|
|
162
|
+
onClick: onClick,
|
|
163
|
+
children: /*#__PURE__*/jsx(Cross, {
|
|
164
|
+
size: 16
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const noop = () => {};
|
|
169
|
+
function SelectInput({
|
|
170
|
+
id: idProp,
|
|
171
|
+
parentId,
|
|
172
|
+
name,
|
|
173
|
+
multiple,
|
|
174
|
+
placeholder,
|
|
175
|
+
autocomplete,
|
|
176
|
+
items,
|
|
177
|
+
defaultValue,
|
|
178
|
+
value: controlledValue,
|
|
179
|
+
compareValues,
|
|
180
|
+
renderValue = String,
|
|
181
|
+
renderFooter,
|
|
182
|
+
renderTrigger = defaultRenderTrigger,
|
|
183
|
+
filterable,
|
|
184
|
+
filterPlaceholder,
|
|
185
|
+
sortFilteredOptions,
|
|
186
|
+
disabled,
|
|
187
|
+
size = 'md',
|
|
188
|
+
className,
|
|
189
|
+
UNSAFE_triggerButtonProps,
|
|
190
|
+
triggerRef: externalTriggerRef,
|
|
191
|
+
onFilterChange = noop,
|
|
192
|
+
onChange,
|
|
193
|
+
onOpen,
|
|
194
|
+
onClose,
|
|
195
|
+
onClear
|
|
196
|
+
}) {
|
|
197
|
+
const inputAttributes = useInputAttributes({
|
|
198
|
+
nonLabelable: true
|
|
199
|
+
});
|
|
200
|
+
const id = idProp ?? inputAttributes.id;
|
|
201
|
+
const [open, setOpen] = useState(false);
|
|
202
|
+
const initialized = useRef(false);
|
|
203
|
+
const handleClose = useEffectEvent(onClose ?? (() => {}));
|
|
204
|
+
const handleOpen = useEffectEvent(onOpen ?? (() => {}));
|
|
205
|
+
useEffect(() => {
|
|
206
|
+
if (initialized.current) {
|
|
207
|
+
if (open) {
|
|
208
|
+
handleOpen?.();
|
|
209
|
+
} else {
|
|
210
|
+
handleClose?.();
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
initialized.current = true;
|
|
214
|
+
}
|
|
215
|
+
}, [handleClose, handleOpen, open]);
|
|
216
|
+
const [filterQuery, _setFilterQuery] = useState('');
|
|
217
|
+
const deferredFilterQuery = useDeferredValue(filterQuery);
|
|
218
|
+
const setFilterQuery = useEffectEvent(query => {
|
|
219
|
+
_setFilterQuery(query);
|
|
220
|
+
if (query !== filterQuery) {
|
|
221
|
+
onFilterChange({
|
|
222
|
+
query,
|
|
223
|
+
queryNormalized: query ? searchableString(query) : null
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
const internalTriggerRef = useRef(null);
|
|
228
|
+
const screenSm = useScreenSize(Breakpoint.SMALL);
|
|
229
|
+
const OptionsOverlay = screenSm ? Popover : BottomSheet;
|
|
230
|
+
const searchInputRef = useRef(null);
|
|
231
|
+
const listboxRef = useRef(null);
|
|
232
|
+
const controllerRef = filterable ? searchInputRef : listboxRef;
|
|
233
|
+
/**
|
|
234
|
+
* Attempts to resolve the `listbox` label
|
|
235
|
+
* @see https://storybook.wise.design/?path=/docs/forms-selectinput-accessibility--docs#labelling
|
|
236
|
+
*/
|
|
237
|
+
const getListBoxLabelProps = () => {
|
|
238
|
+
if (UNSAFE_triggerButtonProps?.['aria-label']) {
|
|
239
|
+
return {
|
|
240
|
+
listBoxLabel: UNSAFE_triggerButtonProps['aria-label']
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
if (UNSAFE_triggerButtonProps?.['aria-labelledby']) {
|
|
244
|
+
return {
|
|
245
|
+
listBoxLabelledBy: UNSAFE_triggerButtonProps['aria-labelledby']
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (inputAttributes['aria-labelledby']) {
|
|
249
|
+
return {
|
|
250
|
+
listBoxLabelledBy: inputAttributes['aria-labelledby']
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
return {};
|
|
254
|
+
};
|
|
255
|
+
return /*#__PURE__*/jsx(Listbox, {
|
|
256
|
+
name: name,
|
|
257
|
+
multiple: multiple,
|
|
258
|
+
defaultValue: defaultValue,
|
|
259
|
+
value: controlledValue,
|
|
260
|
+
by: compareValues,
|
|
261
|
+
disabled: disabled,
|
|
262
|
+
onChange: value => {
|
|
263
|
+
if (!multiple) {
|
|
264
|
+
setOpen(false);
|
|
265
|
+
}
|
|
266
|
+
onChange?.(value);
|
|
267
|
+
},
|
|
268
|
+
children: ({
|
|
269
|
+
disabled: uiDisabled,
|
|
270
|
+
value
|
|
271
|
+
}) => {
|
|
272
|
+
const placeholderShown = multiple && Array.isArray(value) ? value.length === 0 : value == null;
|
|
273
|
+
return /*#__PURE__*/jsx(OptionsOverlay, {
|
|
274
|
+
placement: "bottom-start",
|
|
275
|
+
open: open,
|
|
276
|
+
renderTrigger: ({
|
|
277
|
+
ref,
|
|
278
|
+
getInteractionProps
|
|
279
|
+
}) => /*#__PURE__*/jsx(SelectInputTriggerButtonPropsContext.Provider, {
|
|
280
|
+
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
281
|
+
value: {
|
|
282
|
+
ref: node => {
|
|
283
|
+
ref(node);
|
|
284
|
+
if (externalTriggerRef) {
|
|
285
|
+
// eslint-disable-next-line no-param-reassign
|
|
286
|
+
externalTriggerRef.current = node;
|
|
287
|
+
} else {
|
|
288
|
+
internalTriggerRef.current = node;
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
...inputAttributes,
|
|
292
|
+
...UNSAFE_triggerButtonProps,
|
|
293
|
+
id,
|
|
294
|
+
...mergeProps({
|
|
295
|
+
onClick: () => {
|
|
296
|
+
setOpen(prev => !prev);
|
|
297
|
+
},
|
|
298
|
+
onKeyDown: event => {
|
|
299
|
+
if (event.key === ' ' || event.key === 'Enter' || event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
300
|
+
setOpen(prev => !prev);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}, getInteractionProps())
|
|
304
|
+
},
|
|
305
|
+
children: renderTrigger({
|
|
306
|
+
content: !placeholderShown ? /*#__PURE__*/jsx(SelectInputOptionContentWithinTriggerContext.Provider, {
|
|
307
|
+
value: true,
|
|
308
|
+
children: multiple && Array.isArray(value) ? value.map(option => renderValue(option, true)).filter(node => node != null).join(', ') : renderValue(value, true)
|
|
309
|
+
}) : placeholder,
|
|
310
|
+
placeholderShown,
|
|
311
|
+
clear: onClear != null ? () => {
|
|
312
|
+
onClear();
|
|
313
|
+
(externalTriggerRef?.current ?? internalTriggerRef.current)?.focus({
|
|
314
|
+
preventScroll: true
|
|
315
|
+
});
|
|
316
|
+
} : undefined,
|
|
317
|
+
disabled: uiDisabled,
|
|
318
|
+
size,
|
|
319
|
+
className
|
|
320
|
+
})
|
|
321
|
+
}),
|
|
322
|
+
initialFocusRef: controllerRef,
|
|
323
|
+
size: filterable ? 'lg' : 'md',
|
|
324
|
+
padding: "none",
|
|
325
|
+
onClose: () => {
|
|
326
|
+
setOpen(false);
|
|
327
|
+
},
|
|
328
|
+
onCloseEnd: () => {
|
|
329
|
+
setFilterQuery('');
|
|
330
|
+
},
|
|
331
|
+
children: /*#__PURE__*/jsx(SelectInputOptions, {
|
|
332
|
+
id: id ? `${id}Search` : undefined,
|
|
333
|
+
parentId: parentId,
|
|
334
|
+
items: items,
|
|
335
|
+
compareValues: compareValues,
|
|
336
|
+
renderValue: renderValue,
|
|
337
|
+
renderFooter: renderFooter,
|
|
338
|
+
filterable: filterable,
|
|
339
|
+
filterPlaceholder: filterPlaceholder,
|
|
340
|
+
sortFilteredOptions: sortFilteredOptions,
|
|
341
|
+
searchInputRef: searchInputRef,
|
|
342
|
+
listboxRef: listboxRef,
|
|
343
|
+
filterQuery: deferredFilterQuery,
|
|
344
|
+
autocomplete: autocomplete,
|
|
345
|
+
name: name,
|
|
346
|
+
onFilterChange: setFilterQuery,
|
|
347
|
+
onAutocompleteSelect: matchedValue => {
|
|
348
|
+
onChange?.(matchedValue);
|
|
349
|
+
if (!multiple) {
|
|
350
|
+
setOpen(false);
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
...getListBoxLabelProps()
|
|
354
|
+
})
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
const SelectInputTriggerButtonPropsContext = /*#__PURE__*/createContext({});
|
|
360
|
+
function SelectInputTriggerButton({
|
|
361
|
+
as = 'button',
|
|
362
|
+
...restProps
|
|
363
|
+
}) {
|
|
364
|
+
const {
|
|
365
|
+
ref,
|
|
366
|
+
onClick,
|
|
367
|
+
onKeyDown,
|
|
368
|
+
...interactionProps
|
|
369
|
+
} = useContext(SelectInputTriggerButtonPropsContext);
|
|
370
|
+
return /*#__PURE__*/jsx(ListboxButton, {
|
|
371
|
+
ref: ref,
|
|
372
|
+
as: PolymorphicWithOverrides,
|
|
373
|
+
role: "combobox",
|
|
374
|
+
__overrides: {
|
|
375
|
+
as,
|
|
376
|
+
...interactionProps
|
|
377
|
+
},
|
|
378
|
+
...mergeProps({
|
|
379
|
+
onClick,
|
|
380
|
+
onKeyDown
|
|
381
|
+
}, restProps)
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const SelectInputOptionsContainer = /*#__PURE__*/forwardRef(function SelectInputOptionsContainer({
|
|
385
|
+
'aria-orientation': ariaOrientation,
|
|
386
|
+
'aria-activedescendant': ariaActiveDescendant,
|
|
387
|
+
role,
|
|
388
|
+
tabIndex,
|
|
389
|
+
onAriaActiveDescendantChange,
|
|
390
|
+
onKeyDown,
|
|
391
|
+
...restProps
|
|
392
|
+
}, ref) {
|
|
393
|
+
const handleAriaActiveDescendantChange = useEffectEvent(onAriaActiveDescendantChange);
|
|
394
|
+
useEffect(() => {
|
|
395
|
+
handleAriaActiveDescendantChange(ariaActiveDescendant);
|
|
396
|
+
}, [ariaActiveDescendant, handleAriaActiveDescendantChange]);
|
|
397
|
+
return /*#__PURE__*/jsx("div", {
|
|
398
|
+
ref: ref,
|
|
399
|
+
role: "none",
|
|
400
|
+
onKeyDown: event => {
|
|
401
|
+
// Prevent confirmation close without an active item
|
|
402
|
+
if (event.key === 'Enter' && ariaActiveDescendant == null) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
// Required to make ListBox focusable
|
|
406
|
+
if (event.key === 'Tab') {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
// Prevent absorbing Escape early
|
|
410
|
+
if (event.key === 'Escape') {
|
|
411
|
+
onKeyDown?.({
|
|
412
|
+
...event,
|
|
413
|
+
preventDefault: () => {},
|
|
414
|
+
stopPropagation: () => {}
|
|
415
|
+
});
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
onKeyDown?.(event);
|
|
419
|
+
},
|
|
420
|
+
...restProps
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
function SelectInputOptions({
|
|
424
|
+
id,
|
|
425
|
+
parentId,
|
|
426
|
+
items,
|
|
427
|
+
compareValues: compareValuesProp,
|
|
428
|
+
renderValue = String,
|
|
429
|
+
renderFooter,
|
|
430
|
+
filterable = false,
|
|
431
|
+
filterPlaceholder,
|
|
432
|
+
sortFilteredOptions,
|
|
433
|
+
searchInputRef,
|
|
434
|
+
listboxRef,
|
|
435
|
+
filterQuery,
|
|
436
|
+
onFilterChange,
|
|
437
|
+
listBoxLabel,
|
|
438
|
+
listBoxLabelledBy,
|
|
439
|
+
autocomplete,
|
|
440
|
+
name,
|
|
441
|
+
onAutocompleteSelect
|
|
442
|
+
}) {
|
|
443
|
+
const intl = useIntl();
|
|
444
|
+
const virtualiserHandlerRef = useRef(null);
|
|
445
|
+
const controllerRef = filterable ? searchInputRef : listboxRef;
|
|
446
|
+
const [initialRender, setInitialRender] = useState(true);
|
|
447
|
+
const needle = useMemo(() => {
|
|
448
|
+
if (filterable) {
|
|
449
|
+
return filterQuery ? searchableString(filterQuery) : null;
|
|
450
|
+
}
|
|
451
|
+
return undefined;
|
|
452
|
+
}, [filterQuery, filterable]);
|
|
453
|
+
useEffect(() => {
|
|
454
|
+
if (needle) {
|
|
455
|
+
// Ensure having an active option while filtering.
|
|
456
|
+
// Without `requestAnimationFrame` upon which React depends for scheduling
|
|
457
|
+
// updates, the active status would only show for a split second and then
|
|
458
|
+
// disappear inadvertently.
|
|
459
|
+
requestAnimationFrame(() => {
|
|
460
|
+
if (controllerRef.current != null && !controllerRef.current.hasAttribute('aria-activedescendant')) {
|
|
461
|
+
// Activate first option via synthetic key press
|
|
462
|
+
controllerRef.current.dispatchEvent(new KeyboardEvent('keydown', {
|
|
463
|
+
key: 'Home',
|
|
464
|
+
bubbles: true
|
|
465
|
+
}));
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}, [controllerRef, needle]);
|
|
470
|
+
const compareValues = useMemo(() => {
|
|
471
|
+
if (!compareValuesProp) {
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
if (typeof compareValuesProp === 'function') {
|
|
475
|
+
return (a, b) => compareValuesProp(a, b);
|
|
476
|
+
}
|
|
477
|
+
const key = compareValuesProp;
|
|
478
|
+
return (a, b) => {
|
|
479
|
+
if (typeof a === 'object' && a != null && typeof b === 'object' && b != null) {
|
|
480
|
+
return a[key] === b[key];
|
|
481
|
+
}
|
|
482
|
+
return a === b;
|
|
483
|
+
};
|
|
484
|
+
}, [compareValuesProp]);
|
|
485
|
+
const filteredItems = useMemo(() => {
|
|
486
|
+
if (needle == null) {
|
|
487
|
+
return items;
|
|
488
|
+
}
|
|
489
|
+
const dedupedItems = dedupeSelectInputItems(items, compareValues);
|
|
490
|
+
if (sortFilteredOptions) {
|
|
491
|
+
// When sorting, filter out non-matching items completely to avoid ghost items
|
|
492
|
+
const filtered = dedupedItems.map(item => {
|
|
493
|
+
if (item.type === 'option') {
|
|
494
|
+
return selectInputOptionItemIncludesNeedle(item, needle) ? item : {
|
|
495
|
+
...item,
|
|
496
|
+
value: undefined
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if (item.type === 'group') {
|
|
500
|
+
return {
|
|
501
|
+
...item,
|
|
502
|
+
options: item.options.map(option => selectInputOptionItemIncludesNeedle(option, needle) ? option : {
|
|
503
|
+
...option,
|
|
504
|
+
value: undefined
|
|
505
|
+
})
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return item;
|
|
509
|
+
});
|
|
510
|
+
return sortSelectInputItems(filtered, sortFilteredOptions, filterQuery);
|
|
511
|
+
}
|
|
512
|
+
return filterSelectInputItems(dedupedItems, item => selectInputOptionItemIncludesNeedle(item, needle));
|
|
513
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
514
|
+
}, [needle, items, compareValues]);
|
|
515
|
+
const resultsEmpty = needle != null && filteredItems.length === 0;
|
|
516
|
+
const virtualized = filteredItems.length > MAX_ITEMS_WITHOUT_VIRTUALIZATION;
|
|
517
|
+
// Items shown once shall be kept mounted until the needle changes, otherwise
|
|
518
|
+
// the scroll position may jump around inadvertently. Pattern adopted from:
|
|
519
|
+
// https://inokawa.github.io/virtua/?path=/story/advanced-keep-offscreen-items--append-only
|
|
520
|
+
const [mountedIndexes, setMountedIndexes] = useState([]);
|
|
521
|
+
useEffect(() => {
|
|
522
|
+
// Ensure the 'End' key works as intended by keeping the last item mounted
|
|
523
|
+
setMountedIndexes(prevMountedIndexes => {
|
|
524
|
+
const indexes = new Set(prevMountedIndexes);
|
|
525
|
+
indexes.add(filteredItems.length - 1);
|
|
526
|
+
return [...indexes]; // Sorting is redundant by nature here
|
|
527
|
+
});
|
|
528
|
+
}, [needle,
|
|
529
|
+
// Needed as `filteredItems.length` may be equal between two updates
|
|
530
|
+
filteredItems.length]);
|
|
531
|
+
const listboxContainerRef = useRef(null);
|
|
532
|
+
useEffect(() => {
|
|
533
|
+
if (listboxContainerRef.current != null) {
|
|
534
|
+
listboxContainerRef.current.style.setProperty('--initial-height', `${listboxContainerRef.current.offsetHeight}px`);
|
|
535
|
+
}
|
|
536
|
+
}, []);
|
|
537
|
+
useEffect(() => {
|
|
538
|
+
setInitialRender(false);
|
|
539
|
+
}, []);
|
|
540
|
+
const showStatus = resultsEmpty;
|
|
541
|
+
const statusId = useId();
|
|
542
|
+
const listboxId = useId();
|
|
543
|
+
const getItemNode = index => {
|
|
544
|
+
const item = filteredItems[index];
|
|
545
|
+
return /*#__PURE__*/jsx(SelectInputItemView, {
|
|
546
|
+
item: item,
|
|
547
|
+
renderValue: renderValue,
|
|
548
|
+
needle: needle
|
|
549
|
+
}, index);
|
|
550
|
+
};
|
|
551
|
+
const findMatchingItem = autocompleteValue => {
|
|
552
|
+
const flatOptions = items.flatMap(item => item.type === 'group' ? item.options : item.type === 'option' ? [item] : []).filter(item => item.type === 'option' && item.value != null);
|
|
553
|
+
const exactMatch = flatOptions.find(option => String(option.value) === autocompleteValue || option.filterMatchers?.some(matcher => matcher === autocompleteValue));
|
|
554
|
+
if (exactMatch) {
|
|
555
|
+
return exactMatch.value;
|
|
556
|
+
}
|
|
557
|
+
const fuzzyMatch = flatOptions.find(option => option.filterMatchers?.some(matcher => matcher.toLowerCase().includes(autocompleteValue.toLowerCase())));
|
|
558
|
+
return fuzzyMatch ? fuzzyMatch.value : null;
|
|
559
|
+
};
|
|
560
|
+
return /*#__PURE__*/jsxs(ListboxOptions, {
|
|
561
|
+
modal: true,
|
|
562
|
+
as: SelectInputOptionsContainer,
|
|
563
|
+
static: true,
|
|
564
|
+
className: "np-select-input-options-container",
|
|
565
|
+
onAriaActiveDescendantChange: value => {
|
|
566
|
+
if (controllerRef.current != null) {
|
|
567
|
+
if (!initialRender && value != null) {
|
|
568
|
+
controllerRef.current.setAttribute('aria-activedescendant', value);
|
|
569
|
+
} else {
|
|
570
|
+
controllerRef.current.removeAttribute('aria-activedescendant');
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
},
|
|
574
|
+
children: [filterable ? /*#__PURE__*/jsx("div", {
|
|
575
|
+
className: "np-select-input-query-container",
|
|
576
|
+
children: /*#__PURE__*/jsx(SearchInput, {
|
|
577
|
+
ref: searchInputRef,
|
|
578
|
+
id: id,
|
|
579
|
+
name: name,
|
|
580
|
+
autoComplete: autocomplete,
|
|
581
|
+
role: "combobox",
|
|
582
|
+
shape: "rectangle",
|
|
583
|
+
placeholder: filterPlaceholder,
|
|
584
|
+
"aria-label": filterPlaceholder,
|
|
585
|
+
defaultValue: filterQuery,
|
|
586
|
+
"aria-autocomplete": "list",
|
|
587
|
+
"aria-expanded": true,
|
|
588
|
+
"aria-controls": listboxId,
|
|
589
|
+
"aria-describedby": showStatus ? statusId : undefined,
|
|
590
|
+
onKeyDown: event => {
|
|
591
|
+
// Prevent interfering with the matcher of Headless UI
|
|
592
|
+
// https://mathiasbynens.be/notes/javascript-unicode#regex
|
|
593
|
+
if (/^.$/u.test(event.key)) {
|
|
594
|
+
event.stopPropagation();
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
onChange: event => {
|
|
598
|
+
// Free up resources and ensure not to go out of bounds when the
|
|
599
|
+
// resulting item count is less than before
|
|
600
|
+
const inputValue = event.currentTarget.value;
|
|
601
|
+
// Free up resources and ensure not to go out of bounds
|
|
602
|
+
setMountedIndexes([]);
|
|
603
|
+
onFilterChange(inputValue);
|
|
604
|
+
},
|
|
605
|
+
onInput: event => {
|
|
606
|
+
const inputValue = event.currentTarget.value;
|
|
607
|
+
const inputElement = event.currentTarget;
|
|
608
|
+
if (autocomplete && onAutocompleteSelect && inputValue) {
|
|
609
|
+
setTimeout(() => {
|
|
610
|
+
if (inputElement.value === inputValue && inputValue.length > 2) {
|
|
611
|
+
const matchedValue = findMatchingItem(inputValue);
|
|
612
|
+
if (matchedValue !== null) {
|
|
613
|
+
onAutocompleteSelect(matchedValue);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}, 50);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
})
|
|
620
|
+
}) : null, /*#__PURE__*/jsxs("section", {
|
|
621
|
+
ref: listboxContainerRef,
|
|
622
|
+
tabIndex: -1,
|
|
623
|
+
className: clsx('np-select-input-listbox-container', virtualized && 'np-select-input-listbox-container--virtualized', needle == null &&
|
|
624
|
+
// Groups aren't shown when filtering
|
|
625
|
+
items.some(item => item.type === 'group') && 'np-select-input-listbox-container--has-group'),
|
|
626
|
+
"data-wds-parent": parentId ?? undefined,
|
|
627
|
+
children: [resultsEmpty ? /*#__PURE__*/jsxs("div", {
|
|
628
|
+
id: statusId,
|
|
629
|
+
className: "np-select-input-options-status",
|
|
630
|
+
children: [/*#__PURE__*/jsx(CrossCircle, {
|
|
631
|
+
size: 16,
|
|
632
|
+
className: "np-select-input-options-status-icon"
|
|
633
|
+
}), intl.formatMessage(messages.noResultsFound)]
|
|
634
|
+
}) : null, /*#__PURE__*/jsx("div", {
|
|
635
|
+
ref: listboxRef,
|
|
636
|
+
id: listboxId,
|
|
637
|
+
role: "listbox",
|
|
638
|
+
"aria-orientation": "vertical",
|
|
639
|
+
"aria-label": listBoxLabel,
|
|
640
|
+
"aria-labelledby": listBoxLabelledBy,
|
|
641
|
+
tabIndex: 0,
|
|
642
|
+
className: "np-select-input-listbox",
|
|
643
|
+
children: !virtualized ? filteredItems.map((_, index) => getItemNode(index)) : /*#__PURE__*/jsx(Virtualizer, {
|
|
644
|
+
ref: virtualiserHandlerRef,
|
|
645
|
+
data: filteredItems,
|
|
646
|
+
keepMounted: mountedIndexes,
|
|
647
|
+
scrollRef: listboxRef // `VList` doesn't expose this
|
|
648
|
+
,
|
|
649
|
+
onScroll: async () => {
|
|
650
|
+
if (!virtualiserHandlerRef.current) return;
|
|
651
|
+
const startIndex = virtualiserHandlerRef.current.findItemIndex(virtualiserHandlerRef.current.scrollOffset);
|
|
652
|
+
const endIndex = virtualiserHandlerRef.current.findItemIndex(virtualiserHandlerRef.current.scrollOffset + virtualiserHandlerRef.current.viewportSize);
|
|
653
|
+
setMountedIndexes(prevMountedIndexes => {
|
|
654
|
+
const indexes = new Set(prevMountedIndexes);
|
|
655
|
+
for (let index = startIndex; index <= endIndex; index += 1) {
|
|
656
|
+
indexes.add(index);
|
|
657
|
+
}
|
|
658
|
+
return [...indexes].sort((a, b) => a - b);
|
|
659
|
+
});
|
|
660
|
+
},
|
|
661
|
+
children: (item, index) =>
|
|
662
|
+
/*#__PURE__*/
|
|
663
|
+
// The position of each item can't be inferred by browsers when
|
|
664
|
+
// virtualizing, as some of the items may not be in the DOM
|
|
665
|
+
jsx(SelectInputItemsCountContext.Provider, {
|
|
666
|
+
value: filteredItems.length,
|
|
667
|
+
children: /*#__PURE__*/jsx(SelectInputItemPositionContext.Provider, {
|
|
668
|
+
value: index + 1,
|
|
669
|
+
children: getItemNode(index)
|
|
670
|
+
})
|
|
671
|
+
})
|
|
672
|
+
})
|
|
673
|
+
}), renderFooter != null ? /*#__PURE__*/jsx("footer", {
|
|
674
|
+
className: "np-select-input-footer",
|
|
675
|
+
children: /*#__PURE__*/jsx("div", {
|
|
676
|
+
role: "none",
|
|
677
|
+
onKeyDown: event => {
|
|
678
|
+
// Prevent interfering with Headless UI
|
|
679
|
+
if (event.key !== 'Escape') {
|
|
680
|
+
event.stopPropagation();
|
|
681
|
+
}
|
|
682
|
+
},
|
|
683
|
+
children: renderFooter({
|
|
684
|
+
resultsEmpty,
|
|
685
|
+
queryNormalized: needle
|
|
686
|
+
})
|
|
687
|
+
})
|
|
688
|
+
}) : null]
|
|
689
|
+
})]
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
function SelectInputItemView({
|
|
693
|
+
item,
|
|
694
|
+
renderValue,
|
|
695
|
+
needle
|
|
696
|
+
}) {
|
|
697
|
+
switch (item.type) {
|
|
698
|
+
case 'option':
|
|
699
|
+
{
|
|
700
|
+
if (item.value != null && (needle == null || selectInputOptionItemIncludesNeedle(item, needle))) {
|
|
701
|
+
return /*#__PURE__*/jsx(SelectInputOption, {
|
|
702
|
+
value: item.value,
|
|
703
|
+
disabled: item.disabled,
|
|
704
|
+
children: renderValue(item.value, false)
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
case 'group':
|
|
710
|
+
{
|
|
711
|
+
return /*#__PURE__*/jsx(SelectInputGroupItemView, {
|
|
712
|
+
item: item,
|
|
713
|
+
renderValue: renderValue,
|
|
714
|
+
needle: needle
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
case 'separator':
|
|
718
|
+
{
|
|
719
|
+
if (needle == null) {
|
|
720
|
+
return /*#__PURE__*/jsx("hr", {
|
|
721
|
+
className: "np-select-input-separator-item"
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
function SelectInputGroupItemView({
|
|
730
|
+
item,
|
|
731
|
+
renderValue,
|
|
732
|
+
needle
|
|
733
|
+
}) {
|
|
734
|
+
const headerId = useId();
|
|
735
|
+
const header = /*#__PURE__*/jsx(Header, {
|
|
736
|
+
as: "header",
|
|
737
|
+
role: "none",
|
|
738
|
+
id: headerId,
|
|
739
|
+
title: item.label
|
|
740
|
+
// @ts-expect-error when we migrate ActionButton to new Button this should be sorted
|
|
741
|
+
,
|
|
742
|
+
action: item.action && {
|
|
743
|
+
text: item.action.label,
|
|
744
|
+
onClick: item.action.onClick
|
|
745
|
+
},
|
|
746
|
+
className: "np-select-input-group-item-header p-x-1"
|
|
747
|
+
});
|
|
748
|
+
return (
|
|
749
|
+
/*#__PURE__*/
|
|
750
|
+
// An empty container may be rendered when no options match `needle`
|
|
751
|
+
// However, pre-filtering would result in worse performance overall
|
|
752
|
+
jsxs(Section, {
|
|
753
|
+
as: "section",
|
|
754
|
+
role: "group",
|
|
755
|
+
"aria-labelledby": headerId,
|
|
756
|
+
className: clsx('m-y-0', needle === null && 'np-select-input-group-item--without-needle'),
|
|
757
|
+
children: [needle == null ? header : null, item.options.map((option, index) => /*#__PURE__*/jsx(SelectInputItemView
|
|
758
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
759
|
+
, {
|
|
760
|
+
item: option,
|
|
761
|
+
renderValue: renderValue,
|
|
762
|
+
needle: needle
|
|
763
|
+
}, index))]
|
|
764
|
+
})
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
const SelectInputItemsCountContext = /*#__PURE__*/createContext(undefined);
|
|
768
|
+
const SelectInputItemPositionContext = /*#__PURE__*/createContext(undefined);
|
|
769
|
+
function SelectInputOption({
|
|
770
|
+
value,
|
|
771
|
+
disabled,
|
|
772
|
+
children
|
|
773
|
+
}) {
|
|
774
|
+
const itemsCount = useContext(SelectInputItemsCountContext);
|
|
775
|
+
const itemPosition = useContext(SelectInputItemPositionContext);
|
|
776
|
+
return /*#__PURE__*/jsx(ListboxOption, {
|
|
777
|
+
as: "div",
|
|
778
|
+
value: value,
|
|
779
|
+
"aria-setsize": itemsCount,
|
|
780
|
+
"aria-posinset": itemPosition,
|
|
781
|
+
disabled: disabled,
|
|
782
|
+
className: ({
|
|
783
|
+
active,
|
|
784
|
+
disabled: uiDisabled
|
|
785
|
+
}) => clsx('np-select-input-option-container np-text-body-large', active && 'np-select-input-option-container--active', uiDisabled && 'np-select-input-option-container--disabled'),
|
|
786
|
+
children: ({
|
|
787
|
+
selected
|
|
788
|
+
}) => /*#__PURE__*/jsxs(Fragment, {
|
|
789
|
+
children: [/*#__PURE__*/jsx("div", {
|
|
790
|
+
className: "np-select-input-option",
|
|
791
|
+
children: children
|
|
792
|
+
}), /*#__PURE__*/jsx(Check, {
|
|
793
|
+
size: 16,
|
|
794
|
+
className: clsx('np-select-input-option-check', !selected && 'np-select-input-option-check--not-selected')
|
|
795
|
+
})]
|
|
796
|
+
})
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
const SelectInputOptionContentWithinTriggerContext = /*#__PURE__*/createContext(false);
|
|
800
|
+
function SelectInputOptionContent({
|
|
801
|
+
title,
|
|
802
|
+
note,
|
|
803
|
+
description,
|
|
804
|
+
icon
|
|
805
|
+
}) {
|
|
806
|
+
const withinTrigger = useContext(SelectInputOptionContentWithinTriggerContext);
|
|
807
|
+
return /*#__PURE__*/jsxs("div", {
|
|
808
|
+
className: clsx('np-select-input-option-content-container', (note || description) && 'np-text-body-large'),
|
|
809
|
+
children: [icon ? /*#__PURE__*/jsx("div", {
|
|
810
|
+
className: clsx('np-select-input-option-content-icon', !withinTrigger && 'np-select-input-option-content-icon--not-within-trigger'),
|
|
811
|
+
children: icon
|
|
812
|
+
}) : null, /*#__PURE__*/jsxs("div", {
|
|
813
|
+
className: "np-select-input-option-content-text",
|
|
814
|
+
children: [/*#__PURE__*/jsxs("div", {
|
|
815
|
+
className: clsx('np-select-input-option-content-text-line-1', withinTrigger && 'np-select-input-option-content-text-within-trigger'),
|
|
816
|
+
children: [/*#__PURE__*/jsx("div", {
|
|
817
|
+
className: "d-inline",
|
|
818
|
+
children: title
|
|
819
|
+
}), note ? /*#__PURE__*/jsx("span", {
|
|
820
|
+
className: "np-select-input-option-content-text-secondary np-text-body-default",
|
|
821
|
+
children: note
|
|
822
|
+
}) : null]
|
|
823
|
+
}), description ? /*#__PURE__*/jsx("div", {
|
|
824
|
+
className: clsx('np-select-input-option-content-text-secondary np-text-body-default', withinTrigger && 'np-select-input-option-content-text-within-trigger'),
|
|
825
|
+
children: description
|
|
826
|
+
}) : null]
|
|
827
|
+
})]
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
export { SelectInput, SelectInputOptionContent, SelectInputTriggerButton };
|
|
832
|
+
//# sourceMappingURL=SelectInput.mjs.map
|