UNPKG

1.43 kBJavaScriptView Raw
1import getMatchBounds from './getMatchBounds';
2import getOptionLabel from './getOptionLabel';
3import hasOwnProperty from './hasOwnProperty';
4import { isString } from './nodash';
5
6function getHintText(_ref) {
7 var activeIndex = _ref.activeIndex,
8 initialItem = _ref.initialItem,
9 isFocused = _ref.isFocused,
10 isMenuShown = _ref.isMenuShown,
11 labelKey = _ref.labelKey,
12 multiple = _ref.multiple,
13 selected = _ref.selected,
14 text = _ref.text;
15
16 // Don't display a hint under the following conditions:
17 if ( // No text entered.
18 !text || // The input is not focused.
19 !isFocused || // The menu is hidden.
20 !isMenuShown || // No item in the menu.
21 !initialItem || // The initial item is a custom option.
22 !isString(initialItem) && hasOwnProperty(initialItem, 'customOption') || // One of the menu items is active.
23 activeIndex > -1 || // There's already a selection in single-select mode.
24 !!selected.length && !multiple) {
25 return '';
26 }
27
28 var initialItemStr = getOptionLabel(initialItem, labelKey);
29 var bounds = getMatchBounds(initialItemStr.toLowerCase(), text.toLowerCase());
30
31 if (!(bounds && bounds.start === 0)) {
32 return '';
33 } // Text matching is case- and accent-insensitive, so to display the hint
34 // correctly, splice the input string with the hint string.
35
36
37 return text + initialItemStr.slice(bounds.end, initialItemStr.length);
38}
39
40export default getHintText;
\No newline at end of file