@primer/components 0.0.0-2021821174731 → 0.0.0-2021821193917
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +2 -28
- package/dist/browser.esm.js +619 -665
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +386 -432
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/Divider.js +1 -1
- package/lib/ActionList/Header.js +2 -2
- package/lib/ActionList/Item.js +13 -13
- package/lib/ActionList/List.js +1 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.js +2 -2
- package/lib/Autocomplete/Autocomplete.d.ts +36 -0
- package/lib/Autocomplete/Autocomplete.js +55 -0
- package/lib/Autocomplete/AutocompleteContext.d.ts +13 -0
- package/lib/Autocomplete/AutocompleteContext.js +14 -0
- package/lib/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib/Autocomplete/AutocompleteInput.js +128 -0
- package/lib/Autocomplete/AutocompleteMenu.d.ts +54 -0
- package/lib/Autocomplete/AutocompleteMenu.js +268 -0
- package/lib/Autocomplete/index.d.ts +2 -0
- package/lib/Autocomplete/index.js +15 -0
- package/lib/Avatar.d.ts +0 -4
- package/lib/AvatarPair.js +1 -1
- package/lib/AvatarStack.js +1 -1
- package/lib/Badge/Badge.d.ts +8 -0
- package/lib/Badge/Badge.js +59 -0
- package/lib/Badge/BadgeState.d.ts +13 -0
- package/lib/Badge/BadgeState.js +51 -0
- package/lib/Badge/_badgeStyleUtils.d.ts +3 -0
- package/lib/Badge/_badgeStyleUtils.js +39 -0
- package/lib/BranchName.js +1 -1
- package/lib/Breadcrumb.js +2 -2
- package/lib/Button/Button.d.ts +1 -0
- package/lib/Button/Button.js +1 -1
- package/lib/Button/ButtonClose.d.ts +2 -1
- package/lib/Button/ButtonClose.js +1 -1
- package/lib/Button/ButtonDanger.d.ts +1 -0
- package/lib/Button/ButtonInvisible.d.ts +1 -0
- package/lib/Button/ButtonInvisible.js +1 -1
- package/lib/Button/ButtonOutline.d.ts +1 -0
- package/lib/Button/ButtonPrimary.d.ts +1 -0
- package/lib/Button/ButtonTableList.js +1 -1
- package/lib/CircleBadge.js +1 -1
- package/lib/CircleOcticon.d.ts +1 -0
- package/lib/CircleOcticon.js +1 -1
- package/lib/CounterLabel.js +2 -2
- package/lib/Dialog/ConfirmationDialog.js +1 -1
- package/lib/Dialog/Dialog.js +9 -9
- package/lib/Dialog.d.ts +3 -2
- package/lib/Dialog.js +4 -4
- package/lib/Dropdown.d.ts +4 -0
- package/lib/Dropdown.js +2 -2
- package/lib/DropdownMenu/DropdownButton.d.ts +2 -1
- package/lib/DropdownStyles.js +6 -6
- package/lib/FilterList.d.ts +1 -0
- package/lib/FilterList.js +1 -1
- package/lib/FilteredActionList/FilteredActionList.js +5 -5
- package/lib/Flash.js +1 -1
- package/lib/Label.js +2 -2
- package/lib/Link.js +1 -1
- package/lib/Overlay.d.ts +2 -1
- package/lib/Overlay.js +11 -6
- package/lib/Pagehead.js +1 -1
- package/lib/Pagination/Pagination.js +1 -1
- package/lib/Popover.js +1 -1
- package/lib/Position.d.ts +4 -4
- package/lib/ProgressBar.js +1 -1
- package/lib/SelectMenu/SelectMenu.d.ts +337 -15
- package/lib/SelectMenu/SelectMenuDivider.js +1 -1
- package/lib/SelectMenu/SelectMenuFilter.d.ts +1 -1
- package/lib/SelectMenu/SelectMenuFilter.js +1 -1
- package/lib/SelectMenu/SelectMenuFooter.js +1 -1
- package/lib/SelectMenu/SelectMenuHeader.js +2 -2
- package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib/SelectMenu/SelectMenuItem.js +1 -1
- package/lib/SelectMenu/SelectMenuList.js +1 -1
- package/lib/SelectMenu/SelectMenuLoadingAnimation.js +1 -1
- package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib/SelectMenu/SelectMenuModal.js +2 -2
- package/lib/SelectMenu/SelectMenuTab.js +1 -1
- package/lib/SelectMenu/SelectMenuTabPanel.js +1 -1
- package/lib/SideNav.js +5 -5
- package/lib/StateLabel.js +1 -1
- package/lib/SubNav.js +1 -1
- package/lib/TabNav.js +2 -2
- package/lib/TextInput.d.ts +6 -6
- package/lib/TextInput.js +16 -21
- package/lib/TextInputTokens.d.ts +32 -0
- package/lib/TextInputTokens.js +241 -0
- package/lib/TextInputWithTokens.d.ts +41 -0
- package/lib/TextInputWithTokens.js +396 -0
- package/lib/Timeline.d.ts +1 -0
- package/lib/Timeline.js +19 -16
- package/lib/Token/Token.d.ts +7 -0
- package/lib/Token/Token.js +69 -0
- package/lib/Token/TokenBase.d.ts +16 -0
- package/lib/Token/TokenBase.js +76 -0
- package/lib/Token/TokenLabel.d.ts +10 -0
- package/lib/Token/TokenLabel.js +115 -0
- package/lib/Token/TokenProfile.d.ts +7 -0
- package/lib/Token/TokenProfile.js +45 -0
- package/lib/Token/_AddTokenButton.d.ts +3 -0
- package/lib/Token/_AddTokenButton.js +42 -0
- package/lib/Token/_RemoveTokenButton.d.ts +3 -0
- package/lib/Token/_RemoveTokenButton.js +42 -0
- package/lib/Token/_tokenButtonUtils.d.ts +8 -0
- package/lib/Token/_tokenButtonUtils.js +42 -0
- package/lib/Tooltip.js +1 -1
- package/lib/UnderlineNav.js +2 -2
- package/lib/hooks/useOverlay.d.ts +2 -1
- package/lib/hooks/useOverlay.js +11 -6
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -14
- package/lib/theme-preval.d.ts +6 -12
- package/lib/theme-preval.js +0 -940
- package/lib/utils/testing.d.ts +0 -1854
- package/lib/utils/uniqueId.js +0 -1
- package/lib-esm/ActionList/Divider.js +1 -1
- package/lib-esm/ActionList/Header.js +2 -2
- package/lib-esm/ActionList/Item.js +14 -14
- package/lib-esm/ActionList/List.js +1 -1
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.js +3 -3
- package/lib-esm/Autocomplete/Autocomplete.d.ts +36 -0
- package/lib-esm/Autocomplete/Autocomplete.js +36 -0
- package/lib-esm/Autocomplete/AutocompleteContext.d.ts +13 -0
- package/lib-esm/Autocomplete/AutocompleteContext.js +5 -0
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib-esm/Autocomplete/AutocompleteInput.js +109 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.d.ts +54 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.js +244 -0
- package/lib-esm/Autocomplete/index.d.ts +2 -0
- package/lib-esm/Autocomplete/index.js +1 -0
- package/lib-esm/Avatar.d.ts +0 -4
- package/lib-esm/AvatarPair.js +1 -1
- package/lib-esm/AvatarStack.js +1 -1
- package/lib-esm/Badge/Badge.d.ts +8 -0
- package/lib-esm/Badge/Badge.js +44 -0
- package/lib-esm/Badge/BadgeState.d.ts +13 -0
- package/lib-esm/Badge/BadgeState.js +40 -0
- package/lib-esm/Badge/_badgeStyleUtils.d.ts +3 -0
- package/lib-esm/Badge/_badgeStyleUtils.js +29 -0
- package/lib-esm/BranchName.js +1 -1
- package/lib-esm/Breadcrumb.js +2 -2
- package/lib-esm/Button/Button.d.ts +1 -0
- package/lib-esm/Button/Button.js +1 -1
- package/lib-esm/Button/ButtonClose.d.ts +2 -1
- package/lib-esm/Button/ButtonClose.js +1 -1
- package/lib-esm/Button/ButtonDanger.d.ts +1 -0
- package/lib-esm/Button/ButtonInvisible.d.ts +1 -0
- package/lib-esm/Button/ButtonInvisible.js +1 -1
- package/lib-esm/Button/ButtonOutline.d.ts +1 -0
- package/lib-esm/Button/ButtonPrimary.d.ts +1 -0
- package/lib-esm/Button/ButtonTableList.js +1 -1
- package/lib-esm/CircleBadge.js +1 -1
- package/lib-esm/CircleOcticon.d.ts +1 -0
- package/lib-esm/CircleOcticon.js +1 -1
- package/lib-esm/CounterLabel.js +2 -2
- package/lib-esm/Dialog/ConfirmationDialog.js +1 -1
- package/lib-esm/Dialog/Dialog.js +8 -8
- package/lib-esm/Dialog.d.ts +3 -2
- package/lib-esm/Dialog.js +4 -4
- package/lib-esm/Dropdown.d.ts +4 -0
- package/lib-esm/Dropdown.js +2 -2
- package/lib-esm/DropdownMenu/DropdownButton.d.ts +2 -1
- package/lib-esm/DropdownStyles.js +6 -6
- package/lib-esm/FilterList.d.ts +1 -0
- package/lib-esm/FilterList.js +1 -1
- package/lib-esm/FilteredActionList/FilteredActionList.js +5 -5
- package/lib-esm/Flash.js +1 -1
- package/lib-esm/Label.js +2 -2
- package/lib-esm/Link.js +1 -1
- package/lib-esm/Overlay.d.ts +2 -1
- package/lib-esm/Overlay.js +9 -6
- package/lib-esm/Pagehead.js +1 -1
- package/lib-esm/Pagination/Pagination.js +1 -1
- package/lib-esm/Popover.js +1 -1
- package/lib-esm/Position.d.ts +4 -4
- package/lib-esm/ProgressBar.js +1 -1
- package/lib-esm/SelectMenu/SelectMenu.d.ts +337 -15
- package/lib-esm/SelectMenu/SelectMenuDivider.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuFilter.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenuFilter.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuFooter.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuHeader.js +2 -2
- package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenuItem.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuList.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuLoadingAnimation.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenuModal.js +2 -2
- package/lib-esm/SelectMenu/SelectMenuTab.js +1 -1
- package/lib-esm/SelectMenu/SelectMenuTabPanel.js +1 -1
- package/lib-esm/SideNav.js +5 -5
- package/lib-esm/StateLabel.js +1 -1
- package/lib-esm/SubNav.js +1 -1
- package/lib-esm/TabNav.js +2 -2
- package/lib-esm/TextInput.d.ts +6 -6
- package/lib-esm/TextInput.js +16 -22
- package/lib-esm/TextInputTokens.d.ts +32 -0
- package/lib-esm/TextInputTokens.js +211 -0
- package/lib-esm/TextInputWithTokens.d.ts +41 -0
- package/lib-esm/TextInputWithTokens.js +361 -0
- package/lib-esm/Timeline.d.ts +1 -0
- package/lib-esm/Timeline.js +17 -12
- package/lib-esm/Token/Token.d.ts +7 -0
- package/lib-esm/Token/Token.js +49 -0
- package/lib-esm/Token/TokenBase.d.ts +16 -0
- package/lib-esm/Token/TokenBase.js +56 -0
- package/lib-esm/Token/TokenLabel.d.ts +10 -0
- package/lib-esm/Token/TokenLabel.js +99 -0
- package/lib-esm/Token/TokenProfile.d.ts +7 -0
- package/lib-esm/Token/TokenProfile.js +28 -0
- package/lib-esm/Token/_AddTokenButton.d.ts +3 -0
- package/lib-esm/Token/_AddTokenButton.js +27 -0
- package/lib-esm/Token/_RemoveTokenButton.d.ts +3 -0
- package/lib-esm/Token/_RemoveTokenButton.js +27 -0
- package/lib-esm/Token/_tokenButtonUtils.d.ts +8 -0
- package/lib-esm/Token/_tokenButtonUtils.js +26 -0
- package/lib-esm/Tooltip.js +1 -1
- package/lib-esm/UnderlineNav.js +2 -2
- package/lib-esm/hooks/useOverlay.d.ts +2 -1
- package/lib-esm/hooks/useOverlay.js +11 -6
- package/lib-esm/index.d.ts +0 -1
- package/lib-esm/index.js +1 -2
- package/lib-esm/theme-preval.d.ts +6 -12
- package/lib-esm/theme-preval.js +0 -940
- package/lib-esm/utils/testing.d.ts +0 -1854
- package/lib-esm/utils/uniqueId.js +0 -1
- package/package.json +7 -6
- package/lib/utils/ssr.d.ts +0 -1
- package/lib/utils/ssr.js +0 -19
- package/lib-esm/utils/ssr.d.ts +0 -1
- package/lib-esm/utils/ssr.js +0 -1
package/lib/utils/uniqueId.js
CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.uniqueId = uniqueId;
|
7
|
-
// Note: uniqueId may be unsafe in SSR contexts if it is used create DOM IDs or otherwise cause a hydration warning. Use useSSRSafeId instead.
|
8
7
|
let idSeed = 10000;
|
9
8
|
|
10
9
|
function uniqueId() {
|
@@ -4,7 +4,7 @@ import { get } from '../constants';
|
|
4
4
|
export const StyledDivider = styled.div.withConfig({
|
5
5
|
displayName: "Divider__StyledDivider",
|
6
6
|
componentId: "sc-42656i-0"
|
7
|
-
})(["height:1px;background:", ";margin-top:calc(", " - 1px);margin-bottom:", ";"], get('colors.
|
7
|
+
})(["height:1px;background:", ";margin-top:calc(", " - 1px);margin-bottom:", ";"], get('colors.selectMenu.borderSecondary'), get('space.2'), get('space.2'));
|
8
8
|
/**
|
9
9
|
* Visually separates `Item`s or `Group`s in an `ActionList`.
|
10
10
|
*/
|
@@ -11,9 +11,9 @@ import sx from '../sx';
|
|
11
11
|
export const StyledHeader = styled.div.withConfig({
|
12
12
|
displayName: "Header__StyledHeader",
|
13
13
|
componentId: "qmofje-0"
|
14
|
-
})(["{}padding:6px ", ";font-size:", ";font-weight:", ";color:", ";", " ", ""], get('space.3'), get('fontSizes.0'), get('fontWeights.bold'), get('colors.
|
14
|
+
})(["{}padding:6px ", ";font-size:", ";font-weight:", ";color:", ";", " ", ""], get('space.3'), get('fontSizes.0'), get('fontWeights.bold'), get('colors.text.secondary'), ({
|
15
15
|
variant
|
16
|
-
}) => variant === 'filled' && css(["background:", ";margin:", " 0;border-top:1px solid ", ";border-bottom:1px solid ", ";&:first-child{margin-top:0;}"], get('colors.
|
16
|
+
}) => variant === 'filled' && css(["background:", ";margin:", " 0;border-top:1px solid ", ";border-bottom:1px solid ", ";&:first-child{margin-top:0;}"], get('colors.bg.tertiary'), get('space.2'), get('colors.border.tertiary'), get('colors.border.tertiary')), sx);
|
17
17
|
/**
|
18
18
|
* Displays the name and description of a `Group`.
|
19
19
|
*/
|
@@ -1,7 +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 { CheckIcon } from '@primer/octicons-react';
|
4
|
-
import React, { useCallback } from 'react';
|
4
|
+
import React, { useCallback, useMemo } from 'react';
|
5
5
|
import { get } from '../constants';
|
6
6
|
import sx from '../sx';
|
7
7
|
import Truncate from '../Truncate';
|
@@ -10,7 +10,7 @@ import { StyledHeader } from './Header';
|
|
10
10
|
import { StyledDivider } from './Divider';
|
11
11
|
import { useColorSchemeVar, useTheme } from '../ThemeProvider';
|
12
12
|
import { activeDescendantActivatedDirectly, activeDescendantActivatedIndirectly, isActiveDescendantAttribute } from '../behaviors/focusZone';
|
13
|
-
import {
|
13
|
+
import { uniqueId } from '../utils/uniqueId';
|
14
14
|
/**
|
15
15
|
* These colors are not yet in our default theme. Need to remove this once they are added.
|
16
16
|
*/
|
@@ -48,9 +48,9 @@ const customItemThemes = {
|
|
48
48
|
const getItemVariant = (variant = 'default', disabled) => {
|
49
49
|
if (disabled) {
|
50
50
|
return {
|
51
|
-
color: get('colors.
|
52
|
-
iconColor: get('colors.
|
53
|
-
annotationColor: get('colors.
|
51
|
+
color: get('colors.text.disabled'),
|
52
|
+
iconColor: get('colors.text.disabled'),
|
53
|
+
annotationColor: get('colors.text.disabled'),
|
54
54
|
hoverCursor: 'default'
|
55
55
|
};
|
56
56
|
}
|
@@ -58,17 +58,17 @@ const getItemVariant = (variant = 'default', disabled) => {
|
|
58
58
|
switch (variant) {
|
59
59
|
case 'danger':
|
60
60
|
return {
|
61
|
-
color: get('colors.danger
|
62
|
-
iconColor: get('colors.danger
|
63
|
-
annotationColor: get('colors.
|
61
|
+
color: get('colors.text.danger'),
|
62
|
+
iconColor: get('colors.icon.danger'),
|
63
|
+
annotationColor: get('colors.text.disabled'),
|
64
64
|
hoverCursor: 'pointer'
|
65
65
|
};
|
66
66
|
|
67
67
|
default:
|
68
68
|
return {
|
69
69
|
color: 'inherit',
|
70
|
-
iconColor: get('colors.
|
71
|
-
annotationColor: get('colors.
|
70
|
+
iconColor: get('colors.text.secondary'),
|
71
|
+
annotationColor: get('colors.text.secondary'),
|
72
72
|
hoverCursor: 'pointer'
|
73
73
|
};
|
74
74
|
}
|
@@ -95,7 +95,7 @@ const StyledItem = styled.div.withConfig({
|
|
95
95
|
item
|
96
96
|
}) => getItemVariant(variant, item === null || item === void 0 ? void 0 : item.disabled).hoverCursor, StyledDivider, StyledHeader, ({
|
97
97
|
showDivider
|
98
|
-
}) => showDivider ? `1px` : '0', DividedContent, get('colors.
|
98
|
+
}) => showDivider ? `1px` : '0', DividedContent, get('colors.selectMenu.borderSecondary'), ({
|
99
99
|
showDivider
|
100
100
|
}) => showDivider ? `1px` : '0', DividedContent, DividedContent, DividedContent, DividedContent, isActiveDescendantAttribute, DividedContent, isActiveDescendantAttribute, DividedContent, isActiveDescendantAttribute, activeDescendantActivatedDirectly, ({
|
101
101
|
focusBackground
|
@@ -135,7 +135,7 @@ const TrailingContent = styled(ColoredVisualContainer).withConfig({
|
|
135
135
|
const DescriptionContainer = styled.span.withConfig({
|
136
136
|
displayName: "Item__DescriptionContainer",
|
137
137
|
componentId: "jqpvy8-8"
|
138
|
-
})(["color:", ";font-size:", ";line-height:16px;margin-left:var(--description-container-margin-left);min-width:0;flex-grow:1;flex-basis:var(--description-container-flex-basis);"], get('colors.
|
138
|
+
})(["color:", ";font-size:", ";line-height:16px;margin-left:var(--description-container-margin-left);min-width:0;flex-grow:1;flex-basis:var(--description-container-flex-basis);"], get('colors.text.secondary'), get('fontSizes.0'));
|
139
139
|
const MultiSelectInput = styled.input.withConfig({
|
140
140
|
displayName: "Item__MultiSelectInput",
|
141
141
|
componentId: "jqpvy8-9"
|
@@ -164,8 +164,8 @@ export function Item(itemProps) {
|
|
164
164
|
id,
|
165
165
|
...props
|
166
166
|
} = itemProps;
|
167
|
-
const labelId =
|
168
|
-
const descriptionId =
|
167
|
+
const labelId = useMemo(() => uniqueId(), []);
|
168
|
+
const descriptionId = useMemo(() => uniqueId(), []);
|
169
169
|
const keyPressHandler = useCallback(event => {
|
170
170
|
if (disabled) {
|
171
171
|
return;
|
@@ -23,7 +23,7 @@ function isGroupedListProps(props) {
|
|
23
23
|
const StyledList = styled.div.withConfig({
|
24
24
|
displayName: "List__StyledList",
|
25
25
|
componentId: "yr2k7d-0"
|
26
|
-
})(["font-size:", ";line-height:20px;&[", "],&:focus-within{--item-hover-bg-override:none;--item-hover-divider-border-color-override:", ";}"], get('fontSizes.1'), hasActiveDescendantAttribute, get('colors.
|
26
|
+
})(["font-size:", ";line-height:20px;&[", "],&:focus-within{--item-hover-bg-override:none;--item-hover-divider-border-color-override:", ";}"], get('fontSizes.1'), hasActiveDescendantAttribute, get('colors.selectMenu.borderSecondary'));
|
27
27
|
/**
|
28
28
|
* Returns `sx` prop values for `List` children matching the given `List` style variation.
|
29
29
|
* @param variant `List` style variation.
|
@@ -1,11 +1,11 @@
|
|
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
|
-
import React, { useCallback, useEffect } from 'react';
|
3
|
+
import React, { useCallback, useEffect, useMemo } from 'react';
|
4
4
|
import Overlay from '../Overlay';
|
5
5
|
import { useFocusTrap } from '../hooks/useFocusTrap';
|
6
6
|
import { useFocusZone } from '../hooks/useFocusZone';
|
7
7
|
import { useAnchoredPosition, useProvidedRefOrCreate, useRenderForcingRef } from '../hooks';
|
8
|
-
import {
|
8
|
+
import { uniqueId } from '../utils/uniqueId';
|
9
9
|
|
10
10
|
/**
|
11
11
|
* An `AnchoredOverlay` provides an anchor that will open a floating overlay positioned relative to the anchor.
|
@@ -26,7 +26,7 @@ export const AnchoredOverlay = ({
|
|
26
26
|
}) => {
|
27
27
|
const anchorRef = useProvidedRefOrCreate(externalAnchorRef);
|
28
28
|
const [overlayRef, updateOverlayRef] = useRenderForcingRef();
|
29
|
-
const anchorId =
|
29
|
+
const anchorId = useMemo(uniqueId, []);
|
30
30
|
const onClickOutside = useCallback(() => onClose === null || onClose === void 0 ? void 0 : onClose('click-outside'), [onClose]);
|
31
31
|
const onEscape = useCallback(() => onClose === null || onClose === void 0 ? void 0 : onClose('escape'), [onClose]);
|
32
32
|
const onAnchorKeyDown = useCallback(event => {
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ComponentProps } from '../utils/types';
|
3
|
+
declare const Autocomplete: React.FC;
|
4
|
+
export declare type AutocompleteProps = ComponentProps<typeof Autocomplete>;
|
5
|
+
export type { AutocompleteInputProps } from './AutocompleteInput';
|
6
|
+
export type { AutocompleteMenuProps } from './AutocompleteMenu';
|
7
|
+
declare const _default: React.FC<{}> & {
|
8
|
+
AutocompleteContext: React.Context<{
|
9
|
+
activeDescendantRef?: React.MutableRefObject<HTMLElement | null> | undefined;
|
10
|
+
autocompleteSuggestion?: string | undefined;
|
11
|
+
inputRef?: React.MutableRefObject<HTMLInputElement | null> | undefined;
|
12
|
+
inputValue?: string | undefined;
|
13
|
+
showMenu?: boolean | undefined;
|
14
|
+
setAutocompleteSuggestion?: React.Dispatch<React.SetStateAction<string>> | undefined;
|
15
|
+
setShowMenu?: React.Dispatch<React.SetStateAction<boolean>> | undefined;
|
16
|
+
setInputValue?: React.Dispatch<React.SetStateAction<string>> | undefined;
|
17
|
+
isMenuDirectlyActivated?: boolean | undefined;
|
18
|
+
setIsMenuDirectlyActivated?: React.Dispatch<React.SetStateAction<boolean>> | undefined;
|
19
|
+
}>;
|
20
|
+
Input: import("@radix-ui/react-polymorphic").ForwardRefComponent<"input", {
|
21
|
+
as?: React.ComponentType<any> | undefined;
|
22
|
+
}>;
|
23
|
+
Menu: React.ForwardRefExoticComponent<{
|
24
|
+
addNewItem?: Omit<import("../ActionList/List").ItemInput, "onAction"> | undefined;
|
25
|
+
emptyStateText?: React.ReactNode;
|
26
|
+
filterFn?: ((item: import("../ActionList/List").ItemInput, i: number) => boolean) | undefined;
|
27
|
+
items: import("../ActionList/List").ItemInput[];
|
28
|
+
onItemDeselect?: ((item: import("../ActionList").ItemProps, event: React.KeyboardEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined;
|
29
|
+
onItemSelect?: ((item: import("../ActionList").ItemProps, event: React.KeyboardEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined;
|
30
|
+
loading?: boolean | undefined;
|
31
|
+
selectedItemIds: (string | number)[];
|
32
|
+
selectedSortFn?: ((itemIdA: string | number, itemIdB: string | number) => number) | undefined;
|
33
|
+
selectionVariant?: "multiple" | "single" | undefined;
|
34
|
+
} & Pick<import("..").OverlayProps, "height" | "maxHeight" | "width"> & React.RefAttributes<HTMLInputElement>>;
|
35
|
+
};
|
36
|
+
export default _default;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import React, { useRef, useState } from 'react';
|
2
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
3
|
+
import AutocompleteInput from './AutocompleteInput';
|
4
|
+
import AutocompleteMenu from './AutocompleteMenu';
|
5
|
+
|
6
|
+
const Autocomplete = ({
|
7
|
+
children
|
8
|
+
}) => {
|
9
|
+
const activeDescendantRef = useRef(null);
|
10
|
+
const inputRef = useRef(null);
|
11
|
+
const [inputValue, setInputValue] = useState('');
|
12
|
+
const [showMenu, setShowMenu] = useState(false);
|
13
|
+
const [autocompleteSuggestion, setAutocompleteSuggestion] = useState('');
|
14
|
+
const [isMenuDirectlyActivated, setIsMenuDirectlyActivated] = useState(false);
|
15
|
+
return /*#__PURE__*/React.createElement(AutocompleteContext.Provider, {
|
16
|
+
value: {
|
17
|
+
activeDescendantRef,
|
18
|
+
autocompleteSuggestion,
|
19
|
+
inputRef,
|
20
|
+
inputValue,
|
21
|
+
isMenuDirectlyActivated,
|
22
|
+
setAutocompleteSuggestion,
|
23
|
+
setInputValue,
|
24
|
+
setIsMenuDirectlyActivated,
|
25
|
+
setShowMenu,
|
26
|
+
showMenu
|
27
|
+
}
|
28
|
+
}, children);
|
29
|
+
};
|
30
|
+
|
31
|
+
Autocomplete.displayName = "Autocomplete";
|
32
|
+
export default Object.assign(Autocomplete, {
|
33
|
+
AutocompleteContext,
|
34
|
+
Input: AutocompleteInput,
|
35
|
+
Menu: AutocompleteMenu
|
36
|
+
});
|
@@ -0,0 +1,13 @@
|
|
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
|
+
inputRef?: import("react").MutableRefObject<HTMLInputElement | null> | undefined;
|
6
|
+
inputValue?: string | undefined;
|
7
|
+
showMenu?: boolean | undefined;
|
8
|
+
setAutocompleteSuggestion?: import("react").Dispatch<import("react").SetStateAction<string>> | undefined;
|
9
|
+
setShowMenu?: import("react").Dispatch<import("react").SetStateAction<boolean>> | undefined;
|
10
|
+
setInputValue?: import("react").Dispatch<import("react").SetStateAction<string>> | undefined;
|
11
|
+
isMenuDirectlyActivated?: boolean | undefined;
|
12
|
+
setIsMenuDirectlyActivated?: import("react").Dispatch<import("react").SetStateAction<boolean>> | undefined;
|
13
|
+
}>;
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import { createContext } from 'react'; // TODO:
|
2
|
+
// - figure out how we can make all of these properties required without having to define them when calling `createContext`
|
3
|
+
// - figure out how we can reduce the number of properties that need to be passed
|
4
|
+
|
5
|
+
export const AutocompleteContext = /*#__PURE__*/createContext({});
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import type * as Polymorphic 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: Polymorphic.ForwardRefComponent<"input", InternalAutocompleteInputProps>;
|
8
|
+
export declare type AutocompleteInputProps = ComponentProps<typeof AutocompleteInput>;
|
9
|
+
export default AutocompleteInput;
|
@@ -0,0 +1,109 @@
|
|
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
|
+
onChange,
|
11
|
+
onKeyDown,
|
12
|
+
onKeyPress,
|
13
|
+
value,
|
14
|
+
...props
|
15
|
+
}, forwardedRef) => {
|
16
|
+
const {
|
17
|
+
activeDescendantRef,
|
18
|
+
autocompleteSuggestion = '',
|
19
|
+
inputRef,
|
20
|
+
inputValue = '',
|
21
|
+
isMenuDirectlyActivated,
|
22
|
+
setInputValue,
|
23
|
+
setShowMenu,
|
24
|
+
showMenu
|
25
|
+
} = useContext(AutocompleteContext);
|
26
|
+
const combinedInputRef = useCombinedRefs(inputRef, forwardedRef);
|
27
|
+
const [highlightRemainingText, setHighlightRemainingText] = useState(true);
|
28
|
+
|
29
|
+
const handleInputFocus = () => {
|
30
|
+
if (setShowMenu) {
|
31
|
+
setShowMenu(true);
|
32
|
+
}
|
33
|
+
};
|
34
|
+
|
35
|
+
const handleInputBlur = e => {
|
36
|
+
// 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
|
+
setTimeout(() => {
|
40
|
+
if (setShowMenu && document.activeElement !== combinedInputRef.current) {
|
41
|
+
setShowMenu(false);
|
42
|
+
}
|
43
|
+
}, 0);
|
44
|
+
};
|
45
|
+
|
46
|
+
const handleInputChange = e => {
|
47
|
+
if (setInputValue) {
|
48
|
+
setInputValue(e.currentTarget.value);
|
49
|
+
}
|
50
|
+
|
51
|
+
if (setShowMenu && !showMenu) {
|
52
|
+
setShowMenu(true);
|
53
|
+
}
|
54
|
+
};
|
55
|
+
|
56
|
+
const handleInputKeyDown = e => {
|
57
|
+
if (e.key === 'Backspace') {
|
58
|
+
setHighlightRemainingText(false);
|
59
|
+
}
|
60
|
+
};
|
61
|
+
|
62
|
+
const handleInputKeyUp = e => {
|
63
|
+
if (e.key === 'Backspace') {
|
64
|
+
setHighlightRemainingText(true);
|
65
|
+
}
|
66
|
+
};
|
67
|
+
|
68
|
+
const onInputKeyPress = useCallback(event => {
|
69
|
+
if (activeDescendantRef && event.key === 'Enter' && activeDescendantRef.current) {
|
70
|
+
event.preventDefault();
|
71
|
+
event.nativeEvent.stopImmediatePropagation(); // Forward Enter key press to active descendant so that item gets activated
|
72
|
+
|
73
|
+
const activeDescendantEvent = new KeyboardEvent(event.type, event.nativeEvent);
|
74
|
+
activeDescendantRef.current.dispatchEvent(activeDescendantEvent);
|
75
|
+
}
|
76
|
+
}, [activeDescendantRef]);
|
77
|
+
useEffect(() => {
|
78
|
+
if (!(inputRef !== null && inputRef !== void 0 && inputRef.current)) {
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
|
82
|
+
if (!autocompleteSuggestion) {
|
83
|
+
inputRef.current.value = inputValue;
|
84
|
+
}
|
85
|
+
|
86
|
+
if (highlightRemainingText && autocompleteSuggestion && (inputValue || isMenuDirectlyActivated)) {
|
87
|
+
inputRef.current.value = autocompleteSuggestion;
|
88
|
+
|
89
|
+
if (autocompleteSuggestion.toLowerCase().indexOf(inputValue.toLowerCase()) === 0) {
|
90
|
+
inputRef.current.setSelectionRange(inputValue.length, autocompleteSuggestion.length);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}, [autocompleteSuggestion, inputValue]);
|
94
|
+
useEffect(() => {
|
95
|
+
if (value && setInputValue) {
|
96
|
+
setInputValue(value.toString());
|
97
|
+
}
|
98
|
+
}, [value]);
|
99
|
+
return /*#__PURE__*/React.createElement(Component, _extends({
|
100
|
+
onFocus: handleInputFocus,
|
101
|
+
onBlur: handleInputBlur,
|
102
|
+
onChange: handleInputChange,
|
103
|
+
onKeyDown: handleInputKeyDown,
|
104
|
+
onKeyPress: onInputKeyPress,
|
105
|
+
onKeyUp: handleInputKeyUp,
|
106
|
+
ref: combinedInputRef
|
107
|
+
}, props));
|
108
|
+
});
|
109
|
+
export default AutocompleteInput;
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ItemProps } from '../ActionList';
|
3
|
+
import { ItemInput } from '../ActionList/List';
|
4
|
+
import { OverlayProps } from '../Overlay';
|
5
|
+
import { ComponentProps } from '../utils/types';
|
6
|
+
declare type AutocompleteMenuInternalProps = {
|
7
|
+
/**
|
8
|
+
* A menu item that is used to allow users make a selection that is not available in the array passed to the `items` prop.
|
9
|
+
* This menu item gets appended to the end of the list of options.
|
10
|
+
*/
|
11
|
+
addNewItem?: Omit<ItemInput, 'onAction'>;
|
12
|
+
/**
|
13
|
+
* The text that appears in the menu when there are no options in the array passed to the `items` prop.
|
14
|
+
*/
|
15
|
+
emptyStateText?: React.ReactNode | false;
|
16
|
+
/**
|
17
|
+
* A custom function used to filter the options in the array passed to the `items` prop.
|
18
|
+
* By default, we filter out items that don't match the value of the autocomplete text input. The default filter is not case-sensitive.
|
19
|
+
*/
|
20
|
+
filterFn?: (item: ItemInput, i: number) => boolean;
|
21
|
+
/**
|
22
|
+
* The options for field values that are displayed in the dropdown menu.
|
23
|
+
* One or more may be selected depending on the value of the `selectionVariant` prop.
|
24
|
+
*/
|
25
|
+
items: ItemInput[];
|
26
|
+
/**
|
27
|
+
* The function that is called when an item in the list is de-selected
|
28
|
+
*/
|
29
|
+
onItemDeselect?: NonNullable<ItemProps['onAction']>;
|
30
|
+
/**
|
31
|
+
* The function that is called when an item in the list is selected
|
32
|
+
*/
|
33
|
+
onItemSelect?: NonNullable<ItemProps['onAction']>;
|
34
|
+
/**
|
35
|
+
* Whether the data is loaded for the menu items
|
36
|
+
*/
|
37
|
+
loading?: boolean;
|
38
|
+
/**
|
39
|
+
* The IDs of the selected items
|
40
|
+
*/
|
41
|
+
selectedItemIds: Array<string | number>;
|
42
|
+
/**
|
43
|
+
* The sort function that is applied to the options in the array passed to the `items` prop after the user closes the menu.
|
44
|
+
* By default, selected items are sorted to the top after the user closes the menu.
|
45
|
+
*/
|
46
|
+
selectedSortFn?: (itemIdA: string | number, itemIdB: string | number) => number;
|
47
|
+
/**
|
48
|
+
* Whether there can be one item selected from the menu or multiple items selected from the menu
|
49
|
+
*/
|
50
|
+
selectionVariant?: 'single' | 'multiple';
|
51
|
+
};
|
52
|
+
declare const AutocompleteMenu: React.ForwardRefExoticComponent<AutocompleteMenuInternalProps & Pick<OverlayProps, "height" | "maxHeight" | "width"> & React.RefAttributes<HTMLInputElement>>;
|
53
|
+
export declare type AutocompleteMenuProps = ComponentProps<typeof AutocompleteMenu>;
|
54
|
+
export default AutocompleteMenu;
|
@@ -0,0 +1,244 @@
|
|
1
|
+
import React, { useContext, useEffect, useRef, useState } from 'react';
|
2
|
+
import { ActionList } from '../ActionList';
|
3
|
+
import { useAnchoredPosition } from '../hooks';
|
4
|
+
import { useFocusZone } from '../hooks/useFocusZone';
|
5
|
+
import Overlay from '../Overlay';
|
6
|
+
import { Box, Spinner } from '../';
|
7
|
+
import { registerPortalRoot } from '../Portal';
|
8
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
9
|
+
import { useCombinedRefs } from '../hooks/useCombinedRefs';
|
10
|
+
const DROPDOWN_PORTAL_CONTAINER_NAME = '__listcontainerportal__';
|
11
|
+
|
12
|
+
const getDefaultSortFn = isItemSelectedFn => (itemIdA, itemIdB) => isItemSelectedFn(itemIdA) === isItemSelectedFn(itemIdB) ? 0 : isItemSelectedFn(itemIdA) ? -1 : 1;
|
13
|
+
|
14
|
+
function scrollIntoViewingArea(child, container, margin = 8, behavior = 'smooth') {
|
15
|
+
const {
|
16
|
+
top: childTop,
|
17
|
+
bottom: childBottom
|
18
|
+
} = child.getBoundingClientRect();
|
19
|
+
const {
|
20
|
+
top: containerTop,
|
21
|
+
bottom: containerBottom
|
22
|
+
} = container.getBoundingClientRect();
|
23
|
+
const isChildTopAboveViewingArea = childTop < containerTop + margin;
|
24
|
+
const isChildBottomBelowViewingArea = childBottom > containerBottom - margin;
|
25
|
+
|
26
|
+
if (isChildTopAboveViewingArea) {
|
27
|
+
const scrollHeightToChildTop = childTop - containerTop + container.scrollTop;
|
28
|
+
container.scrollTo({
|
29
|
+
behavior,
|
30
|
+
top: scrollHeightToChildTop - margin
|
31
|
+
});
|
32
|
+
} else if (isChildBottomBelowViewingArea) {
|
33
|
+
const scrollHeightToChildBottom = childBottom - containerBottom + container.scrollTop;
|
34
|
+
container.scrollTo({
|
35
|
+
behavior,
|
36
|
+
top: scrollHeightToChildBottom + margin
|
37
|
+
});
|
38
|
+
} // either completely in view or outside viewing area on both ends, don't scroll
|
39
|
+
|
40
|
+
}
|
41
|
+
|
42
|
+
const defaultItemFilter = filterValue => (item, _i) => {
|
43
|
+
var _item$text;
|
44
|
+
|
45
|
+
return Boolean(item === null || item === void 0 ? void 0 : (_item$text = item.text) === null || _item$text === void 0 ? void 0 : _item$text.toLowerCase().startsWith(filterValue.toLowerCase()));
|
46
|
+
}; // TODO:
|
47
|
+
// insteaad of using `forwardRef`, just use a regular Functional Component
|
48
|
+
// get rid of unused props
|
49
|
+
|
50
|
+
|
51
|
+
const AutocompleteMenu = /*#__PURE__*/React.forwardRef(({
|
52
|
+
items,
|
53
|
+
selectedItemIds,
|
54
|
+
selectedSortFn,
|
55
|
+
onItemSelect,
|
56
|
+
onItemDeselect,
|
57
|
+
emptyStateText,
|
58
|
+
addNewItem,
|
59
|
+
loading,
|
60
|
+
selectionVariant,
|
61
|
+
filterFn: externalFilterFn,
|
62
|
+
width,
|
63
|
+
height,
|
64
|
+
maxHeight
|
65
|
+
}, ref) => {
|
66
|
+
const {
|
67
|
+
activeDescendantRef,
|
68
|
+
inputRef,
|
69
|
+
inputValue = '',
|
70
|
+
setAutocompleteSuggestion,
|
71
|
+
setShowMenu,
|
72
|
+
setInputValue,
|
73
|
+
setIsMenuDirectlyActivated,
|
74
|
+
showMenu
|
75
|
+
} = useContext(AutocompleteContext);
|
76
|
+
const filterFn = externalFilterFn ? externalFilterFn : defaultItemFilter(inputValue);
|
77
|
+
const listContainerRef = useRef(null);
|
78
|
+
const scrollContainerRef = useRef(null);
|
79
|
+
const [highlightedItem, setHighlightedItem] = useState(); // TODO: clean up this mess by making id required on ItemProps
|
80
|
+
|
81
|
+
const [sortedItemIds, setSortedItemIds] = useState(items.map(({
|
82
|
+
id
|
83
|
+
}) => id || id === 0 ? id : ''));
|
84
|
+
const {
|
85
|
+
floatingElementRef,
|
86
|
+
position
|
87
|
+
} = useAnchoredPosition({
|
88
|
+
side: 'outside-bottom',
|
89
|
+
align: 'start',
|
90
|
+
anchorElementRef: inputRef
|
91
|
+
}, [showMenu, selectedItemIds]);
|
92
|
+
const combinedOverlayRef = useCombinedRefs(scrollContainerRef, floatingElementRef);
|
93
|
+
|
94
|
+
const closeOptionList = () => {
|
95
|
+
if (setShowMenu) {
|
96
|
+
setShowMenu(false);
|
97
|
+
}
|
98
|
+
};
|
99
|
+
|
100
|
+
const isItemSelected = itemId => {
|
101
|
+
var _items$find;
|
102
|
+
|
103
|
+
return ((_items$find = items.find(selectableItem => selectableItem.id === itemId)) === null || _items$find === void 0 ? void 0 : _items$find.selected) || selectedItemIds.includes(itemId);
|
104
|
+
};
|
105
|
+
|
106
|
+
const itemsToRender = [// selectable tokens
|
107
|
+
...items.map(selectableItem => {
|
108
|
+
return { ...selectableItem,
|
109
|
+
//TODO: just make `id` required
|
110
|
+
selected: selectionVariant === 'multiple' ? isItemSelected(selectableItem.id) : undefined,
|
111
|
+
onAction: (item, e) => {
|
112
|
+
// TODO: clean up all of these `if/else` statements
|
113
|
+
if (item.selected) {
|
114
|
+
// TODO: make `onItemDeselect` optional
|
115
|
+
if (onItemDeselect) {
|
116
|
+
onItemDeselect(item, e);
|
117
|
+
}
|
118
|
+
} else {
|
119
|
+
// TODO: make `onItemSelect` optional
|
120
|
+
if (onItemSelect) {
|
121
|
+
onItemSelect(item, e);
|
122
|
+
}
|
123
|
+
|
124
|
+
if (selectionVariant === 'multiple') {
|
125
|
+
if (setInputValue) {
|
126
|
+
setInputValue('');
|
127
|
+
}
|
128
|
+
|
129
|
+
if (setAutocompleteSuggestion) {
|
130
|
+
setAutocompleteSuggestion('');
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
if (selectionVariant === 'single') {
|
136
|
+
if (setShowMenu) {
|
137
|
+
setShowMenu(false);
|
138
|
+
}
|
139
|
+
|
140
|
+
if (inputRef !== null && inputRef !== void 0 && inputRef.current) {
|
141
|
+
inputRef.current.setSelectionRange(inputRef.current.value.length, inputRef.current.value.length);
|
142
|
+
}
|
143
|
+
}
|
144
|
+
}
|
145
|
+
};
|
146
|
+
}), // menu item used for creating a token from whatever is in the text input
|
147
|
+
...(addNewItem ? [{ ...addNewItem,
|
148
|
+
onAction: onItemSelect ? (_item, e) => {
|
149
|
+
onItemSelect({
|
150
|
+
text: inputValue,
|
151
|
+
id: `randomlyGeneratedId-${inputValue}`
|
152
|
+
}, e);
|
153
|
+
} : undefined
|
154
|
+
}] : [])];
|
155
|
+
useFocusZone({
|
156
|
+
containerRef: listContainerRef,
|
157
|
+
focusOutBehavior: 'wrap',
|
158
|
+
focusableElementFilter: element => {
|
159
|
+
return !(element instanceof HTMLInputElement);
|
160
|
+
},
|
161
|
+
activeDescendantFocus: inputRef,
|
162
|
+
onActiveDescendantChanged: (current, _previous, directlyActivated) => {
|
163
|
+
if (activeDescendantRef) {
|
164
|
+
activeDescendantRef.current = current || null;
|
165
|
+
}
|
166
|
+
|
167
|
+
if (current) {
|
168
|
+
const selectedItem = itemsToRender.find(item => {
|
169
|
+
var _item$id;
|
170
|
+
|
171
|
+
return ((_item$id = item.id) === null || _item$id === void 0 ? void 0 : _item$id.toString()) === (current === null || current === void 0 ? void 0 : current.dataset.id);
|
172
|
+
});
|
173
|
+
setHighlightedItem({ ...selectedItem,
|
174
|
+
isDirectlyActivated: directlyActivated
|
175
|
+
});
|
176
|
+
|
177
|
+
if (setIsMenuDirectlyActivated) {
|
178
|
+
setIsMenuDirectlyActivated(directlyActivated);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
if (current && scrollContainerRef.current && directlyActivated) {
|
183
|
+
scrollIntoViewingArea(current, scrollContainerRef.current);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
});
|
187
|
+
useEffect(() => {
|
188
|
+
var _highlightedItem$text;
|
189
|
+
|
190
|
+
if (!setAutocompleteSuggestion) {
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
|
194
|
+
if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(inputValue || '')) {
|
195
|
+
setAutocompleteSuggestion(highlightedItem.text);
|
196
|
+
} else {
|
197
|
+
setAutocompleteSuggestion('');
|
198
|
+
}
|
199
|
+
}, [highlightedItem, inputValue]);
|
200
|
+
useEffect(() => {
|
201
|
+
setSortedItemIds([...sortedItemIds].sort(selectedSortFn ? selectedSortFn : getDefaultSortFn(isItemSelected)));
|
202
|
+
}, [showMenu]);
|
203
|
+
|
204
|
+
if (listContainerRef.current) {
|
205
|
+
registerPortalRoot(listContainerRef.current, DROPDOWN_PORTAL_CONTAINER_NAME);
|
206
|
+
}
|
207
|
+
|
208
|
+
const itemSortOrderData = sortedItemIds.reduce((acc, curr, i) => {
|
209
|
+
acc[curr] = i;
|
210
|
+
return acc;
|
211
|
+
}, {});
|
212
|
+
const sortedAndFilteredItemsToRender = [...(filterFn ? itemsToRender.filter(filterFn) : itemsToRender)].sort((a, b) => itemSortOrderData[a.id] - itemSortOrderData[b.id]);
|
213
|
+
return /*#__PURE__*/React.createElement("div", {
|
214
|
+
ref: listContainerRef
|
215
|
+
}, showMenu && emptyStateText ? /*#__PURE__*/React.createElement(Overlay, {
|
216
|
+
returnFocusRef: inputRef,
|
217
|
+
portalContainerName: DROPDOWN_PORTAL_CONTAINER_NAME,
|
218
|
+
preventFocusOnOpen: true,
|
219
|
+
onClickOutside: closeOptionList,
|
220
|
+
onEscape: closeOptionList,
|
221
|
+
ref: combinedOverlayRef,
|
222
|
+
top: position === null || position === void 0 ? void 0 : position.top,
|
223
|
+
left: position === null || position === void 0 ? void 0 : position.left,
|
224
|
+
width: width,
|
225
|
+
height: height,
|
226
|
+
maxHeight: maxHeight
|
227
|
+
}, loading ? /*#__PURE__*/React.createElement(Box, {
|
228
|
+
p: 3,
|
229
|
+
display: "flex",
|
230
|
+
justifyContent: "center"
|
231
|
+
}, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement(React.Fragment, null, sortedAndFilteredItemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
|
232
|
+
selectionVariant: "multiple",
|
233
|
+
items: sortedAndFilteredItemsToRender,
|
234
|
+
role: "listbox"
|
235
|
+
}) : /*#__PURE__*/React.createElement(Box, {
|
236
|
+
p: 3
|
237
|
+
}, emptyStateText))) : null);
|
238
|
+
});
|
239
|
+
AutocompleteMenu.defaultProps = {
|
240
|
+
emptyStateText: 'No selectable options',
|
241
|
+
selectionVariant: 'single'
|
242
|
+
};
|
243
|
+
AutocompleteMenu.displayName = 'AutocompleteMenu';
|
244
|
+
export default AutocompleteMenu;
|