@primer/components 0.0.0-2021829202552 → 0.0.0-2021829233555

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/browser.esm.js +250 -290
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +318 -358
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/FilteredActionList/FilteredActionList.js +31 -6
  7. package/lib/Overlay.d.ts +1 -2
  8. package/lib/Overlay.js +5 -10
  9. package/lib/SelectMenu/SelectMenu.d.ts +2 -8
  10. package/lib/TextInput.d.ts +3 -6
  11. package/lib/TextInput.js +10 -9
  12. package/lib/hooks/useOverlay.d.ts +1 -2
  13. package/lib/hooks/useOverlay.js +6 -11
  14. package/lib/index.d.ts +0 -2
  15. package/lib/index.js +0 -16
  16. package/lib/utils/types.d.ts +0 -3
  17. package/lib-esm/FilteredActionList/FilteredActionList.js +31 -4
  18. package/lib-esm/Overlay.d.ts +1 -2
  19. package/lib-esm/Overlay.js +5 -8
  20. package/lib-esm/SelectMenu/SelectMenu.d.ts +2 -8
  21. package/lib-esm/TextInput.d.ts +3 -6
  22. package/lib-esm/TextInput.js +9 -8
  23. package/lib-esm/hooks/useOverlay.d.ts +1 -2
  24. package/lib-esm/hooks/useOverlay.js +6 -11
  25. package/lib-esm/index.d.ts +0 -2
  26. package/lib-esm/index.js +0 -2
  27. package/lib-esm/utils/types.d.ts +0 -3
  28. package/package.json +4 -7
  29. package/lib/Autocomplete/Autocomplete.d.ts +0 -31
  30. package/lib/Autocomplete/Autocomplete.js +0 -60
  31. package/lib/Autocomplete/AutocompleteContext.d.ts +0 -14
  32. package/lib/Autocomplete/AutocompleteContext.js +0 -11
  33. package/lib/Autocomplete/AutocompleteInput.d.ts +0 -9
  34. package/lib/Autocomplete/AutocompleteInput.js +0 -148
  35. package/lib/Autocomplete/AutocompleteMenu.d.ts +0 -70
  36. package/lib/Autocomplete/AutocompleteMenu.js +0 -252
  37. package/lib/Autocomplete/index.d.ts +0 -2
  38. package/lib/Autocomplete/index.js +0 -15
  39. package/lib/Badge/Badge.d.ts +0 -8
  40. package/lib/Badge/Badge.js +0 -59
  41. package/lib/Badge/BadgeState.d.ts +0 -13
  42. package/lib/Badge/BadgeState.js +0 -51
  43. package/lib/Badge/_badgeStyleUtils.d.ts +0 -3
  44. package/lib/Badge/_badgeStyleUtils.js +0 -39
  45. package/lib/TextInputTokens.d.ts +0 -43
  46. package/lib/TextInputTokens.js +0 -237
  47. package/lib/Token/Token.d.ts +0 -14
  48. package/lib/Token/Token.js +0 -75
  49. package/lib/Token/TokenBase.d.ts +0 -16
  50. package/lib/Token/TokenBase.js +0 -90
  51. package/lib/Token/TokenLabel.d.ts +0 -14
  52. package/lib/Token/TokenLabel.js +0 -135
  53. package/lib/Token/TokenProfile.d.ts +0 -7
  54. package/lib/Token/TokenProfile.js +0 -50
  55. package/lib/Token/_RemoveTokenButton.d.ts +0 -3
  56. package/lib/Token/_RemoveTokenButton.js +0 -45
  57. package/lib/Token/_tokenButtonUtils.d.ts +0 -10
  58. package/lib/Token/_tokenButtonUtils.js +0 -42
  59. package/lib/_UnstyledTextInput.d.ts +0 -2
  60. package/lib/_UnstyledTextInput.js +0 -20
  61. package/lib/utils/scrollIntoViewingArea.d.ts +0 -1
  62. package/lib/utils/scrollIntoViewingArea.js +0 -39
  63. package/lib-esm/Autocomplete/Autocomplete.d.ts +0 -31
  64. package/lib-esm/Autocomplete/Autocomplete.js +0 -40
  65. package/lib-esm/Autocomplete/AutocompleteContext.d.ts +0 -14
  66. package/lib-esm/Autocomplete/AutocompleteContext.js +0 -2
  67. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +0 -9
  68. package/lib-esm/Autocomplete/AutocompleteInput.js +0 -129
  69. package/lib-esm/Autocomplete/AutocompleteMenu.d.ts +0 -70
  70. package/lib-esm/Autocomplete/AutocompleteMenu.js +0 -226
  71. package/lib-esm/Autocomplete/index.d.ts +0 -2
  72. package/lib-esm/Autocomplete/index.js +0 -1
  73. package/lib-esm/Badge/Badge.d.ts +0 -8
  74. package/lib-esm/Badge/Badge.js +0 -44
  75. package/lib-esm/Badge/BadgeState.d.ts +0 -13
  76. package/lib-esm/Badge/BadgeState.js +0 -40
  77. package/lib-esm/Badge/_badgeStyleUtils.d.ts +0 -3
  78. package/lib-esm/Badge/_badgeStyleUtils.js +0 -29
  79. package/lib-esm/TextInputTokens.d.ts +0 -43
  80. package/lib-esm/TextInputTokens.js +0 -210
  81. package/lib-esm/Token/Token.d.ts +0 -14
  82. package/lib-esm/Token/Token.js +0 -56
  83. package/lib-esm/Token/TokenBase.d.ts +0 -16
  84. package/lib-esm/Token/TokenBase.js +0 -70
  85. package/lib-esm/Token/TokenLabel.d.ts +0 -14
  86. package/lib-esm/Token/TokenLabel.js +0 -115
  87. package/lib-esm/Token/TokenProfile.d.ts +0 -7
  88. package/lib-esm/Token/TokenProfile.js +0 -29
  89. package/lib-esm/Token/_RemoveTokenButton.d.ts +0 -3
  90. package/lib-esm/Token/_RemoveTokenButton.js +0 -30
  91. package/lib-esm/Token/_tokenButtonUtils.d.ts +0 -10
  92. package/lib-esm/Token/_tokenButtonUtils.js +0 -26
  93. package/lib-esm/_UnstyledTextInput.d.ts +0 -2
  94. package/lib-esm/_UnstyledTextInput.js +0 -7
  95. package/lib-esm/utils/scrollIntoViewingArea.d.ts +0 -1
  96. package/lib-esm/utils/scrollIntoViewingArea.js +0 -30
@@ -1,226 +0,0 @@
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
- import { PlusIcon } from '@primer/octicons-react';
11
- import { uniqueId } from '../utils/uniqueId';
12
- import { scrollIntoViewingArea } from '../utils/scrollIntoViewingArea';
13
- const DROPDOWN_PORTAL_CONTAINER_NAME = '__listcontainerportal__';
14
-
15
- const getDefaultSortFn = isItemSelectedFn => (itemIdA, itemIdB) => isItemSelectedFn(itemIdA) === isItemSelectedFn(itemIdB) ? 0 : isItemSelectedFn(itemIdA) ? -1 : 1;
16
-
17
- function getDefaultItemFilter(filterValue) {
18
- return function (item, _i) {
19
- var _item$text;
20
-
21
- return Boolean((_item$text = item.text) === null || _item$text === void 0 ? void 0 : _item$text.toLowerCase().startsWith(filterValue.toLowerCase()));
22
- };
23
- }
24
-
25
- // TODO: consider making 'aria-labelledby' required
26
- function getDefaultOnItemSelectFn(setInputValueFn) {
27
- if (setInputValueFn) {
28
- return function ({
29
- text = ''
30
- }) {
31
- setInputValueFn(text);
32
- };
33
- }
34
-
35
- return ({
36
- text
37
- }) => {
38
- console.error(`getDefaultOnItemSelectFn could not be called with ${text} because a function to set the text input was undefined`);
39
- };
40
- }
41
-
42
- function AutocompleteMenu(props) {
43
- const {
44
- activeDescendantRef,
45
- id,
46
- inputRef,
47
- inputValue = '',
48
- setAutocompleteSuggestion,
49
- setShowMenu,
50
- setInputValue,
51
- setIsMenuDirectlyActivated,
52
- showMenu
53
- } = useContext(AutocompleteContext);
54
- const {
55
- items,
56
- selectedItemIds,
57
- sortOnCloseFn,
58
- onItemSelect = getDefaultOnItemSelectFn(setInputValue),
59
- onItemDeselect,
60
- emptyStateText,
61
- addNewItem,
62
- loading,
63
- selectionVariant,
64
- filterFn = getDefaultItemFilter(inputValue),
65
- width,
66
- height,
67
- maxHeight,
68
- menuAnchorRef,
69
- "aria-labelledby": ariaLabelledBy
70
- } = props;
71
- const listContainerRef = useRef(null);
72
- const scrollContainerRef = useRef(null);
73
- const [highlightedItem, setHighlightedItem] = useState();
74
- const [sortedItemIds, setSortedItemIds] = useState(items.map(({
75
- id
76
- }) => id));
77
- const {
78
- floatingElementRef,
79
- position
80
- } = useAnchoredPosition({
81
- side: 'outside-bottom',
82
- align: 'start',
83
- anchorElementRef: menuAnchorRef ? menuAnchorRef : inputRef
84
- }, [showMenu, selectedItemIds]);
85
- const combinedOverlayRef = useCombinedRefs(scrollContainerRef, floatingElementRef);
86
-
87
- const closeOptionList = () => {
88
- setShowMenu && setShowMenu(false);
89
- };
90
-
91
- const isItemSelected = itemId => selectedItemIds.includes(itemId);
92
-
93
- const selectableItems = items.map(selectableItem => ({ ...selectableItem,
94
- role: "option",
95
- id: selectableItem.id,
96
- selected: selectionVariant === 'multiple' ? isItemSelected(selectableItem.id) : undefined,
97
- onAction: (item, e) => {
98
- const handleItemSelection = () => {
99
- onItemSelect(item, e);
100
-
101
- if (selectionVariant === 'multiple') {
102
- setInputValue && setInputValue('');
103
- setAutocompleteSuggestion && setAutocompleteSuggestion('');
104
- }
105
- };
106
-
107
- if (item.selected) {
108
- onItemDeselect && onItemDeselect(item, e);
109
- } else {
110
- handleItemSelection();
111
- }
112
-
113
- if (selectionVariant === 'single') {
114
- var _inputRef$current;
115
-
116
- setShowMenu && setShowMenu(false);
117
- 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);
118
- }
119
- }
120
- }));
121
- const itemSortOrderData = sortedItemIds.reduce((acc, curr, i) => {
122
- acc[curr] = i;
123
- return acc;
124
- }, {});
125
- const sortedAndFilteredItemsToRender = selectableItems.filter((item, i) => filterFn(item, i)).sort((a, b) => itemSortOrderData[a.id] - itemSortOrderData[b.id]);
126
- const allItemsToRender = [// sorted and filtered selectable items
127
- ...sortedAndFilteredItemsToRender, // menu item used for creating a token from whatever is in the text input
128
- ...(addNewItem ? [{ ...addNewItem,
129
- leadingVisual: () => /*#__PURE__*/React.createElement(PlusIcon, null),
130
- onAction: (item, e) => {
131
- // TODO: make it possible to pass a leadingVisual when using `addNewItem`
132
- addNewItem.handleAddItem({ ...item,
133
- id: item.id || uniqueId(),
134
- leadingVisual: undefined
135
- });
136
-
137
- if (selectionVariant === 'multiple') {
138
- setInputValue && setInputValue('');
139
- setAutocompleteSuggestion && setAutocompleteSuggestion('');
140
- }
141
- }
142
- }] : [])];
143
- useFocusZone({
144
- containerRef: listContainerRef,
145
- focusOutBehavior: 'wrap',
146
- focusableElementFilter: element => {
147
- return !(element instanceof HTMLInputElement);
148
- },
149
- activeDescendantFocus: inputRef,
150
- onActiveDescendantChanged: (current, _previous, directlyActivated) => {
151
- console.log('onActiveDescendantChanged', current === null || current === void 0 ? void 0 : current.textContent);
152
-
153
- if (activeDescendantRef) {
154
- activeDescendantRef.current = current || null;
155
- }
156
-
157
- if (current) {
158
- const selectedItem = selectableItems.find(item => item.id.toString() === (current === null || current === void 0 ? void 0 : current.dataset.id));
159
- setHighlightedItem(selectedItem);
160
- setIsMenuDirectlyActivated && setIsMenuDirectlyActivated(directlyActivated);
161
- }
162
-
163
- if (current && scrollContainerRef.current && directlyActivated) {
164
- scrollIntoViewingArea(current, scrollContainerRef.current);
165
- }
166
- }
167
- });
168
- useEffect(() => {
169
- var _highlightedItem$text;
170
-
171
- if (!setAutocompleteSuggestion) {
172
- return;
173
- }
174
-
175
- if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(inputValue) && !selectedItemIds.includes(highlightedItem.id)) {
176
- setAutocompleteSuggestion(highlightedItem.text);
177
- } else {
178
- setAutocompleteSuggestion('');
179
- }
180
- }, [highlightedItem, inputValue]);
181
- useEffect(() => {
182
- setSortedItemIds([...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(isItemSelected)));
183
- }, [showMenu]);
184
-
185
- if (listContainerRef.current) {
186
- registerPortalRoot(listContainerRef.current, DROPDOWN_PORTAL_CONTAINER_NAME);
187
- }
188
-
189
- return /*#__PURE__*/React.createElement(Overlay, {
190
- returnFocusRef: inputRef,
191
- preventFocusOnOpen: true,
192
- onClickOutside: closeOptionList,
193
- onEscape: closeOptionList,
194
- ref: combinedOverlayRef,
195
- top: position === null || position === void 0 ? void 0 : position.top,
196
- left: position === null || position === void 0 ? void 0 : position.left,
197
- width: width,
198
- height: height,
199
- maxHeight: maxHeight,
200
- visibility: showMenu ? 'visible' : 'hidden'
201
- }, loading ? /*#__PURE__*/React.createElement(Box, {
202
- p: 3,
203
- display: "flex",
204
- justifyContent: "center"
205
- }, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement("div", {
206
- ref: listContainerRef
207
- }, allItemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
208
- selectionVariant: "multiple" // have to typecast to `ItemProps` because we have an extra property
209
- // on `items` for Autocomplete: `metadata`
210
- ,
211
- items: allItemsToRender,
212
- role: "listbox",
213
- id: `${id}-listbox`,
214
- "aria-labelledby": ariaLabelledBy
215
- }) : /*#__PURE__*/React.createElement(Box, {
216
- p: 3
217
- }, emptyStateText)));
218
- }
219
-
220
- AutocompleteMenu.displayName = "AutocompleteMenu";
221
- AutocompleteMenu.defaultProps = {
222
- emptyStateText: 'No selectable options',
223
- selectionVariant: 'single'
224
- };
225
- AutocompleteMenu.displayName = 'AutocompleteMenu';
226
- export default AutocompleteMenu;
@@ -1,2 +0,0 @@
1
- export { default } from './Autocomplete';
2
- export type { AutocompleteMenuProps, AutocompleteInputProps, } from './Autocomplete';
@@ -1 +0,0 @@
1
- export { default } from './Autocomplete';
@@ -1,8 +0,0 @@
1
- import { BadgeSizeKeys } from './_badgeStyleUtils';
2
- declare type BadgeColorOptions = 'default' | 'primary' | 'secondary' | 'info' | 'success' | 'warning' | 'danger';
3
- interface Props {
4
- color?: BadgeColorOptions;
5
- variant?: BadgeSizeKeys;
6
- }
7
- declare const Badge: import("styled-components").StyledComponent<"span", any, Props, never>;
8
- export default Badge;
@@ -1,44 +0,0 @@
1
- import styled from 'styled-components';
2
- import { get } from '../constants';
3
- import { badgeVariants } from './_badgeStyleUtils';
4
- const badgeColorMap = {
5
- default: {
6
- borderColor: get('colors.border.default')
7
- },
8
- primary: {
9
- borderColor: get('colors.fg.default')
10
- },
11
- secondary: {
12
- borderColor: get('colors.border.muted'),
13
- textColor: get('colors.fg.muted')
14
- },
15
- info: {
16
- borderColor: get('colors.accent.fg'),
17
- textColor: get('colors.accent.fg')
18
- },
19
- success: {
20
- borderColor: get('colors.success.fg'),
21
- textColor: get('colors.success.emphasis')
22
- },
23
- warning: {
24
- borderColor: get('colors.attention.fg'),
25
- textColor: get('colors.attention.emphasis')
26
- },
27
- danger: {
28
- borderColor: get('colors.danger.fg'),
29
- textColor: get('colors.danger.emphasis')
30
- }
31
- };
32
- const Badge = styled.span.withConfig({
33
- displayName: "Badge",
34
- componentId: "sc-15t9m5z-0"
35
- })(["align-items:center;background-color:transparent;border-width:1px;border-radius:999px;border-style:solid;border-color:", ";color:", ";display:inline-flex;font-weight:", ";line-height:1;white-space:nowrap;", ";"], ({
36
- color = 'default'
37
- }) => badgeColorMap[color].borderColor, ({
38
- color = 'default'
39
- }) => badgeColorMap[color].textColor, get('fontWeights.bold'), badgeVariants);
40
- Badge.defaultProps = {
41
- variant: 'sm',
42
- color: 'default'
43
- };
44
- export default Badge;
@@ -1,13 +0,0 @@
1
- /// <reference types="react" />
2
- import { IconProps } from '@primer/octicons-react';
3
- import { BadgeSizeKeys } from './_badgeStyleUtils';
4
- declare type BadgeStateColorOptions = 'default' | 'open' | 'merged' | 'closed';
5
- interface Props {
6
- color?: BadgeStateColorOptions;
7
- icon?: React.ComponentType<{
8
- size?: IconProps['size'];
9
- }>;
10
- variant?: BadgeSizeKeys;
11
- }
12
- declare const BadgeState: React.FC<Props>;
13
- export default BadgeState;
@@ -1,40 +0,0 @@
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 styled from 'styled-components';
4
- import { get } from '../constants';
5
- import { badgeVariants } from './_badgeStyleUtils'; // TODO: consider making these a little less specific by using:
6
- // 'default' | 'success' | 'done' | 'danger'
7
-
8
- const badgeStateColorMap = {
9
- default: get('colors.neutral.emphasis'),
10
- open: get('colors.success.emphasis'),
11
- merged: get('colors.done.emphasis'),
12
- closed: get('colors.danger.emphasis')
13
- }; // const badgeStateIconSizeMap: Record<BadgeSizeKeys, number> = {
14
- // 'sm':
15
- // };
16
-
17
- const StyledBadgeState = styled.span.withConfig({
18
- displayName: "BadgeState__StyledBadgeState",
19
- componentId: "sc-5hwzjt-0"
20
- })(["align-items:center;background-color:", ";border-radius:999px;color:", ";display:inline-flex;font-weight:", ";gap:", ";line-height:1;white-space:nowrap;", ";"], ({
21
- color = 'default'
22
- }) => badgeStateColorMap[color], get('colors.fg.onEmphasis'), get('fontWeights.bold'), get('space.1'), badgeVariants);
23
-
24
- const BadgeState = ({
25
- children,
26
- icon: IconComponent,
27
- variant,
28
- ...rest
29
- }) => /*#__PURE__*/React.createElement(StyledBadgeState, _extends({
30
- variant: variant
31
- }, rest), IconComponent ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconComponent, {
32
- size: variant === 'sm' ? 12 : 'small'
33
- }), children) : children);
34
-
35
- BadgeState.displayName = "BadgeState";
36
- BadgeState.defaultProps = {
37
- variant: 'lg',
38
- color: 'default'
39
- };
40
- export default BadgeState;
@@ -1,3 +0,0 @@
1
- export declare type BadgeSizeKeys = 'sm' | 'md' | 'lg';
2
- export declare const badgeSizes: Record<BadgeSizeKeys, number>;
3
- export declare const badgeVariants: (...args: any[]) => any;
@@ -1,29 +0,0 @@
1
- import { variant } from 'styled-system';
2
- // TODO: consider moving to Primitives
3
- export const badgeSizes = {
4
- sm: 20,
5
- md: 24,
6
- lg: 32
7
- };
8
- export const badgeVariants = variant({
9
- variants: {
10
- sm: {
11
- fontSize: 0,
12
- height: `${badgeSizes.sm}px`,
13
- paddingLeft: 2,
14
- paddingRight: 2
15
- },
16
- md: {
17
- fontSize: 0,
18
- height: `${badgeSizes.md}px`,
19
- paddingLeft: 2,
20
- paddingRight: 2
21
- },
22
- lg: {
23
- fontSize: 1,
24
- height: `${badgeSizes.lg}px`,
25
- paddingLeft: 3,
26
- paddingRight: 3
27
- }
28
- }
29
- });
@@ -1,43 +0,0 @@
1
- import React, { ComponentPropsWithoutRef } from 'react';
2
- import { ComponentProps, MandateProps } from './utils/types';
3
- import Token, { TokenProps } from './Token/Token';
4
- import TokenLabel, { TokenLabelProps } from './Token/TokenLabel';
5
- import TokenProfile, { TokenProfileProps } from './Token/TokenProfile';
6
- import { TokenSizeKeys } from './Token/TokenBase';
7
- import { TextInputProps } from './TextInput';
8
- declare type AnyTokenProps = Partial<TokenProps & TokenLabelProps & TokenProfileProps>;
9
- declare type TokenDatum = MandateProps<AnyTokenProps, 'id' | 'text'>;
10
- declare type TextInputWithTokensInternalProps = {
11
- /**
12
- * The array of tokens to render
13
- */
14
- tokens: TokenDatum[];
15
- /**
16
- * The function that gets called when a token is removed
17
- */
18
- onTokenRemove: (tokenId: string | number) => void;
19
- /**
20
- * The component used to render each token
21
- */
22
- tokenComponent?: React.ComponentType<ComponentPropsWithoutRef<typeof Token> | ComponentPropsWithoutRef<typeof TokenLabel> | ComponentPropsWithoutRef<typeof TokenProfile>>;
23
- /**
24
- * The maximum height of the component. If the content in the input exceeds this height,
25
- * it will scroll vertically
26
- */
27
- maxHeight?: React.CSSProperties['maxHeight'];
28
- /**
29
- * Whether tokens should render inline horizontally. By default, tokens wrap to new lines.
30
- */
31
- preventTokenWrapping?: boolean;
32
- /**
33
- * The size of the tokens
34
- */
35
- tokenSizeVariant?: TokenSizeKeys;
36
- /**
37
- * Whether the remove buttons should be rendered in the tokens
38
- */
39
- hideTokenRemoveButtons?: boolean;
40
- } & TextInputProps;
41
- declare const TextInputWithTokens: React.ForwardRefExoticComponent<Pick<TextInputWithTokensInternalProps, string | number | symbol> & React.RefAttributes<HTMLInputElement>>;
42
- export declare type TextInputWithTokensProps = ComponentProps<typeof TextInputWithTokens>;
43
- export default TextInputWithTokens;
@@ -1,210 +0,0 @@
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 } from '@styled-system/props';
5
- import styled from 'styled-components';
6
- import { FocusKeys } from './behaviors/focusZone';
7
- import { useCombinedRefs } from './hooks/useCombinedRefs';
8
- import { useFocusZone } from './hooks/useFocusZone';
9
- import Token from './Token/Token';
10
- import TextInput from './TextInput';
11
- import { useProvidedRefOrCreate } from './hooks';
12
- import UnstyledTextInput from './_UnstyledTextInput';
13
- const InputWrapper = styled.div.withConfig({
14
- displayName: "TextInputTokens__InputWrapper",
15
- componentId: "sc-1k54xiz-0"
16
- })(["order:1;flex-grow:1;"]);
17
- // using forwardRef is important so that other components (ex. Autocomplete) can use the ref
18
- const TextInputWithTokensComponent = /*#__PURE__*/React.forwardRef(({
19
- icon: IconComponent,
20
- contrast,
21
- className,
22
- block,
23
- disabled,
24
- theme,
25
- sx: sxProp,
26
- tokens,
27
- onTokenRemove,
28
- tokenComponent: TokenComponent,
29
- preventTokenWrapping,
30
- tokenSizeVariant,
31
- hideTokenRemoveButtons,
32
- selectedTokenIdx,
33
- setSelectedTokenIdx,
34
- ...rest
35
- }, externalRef) => {
36
- const ref = useProvidedRefOrCreate(externalRef);
37
- const {
38
- onFocus,
39
- onKeyDown,
40
- ...inputPropsRest
41
- } = omit(rest);
42
-
43
- const handleTokenFocus = tokenIdx => () => {
44
- setSelectedTokenIdx(tokenIdx);
45
- };
46
-
47
- const handleTokenBlur = () => {
48
- setSelectedTokenIdx(undefined);
49
- };
50
-
51
- const handleTokenKeyUp = e => {
52
- if (e.key === 'Escape') {
53
- var _ref$current;
54
-
55
- ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.focus();
56
- }
57
- };
58
-
59
- const handleInputFocus = e => {
60
- onFocus && onFocus(e);
61
- setSelectedTokenIdx(undefined);
62
- };
63
-
64
- const handleInputKeyDown = e => {
65
- var _ref$current2;
66
-
67
- if (onKeyDown) {
68
- onKeyDown(e);
69
- }
70
-
71
- if (ref !== null && ref !== void 0 && (_ref$current2 = ref.current) !== null && _ref$current2 !== void 0 && _ref$current2.value) {
72
- return;
73
- }
74
-
75
- const lastToken = tokens[tokens.length - 1];
76
-
77
- if (e.key === 'Backspace' && lastToken) {
78
- onTokenRemove(lastToken.id);
79
-
80
- if (ref !== null && ref !== void 0 && ref.current) {
81
- // TODO: eliminate the first hack by making changes to the Autocomplete component
82
- // COLEHELP
83
- //
84
- // HACKS:
85
- // 1. Directly setting `ref.current.value` instead of updating state because the autocomplete
86
- // highlight behavior doesn't work correctly if we update the value with a setState action in onChange
87
- // 2. Adding an extra space so that when I backspace, it doesn't delete the last letter
88
- ref.current.value = `${lastToken.text} `;
89
- } // HACK: for some reason we need to wait a tick for `.select()` to work
90
-
91
-
92
- setTimeout(() => {
93
- var _ref$current3;
94
-
95
- ref === null || ref === void 0 ? void 0 : (_ref$current3 = ref.current) === null || _ref$current3 === void 0 ? void 0 : _ref$current3.select();
96
- }, 1);
97
- }
98
- };
99
-
100
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(InputWrapper, {
101
- key: "inputWrapper"
102
- }, /*#__PURE__*/React.createElement(UnstyledTextInput, _extends({
103
- ref: ref,
104
- disabled: disabled,
105
- onFocus: handleInputFocus,
106
- onKeyDown: handleInputKeyDown,
107
- type: "text",
108
- sx: {
109
- height: '100%'
110
- }
111
- }, inputPropsRest))), tokens !== null && tokens !== void 0 && tokens.length && TokenComponent ? tokens.map(({
112
- id,
113
- ...tokenRest
114
- }, i) => /*#__PURE__*/React.createElement(TokenComponent, _extends({
115
- key: id,
116
- onFocus: handleTokenFocus(i),
117
- onBlur: handleTokenBlur,
118
- onKeyUp: handleTokenKeyUp,
119
- isSelected: selectedTokenIdx === i,
120
- handleRemove: () => {
121
- onTokenRemove(id);
122
- },
123
- hideRemoveButton: hideTokenRemoveButtons,
124
- variant: tokenSizeVariant,
125
- tabIndex: 0
126
- }, tokenRest))) : null);
127
- });
128
- const TextInputWithTokens = /*#__PURE__*/React.forwardRef(({
129
- tokens,
130
- onTokenRemove,
131
- sx: sxProp,
132
- ...props
133
- }, ref) => {
134
- const localInputRef = useRef(null);
135
- const combinedInputRef = useCombinedRefs(localInputRef, ref);
136
- const [selectedTokenIdx, setSelectedTokenIdx] = useState();
137
- const {
138
- containerRef
139
- } = useFocusZone({
140
- focusOutBehavior: 'wrap',
141
- bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
142
- focusableElementFilter: element => {
143
- return !element.getAttributeNames().includes('aria-hidden');
144
- },
145
- getNextFocusable: direction => {
146
- var _containerRef$current;
147
-
148
- if (!selectedTokenIdx && selectedTokenIdx !== 0) {
149
- return undefined;
150
- }
151
-
152
- let nextIndex = selectedTokenIdx + 1; // "+ 1" accounts for the first element: the text input
153
-
154
- if (direction === 'next') {
155
- nextIndex += 1;
156
- }
157
-
158
- if (direction === 'previous') {
159
- nextIndex -= 1;
160
- }
161
-
162
- if (nextIndex > tokens.length || nextIndex < 1) {
163
- return combinedInputRef.current || undefined;
164
- }
165
-
166
- return containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.children[nextIndex];
167
- }
168
- }, [selectedTokenIdx]);
169
-
170
- const handleTokenRemove = tokenId => {
171
- onTokenRemove(tokenId);
172
-
173
- if (selectedTokenIdx) {
174
- var _containerRef$current2;
175
-
176
- const nextElementToFocus = containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current2 = containerRef.current) === null || _containerRef$current2 === void 0 ? void 0 : _containerRef$current2.children[selectedTokenIdx];
177
- nextElementToFocus.focus();
178
- }
179
- };
180
-
181
- return /*#__PURE__*/React.createElement(TextInput, _extends({
182
- ref: combinedInputRef,
183
- wrapperRef: containerRef,
184
- as: TextInputWithTokensComponent,
185
- selectedTokenIdx: selectedTokenIdx,
186
- setSelectedTokenIdx: setSelectedTokenIdx,
187
- tokens: tokens,
188
- onTokenRemove: handleTokenRemove,
189
- sx: {
190
- 'alignItems': props.preventTokenWrapping ? 'center' : undefined,
191
- 'flexWrap': props.preventTokenWrapping ? 'nowrap' : 'wrap',
192
- 'gap': '0.25rem',
193
- '> *': {
194
- 'flexShrink': 0
195
- },
196
- ...(props.block ? {
197
- display: 'flex',
198
- width: '100%'
199
- } : {}),
200
- ...sxProp
201
- }
202
- }, props));
203
- });
204
- TextInputWithTokens.defaultProps = {
205
- tokenComponent: Token,
206
- tokenSizeVariant: "xl",
207
- hideTokenRemoveButtons: false
208
- };
209
- TextInputWithTokens.displayName = 'TextInputWithTokens';
210
- export default TextInputWithTokens;
@@ -1,14 +0,0 @@
1
- import React from 'react';
2
- import { TokenBaseProps } from './TokenBase';
3
- export interface TokenProps extends TokenBaseProps {
4
- /**
5
- * A function that renders a component before the token text
6
- */
7
- leadingVisual?: React.FunctionComponent<any>;
8
- /**
9
- * Whether the remove button should be rendered in the token
10
- */
11
- hideRemoveButton?: boolean;
12
- }
13
- declare const Token: React.ForwardRefExoticComponent<Pick<TokenProps, "sizes" | "color" | "content" | "height" | "translate" | "width" | "hidden" | "children" | "value" | "cite" | "data" | "form" | "label" | "slot" | "span" | "style" | "summary" | "title" | "pattern" | "text" | "list" | "default" | "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" | "start" | "step" | "wrap" | "open" | "max" | "media" | "method" | "min" | "target" | "crossOrigin" | "href" | "classID" | "useMap" | "wmode" | "download" | "hrefLang" | "rel" | "alt" | "coords" | "shape" | "autoPlay" | "controls" | "loop" | "mediaGroup" | "muted" | "playsInline" | "preload" | "src" | "autoFocus" | "disabled" | "formAction" | "formEncType" | "formMethod" | "formNoValidate" | "formTarget" | "dateTime" | "acceptCharset" | "action" | "autoComplete" | "encType" | "noValidate" | "manifest" | "allowFullScreen" | "allowTransparency" | "frameBorder" | "marginHeight" | "marginWidth" | "sandbox" | "scrolling" | "seamless" | "srcDoc" | "srcSet" | "async" | "accept" | "capture" | "checked" | "maxLength" | "minLength" | "multiple" | "readOnly" | "required" | "challenge" | "keyType" | "keyParams" | "htmlFor" | "as" | "integrity" | "charSet" | "httpEquiv" | "high" | "low" | "optimum" | "reversed" | "selected" | "defer" | "nonce" | "scoped" | "cellPadding" | "cellSpacing" | "colSpan" | "headers" | "rowSpan" | "scope" | "cols" | "rows" | "kind" | "srcLang" | "poster" | "variant" | "leadingVisual" | "handleRemove" | "isSelected" | "hideRemoveButton"> & React.RefAttributes<HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement>>;
14
- export default Token;