@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
@@ -0,0 +1,211 @@
|
|
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, { useRef, useState } from 'react';
|
4
|
+
import { omit, pick } from '@styled-system/props';
|
5
|
+
import classnames from 'classnames';
|
6
|
+
import styled, { css } from 'styled-components';
|
7
|
+
import { maxWidth, minWidth, variant, width } from 'styled-system';
|
8
|
+
import { FocusKeys } from './behaviors/focusZone';
|
9
|
+
import { COMMON, get } from './constants';
|
10
|
+
import { useCombinedRefs } from './hooks/useCombinedRefs';
|
11
|
+
import { useFocusZone } from './hooks/useFocusZone';
|
12
|
+
import sx from './sx';
|
13
|
+
import Token from './Token/Token';
|
14
|
+
const sizeVariants = variant({
|
15
|
+
variants: {
|
16
|
+
small: {
|
17
|
+
minHeight: '28px',
|
18
|
+
px: 2,
|
19
|
+
py: '3px',
|
20
|
+
fontSize: 0,
|
21
|
+
lineHeight: '20px'
|
22
|
+
},
|
23
|
+
large: {
|
24
|
+
px: 2,
|
25
|
+
py: '10px',
|
26
|
+
fontSize: 3
|
27
|
+
}
|
28
|
+
}
|
29
|
+
});
|
30
|
+
const Input = styled.input.withConfig({
|
31
|
+
displayName: "TextInputTokens__Input",
|
32
|
+
componentId: "sc-1k54xiz-0"
|
33
|
+
})(["border:0;font-size:inherit;font-family:inherit;background-color:transparent;-webkit-appearance:none;color:inherit;height:100%;width:100%;padding:0;&:focus{outline:0;}"]);
|
34
|
+
const InputWrapper = styled.div.withConfig({
|
35
|
+
displayName: "TextInputTokens__InputWrapper",
|
36
|
+
componentId: "sc-1k54xiz-1"
|
37
|
+
})(["position:relative;order:1;flex-grow:1;&:after{pointer-events:none;display:flex;align-items:center;position:absolute;left:0;top:1px;width:100%;height:100%;display:flex;align-items:center;color:rgba(0,0,0,0.5);}"]);
|
38
|
+
const Wrapper = styled.span.withConfig({
|
39
|
+
displayName: "TextInputTokens__Wrapper",
|
40
|
+
componentId: "sc-1k54xiz-2"
|
41
|
+
})(["display:inline-flex;align-items:stretch;min-height:34px;font-size:", ";line-height:20px;color:", ";vertical-align:middle;background-repeat:no-repeat;background-position:right 8px center;border:1px solid ", ";border-radius:", ";outline:none;box-shadow:", ";flex-wrap:wrap;gap:0.25rem;", " ", " .TextInput-icon{align-self:center;color:", ";margin:0 ", ";flex-shrink:0;}&:focus-within{border-color:", ";box-shadow:", ";}", " ", " ", " @media (min-width:", "){font-size:", ";}", " ", " ", " ", " ", " ", ";"], get('fontSizes.1'), get('colors.text.primary'), get('colors.border.primary'), get('radii.2'), get('shadows.shadow.inset'), props => {
|
42
|
+
if (props.hasIcon) {
|
43
|
+
return css(["padding:0;"]);
|
44
|
+
} else {
|
45
|
+
return css(["padding:6px 12px;"]);
|
46
|
+
}
|
47
|
+
}, props => {
|
48
|
+
if (props.maxHeight) {
|
49
|
+
return css(["max-height:", ";overflow:auto;"], props.maxHeight);
|
50
|
+
}
|
51
|
+
}, get('colors.icon.tertiary'), get('space.2'), get('colors.state.focus.border'), get('shadows.state.focus.shadow'), props => props.contrast && css(["background-color:", ";"], get('colors.input.contrastBg')), props => props.disabled && css(["color:", ";background-color:", ";border-color:", ";"], get('colors.text.secondary'), get('colors.input.disabledBg'), get('colors.input.disabledBorder')), props => props.block && css(["display:block;width:100%;"]), get('breakpoints.1'), get('fontSizes.1'), COMMON, width, minWidth, maxWidth, sizeVariants, sx);
|
52
|
+
// using forwardRef is important so that other components (ex. SelectMenu) can autofocus the input
|
53
|
+
const TextInputWithTokens = /*#__PURE__*/React.forwardRef(({
|
54
|
+
icon: IconComponent,
|
55
|
+
contrast,
|
56
|
+
className,
|
57
|
+
block,
|
58
|
+
disabled,
|
59
|
+
theme,
|
60
|
+
sx: sxProp,
|
61
|
+
tokens,
|
62
|
+
onTokenRemove,
|
63
|
+
tokenComponent: TokenComponent,
|
64
|
+
...rest
|
65
|
+
}, ref) => {
|
66
|
+
const localInputRef = useRef(null);
|
67
|
+
const combinedInputRef = useCombinedRefs(localInputRef, ref); // this class is necessary to style FilterSearch, plz no touchy!
|
68
|
+
|
69
|
+
const wrapperClasses = classnames(className, 'TextInput-wrapper');
|
70
|
+
const wrapperProps = pick(rest);
|
71
|
+
const {
|
72
|
+
onFocus,
|
73
|
+
onKeyDown,
|
74
|
+
...inputPropsRest
|
75
|
+
} = omit(rest);
|
76
|
+
const [selectedTokenIdx, setSelectedTokenIdx] = useState();
|
77
|
+
const {
|
78
|
+
containerRef
|
79
|
+
} = useFocusZone({
|
80
|
+
focusOutBehavior: 'wrap',
|
81
|
+
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
|
82
|
+
focusableElementFilter: element => {
|
83
|
+
return !(element instanceof HTMLButtonElement);
|
84
|
+
},
|
85
|
+
getNextFocusable: (direction, from, event) => {
|
86
|
+
var _containerRef$current;
|
87
|
+
|
88
|
+
if (!selectedTokenIdx && selectedTokenIdx !== 0) {
|
89
|
+
return undefined;
|
90
|
+
}
|
91
|
+
|
92
|
+
let nextIndex = selectedTokenIdx + 1;
|
93
|
+
|
94
|
+
if (direction === 'next') {
|
95
|
+
console.log('direction is next');
|
96
|
+
nextIndex += 1;
|
97
|
+
}
|
98
|
+
|
99
|
+
if (direction === 'previous') {
|
100
|
+
console.log('direction is previous');
|
101
|
+
nextIndex -= 1;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (nextIndex > tokens.length || nextIndex < 1) {
|
105
|
+
return combinedInputRef.current || undefined;
|
106
|
+
}
|
107
|
+
|
108
|
+
return containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.children[nextIndex];
|
109
|
+
}
|
110
|
+
}, [selectedTokenIdx]);
|
111
|
+
|
112
|
+
const handleTokenRemove = tokenId => {
|
113
|
+
onTokenRemove(tokenId);
|
114
|
+
};
|
115
|
+
|
116
|
+
const handleTokenFocus = tokenIdx => () => {
|
117
|
+
setSelectedTokenIdx(tokenIdx);
|
118
|
+
};
|
119
|
+
|
120
|
+
const handleTokenBlur = () => {
|
121
|
+
setSelectedTokenIdx(undefined);
|
122
|
+
};
|
123
|
+
|
124
|
+
const handleTokenKeyUp = tokenId => e => {
|
125
|
+
if (e.key === 'Backspace') {
|
126
|
+
handleTokenRemove(tokenId);
|
127
|
+
}
|
128
|
+
|
129
|
+
if (e.key === 'Escape') {
|
130
|
+
var _combinedInputRef$cur;
|
131
|
+
|
132
|
+
combinedInputRef === null || combinedInputRef === void 0 ? void 0 : (_combinedInputRef$cur = combinedInputRef.current) === null || _combinedInputRef$cur === void 0 ? void 0 : _combinedInputRef$cur.focus();
|
133
|
+
}
|
134
|
+
};
|
135
|
+
|
136
|
+
const handleInputFocus = e => {
|
137
|
+
if (onFocus) {
|
138
|
+
onFocus(e);
|
139
|
+
}
|
140
|
+
|
141
|
+
setSelectedTokenIdx(undefined);
|
142
|
+
};
|
143
|
+
|
144
|
+
const handleInputKeyDown = e => {
|
145
|
+
var _combinedInputRef$cur2;
|
146
|
+
|
147
|
+
if (onKeyDown) {
|
148
|
+
onKeyDown(e);
|
149
|
+
}
|
150
|
+
|
151
|
+
if ((_combinedInputRef$cur2 = combinedInputRef.current) !== null && _combinedInputRef$cur2 !== void 0 && _combinedInputRef$cur2.value) {
|
152
|
+
return;
|
153
|
+
}
|
154
|
+
|
155
|
+
const lastToken = tokens[tokens.length - 1];
|
156
|
+
|
157
|
+
if (e.key === 'Backspace') {
|
158
|
+
handleTokenRemove(lastToken.id);
|
159
|
+
|
160
|
+
if (combinedInputRef.current) {
|
161
|
+
// TODO: eliminate the first hack by making changes to the Autocomplete component
|
162
|
+
// HACKS:
|
163
|
+
// 1. Directly setting `combinedInputRef.current.value` instead of updating state because the autocomplete highlight behavior doesn't work correctly if we update the value with a setState action in onChange
|
164
|
+
// 2. Adding an extra space so that when I backspace, it doesn't delete the last letter
|
165
|
+
combinedInputRef.current.value = `${lastToken.text} `;
|
166
|
+
} // HACK: for some reason we need to wait a tick for `.select()` to work
|
167
|
+
|
168
|
+
|
169
|
+
setTimeout(() => {
|
170
|
+
var _combinedInputRef$cur3;
|
171
|
+
|
172
|
+
combinedInputRef === null || combinedInputRef === void 0 ? void 0 : (_combinedInputRef$cur3 = combinedInputRef.current) === null || _combinedInputRef$cur3 === void 0 ? void 0 : _combinedInputRef$cur3.select();
|
173
|
+
}, 1);
|
174
|
+
}
|
175
|
+
};
|
176
|
+
|
177
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Wrapper, _extends({
|
178
|
+
className: wrapperClasses,
|
179
|
+
hasIcon: !!IconComponent,
|
180
|
+
block: block,
|
181
|
+
theme: theme,
|
182
|
+
disabled: disabled,
|
183
|
+
contrast: contrast,
|
184
|
+
sx: sxProp,
|
185
|
+
ref: containerRef
|
186
|
+
}, wrapperProps), /*#__PURE__*/React.createElement(InputWrapper, null, /*#__PURE__*/React.createElement(Input, _extends({
|
187
|
+
ref: combinedInputRef,
|
188
|
+
disabled: disabled,
|
189
|
+
onFocus: handleInputFocus,
|
190
|
+
onKeyDown: handleInputKeyDown,
|
191
|
+
type: "text"
|
192
|
+
}, inputPropsRest))), tokens !== null && tokens !== void 0 && tokens.length && TokenComponent ? tokens.map((token, i) => /*#__PURE__*/React.createElement(TokenComponent, {
|
193
|
+
onFocus: handleTokenFocus(i),
|
194
|
+
onBlur: handleTokenBlur,
|
195
|
+
onKeyUp: handleTokenKeyUp(token.id),
|
196
|
+
text: token.text || '' // TODO: just make token.text required
|
197
|
+
,
|
198
|
+
isSelected: selectedTokenIdx === i,
|
199
|
+
handleRemove: () => {
|
200
|
+
handleTokenRemove(token.id);
|
201
|
+
},
|
202
|
+
variant: "xl",
|
203
|
+
fillColor: token.labelColor ? token.labelColor : undefined,
|
204
|
+
tabIndex: 0
|
205
|
+
})) : null));
|
206
|
+
});
|
207
|
+
TextInputWithTokens.defaultProps = {
|
208
|
+
tokenComponent: Token
|
209
|
+
};
|
210
|
+
TextInputWithTokens.displayName = 'TextInputWithTokens';
|
211
|
+
export default TextInputWithTokens;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { MaxWidthProps, MinWidthProps, WidthProps } from 'styled-system';
|
3
|
+
import { ItemProps } from './ActionList';
|
4
|
+
import { ItemInput } from './ActionList/List';
|
5
|
+
import { SystemCommonProps } from './constants';
|
6
|
+
import { SxProp } from './sx';
|
7
|
+
import { ComponentProps } from './utils/types';
|
8
|
+
import Token from './Token/Token';
|
9
|
+
declare const Input: import("styled-components").StyledComponent<"input", any, {}, never>;
|
10
|
+
declare const Wrapper: import("styled-components").StyledComponent<"span", any, {
|
11
|
+
disabled?: boolean | undefined;
|
12
|
+
hasIcon?: boolean | undefined;
|
13
|
+
block?: boolean | undefined;
|
14
|
+
contrast?: boolean | undefined;
|
15
|
+
variant?: "large" | "small" | undefined;
|
16
|
+
maxHeight?: React.CSSProperties['maxHeight'];
|
17
|
+
} & SystemCommonProps & WidthProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>, import("csstype").Property.Width<import("styled-system").TLengthStyledSystem>> & MinWidthProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>, import("csstype").Property.MinWidth<import("styled-system").TLengthStyledSystem>> & MaxWidthProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>, import("csstype").Property.MaxWidth<import("styled-system").TLengthStyledSystem>> & SxProp, never>;
|
18
|
+
interface Token {
|
19
|
+
text?: string;
|
20
|
+
id: string | number;
|
21
|
+
}
|
22
|
+
declare type TextInputWithTokensInternalProps = {
|
23
|
+
as?: any;
|
24
|
+
icon?: React.ComponentType<{
|
25
|
+
className?: string;
|
26
|
+
}>;
|
27
|
+
tokens: Token[];
|
28
|
+
onTokenRemove: (tokenId: string | number) => void;
|
29
|
+
selectableItems: ItemInput[];
|
30
|
+
onFilterChange: (value: string, e: React.ChangeEvent<HTMLInputElement>) => void;
|
31
|
+
onItemSelect: NonNullable<ItemProps['onAction']>;
|
32
|
+
tokenComponent?: React.FunctionComponent<any>;
|
33
|
+
emptyStateText?: React.ReactNode | false;
|
34
|
+
addNewTokenItem?: Omit<ItemInput, 'onAction'>;
|
35
|
+
onCloseOptionsList?: () => void;
|
36
|
+
maxHeight?: React.CSSProperties['maxHeight'];
|
37
|
+
loading?: boolean;
|
38
|
+
} & ComponentProps<typeof Wrapper> & ComponentProps<typeof Input>;
|
39
|
+
declare const TextInputWithTokens: React.ForwardRefExoticComponent<Pick<TextInputWithTokensInternalProps, "backgroundColor" | "color" | "display" | "height" | "marginBottom" | "marginLeft" | "marginRight" | "marginTop" | "maxHeight" | "maxWidth" | "minWidth" | "opacity" | "paddingBottom" | "paddingLeft" | "paddingRight" | "paddingTop" | "translate" | "width" | "margin" | "padding" | "hidden" | "children" | "theme" | "value" | "form" | "p" | "slot" | "style" | "title" | "pattern" | "list" | "type" | "name" | "key" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | "suppressHydrationWarning" | "accessKey" | "className" | "contentEditable" | "contextMenu" | "dir" | "draggable" | "id" | "lang" | "placeholder" | "spellCheck" | "tabIndex" | "radioGroup" | "role" | "about" | "datatype" | "inlist" | "prefix" | "property" | "resource" | "typeof" | "vocab" | "autoCapitalize" | "autoCorrect" | "autoSave" | "itemProp" | "itemScope" | "itemType" | "itemID" | "itemRef" | "results" | "security" | "unselectable" | "inputMode" | "is" | "aria-activedescendant" | "aria-atomic" | "aria-autocomplete" | "aria-busy" | "aria-checked" | "aria-colcount" | "aria-colindex" | "aria-colspan" | "aria-controls" | "aria-current" | "aria-describedby" | "aria-details" | "aria-disabled" | "aria-dropeffect" | "aria-errormessage" | "aria-expanded" | "aria-flowto" | "aria-grabbed" | "aria-haspopup" | "aria-hidden" | "aria-invalid" | "aria-keyshortcuts" | "aria-label" | "aria-labelledby" | "aria-level" | "aria-live" | "aria-modal" | "aria-multiline" | "aria-multiselectable" | "aria-orientation" | "aria-owns" | "aria-placeholder" | "aria-posinset" | "aria-pressed" | "aria-readonly" | "aria-relevant" | "aria-required" | "aria-roledescription" | "aria-rowcount" | "aria-rowindex" | "aria-rowspan" | "aria-selected" | "aria-setsize" | "aria-sort" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "aria-valuetext" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onBeforeInput" | "onBeforeInputCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onAuxClick" | "onAuxClickCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onPointerDown" | "onPointerDownCapture" | "onPointerMove" | "onPointerMoveCapture" | "onPointerUp" | "onPointerUpCapture" | "onPointerCancel" | "onPointerCancelCapture" | "onPointerEnter" | "onPointerEnterCapture" | "onPointerLeave" | "onPointerLeaveCapture" | "onPointerOver" | "onPointerOverCapture" | "onPointerOut" | "onPointerOutCapture" | "onGotPointerCapture" | "onGotPointerCaptureCapture" | "onLostPointerCapture" | "onLostPointerCaptureCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture" | "css" | "block" | "bg" | "m" | "mt" | "mr" | "mb" | "ml" | "mx" | "marginX" | "my" | "marginY" | "pt" | "pr" | "pb" | "pl" | "px" | "paddingX" | "py" | "paddingY" | "step" | "size" | "icon" | "sx" | "max" | "min" | "crossOrigin" | "alt" | "src" | "autoFocus" | "disabled" | "formAction" | "formEncType" | "formMethod" | "formNoValidate" | "formTarget" | "autoComplete" | "loading" | "accept" | "capture" | "checked" | "enterKeyHint" | "maxLength" | "minLength" | "multiple" | "readOnly" | "required" | "as" | "variant" | "hasIcon" | "contrast" | "onItemSelect" | "emptyStateText" | "onFilterChange" | "tokens" | "onTokenRemove" | "tokenComponent" | "selectableItems" | "addNewTokenItem" | "onCloseOptionsList"> & React.RefAttributes<HTMLInputElement>>;
|
40
|
+
export declare type TextInputWithTokensProps = ComponentProps<typeof TextInputWithTokens>;
|
41
|
+
export default TextInputWithTokens;
|
@@ -0,0 +1,361 @@
|
|
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, useEffect, useRef, useState } from 'react';
|
4
|
+
import { omit, pick } from '@styled-system/props';
|
5
|
+
import classnames from 'classnames';
|
6
|
+
import styled, { css } from 'styled-components';
|
7
|
+
import { maxWidth, minWidth, variant, width } from 'styled-system';
|
8
|
+
import { ActionList } from './ActionList';
|
9
|
+
import { FocusKeys } from './behaviors/focusZone';
|
10
|
+
import { COMMON, get } from './constants';
|
11
|
+
import { useAnchoredPosition } from './hooks';
|
12
|
+
import { useCombinedRefs } from './hooks/useCombinedRefs';
|
13
|
+
import { useFocusZone } from './hooks/useFocusZone';
|
14
|
+
import Overlay from './Overlay';
|
15
|
+
import sx from './sx';
|
16
|
+
import Token from './Token/Token';
|
17
|
+
import { Box, Spinner } from '.';
|
18
|
+
import { registerPortalRoot } from './Portal';
|
19
|
+
const DROPDOWN_PORTAL_CONTAINER_NAME = '__listcontainerportal__';
|
20
|
+
|
21
|
+
function scrollIntoViewingArea(child, container, margin = 8, behavior = 'smooth') {
|
22
|
+
const {
|
23
|
+
top: childTop,
|
24
|
+
bottom: childBottom
|
25
|
+
} = child.getBoundingClientRect();
|
26
|
+
const {
|
27
|
+
top: containerTop,
|
28
|
+
bottom: containerBottom
|
29
|
+
} = container.getBoundingClientRect();
|
30
|
+
const isChildTopAboveViewingArea = childTop < containerTop + margin;
|
31
|
+
const isChildBottomBelowViewingArea = childBottom > containerBottom - margin;
|
32
|
+
|
33
|
+
if (isChildTopAboveViewingArea) {
|
34
|
+
const scrollHeightToChildTop = childTop - containerTop + container.scrollTop;
|
35
|
+
container.scrollTo({
|
36
|
+
behavior,
|
37
|
+
top: scrollHeightToChildTop - margin
|
38
|
+
});
|
39
|
+
} else if (isChildBottomBelowViewingArea) {
|
40
|
+
const scrollHeightToChildBottom = childBottom - containerBottom + container.scrollTop;
|
41
|
+
container.scrollTo({
|
42
|
+
behavior,
|
43
|
+
top: scrollHeightToChildBottom + margin
|
44
|
+
});
|
45
|
+
} // either completely in view or outside viewing area on both ends, don't scroll
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
const sizeVariants = variant({
|
50
|
+
variants: {
|
51
|
+
small: {
|
52
|
+
minHeight: '28px',
|
53
|
+
px: 2,
|
54
|
+
py: '3px',
|
55
|
+
fontSize: 0,
|
56
|
+
lineHeight: '20px'
|
57
|
+
},
|
58
|
+
large: {
|
59
|
+
px: 2,
|
60
|
+
py: '10px',
|
61
|
+
fontSize: 3
|
62
|
+
}
|
63
|
+
}
|
64
|
+
});
|
65
|
+
const Input = styled.input.withConfig({
|
66
|
+
displayName: "TextInputWithTokens__Input",
|
67
|
+
componentId: "sc-8z94t5-0"
|
68
|
+
})(["border:0;font-size:inherit;font-family:inherit;background-color:transparent;-webkit-appearance:none;color:inherit;height:100%;width:100%;padding:0;&:focus{outline:0;}"]);
|
69
|
+
const InputWrapper = styled.div.withConfig({
|
70
|
+
displayName: "TextInputWithTokens__InputWrapper",
|
71
|
+
componentId: "sc-8z94t5-1"
|
72
|
+
})(["position:relative;order:1;flex-grow:1;&:after{content:attr(data-autocompleteSuggestion);pointer-events:none;display:flex;align-items:center;position:absolute;left:0;top:1px;width:100%;height:100%;display:flex;align-items:center;color:rgba(0,0,0,0.5);}"]);
|
73
|
+
const Wrapper = styled.span.withConfig({
|
74
|
+
displayName: "TextInputWithTokens__Wrapper",
|
75
|
+
componentId: "sc-8z94t5-2"
|
76
|
+
})(["display:inline-flex;align-items:stretch;min-height:34px;font-size:", ";line-height:20px;color:", ";vertical-align:middle;background-repeat:no-repeat;background-position:right 8px center;border:1px solid ", ";border-radius:", ";outline:none;box-shadow:", ";flex-wrap:wrap;gap:0.25rem;", " ", " .TextInput-icon{align-self:center;color:", ";margin:0 ", ";flex-shrink:0;}&:focus-within{border-color:", ";box-shadow:", ";}", " ", " ", " @media (min-width:", "){font-size:", ";}", " ", " ", " ", " ", " ", ";"], get('fontSizes.1'), get('colors.text.primary'), get('colors.border.primary'), get('radii.2'), get('shadows.shadow.inset'), props => {
|
77
|
+
if (props.hasIcon) {
|
78
|
+
return css(["padding:0;"]);
|
79
|
+
} else {
|
80
|
+
return css(["padding:6px 12px;"]);
|
81
|
+
}
|
82
|
+
}, props => {
|
83
|
+
if (props.maxHeight) {
|
84
|
+
return css(["max-height:", ";overflow:auto;"], props.maxHeight);
|
85
|
+
}
|
86
|
+
}, get('colors.icon.tertiary'), get('space.2'), get('colors.state.focus.border'), get('shadows.state.focus.shadow'), props => props.contrast && css(["background-color:", ";"], get('colors.input.contrastBg')), props => props.disabled && css(["color:", ";background-color:", ";border-color:", ";"], get('colors.text.secondary'), get('colors.input.disabledBg'), get('colors.input.disabledBorder')), props => props.block && css(["display:block;width:100%;"]), get('breakpoints.1'), get('fontSizes.1'), COMMON, width, minWidth, maxWidth, sizeVariants, sx);
|
87
|
+
// using forwardRef is important so that other components (ex. SelectMenu) can autofocus the input
|
88
|
+
const TextInputWithTokens = /*#__PURE__*/React.forwardRef(({
|
89
|
+
icon: IconComponent,
|
90
|
+
contrast,
|
91
|
+
className,
|
92
|
+
block,
|
93
|
+
disabled,
|
94
|
+
theme,
|
95
|
+
sx: sxProp,
|
96
|
+
tokens,
|
97
|
+
selectableItems,
|
98
|
+
onFilterChange,
|
99
|
+
onItemSelect,
|
100
|
+
onTokenRemove,
|
101
|
+
tokenComponent: TokenComponent,
|
102
|
+
emptyStateText,
|
103
|
+
addNewTokenItem,
|
104
|
+
onCloseOptionsList,
|
105
|
+
loading,
|
106
|
+
...rest
|
107
|
+
}, ref) => {
|
108
|
+
const listContainerRef = useRef(null);
|
109
|
+
const localInputRef = useRef(null);
|
110
|
+
const scrollContainerRef = useRef(null);
|
111
|
+
const activeDescendantRef = useRef();
|
112
|
+
const combinedInputRef = useCombinedRefs(localInputRef, ref); // this class is necessary to style FilterSearch, plz no touchy!
|
113
|
+
|
114
|
+
const wrapperClasses = classnames(className, 'TextInput-wrapper');
|
115
|
+
const wrapperProps = pick(rest);
|
116
|
+
const inputProps = omit(rest);
|
117
|
+
const [selectedTokenIdx, setSelectedTokenIdx] = useState();
|
118
|
+
const [inputVal, setInputVal] = useState('');
|
119
|
+
const [showMenu, setShowMenu] = useState(false);
|
120
|
+
const [selectedItems, setSelectedItems] = useState(tokens.map(({
|
121
|
+
id,
|
122
|
+
text
|
123
|
+
}) => ({
|
124
|
+
id,
|
125
|
+
text
|
126
|
+
})));
|
127
|
+
const [autocompleteSuggestion, setAutocompleteSuggestion] = useState('');
|
128
|
+
const [highlightedItem, setHighlightedItem] = useState();
|
129
|
+
const {
|
130
|
+
containerRef
|
131
|
+
} = useFocusZone({
|
132
|
+
focusOutBehavior: 'wrap',
|
133
|
+
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
|
134
|
+
focusableElementFilter: element => {
|
135
|
+
return !(element instanceof HTMLButtonElement);
|
136
|
+
}
|
137
|
+
});
|
138
|
+
const {
|
139
|
+
floatingElementRef,
|
140
|
+
position
|
141
|
+
} = useAnchoredPosition({
|
142
|
+
side: 'outside-bottom',
|
143
|
+
align: 'start',
|
144
|
+
anchorElementRef: combinedInputRef
|
145
|
+
}, [showMenu, tokens]);
|
146
|
+
|
147
|
+
const closeOptionList = () => {
|
148
|
+
setShowMenu(false);
|
149
|
+
|
150
|
+
if (onCloseOptionsList) {
|
151
|
+
onCloseOptionsList();
|
152
|
+
}
|
153
|
+
};
|
154
|
+
|
155
|
+
const showOptionList = () => {
|
156
|
+
setShowMenu(true);
|
157
|
+
};
|
158
|
+
|
159
|
+
const handleTokenRemove = tokenId => {
|
160
|
+
onTokenRemove(tokenId);
|
161
|
+
setSelectedItems(selectedItems.filter(selectedItem => selectedItem.id !== tokenId));
|
162
|
+
};
|
163
|
+
|
164
|
+
const handleTokenFocus = tokenIdx => () => {
|
165
|
+
setSelectedTokenIdx(tokenIdx);
|
166
|
+
closeOptionList();
|
167
|
+
};
|
168
|
+
|
169
|
+
const handleTokenBlur = () => {
|
170
|
+
setSelectedTokenIdx(undefined);
|
171
|
+
};
|
172
|
+
|
173
|
+
const handleTokenKeyUp = tokenId => e => {
|
174
|
+
if (e.key === 'Backspace') {
|
175
|
+
handleTokenRemove(tokenId);
|
176
|
+
}
|
177
|
+
|
178
|
+
if (e.key === 'Escape') {
|
179
|
+
var _combinedInputRef$cur;
|
180
|
+
|
181
|
+
combinedInputRef === null || combinedInputRef === void 0 ? void 0 : (_combinedInputRef$cur = combinedInputRef.current) === null || _combinedInputRef$cur === void 0 ? void 0 : _combinedInputRef$cur.focus();
|
182
|
+
}
|
183
|
+
};
|
184
|
+
|
185
|
+
const handleInputFocus = () => {
|
186
|
+
setSelectedTokenIdx(undefined);
|
187
|
+
showOptionList();
|
188
|
+
};
|
189
|
+
|
190
|
+
const handleInputChange = e => {
|
191
|
+
onFilterChange(e.currentTarget.value, e);
|
192
|
+
setInputVal(e.currentTarget.value);
|
193
|
+
};
|
194
|
+
|
195
|
+
const handleInputKeyDown = e => {
|
196
|
+
if (e.key === 'ArrowRight' && autocompleteSuggestion) {
|
197
|
+
setInputVal(autocompleteSuggestion); // TODO: use hooks or something to always trigger `onFilterChange` when `inputVal` is changed
|
198
|
+
|
199
|
+
onFilterChange(autocompleteSuggestion, e);
|
200
|
+
}
|
201
|
+
|
202
|
+
if (inputVal) {
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
|
206
|
+
const lastToken = tokens[tokens.length - 1];
|
207
|
+
|
208
|
+
if (e.key === 'Backspace') {
|
209
|
+
handleTokenRemove(lastToken.id);
|
210
|
+
setInputVal(`${lastToken.text}` || ''); // TODO: use hooks or something to always trigger `onFilterChange` when `inputVal` is changed
|
211
|
+
|
212
|
+
onFilterChange(lastToken.text || '', e); // HACK: for some reason we need to wait a tick for `.select()` to work
|
213
|
+
|
214
|
+
setTimeout(() => {
|
215
|
+
var _combinedInputRef$cur2;
|
216
|
+
|
217
|
+
combinedInputRef === null || combinedInputRef === void 0 ? void 0 : (_combinedInputRef$cur2 = combinedInputRef.current) === null || _combinedInputRef$cur2 === void 0 ? void 0 : _combinedInputRef$cur2.select();
|
218
|
+
}, 1);
|
219
|
+
}
|
220
|
+
};
|
221
|
+
|
222
|
+
const onInputKeyPress = useCallback(event => {
|
223
|
+
if (event.key === 'Enter' && activeDescendantRef.current) {
|
224
|
+
event.preventDefault();
|
225
|
+
event.nativeEvent.stopImmediatePropagation(); // Forward Enter key press to active descendant so that item gets activated
|
226
|
+
|
227
|
+
const activeDescendantEvent = new KeyboardEvent(event.type, event.nativeEvent);
|
228
|
+
activeDescendantRef.current.dispatchEvent(activeDescendantEvent);
|
229
|
+
}
|
230
|
+
}, [activeDescendantRef]);
|
231
|
+
|
232
|
+
const getSelectedItems = (itemId, checked) => {
|
233
|
+
const newlySelectedItem = selectableItems.find(item => item.id === itemId);
|
234
|
+
|
235
|
+
if (checked) {
|
236
|
+
return [...selectedItems, ...(newlySelectedItem ? [newlySelectedItem] : [])];
|
237
|
+
}
|
238
|
+
|
239
|
+
return selectedItems.filter(selectedChoice => selectedChoice.id !== itemId);
|
240
|
+
};
|
241
|
+
|
242
|
+
const itemsToRender = [// selectable tokens
|
243
|
+
...selectableItems.map(selectableItem => ({ ...selectableItem,
|
244
|
+
selected: selectableItem.selected || selectedItems.map(item => item.id).includes(selectableItem.id),
|
245
|
+
onAction: (item, e) => {
|
246
|
+
setSelectedItems(getSelectedItems(item.id, !item.selected) || []);
|
247
|
+
|
248
|
+
if (!item.selected) {
|
249
|
+
onItemSelect(item, e);
|
250
|
+
setInputVal('');
|
251
|
+
setAutocompleteSuggestion('');
|
252
|
+
} else {
|
253
|
+
handleTokenRemove(item.id !== null && item.id !== undefined ? item.id : '');
|
254
|
+
}
|
255
|
+
}
|
256
|
+
})), // menu item used for creating a token from whatever is in the text input
|
257
|
+
...(addNewTokenItem ? [{ ...addNewTokenItem,
|
258
|
+
onAction: (_item, e) => {
|
259
|
+
onItemSelect({
|
260
|
+
text: inputVal,
|
261
|
+
id: `randomlyGeneratedId-${inputVal}`
|
262
|
+
}, e);
|
263
|
+
}
|
264
|
+
}] : [])];
|
265
|
+
useFocusZone({
|
266
|
+
containerRef: listContainerRef,
|
267
|
+
focusOutBehavior: 'wrap',
|
268
|
+
focusableElementFilter: element => {
|
269
|
+
return !(element instanceof HTMLInputElement);
|
270
|
+
},
|
271
|
+
activeDescendantFocus: combinedInputRef,
|
272
|
+
onActiveDescendantChanged: (current, _previous, directlyActivated) => {
|
273
|
+
activeDescendantRef.current = current;
|
274
|
+
const selectedItem = itemsToRender.find(item => {
|
275
|
+
var _item$id;
|
276
|
+
|
277
|
+
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);
|
278
|
+
});
|
279
|
+
setHighlightedItem(selectedItem);
|
280
|
+
|
281
|
+
if (current && scrollContainerRef.current && directlyActivated) {
|
282
|
+
scrollIntoViewingArea(current, scrollContainerRef.current);
|
283
|
+
}
|
284
|
+
}
|
285
|
+
});
|
286
|
+
useEffect(() => {
|
287
|
+
var _highlightedItem$text;
|
288
|
+
|
289
|
+
if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(inputVal)) {
|
290
|
+
setAutocompleteSuggestion(highlightedItem.text);
|
291
|
+
} else {
|
292
|
+
setAutocompleteSuggestion('');
|
293
|
+
}
|
294
|
+
}, [highlightedItem, inputVal]);
|
295
|
+
|
296
|
+
if (listContainerRef.current) {
|
297
|
+
registerPortalRoot(listContainerRef.current, DROPDOWN_PORTAL_CONTAINER_NAME);
|
298
|
+
}
|
299
|
+
|
300
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Wrapper, _extends({
|
301
|
+
className: wrapperClasses,
|
302
|
+
hasIcon: !!IconComponent,
|
303
|
+
block: block,
|
304
|
+
theme: theme,
|
305
|
+
disabled: disabled,
|
306
|
+
contrast: contrast,
|
307
|
+
sx: sxProp,
|
308
|
+
ref: containerRef
|
309
|
+
}, wrapperProps), /*#__PURE__*/React.createElement(InputWrapper, {
|
310
|
+
"data-autocompleteSuggestion": autocompleteSuggestion
|
311
|
+
}, /*#__PURE__*/React.createElement(Input, _extends({
|
312
|
+
ref: combinedInputRef,
|
313
|
+
disabled: disabled,
|
314
|
+
onFocus: handleInputFocus,
|
315
|
+
onKeyPress: onInputKeyPress,
|
316
|
+
onKeyDown: handleInputKeyDown,
|
317
|
+
onChange: handleInputChange,
|
318
|
+
type: "text",
|
319
|
+
value: inputVal
|
320
|
+
}, inputProps))), tokens !== null && tokens !== void 0 && tokens.length && TokenComponent ? tokens.map((token, i) => /*#__PURE__*/React.createElement(TokenComponent, {
|
321
|
+
onFocus: handleTokenFocus(i),
|
322
|
+
onBlur: handleTokenBlur,
|
323
|
+
onKeyUp: handleTokenKeyUp(token.id),
|
324
|
+
text: token.text || '' // TODO: just make token.text required
|
325
|
+
,
|
326
|
+
isSelected: selectedTokenIdx === i,
|
327
|
+
handleRemove: () => {
|
328
|
+
handleTokenRemove(token.id);
|
329
|
+
},
|
330
|
+
variant: "xl",
|
331
|
+
fillColor: token.labelColor ? token.labelColor : undefined,
|
332
|
+
tabIndex: 0
|
333
|
+
})) : null), /*#__PURE__*/React.createElement("div", {
|
334
|
+
ref: listContainerRef
|
335
|
+
}, showMenu && emptyStateText ? /*#__PURE__*/React.createElement(Overlay, {
|
336
|
+
returnFocusRef: combinedInputRef,
|
337
|
+
portalContainerName: DROPDOWN_PORTAL_CONTAINER_NAME,
|
338
|
+
preventFocusOnOpen: true,
|
339
|
+
onClickOutside: closeOptionList,
|
340
|
+
onEscape: closeOptionList,
|
341
|
+
ref: floatingElementRef,
|
342
|
+
top: position === null || position === void 0 ? void 0 : position.top,
|
343
|
+
left: position === null || position === void 0 ? void 0 : position.left
|
344
|
+
}, loading ? /*#__PURE__*/React.createElement(Box, {
|
345
|
+
p: 3,
|
346
|
+
display: "flex",
|
347
|
+
justifyContent: "center"
|
348
|
+
}, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement(React.Fragment, null, itemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
|
349
|
+
selectionVariant: "multiple",
|
350
|
+
items: itemsToRender,
|
351
|
+
role: "listbox"
|
352
|
+
}) : /*#__PURE__*/React.createElement(Box, {
|
353
|
+
p: 3
|
354
|
+
}, emptyStateText))) : null));
|
355
|
+
});
|
356
|
+
TextInputWithTokens.defaultProps = {
|
357
|
+
tokenComponent: Token,
|
358
|
+
emptyStateText: 'No selectable options'
|
359
|
+
};
|
360
|
+
TextInputWithTokens.displayName = 'TextInputWithTokens';
|
361
|
+
export default TextInputWithTokens;
|
package/lib-esm/Timeline.d.ts
CHANGED
@@ -374,6 +374,7 @@ declare const _default: string & import("styled-components").StyledComponentBase
|
|
374
374
|
onAnimationIterationCapture?: React.AnimationEventHandler<HTMLDivElement> | undefined;
|
375
375
|
onTransitionEnd?: React.TransitionEventHandler<HTMLDivElement> | undefined;
|
376
376
|
onTransitionEndCapture?: React.TransitionEventHandler<HTMLDivElement> | undefined;
|
377
|
+
css?: import("@emotion/core").InterpolationWithTheme<any>;
|
377
378
|
bg?: import("styled-system").ResponsiveValue<string | number | symbol, Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>> | undefined;
|
378
379
|
m?: import("styled-system").ResponsiveValue<string | number | symbol, Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>> | undefined;
|
379
380
|
mt?: import("styled-system").ResponsiveValue<string | number | symbol, Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>> | undefined;
|