@primer/components 0.0.0-20219815255 → 0.0.0-20219835928
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/CHANGELOG.md +1 -1
- package/dist/browser.esm.js +252 -256
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +331 -335
- package/dist/browser.umd.js.map +1 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.js +11 -3
- package/lib/Autocomplete/Autocomplete.d.ts +40 -0
- package/lib/Autocomplete/Autocomplete.js +68 -0
- package/lib/Autocomplete/AutocompleteContext.d.ts +17 -0
- package/lib/Autocomplete/AutocompleteContext.js +11 -0
- package/lib/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib/Autocomplete/AutocompleteInput.js +150 -0
- package/lib/Autocomplete/AutocompleteMenu.d.ts +71 -0
- package/lib/Autocomplete/AutocompleteMenu.js +223 -0
- package/lib/Autocomplete/AutocompleteOverlay.d.ts +17 -0
- package/lib/Autocomplete/AutocompleteOverlay.js +69 -0
- package/lib/Autocomplete/index.d.ts +2 -0
- package/lib/Autocomplete/index.js +15 -0
- package/lib/FilteredActionList/FilteredActionList.js +5 -31
- package/lib/Overlay.d.ts +2 -1
- package/lib/Overlay.js +10 -5
- package/lib/SelectMenu/SelectMenu.d.ts +3 -8
- package/lib/TextInput.d.ts +3 -13
- package/lib/TextInput.js +3 -43
- package/lib/TextInputWithTokens.d.ts +11 -14
- package/lib/TextInputWithTokens.js +119 -119
- package/lib/_TextInputWrapper.d.ts +10 -0
- package/lib/_TextInputWrapper.js +51 -0
- package/lib/hooks/useOpenAndCloseFocus.d.ts +2 -1
- package/lib/hooks/useOpenAndCloseFocus.js +7 -2
- package/lib/hooks/useOverlay.d.ts +2 -1
- package/lib/hooks/useOverlay.js +4 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +8 -0
- package/lib/utils/scrollIntoViewingArea.d.ts +1 -0
- package/lib/utils/scrollIntoViewingArea.js +39 -0
- package/lib/utils/types/MandateProps.d.ts +3 -0
- package/lib/utils/types/MandateProps.js +1 -0
- package/lib/utils/types/index.d.ts +1 -0
- package/lib/utils/types/index.js +13 -0
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -1
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.js +11 -3
- package/lib-esm/Autocomplete/Autocomplete.d.ts +40 -0
- package/lib-esm/Autocomplete/Autocomplete.js +47 -0
- package/lib-esm/Autocomplete/AutocompleteContext.d.ts +17 -0
- package/lib-esm/Autocomplete/AutocompleteContext.js +2 -0
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib-esm/Autocomplete/AutocompleteInput.js +131 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.d.ts +71 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.js +204 -0
- package/lib-esm/Autocomplete/AutocompleteOverlay.d.ts +17 -0
- package/lib-esm/Autocomplete/AutocompleteOverlay.js +51 -0
- package/lib-esm/Autocomplete/index.d.ts +2 -0
- package/lib-esm/Autocomplete/index.js +1 -0
- package/lib-esm/FilteredActionList/FilteredActionList.js +3 -31
- package/lib-esm/Overlay.d.ts +2 -1
- package/lib-esm/Overlay.js +8 -5
- package/lib-esm/SelectMenu/SelectMenu.d.ts +3 -8
- package/lib-esm/TextInput.d.ts +3 -13
- package/lib-esm/TextInput.js +3 -34
- package/lib-esm/TextInputWithTokens.d.ts +11 -14
- package/lib-esm/TextInputWithTokens.js +117 -115
- package/lib-esm/_TextInputWrapper.d.ts +10 -0
- package/lib-esm/_TextInputWrapper.js +31 -0
- package/lib-esm/hooks/useOpenAndCloseFocus.d.ts +2 -1
- package/lib-esm/hooks/useOpenAndCloseFocus.js +7 -2
- package/lib-esm/hooks/useOverlay.d.ts +2 -1
- package/lib-esm/hooks/useOverlay.js +4 -2
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/utils/scrollIntoViewingArea.d.ts +1 -0
- package/lib-esm/utils/scrollIntoViewingArea.js +30 -0
- package/lib-esm/utils/types/MandateProps.d.ts +3 -0
- package/lib-esm/utils/types/MandateProps.js +1 -0
- package/lib-esm/utils/types/index.d.ts +1 -0
- package/lib-esm/utils/types/index.js +2 -1
- package/package.json +1 -1
@@ -0,0 +1,47 @@
|
|
1
|
+
import React, { useRef, useState } from 'react';
|
2
|
+
import { uniqueId } from '../utils/uniqueId';
|
3
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
4
|
+
import AutocompleteInput from './AutocompleteInput';
|
5
|
+
import AutocompleteMenu from './AutocompleteMenu';
|
6
|
+
import AutocompleteOverlay from './AutocompleteOverlay';
|
7
|
+
|
8
|
+
const Autocomplete = ({
|
9
|
+
children,
|
10
|
+
id: idProp
|
11
|
+
}) => {
|
12
|
+
const activeDescendantRef = useRef(null);
|
13
|
+
const scrollContainerRef = useRef(null);
|
14
|
+
const inputRef = useRef(null);
|
15
|
+
const [inputValue, setInputValue] = useState('');
|
16
|
+
const [showMenu, setShowMenu] = useState(false);
|
17
|
+
const [autocompleteSuggestion, setAutocompleteSuggestion] = useState('');
|
18
|
+
const [isMenuDirectlyActivated, setIsMenuDirectlyActivated] = useState(false);
|
19
|
+
const [selectedItemLength, setSelectedItemLength] = useState();
|
20
|
+
const id = idProp || uniqueId();
|
21
|
+
return /*#__PURE__*/React.createElement(AutocompleteContext.Provider, {
|
22
|
+
value: {
|
23
|
+
activeDescendantRef,
|
24
|
+
autocompleteSuggestion,
|
25
|
+
id,
|
26
|
+
inputRef,
|
27
|
+
inputValue,
|
28
|
+
isMenuDirectlyActivated,
|
29
|
+
scrollContainerRef,
|
30
|
+
selectedItemLength,
|
31
|
+
setAutocompleteSuggestion,
|
32
|
+
setInputValue,
|
33
|
+
setIsMenuDirectlyActivated,
|
34
|
+
setShowMenu,
|
35
|
+
setSelectedItemLength,
|
36
|
+
showMenu
|
37
|
+
}
|
38
|
+
}, children);
|
39
|
+
};
|
40
|
+
|
41
|
+
Autocomplete.displayName = "Autocomplete";
|
42
|
+
export default Object.assign(Autocomplete, {
|
43
|
+
AutocompleteContext,
|
44
|
+
Input: AutocompleteInput,
|
45
|
+
Menu: AutocompleteMenu,
|
46
|
+
Overlay: AutocompleteOverlay
|
47
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/// <reference types="react" />
|
2
|
+
export declare const AutocompleteContext: import("react").Context<{
|
3
|
+
activeDescendantRef?: import("react").MutableRefObject<HTMLElement | null> | undefined;
|
4
|
+
autocompleteSuggestion?: string | undefined;
|
5
|
+
id?: string | undefined;
|
6
|
+
inputRef?: import("react").MutableRefObject<HTMLInputElement | null> | undefined;
|
7
|
+
inputValue?: string | undefined;
|
8
|
+
isMenuDirectlyActivated?: boolean | undefined;
|
9
|
+
scrollContainerRef?: import("react").MutableRefObject<HTMLElement | null> | undefined;
|
10
|
+
selectedItemLength?: number | undefined;
|
11
|
+
setAutocompleteSuggestion?: import("react").Dispatch<import("react").SetStateAction<string>> | undefined;
|
12
|
+
setInputValue?: import("react").Dispatch<import("react").SetStateAction<string>> | undefined;
|
13
|
+
setIsMenuDirectlyActivated?: import("react").Dispatch<import("react").SetStateAction<boolean>> | undefined;
|
14
|
+
setSelectedItemLength?: import("react").Dispatch<import("react").SetStateAction<number | undefined>> | undefined;
|
15
|
+
setShowMenu?: import("react").Dispatch<import("react").SetStateAction<boolean>> | undefined;
|
16
|
+
showMenu?: boolean | undefined;
|
17
|
+
}>;
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ForwardRefComponent as PolymorphicForwardRefComponent } from '@radix-ui/react-polymorphic';
|
3
|
+
import { ComponentProps } from '../utils/types';
|
4
|
+
declare type InternalAutocompleteInputProps = {
|
5
|
+
as?: React.ComponentType<any>;
|
6
|
+
};
|
7
|
+
declare const AutocompleteInput: PolymorphicForwardRefComponent<"input", InternalAutocompleteInputProps>;
|
8
|
+
export declare type AutocompleteInputProps = ComponentProps<typeof AutocompleteInput>;
|
9
|
+
export default AutocompleteInput;
|
@@ -0,0 +1,131 @@
|
|
1
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2
|
+
|
3
|
+
import React, { useCallback, useContext, useEffect, useState } from 'react';
|
4
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
5
|
+
import TextInput from '../TextInput';
|
6
|
+
import { useCombinedRefs } from '../hooks/useCombinedRefs';
|
7
|
+
const AutocompleteInput = /*#__PURE__*/React.forwardRef(({
|
8
|
+
as: Component = TextInput,
|
9
|
+
onFocus,
|
10
|
+
onBlur,
|
11
|
+
onChange,
|
12
|
+
onKeyDown,
|
13
|
+
onKeyUp,
|
14
|
+
onKeyPress,
|
15
|
+
value,
|
16
|
+
...props
|
17
|
+
}, forwardedRef) => {
|
18
|
+
const {
|
19
|
+
activeDescendantRef,
|
20
|
+
autocompleteSuggestion = '',
|
21
|
+
id,
|
22
|
+
inputRef,
|
23
|
+
inputValue = '',
|
24
|
+
isMenuDirectlyActivated,
|
25
|
+
setInputValue,
|
26
|
+
setShowMenu,
|
27
|
+
showMenu
|
28
|
+
} = useContext(AutocompleteContext);
|
29
|
+
const combinedInputRef = useCombinedRefs(inputRef, forwardedRef);
|
30
|
+
const [highlightRemainingText, setHighlightRemainingText] = useState(true);
|
31
|
+
const handleInputFocus = useCallback(event => {
|
32
|
+
onFocus && onFocus(event);
|
33
|
+
setShowMenu && setShowMenu(true);
|
34
|
+
}, [onFocus, setShowMenu]);
|
35
|
+
const handleInputBlur = useCallback(event => {
|
36
|
+
onBlur && onBlur(event); // HACK: wait a tick and check the focused element before hiding the autocomplete menu
|
37
|
+
// this prevents the menu from hiding when the user is clicking an option in the Autoselect.Menu,
|
38
|
+
// but still hides the menu when the user blurs the input by tabbing out or clicking somewhere else on the page
|
39
|
+
|
40
|
+
setTimeout(() => {
|
41
|
+
if (document.activeElement !== combinedInputRef.current) {
|
42
|
+
setShowMenu && setShowMenu(false);
|
43
|
+
}
|
44
|
+
}, 0);
|
45
|
+
}, [onBlur, setShowMenu, combinedInputRef]);
|
46
|
+
const handleInputChange = useCallback(event => {
|
47
|
+
onChange && onChange(event);
|
48
|
+
setInputValue && setInputValue(event.currentTarget.value);
|
49
|
+
|
50
|
+
if (!showMenu) {
|
51
|
+
setShowMenu && setShowMenu(true);
|
52
|
+
}
|
53
|
+
}, [onChange, setInputValue, setShowMenu, showMenu]);
|
54
|
+
const handleInputKeyDown = useCallback(event => {
|
55
|
+
var _inputRef$current;
|
56
|
+
|
57
|
+
onKeyDown && onKeyDown(event);
|
58
|
+
|
59
|
+
if (event.key === 'Backspace') {
|
60
|
+
setHighlightRemainingText(false);
|
61
|
+
}
|
62
|
+
|
63
|
+
if (event.key === 'Escape' && inputRef !== null && inputRef !== void 0 && (_inputRef$current = inputRef.current) !== null && _inputRef$current !== void 0 && _inputRef$current.value) {
|
64
|
+
setInputValue && setInputValue('');
|
65
|
+
inputRef.current.value = '';
|
66
|
+
}
|
67
|
+
}, [inputRef, setInputValue, setHighlightRemainingText, onKeyDown]);
|
68
|
+
const handleInputKeyUp = useCallback(event => {
|
69
|
+
onKeyUp && onKeyUp(event);
|
70
|
+
|
71
|
+
if (event.key === 'Backspace') {
|
72
|
+
setHighlightRemainingText(true);
|
73
|
+
}
|
74
|
+
}, [setHighlightRemainingText, onKeyUp]);
|
75
|
+
const onInputKeyPress = useCallback(event => {
|
76
|
+
onKeyPress && onKeyPress(event);
|
77
|
+
|
78
|
+
if (showMenu && activeDescendantRef && event.key === 'Enter' && activeDescendantRef.current) {
|
79
|
+
event.preventDefault();
|
80
|
+
event.nativeEvent.stopImmediatePropagation(); // Forward Enter key press to active descendant so that item gets activated
|
81
|
+
|
82
|
+
const activeDescendantEvent = new KeyboardEvent(event.type, event.nativeEvent);
|
83
|
+
activeDescendantRef.current.dispatchEvent(activeDescendantEvent);
|
84
|
+
}
|
85
|
+
}, [activeDescendantRef, showMenu, onKeyPress]);
|
86
|
+
useEffect(() => {
|
87
|
+
if (!(inputRef !== null && inputRef !== void 0 && inputRef.current)) {
|
88
|
+
return;
|
89
|
+
} // resets input value to being empty after a selection has been made
|
90
|
+
|
91
|
+
|
92
|
+
if (!autocompleteSuggestion) {
|
93
|
+
inputRef.current.value = inputValue;
|
94
|
+
} // TODO: fix bug where this function prevents `onChange` from being triggered if the highlighted item text
|
95
|
+
// is the same as what I'm typing
|
96
|
+
// e.g.: typing 'tw' highights 'two', but when I 'two', the text input change does not get triggered
|
97
|
+
|
98
|
+
|
99
|
+
if (highlightRemainingText && autocompleteSuggestion && (inputValue || isMenuDirectlyActivated)) {
|
100
|
+
inputRef.current.value = autocompleteSuggestion;
|
101
|
+
|
102
|
+
if (autocompleteSuggestion.toLowerCase().indexOf(inputValue.toLowerCase()) === 0) {
|
103
|
+
inputRef.current.setSelectionRange(inputValue.length, autocompleteSuggestion.length);
|
104
|
+
}
|
105
|
+
} // calling this useEffeect when `highlightRemainingText` changes breaks backspace functionality
|
106
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
107
|
+
|
108
|
+
}, [autocompleteSuggestion, inputValue, inputRef, isMenuDirectlyActivated]);
|
109
|
+
useEffect(() => {
|
110
|
+
if (value) {
|
111
|
+
setInputValue && setInputValue(value.toString());
|
112
|
+
}
|
113
|
+
}, [value, setInputValue]);
|
114
|
+
return /*#__PURE__*/React.createElement(Component, _extends({
|
115
|
+
onFocus: handleInputFocus,
|
116
|
+
onBlur: handleInputBlur,
|
117
|
+
onChange: handleInputChange,
|
118
|
+
onKeyDown: handleInputKeyDown,
|
119
|
+
onKeyPress: onInputKeyPress,
|
120
|
+
onKeyUp: handleInputKeyUp,
|
121
|
+
ref: combinedInputRef,
|
122
|
+
"aria-controls": `${id}-listbox`,
|
123
|
+
"aria-autocomplete": "both",
|
124
|
+
role: "combobox",
|
125
|
+
"aria-expanded": showMenu,
|
126
|
+
"aria-haspopup": "listbox",
|
127
|
+
"aria-owns": `${id}-listbox`,
|
128
|
+
autocomplete: "off"
|
129
|
+
}, props));
|
130
|
+
});
|
131
|
+
export default AutocompleteInput;
|
@@ -0,0 +1,71 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ItemProps } from '../ActionList';
|
3
|
+
import { ComponentProps, MandateProps } from '../utils/types';
|
4
|
+
declare type OnSelectedChange<T> = (item: T | T[]) => void;
|
5
|
+
declare type AutocompleteItemProps<T = Record<string, any>> = MandateProps<ItemProps, 'id'> & {
|
6
|
+
metadata?: T;
|
7
|
+
};
|
8
|
+
declare type AutocompleteMenuInternalProps<T extends AutocompleteItemProps> = {
|
9
|
+
/**
|
10
|
+
* A menu item that is used to allow users make a selection that is not available in the array passed to the `items` prop.
|
11
|
+
* This menu item gets appended to the end of the list of options.
|
12
|
+
*/
|
13
|
+
addNewItem?: Omit<T, 'onAction' | 'leadingVisual' | 'id'> & {
|
14
|
+
handleAddItem: (item: Omit<T, 'onAction' | 'leadingVisual'>) => void;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* The text that appears in the menu when there are no options in the array passed to the `items` prop.
|
18
|
+
*/
|
19
|
+
emptyStateText?: React.ReactNode | false;
|
20
|
+
/**
|
21
|
+
* A custom function used to filter the options in the array passed to the `items` prop.
|
22
|
+
* By default, we filter out items that don't match the value of the autocomplete text input. The default filter is not case-sensitive.
|
23
|
+
*/
|
24
|
+
filterFn?: (item: T, i: number) => boolean;
|
25
|
+
/**
|
26
|
+
* The options for field values that are displayed in the dropdown menu.
|
27
|
+
* One or more may be selected depending on the value of the `selectionVariant` prop.
|
28
|
+
*/
|
29
|
+
items: T[];
|
30
|
+
/**
|
31
|
+
* Whether the data is loaded for the menu items
|
32
|
+
*/
|
33
|
+
loading?: boolean;
|
34
|
+
/**
|
35
|
+
* The IDs of the selected items
|
36
|
+
*/
|
37
|
+
selectedItemIds: Array<string | number>;
|
38
|
+
/**
|
39
|
+
* The sort function that is applied to the options in the array passed to the `items` prop after the user closes the menu.
|
40
|
+
* By default, selected items are sorted to the top after the user closes the menu.
|
41
|
+
*/
|
42
|
+
sortOnCloseFn?: (itemIdA: string | number, itemIdB: string | number) => number;
|
43
|
+
/**
|
44
|
+
* Whether there can be one item selected from the menu or multiple items selected from the menu
|
45
|
+
*/
|
46
|
+
selectionVariant?: 'single' | 'multiple';
|
47
|
+
/**
|
48
|
+
* Function that gets called when the menu is opened or closed
|
49
|
+
*/
|
50
|
+
onOpenChange?: (open: boolean) => void;
|
51
|
+
/**
|
52
|
+
* The function that is called when an item in the list is selected or deselected
|
53
|
+
*/
|
54
|
+
onSelectedChange?: OnSelectedChange<T>;
|
55
|
+
/**
|
56
|
+
* If the menu is rendered in a scrolling element other than the `Autocomplete.Overlay` component,
|
57
|
+
* pass the ref of that element to `customScrollContainerRef` to ensure the container automatically
|
58
|
+
* scrolls when the user highlights an item in the menu that is outside the scroll container
|
59
|
+
*/
|
60
|
+
customScrollContainerRef?: React.MutableRefObject<HTMLElement | null>;
|
61
|
+
} & Pick<React.AriaAttributes, 'aria-labelledby'>;
|
62
|
+
declare function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMenuInternalProps<T>): JSX.Element;
|
63
|
+
declare namespace AutocompleteMenu {
|
64
|
+
var defaultProps: {
|
65
|
+
emptyStateText: string;
|
66
|
+
selectionVariant: string;
|
67
|
+
};
|
68
|
+
var displayName: string;
|
69
|
+
}
|
70
|
+
export declare type AutocompleteMenuProps = ComponentProps<typeof AutocompleteMenu>;
|
71
|
+
export default AutocompleteMenu;
|
@@ -0,0 +1,204 @@
|
|
1
|
+
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
2
|
+
import { ActionList } from '../ActionList';
|
3
|
+
import { useFocusZone } from '../hooks/useFocusZone';
|
4
|
+
import { Box, Spinner } from '../';
|
5
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
6
|
+
import { PlusIcon } from '@primer/octicons-react';
|
7
|
+
import { uniqueId } from '../utils/uniqueId';
|
8
|
+
import { scrollIntoViewingArea } from '../utils/scrollIntoViewingArea';
|
9
|
+
|
10
|
+
const getDefaultSortFn = isItemSelectedFn => (itemIdA, itemIdB) => isItemSelectedFn(itemIdA) === isItemSelectedFn(itemIdB) ? 0 : isItemSelectedFn(itemIdA) ? -1 : 1;
|
11
|
+
|
12
|
+
function getDefaultItemFilter(filterValue) {
|
13
|
+
return function (item, _i) {
|
14
|
+
var _item$text;
|
15
|
+
|
16
|
+
return Boolean((_item$text = item.text) === null || _item$text === void 0 ? void 0 : _item$text.toLowerCase().startsWith(filterValue.toLowerCase()));
|
17
|
+
};
|
18
|
+
}
|
19
|
+
|
20
|
+
function getDefaultOnSelectionChange(setInputValueFn) {
|
21
|
+
return function (itemOrItems) {
|
22
|
+
const {
|
23
|
+
text = ''
|
24
|
+
} = Array.isArray(itemOrItems) ? itemOrItems.slice(-1)[0] : itemOrItems;
|
25
|
+
setInputValueFn && setInputValueFn(text);
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
const isItemSelected = (itemId, selectedItemIds) => selectedItemIds.includes(itemId);
|
30
|
+
|
31
|
+
function getItemById(itemId, items) {
|
32
|
+
return items.find(item => item.id === itemId);
|
33
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
34
|
+
|
35
|
+
|
36
|
+
function AutocompleteMenu(props) {
|
37
|
+
const {
|
38
|
+
activeDescendantRef,
|
39
|
+
id,
|
40
|
+
inputRef,
|
41
|
+
inputValue = '',
|
42
|
+
scrollContainerRef,
|
43
|
+
setAutocompleteSuggestion,
|
44
|
+
setShowMenu,
|
45
|
+
setInputValue,
|
46
|
+
setIsMenuDirectlyActivated,
|
47
|
+
setSelectedItemLength,
|
48
|
+
showMenu
|
49
|
+
} = useContext(AutocompleteContext);
|
50
|
+
const {
|
51
|
+
items,
|
52
|
+
selectedItemIds,
|
53
|
+
sortOnCloseFn,
|
54
|
+
emptyStateText,
|
55
|
+
addNewItem,
|
56
|
+
loading,
|
57
|
+
selectionVariant,
|
58
|
+
filterFn = getDefaultItemFilter(inputValue),
|
59
|
+
'aria-labelledby': ariaLabelledBy,
|
60
|
+
onOpenChange,
|
61
|
+
onSelectedChange = getDefaultOnSelectionChange(setInputValue),
|
62
|
+
customScrollContainerRef
|
63
|
+
} = props;
|
64
|
+
const listContainerRef = useRef(null);
|
65
|
+
const [highlightedItem, setHighlightedItem] = useState();
|
66
|
+
const [sortedItemIds, setSortedItemIds] = useState(items.map(({
|
67
|
+
id: itemId
|
68
|
+
}) => itemId));
|
69
|
+
const selectableItems = useMemo(() => items.map(selectableItem => {
|
70
|
+
return { ...selectableItem,
|
71
|
+
role: 'option',
|
72
|
+
id: selectableItem.id,
|
73
|
+
selected: selectionVariant === 'multiple' ? selectedItemIds.includes(selectableItem.id) : undefined,
|
74
|
+
onAction: item => {
|
75
|
+
const otherSelectedItemIds = selectedItemIds.filter(selectedItemId => selectedItemId !== item.id);
|
76
|
+
const newSelectedItemIds = selectedItemIds.includes(item.id) ? otherSelectedItemIds : [...otherSelectedItemIds, item.id];
|
77
|
+
onSelectedChange && onSelectedChange(newSelectedItemIds.map(newSelectedItemId => getItemById(newSelectedItemId, items)));
|
78
|
+
|
79
|
+
if (selectionVariant === 'multiple') {
|
80
|
+
setInputValue && setInputValue('');
|
81
|
+
setAutocompleteSuggestion && setAutocompleteSuggestion('');
|
82
|
+
} else {
|
83
|
+
var _inputRef$current;
|
84
|
+
|
85
|
+
setShowMenu && setShowMenu(false);
|
86
|
+
inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.setSelectionRange(inputRef.current.value.length, inputRef.current.value.length);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
};
|
90
|
+
}), [items, selectedItemIds, inputRef, onSelectedChange, selectionVariant, setAutocompleteSuggestion, setInputValue, setShowMenu]);
|
91
|
+
const itemSortOrderData = useMemo(() => sortedItemIds.reduce((acc, curr, i) => {
|
92
|
+
acc[curr] = i;
|
93
|
+
return acc;
|
94
|
+
}, {}), [sortedItemIds]);
|
95
|
+
const sortedAndFilteredItemsToRender = useMemo(() => selectableItems.filter((item, i) => filterFn(item, i)).sort((a, b) => itemSortOrderData[a.id] - itemSortOrderData[b.id]), [selectableItems, itemSortOrderData, filterFn]);
|
96
|
+
const allItemsToRender = useMemo(() => [// sorted and filtered selectable items
|
97
|
+
...sortedAndFilteredItemsToRender, // menu item used for creating a token from whatever is in the text input
|
98
|
+
...(addNewItem ? [{ ...addNewItem,
|
99
|
+
leadingVisual: () => /*#__PURE__*/React.createElement(PlusIcon, null),
|
100
|
+
onAction: item => {
|
101
|
+
// TODO: make it possible to pass a leadingVisual when using `addNewItem`
|
102
|
+
addNewItem.handleAddItem({ ...item,
|
103
|
+
id: item.id || uniqueId(),
|
104
|
+
leadingVisual: undefined
|
105
|
+
});
|
106
|
+
|
107
|
+
if (selectionVariant === 'multiple') {
|
108
|
+
setInputValue && setInputValue('');
|
109
|
+
setAutocompleteSuggestion && setAutocompleteSuggestion('');
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}] : [])], [sortedAndFilteredItemsToRender, addNewItem, setAutocompleteSuggestion, selectionVariant, setInputValue]);
|
113
|
+
useFocusZone({
|
114
|
+
containerRef: listContainerRef,
|
115
|
+
focusOutBehavior: 'wrap',
|
116
|
+
focusableElementFilter: element => {
|
117
|
+
return !(element instanceof HTMLInputElement);
|
118
|
+
},
|
119
|
+
activeDescendantFocus: inputRef,
|
120
|
+
onActiveDescendantChanged: (current, _previous, directlyActivated) => {
|
121
|
+
if (activeDescendantRef) {
|
122
|
+
activeDescendantRef.current = current || null;
|
123
|
+
}
|
124
|
+
|
125
|
+
if (current) {
|
126
|
+
const selectedItem = selectableItems.find(item => item.id.toString() === current.getAttribute('data-id'));
|
127
|
+
setHighlightedItem(selectedItem);
|
128
|
+
setIsMenuDirectlyActivated && setIsMenuDirectlyActivated(directlyActivated);
|
129
|
+
}
|
130
|
+
|
131
|
+
if (current && customScrollContainerRef && customScrollContainerRef.current && directlyActivated) {
|
132
|
+
scrollIntoViewingArea(current, customScrollContainerRef.current);
|
133
|
+
} else if (current && scrollContainerRef && scrollContainerRef.current && directlyActivated) {
|
134
|
+
scrollIntoViewingArea(current, scrollContainerRef.current);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}, [loading]);
|
138
|
+
useEffect(() => {
|
139
|
+
var _highlightedItem$text;
|
140
|
+
|
141
|
+
if (!setAutocompleteSuggestion) {
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
|
145
|
+
if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(inputValue) && !selectedItemIds.includes(highlightedItem.id)) {
|
146
|
+
setAutocompleteSuggestion(highlightedItem.text);
|
147
|
+
} else {
|
148
|
+
setAutocompleteSuggestion('');
|
149
|
+
}
|
150
|
+
}, [highlightedItem, inputValue, selectedItemIds, setAutocompleteSuggestion]);
|
151
|
+
useEffect(() => {
|
152
|
+
const itemIdSortResult = [...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds)));
|
153
|
+
const sortResultMatchesState = itemIdSortResult.length === sortedItemIds.length && itemIdSortResult.every((element, index) => element === sortedItemIds[index]);
|
154
|
+
|
155
|
+
if (showMenu === false && !sortResultMatchesState) {
|
156
|
+
setSortedItemIds(itemIdSortResult);
|
157
|
+
}
|
158
|
+
|
159
|
+
onOpenChange && onOpenChange(Boolean(showMenu));
|
160
|
+
}, [showMenu, onOpenChange, selectedItemIds, sortOnCloseFn, sortedItemIds]);
|
161
|
+
useEffect(() => {
|
162
|
+
if (selectedItemIds.length) {
|
163
|
+
setSelectedItemLength && setSelectedItemLength(selectedItemIds.length);
|
164
|
+
}
|
165
|
+
}, [selectedItemIds, setSelectedItemLength]);
|
166
|
+
return /*#__PURE__*/React.createElement(Box, {
|
167
|
+
sx: !showMenu ? {
|
168
|
+
// visually hides this label for sighted users
|
169
|
+
position: 'absolute',
|
170
|
+
width: '1px',
|
171
|
+
height: '1px',
|
172
|
+
padding: '0',
|
173
|
+
margin: '-1px',
|
174
|
+
overflow: 'hidden',
|
175
|
+
clip: 'rect(0, 0, 0, 0)',
|
176
|
+
whiteSpace: 'nowrap',
|
177
|
+
borderWidth: '0'
|
178
|
+
} : {}
|
179
|
+
}, loading ? /*#__PURE__*/React.createElement(Box, {
|
180
|
+
p: 3,
|
181
|
+
display: "flex",
|
182
|
+
justifyContent: "center"
|
183
|
+
}, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement("div", {
|
184
|
+
ref: listContainerRef
|
185
|
+
}, allItemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
|
186
|
+
selectionVariant: "multiple" // have to typecast to `ItemProps` because we have an extra property
|
187
|
+
// on `items` for Autocomplete: `metadata`
|
188
|
+
,
|
189
|
+
items: allItemsToRender,
|
190
|
+
role: "listbox",
|
191
|
+
id: `${id}-listbox`,
|
192
|
+
"aria-labelledby": ariaLabelledBy
|
193
|
+
}) : /*#__PURE__*/React.createElement(Box, {
|
194
|
+
p: 3
|
195
|
+
}, emptyStateText)));
|
196
|
+
}
|
197
|
+
|
198
|
+
AutocompleteMenu.displayName = "AutocompleteMenu";
|
199
|
+
AutocompleteMenu.defaultProps = {
|
200
|
+
emptyStateText: 'No selectable options',
|
201
|
+
selectionVariant: 'single'
|
202
|
+
};
|
203
|
+
AutocompleteMenu.displayName = 'AutocompleteMenu';
|
204
|
+
export default AutocompleteMenu;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { OverlayProps } from '../Overlay';
|
3
|
+
import { ComponentProps } from '../utils/types';
|
4
|
+
declare type AutocompleteOverlayInternalProps = {
|
5
|
+
/**
|
6
|
+
* The ref of the element that the position of the menu is based on. By default, the menu is positioned based on the text input
|
7
|
+
*/
|
8
|
+
menuAnchorRef?: React.RefObject<HTMLElement>;
|
9
|
+
/**
|
10
|
+
* Props to be spread on the internal `Overlay` component.
|
11
|
+
*/
|
12
|
+
overlayProps?: Partial<OverlayProps>;
|
13
|
+
children?: React.ReactNode;
|
14
|
+
} & Pick<React.AriaAttributes, 'aria-labelledby'>;
|
15
|
+
declare const AutocompleteOverlay: React.FC<AutocompleteOverlayInternalProps>;
|
16
|
+
export declare type AutocompleteOverlayProps = ComponentProps<typeof AutocompleteOverlay>;
|
17
|
+
export default AutocompleteOverlay;
|
@@ -0,0 +1,51 @@
|
|
1
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2
|
+
|
3
|
+
import React, { useCallback, useContext } from 'react';
|
4
|
+
import { useAnchoredPosition } from '../hooks';
|
5
|
+
import Overlay from '../Overlay';
|
6
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
7
|
+
import { useCombinedRefs } from '../hooks/useCombinedRefs';
|
8
|
+
|
9
|
+
// TODO: consider making 'aria-labelledby' required
|
10
|
+
const AutocompleteOverlay = ({
|
11
|
+
menuAnchorRef,
|
12
|
+
overlayProps,
|
13
|
+
children
|
14
|
+
}) => {
|
15
|
+
const {
|
16
|
+
inputRef,
|
17
|
+
scrollContainerRef,
|
18
|
+
selectedItemLength,
|
19
|
+
setShowMenu,
|
20
|
+
showMenu = false
|
21
|
+
} = useContext(AutocompleteContext);
|
22
|
+
const {
|
23
|
+
floatingElementRef,
|
24
|
+
position
|
25
|
+
} = useAnchoredPosition({
|
26
|
+
side: 'outside-bottom',
|
27
|
+
align: 'start',
|
28
|
+
anchorElementRef: menuAnchorRef ? menuAnchorRef : inputRef
|
29
|
+
}, [showMenu, selectedItemLength]);
|
30
|
+
const combinedOverlayRef = useCombinedRefs(scrollContainerRef, floatingElementRef);
|
31
|
+
const closeOptionList = useCallback(() => {
|
32
|
+
setShowMenu && setShowMenu(false);
|
33
|
+
}, [setShowMenu]);
|
34
|
+
return /*#__PURE__*/React.createElement(Overlay, _extends({
|
35
|
+
returnFocusRef: inputRef,
|
36
|
+
preventFocusOnOpen: true,
|
37
|
+
onClickOutside: closeOptionList,
|
38
|
+
onEscape: closeOptionList,
|
39
|
+
ref: combinedOverlayRef,
|
40
|
+
top: position === null || position === void 0 ? void 0 : position.top,
|
41
|
+
left: position === null || position === void 0 ? void 0 : position.left,
|
42
|
+
visibility: showMenu ? 'visible' : 'hidden',
|
43
|
+
sx: {
|
44
|
+
overflow: 'auto'
|
45
|
+
}
|
46
|
+
}, overlayProps), children);
|
47
|
+
};
|
48
|
+
|
49
|
+
AutocompleteOverlay.displayName = "AutocompleteOverlay";
|
50
|
+
AutocompleteOverlay.displayName = 'AutocompleteOverlay';
|
51
|
+
export default AutocompleteOverlay;
|
@@ -0,0 +1 @@
|
|
1
|
+
export { default } from './Autocomplete';
|
@@ -1,6 +1,7 @@
|
|
1
1
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2
2
|
|
3
3
|
import React, { useCallback, useEffect, useRef } from 'react';
|
4
|
+
import { useSSRSafeId } from '@react-aria/ssr';
|
4
5
|
import TextInput from '../TextInput';
|
5
6
|
import Box from '../Box';
|
6
7
|
import { ActionList } from '../ActionList';
|
@@ -11,36 +12,7 @@ import styled from 'styled-components';
|
|
11
12
|
import { get } from '../constants';
|
12
13
|
import { useProvidedRefOrCreate } from '../hooks/useProvidedRefOrCreate';
|
13
14
|
import useScrollFlash from '../hooks/useScrollFlash';
|
14
|
-
import {
|
15
|
-
|
16
|
-
function scrollIntoViewingArea(child, container, margin = 8, behavior = 'smooth') {
|
17
|
-
const {
|
18
|
-
top: childTop,
|
19
|
-
bottom: childBottom
|
20
|
-
} = child.getBoundingClientRect();
|
21
|
-
const {
|
22
|
-
top: containerTop,
|
23
|
-
bottom: containerBottom
|
24
|
-
} = container.getBoundingClientRect();
|
25
|
-
const isChildTopAboveViewingArea = childTop < containerTop + margin;
|
26
|
-
const isChildBottomBelowViewingArea = childBottom > containerBottom - margin;
|
27
|
-
|
28
|
-
if (isChildTopAboveViewingArea) {
|
29
|
-
const scrollHeightToChildTop = childTop - containerTop + container.scrollTop;
|
30
|
-
container.scrollTo({
|
31
|
-
behavior,
|
32
|
-
top: scrollHeightToChildTop - margin
|
33
|
-
});
|
34
|
-
} else if (isChildBottomBelowViewingArea) {
|
35
|
-
const scrollHeightToChildBottom = childBottom - containerBottom + container.scrollTop;
|
36
|
-
container.scrollTo({
|
37
|
-
behavior,
|
38
|
-
top: scrollHeightToChildBottom + margin
|
39
|
-
});
|
40
|
-
} // either completely in view or outside viewing area on both ends, don't scroll
|
41
|
-
|
42
|
-
}
|
43
|
-
|
15
|
+
import { scrollIntoViewingArea } from '../utils/scrollIntoViewingArea';
|
44
16
|
const StyledHeader = styled.div.withConfig({
|
45
17
|
displayName: "FilteredActionList__StyledHeader",
|
46
18
|
componentId: "yg3jkv-0"
|
@@ -95,7 +67,7 @@ export function FilteredActionList({
|
|
95
67
|
useEffect(() => {
|
96
68
|
// if items changed, we want to instantly move active descendant into view
|
97
69
|
if (activeDescendantRef.current && scrollContainerRef.current) {
|
98
|
-
scrollIntoViewingArea(activeDescendantRef.current, scrollContainerRef.current, undefined, 'auto');
|
70
|
+
scrollIntoViewingArea(activeDescendantRef.current, scrollContainerRef.current, 'vertical', undefined, undefined, 'auto');
|
99
71
|
}
|
100
72
|
}, [items]);
|
101
73
|
useScrollFlash(scrollContainerRef);
|
package/lib-esm/Overlay.d.ts
CHANGED
@@ -4,7 +4,7 @@ import { ComponentProps } from './utils/types';
|
|
4
4
|
import { TouchOrMouseEvent } from './hooks';
|
5
5
|
import { SxProp } from './sx';
|
6
6
|
import { AnchorSide } from './behaviors/anchoredPosition';
|
7
|
-
declare type StyledOverlayProps = {
|
7
|
+
export declare type StyledOverlayProps = {
|
8
8
|
width?: keyof typeof widthMap;
|
9
9
|
height?: keyof typeof heightMap;
|
10
10
|
maxHeight?: keyof Omit<typeof heightMap, 'auto' | 'initial'>;
|
@@ -40,6 +40,7 @@ export declare type OverlayProps = {
|
|
40
40
|
top: number;
|
41
41
|
left: number;
|
42
42
|
portalContainerName?: string;
|
43
|
+
preventFocusOnOpen?: boolean;
|
43
44
|
} & Omit<ComponentProps<typeof StyledOverlay>, 'visibility' | keyof SystemPositionProps>;
|
44
45
|
/**
|
45
46
|
* An `Overlay` is a flexible floating surface, used to display transient content such as menus,
|