@teselagen/ui 0.7.33-beta.3 → 0.7.33-beta.4

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 (137) hide show
  1. package/package.json +1 -1
  2. package/src/AdvancedOptions.spec.js +26 -0
  3. package/src/AsyncValidateFieldSpinner/index.js +12 -0
  4. package/src/BlueprintError/index.js +14 -0
  5. package/src/BounceLoader/index.js +16 -0
  6. package/src/BounceLoader/style.css +45 -0
  7. package/src/CollapsibleCard/index.js +68 -0
  8. package/src/CollapsibleCard/style.css +23 -0
  9. package/src/DNALoader/index.js +20 -0
  10. package/src/DNALoader/style.css +251 -0
  11. package/src/DataTable/index.js +3209 -0
  12. package/src/DataTable/style.css +608 -0
  13. package/src/DataTable/utils/filterLocalEntitiesToHasura.test.js +515 -0
  14. package/src/DataTable/utils/index.js +55 -0
  15. package/src/DataTable/utils/simplifyHasuraWhere.test.js +73 -0
  16. package/src/DataTable/utils/tableQueryParamsToHasuraClauses.test.js +219 -0
  17. package/src/DialogFooter/index.js +86 -0
  18. package/src/DialogFooter/style.css +9 -0
  19. package/src/FormComponents/index.js +1266 -0
  20. package/src/FormComponents/style.css +275 -0
  21. package/src/FormComponents/utils.js +6 -0
  22. package/src/HotkeysDialog/index.js +79 -0
  23. package/src/HotkeysDialog/style.css +54 -0
  24. package/src/InfoHelper/index.js +78 -0
  25. package/src/InfoHelper/style.css +7 -0
  26. package/src/IntentText/index.js +18 -0
  27. package/src/Loading/index.js +70 -0
  28. package/src/Loading/style.css +4 -0
  29. package/src/MenuBar/index.js +423 -0
  30. package/src/MenuBar/style.css +45 -0
  31. package/src/PromptUnsavedChanges/index.js +38 -0
  32. package/src/ResizableDraggableDialog/index.js +141 -0
  33. package/src/ResizableDraggableDialog/style.css +42 -0
  34. package/src/ScrollToTop/index.js +72 -0
  35. package/src/TagSelect/index.js +69 -0
  36. package/src/TagSelect/style.css +13 -0
  37. package/src/TgHtmlSelect/index.js +20 -0
  38. package/src/TgSelect/index.js +537 -0
  39. package/src/TgSelect/style.css +61 -0
  40. package/src/TgSuggest/index.js +124 -0
  41. package/src/Timeline/index.js +15 -0
  42. package/src/Timeline/style.css +29 -0
  43. package/src/enhancers/withDialog/index.js +196 -0
  44. package/src/index.js +88 -1
  45. package/src/showConfirmationDialog/index.js +148 -0
  46. package/src/style.css +261 -9
  47. package/src/utils/hooks/index.js +1 -0
  48. /package/src/{CellDragHandle.js → DataTable/CellDragHandle.js} +0 -0
  49. /package/src/{ColumnFilterMenu.js → DataTable/ColumnFilterMenu.js} +0 -0
  50. /package/src/{Columns.js → DataTable/Columns.js} +0 -0
  51. /package/src/{DisabledLoadingComponent.js → DataTable/DisabledLoadingComponent.js} +0 -0
  52. /package/src/{DisplayOptions.js → DataTable/DisplayOptions.js} +0 -0
  53. /package/src/{DropdownCell.js → DataTable/DropdownCell.js} +0 -0
  54. /package/src/{EditableCell.js → DataTable/EditableCell.js} +0 -0
  55. /package/src/{FilterAndSortMenu.js → DataTable/FilterAndSortMenu.js} +0 -0
  56. /package/src/{PagingTool.js → DataTable/PagingTool.js} +0 -0
  57. /package/src/{RenderCell.js → DataTable/RenderCell.js} +0 -0
  58. /package/src/{SearchBar.js → DataTable/SearchBar.js} +0 -0
  59. /package/src/{SortableColumns.js → DataTable/SortableColumns.js} +0 -0
  60. /package/src/{TableFormTrackerContext.js → DataTable/TableFormTrackerContext.js} +0 -0
  61. /package/src/{ThComponent.js → DataTable/ThComponent.js} +0 -0
  62. /package/src/{dataTableEnhancer.js → DataTable/dataTableEnhancer.js} +0 -0
  63. /package/src/{defaultFormatters.js → DataTable/defaultFormatters.js} +0 -0
  64. /package/src/{defaultValidators.js → DataTable/defaultValidators.js} +0 -0
  65. /package/src/{editCellHelper.js → DataTable/editCellHelper.js} +0 -0
  66. /package/src/{getCellVal.js → DataTable/getCellVal.js} +0 -0
  67. /package/src/{getVals.js → DataTable/getVals.js} +0 -0
  68. /package/src/{isTruthy.js → DataTable/isTruthy.js} +0 -0
  69. /package/src/{isValueEmpty.js → DataTable/isValueEmpty.js} +0 -0
  70. /package/src/{convertSchema.js → DataTable/utils/convertSchema.js} +0 -0
  71. /package/src/{filterLocalEntitiesToHasura.js → DataTable/utils/filterLocalEntitiesToHasura.js} +0 -0
  72. /package/src/{formatPasteData.js → DataTable/utils/formatPasteData.js} +0 -0
  73. /package/src/{getAllRows.js → DataTable/utils/getAllRows.js} +0 -0
  74. /package/src/{getCellCopyText.js → DataTable/utils/getCellCopyText.js} +0 -0
  75. /package/src/{getCellInfo.js → DataTable/utils/getCellInfo.js} +0 -0
  76. /package/src/{getFieldPathToField.js → DataTable/utils/getFieldPathToField.js} +0 -0
  77. /package/src/{getIdOrCodeOrIndex.js → DataTable/utils/getIdOrCodeOrIndex.js} +0 -0
  78. /package/src/{getLastSelectedEntity.js → DataTable/utils/getLastSelectedEntity.js} +0 -0
  79. /package/src/{getNewEntToSelect.js → DataTable/utils/getNewEntToSelect.js} +0 -0
  80. /package/src/{getRowCopyText.js → DataTable/utils/getRowCopyText.js} +0 -0
  81. /package/src/{getTableConfigFromStorage.js → DataTable/utils/getTableConfigFromStorage.js} +0 -0
  82. /package/src/{handleCopyColumn.js → DataTable/utils/handleCopyColumn.js} +0 -0
  83. /package/src/{handleCopyHelper.js → DataTable/utils/handleCopyHelper.js} +0 -0
  84. /package/src/{handleCopyRows.js → DataTable/utils/handleCopyRows.js} +0 -0
  85. /package/src/{handleCopyTable.js → DataTable/utils/handleCopyTable.js} +0 -0
  86. /package/src/{initializeHasuraWhereAndFilter.js → DataTable/utils/initializeHasuraWhereAndFilter.js} +0 -0
  87. /package/src/{isBottomRightCornerOfRectangle.js → DataTable/utils/isBottomRightCornerOfRectangle.js} +0 -0
  88. /package/src/{isEntityClean.js → DataTable/utils/isEntityClean.js} +0 -0
  89. /package/src/{primarySelectedValue.js → DataTable/utils/primarySelectedValue.js} +0 -0
  90. /package/src/{queryParams.js → DataTable/utils/queryParams.js} +0 -0
  91. /package/src/{removeCleanRows.js → DataTable/utils/removeCleanRows.js} +0 -0
  92. /package/src/{rowClick.js → DataTable/utils/rowClick.js} +0 -0
  93. /package/src/{selection.js → DataTable/utils/selection.js} +0 -0
  94. /package/src/{simplifyHasuraWhere.js → DataTable/utils/simplifyHasuraWhere.js} +0 -0
  95. /package/src/{tableQueryParamsToHasuraClauses.js → DataTable/utils/tableQueryParamsToHasuraClauses.js} +0 -0
  96. /package/src/{useTableEntities.js → DataTable/utils/useTableEntities.js} +0 -0
  97. /package/src/{utils.js → DataTable/utils/utils.js} +0 -0
  98. /package/src/{withSelectedEntities.js → DataTable/utils/withSelectedEntities.js} +0 -0
  99. /package/src/{withTableParams.js → DataTable/utils/withTableParams.js} +0 -0
  100. /package/src/{validateTableWideErrors.js → DataTable/validateTableWideErrors.js} +0 -0
  101. /package/src/{viewColumn.js → DataTable/viewColumn.js} +0 -0
  102. /package/src/{FormSeparator.js → FormComponents/FormSeparator.js} +0 -0
  103. /package/src/{LoadingDots.js → FormComponents/LoadingDots.js} +0 -0
  104. /package/src/{Uploader.js → FormComponents/Uploader.js} +0 -0
  105. /package/src/{getNewName.js → FormComponents/getNewName.js} +0 -0
  106. /package/src/{itemUpload.js → FormComponents/itemUpload.js} +0 -0
  107. /package/src/{sortify.js → FormComponents/sortify.js} +0 -0
  108. /package/src/{tryToMatchSchemas.js → FormComponents/tryToMatchSchemas.js} +0 -0
  109. /package/src/{TimelineEvent.js → Timeline/TimelineEvent.js} +0 -0
  110. /package/src/{tg_modalState.js → enhancers/withDialog/tg_modalState.js} +0 -0
  111. /package/src/{withField.js → enhancers/withField.js} +0 -0
  112. /package/src/{withFields.js → enhancers/withFields.js} +0 -0
  113. /package/src/{withLocalStorage.js → enhancers/withLocalStorage.js} +0 -0
  114. /package/src/{adHoc.js → utils/adHoc.js} +0 -0
  115. /package/src/{basicHandleActionsWithFullState.js → utils/basicHandleActionsWithFullState.js} +0 -0
  116. /package/src/{browserUtils.js → utils/browserUtils.js} +0 -0
  117. /package/src/{combineReducersWithFullState.js → utils/combineReducersWithFullState.js} +0 -0
  118. /package/src/{commandControls.js → utils/commandControls.js} +0 -0
  119. /package/src/{commandUtils.js → utils/commandUtils.js} +0 -0
  120. /package/src/{determineBlackOrWhiteTextColor.js → utils/determineBlackOrWhiteTextColor.js} +0 -0
  121. /package/src/{getDayjsFormatter.js → utils/getDayjsFormatter.js} +0 -0
  122. /package/src/{getTextFromEl.js → utils/getTextFromEl.js} +0 -0
  123. /package/src/{handlerHelpers.js → utils/handlerHelpers.js} +0 -0
  124. /package/src/{useDeepEqualMemo.js → utils/hooks/useDeepEqualMemo.js} +0 -0
  125. /package/src/{useStableReference.js → utils/hooks/useStableReference.js} +0 -0
  126. /package/src/{hotkeyUtils.js → utils/hotkeyUtils.js} +0 -0
  127. /package/src/{isBeingCalledExcessively.js → utils/isBeingCalledExcessively.js} +0 -0
  128. /package/src/{menuUtils.js → utils/menuUtils.js} +0 -0
  129. /package/src/{popoverOverflowModifiers.js → utils/popoverOverflowModifiers.js} +0 -0
  130. /package/src/{pureNoFunc.js → utils/pureNoFunc.js} +0 -0
  131. /package/src/{renderOnDoc.js → utils/renderOnDoc.js} +0 -0
  132. /package/src/{showProgressToast.js → utils/showProgressToast.js} +0 -0
  133. /package/src/{tagUtils.js → utils/tagUtils.js} +0 -0
  134. /package/src/{tgFormValues.js → utils/tgFormValues.js} +0 -0
  135. /package/src/{useTraceUpdate.js → utils/useTraceUpdate.js} +0 -0
  136. /package/src/{withSelectTableRecords.js → utils/withSelectTableRecords.js} +0 -0
  137. /package/src/{withStore.js → utils/withStore.js} +0 -0
@@ -0,0 +1,72 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { Button } from "@blueprintjs/core";
3
+ import { Transition } from "react-transition-group";
4
+
5
+ const duration = 300;
6
+
7
+ const defaultStyle = {
8
+ transition: `opacity ${duration}ms ease-in-out`,
9
+ opacity: 0
10
+ };
11
+
12
+ const transitionStyles = {
13
+ entering: { opacity: 1 },
14
+ entered: { opacity: 1 },
15
+ exiting: { opacity: 0 },
16
+ exited: { opacity: 0 }
17
+ };
18
+
19
+ function ScrollToTop({
20
+ showAt = 150,
21
+ scrollContainer = document.scrollingElement
22
+ }) {
23
+ const [shouldShow, setShouldShow] = useState(
24
+ scrollContainer.scrollTop >= showAt
25
+ );
26
+
27
+ useEffect(() => {
28
+ const scrollListener = () => {
29
+ const newShouldShow = scrollContainer.scrollTop >= showAt;
30
+ if (newShouldShow !== shouldShow) setShouldShow(newShouldShow);
31
+ };
32
+ const listenerContainer =
33
+ scrollContainer === document.scrollingElement ? window : scrollContainer;
34
+ listenerContainer.addEventListener("scroll", scrollListener);
35
+ // Specify how to clean up after this effect:
36
+ return function cleanup() {
37
+ listenerContainer.removeEventListener("scroll", scrollListener);
38
+ };
39
+ }, [shouldShow, showAt, scrollContainer]);
40
+
41
+ const scrollToTop = () => {
42
+ const c = scrollContainer.scrollTop;
43
+ if (c > 0) {
44
+ window.requestAnimationFrame(scrollToTop);
45
+ scrollContainer.scrollTo(0, c - c / 8);
46
+ }
47
+ };
48
+
49
+ return (
50
+ <Transition in={shouldShow} timeout={duration}>
51
+ {state =>
52
+ state === "exited" ? null : (
53
+ <div style={{ position: "fixed", bottom: 25, right: 25, zIndex: 10 }}>
54
+ <Button
55
+ style={{
56
+ borderRadius: "50%",
57
+ ...defaultStyle,
58
+ ...transitionStyles[state]
59
+ }}
60
+ intent="primary"
61
+ icon="arrow-up"
62
+ large
63
+ onClick={scrollToTop}
64
+ />
65
+ </div>
66
+ )
67
+ }
68
+ </Transition>
69
+ );
70
+ }
71
+
72
+ export default ScrollToTop;
@@ -0,0 +1,69 @@
1
+ import { Button, InputGroup, Popover } from "@blueprintjs/core";
2
+ import React from "react";
3
+
4
+ import Tag from "../Tag";
5
+ import "./style.css";
6
+ import { useState } from "react";
7
+ import { itemListPredicate } from "../TgSelect";
8
+
9
+ export default ({ value = {}, options = [], onChange }) => {
10
+ const [open, setOpen] = useState(false);
11
+ const [searchTerm, setSearchTerm] = useState("");
12
+
13
+ const filteredOptions = itemListPredicate(searchTerm, options, false);
14
+
15
+ return (
16
+ <Popover
17
+ isOpen={open}
18
+ onInteraction={setOpen}
19
+ content={
20
+ <div className="tag-select-popover">
21
+ <InputGroup
22
+ rightElement={
23
+ // clear button
24
+ searchTerm ? (
25
+ <Button
26
+ icon="cross"
27
+ minimal
28
+ onClick={() => setSearchTerm("")}
29
+ />
30
+ ) : null
31
+ }
32
+ type="text"
33
+ placeholder="Search..."
34
+ autoFocus
35
+ value={searchTerm}
36
+ onChange={e => setSearchTerm(e.target.value)}
37
+ />
38
+ <div className="tag-select-popover-inner">
39
+ {filteredOptions.map(option => {
40
+ return (
41
+ <Tag
42
+ key={option.name}
43
+ onClick={() => {
44
+ onChange(option);
45
+ setOpen(false);
46
+ }}
47
+ clickable
48
+ style={{ maxWidth: 150 }}
49
+ {...convertTagVals(option)}
50
+ doNotFillWidth
51
+ ></Tag>
52
+ );
53
+ })}
54
+ </div>
55
+ </div>
56
+ }
57
+ >
58
+ <Tag style={{ maxWidth: 150 }} clickable {...convertTagVals(value)}></Tag>
59
+ </Popover>
60
+ );
61
+ };
62
+
63
+ function convertTagVals(tagVals) {
64
+ return {
65
+ ...tagVals,
66
+
67
+ name: tagVals.name || tagVals.label
68
+ };
69
+ }
@@ -0,0 +1,13 @@
1
+ .tag-select-popover {
2
+ padding: 10px;
3
+ }
4
+
5
+ .tag-select-popover-inner {
6
+ max-height: 150px;
7
+ overflow: auto;
8
+ display: grid;
9
+ padding-top: 10px;
10
+ grid-column-gap: 5px;
11
+ row-gap: 8px;
12
+ grid-template-columns: max-content max-content;
13
+ }
@@ -0,0 +1,20 @@
1
+ import { HTMLSelect } from "@blueprintjs/core";
2
+ import React from "react";
3
+ import classNames from "classnames";
4
+
5
+ // so far this component just makes the styling of the disabled html select
6
+ // look like a regular html select with the selected option displayed
7
+ // (instead of greyed out and without the ability to have tooltips working)
8
+ const TgHTMLSelect = ({ disabled, ...rest }) => {
9
+ if (disabled) {
10
+ const opt = rest.options.find(o => o.value === rest.value);
11
+ return (
12
+ <div {...rest} className={classNames("bp3-html-select", rest.className)}>
13
+ {opt.label}
14
+ </div>
15
+ );
16
+ }
17
+ return <HTMLSelect {...rest}></HTMLSelect>;
18
+ };
19
+
20
+ export default TgHTMLSelect;
@@ -0,0 +1,537 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ import { MultiSelect, getCreateNewItem } from "@blueprintjs/select";
3
+ import { Keys, Button, MenuItem, Tag } from "@blueprintjs/core";
4
+ import React, { useEffect, useState } from "react";
5
+ import { filter, isEqual } from "lodash-es";
6
+ import classNames from "classnames";
7
+ import "./style.css";
8
+ import { withProps } from "recompose";
9
+ import fuzzysearch from "fuzzysearch";
10
+ import getTextFromEl from "../utils/getTextFromEl";
11
+ import { getTagColorStyle, getTagProps } from "../utils/tagUtils";
12
+ import popoverOverflowModifiers from "../utils/popoverOverflowModifiers";
13
+ import { compose } from "redux";
14
+
15
+ class TgSelect extends React.Component {
16
+ constructor(props) {
17
+ super(props);
18
+ const { autoOpen = false } = this.props;
19
+
20
+ this.state = {
21
+ isOpen: autoOpen,
22
+ activeItem: null,
23
+ query: ""
24
+ };
25
+ }
26
+ setOpenState = isOpen => {
27
+ const { handleOpenChange } = this.props;
28
+ if (handleOpenChange) {
29
+ handleOpenChange(isOpen);
30
+ }
31
+ this.setState({ isOpen });
32
+ };
33
+
34
+ static defaultProps = {
35
+ onChange: () => {},
36
+ options: [],
37
+ unfilteredOptions: [],
38
+ value: undefined
39
+ };
40
+
41
+ itemRenderer = (i, { index, handleClick, modifiers }) => {
42
+ const optionRenderer = this.getOptionRenderer();
43
+ const onClick = i.onClick || handleClick;
44
+ return (
45
+ <div //we specifically don't use a BP MenuItem component here because the menu item is too slow when 100s are loaded and will cause the component to lag
46
+ onClick={modifiers.disabled ? undefined : onClick}
47
+ key={index}
48
+ className={classNames(
49
+ "tg-select-option bp3-menu-item bp3-fill bp3-text-overflow-ellipsis",
50
+ {
51
+ "bp3-active": modifiers.active,
52
+ "bp3-disabled": modifiers.disabled
53
+ }
54
+ )}
55
+ >
56
+ {optionRenderer ? optionRenderer(i, this.props) : i.label}
57
+ </div>
58
+ );
59
+ };
60
+ tagRenderer = i => {
61
+ if (!i || (!this.props.multi && this.state.query)) {
62
+ return null;
63
+ }
64
+ // return i
65
+ return i.label;
66
+ };
67
+
68
+ handleItemSelect = (item, e) => {
69
+ e.stopPropagation();
70
+ const { onChange, value, multi, closeOnSelect, isTagSelect } = this.props;
71
+ this.setState({ activeItem: null });
72
+ if (multi) {
73
+ let valArray = getValueArray(value);
74
+
75
+ if (closeOnSelect || item.closeOnSelect) {
76
+ this.setOpenState(false);
77
+ this.input && this.input.blur();
78
+ }
79
+ if (
80
+ isTagSelect &&
81
+ item.value &&
82
+ item.value.includes &&
83
+ item.value.includes(":")
84
+ ) {
85
+ const topLevelId = item.value.split(":")[0];
86
+ valArray = valArray.filter(val => {
87
+ if (val?.value && val.value.includes && val.value.includes(":")) {
88
+ const valId = val.value.split(":")[0];
89
+ if (valId === topLevelId) {
90
+ return false;
91
+ }
92
+ }
93
+ return true;
94
+ });
95
+ }
96
+ return onChange([...valArray, item], e);
97
+ } else {
98
+ this.setOpenState(false);
99
+ this.input && this.input.blur();
100
+ return onChange(item, e);
101
+ }
102
+ };
103
+
104
+ handleTagRemove = (e, tagProps) => {
105
+ const { onChange, value } = this.props;
106
+ const filteredVals = filter(
107
+ value,
108
+ (obj, i) => !isEqual(i, tagProps["data-tag-index"])
109
+ );
110
+ e.stopPropagation();
111
+ onChange(filteredVals);
112
+ this.setOpenState(false);
113
+ this.input.focus();
114
+ };
115
+ handleTagInputRemove = (val, index) => {
116
+ const { onChange, value } = this.props;
117
+ const filteredVals = filter(value, (obj, i) => !isEqual(i, index));
118
+ // e.stopPropagation();
119
+ return onChange(filteredVals);
120
+ };
121
+
122
+ handleClear = e => {
123
+ const { multi, value } = this.props;
124
+
125
+ e.stopPropagation();
126
+ e.preventDefault();
127
+ let newValue = null;
128
+ if (multi) {
129
+ newValue = filter(value, obj => obj?.disabled) || [];
130
+ } else if (value && value.disabled) {
131
+ newValue = value;
132
+ }
133
+ const { onChange } = this.props;
134
+ this.setState({ query: "" });
135
+ onChange(newValue);
136
+ this.setOpenState(false);
137
+ this.input.focus();
138
+ };
139
+
140
+ itemListPredicate = (queryString, item) => {
141
+ // this will hide an option if it returns false
142
+ // each item is an individual option item
143
+ const { isSimpleSearch } = this.props;
144
+
145
+ return itemListPredicate(queryString, item, isSimpleSearch);
146
+ };
147
+
148
+ onQueryChange = query => {
149
+ const { onInputChange = () => {} } = this.props;
150
+ this.setState({
151
+ query
152
+ });
153
+ onInputChange(query);
154
+ };
155
+ handleActiveItemChange = (item, isCreateNewItem) => {
156
+ this.setState({
157
+ activeItem:
158
+ item ||
159
+ //if there's no item and we're in creatable mode, auto-select the create-new option
160
+ (isCreateNewItem || this.props.creatable ? getCreateNewItem() : null)
161
+ });
162
+ };
163
+ onInteraction = () => {
164
+ if (this.input != null && this.input !== document.activeElement) {
165
+ // the input is no longer focused so we can close the popover
166
+ this.setOpenState(false);
167
+ this.setState({
168
+ query: ""
169
+ });
170
+ } else if (!this.props.openOnKeyDown) {
171
+ // open the popover when focusing the tag input
172
+ this.setOpenState(true);
173
+ }
174
+ };
175
+
176
+ queryHasExactOptionMatch = () => {
177
+ //we don't want to show the creatable if the thing being created already exactly matches the label
178
+ return (
179
+ [
180
+ ...(this.props.options || []),
181
+ ...(Array.isArray(this.props.value)
182
+ ? this.props.value
183
+ : [this.props.value])
184
+ ].filter(o => {
185
+ const { label, value } = o || {};
186
+ const lowerQuery = (this.state.query || "").toLowerCase();
187
+ const lowerLabelOrVal =
188
+ label && label.toLowerCase
189
+ ? label.toLowerCase()
190
+ : value && value.toLowerCase && value.toLowerCase();
191
+ const textFromEl = getTextFromEl(label);
192
+
193
+ return lowerQuery === lowerLabelOrVal || lowerQuery === textFromEl;
194
+ }).length > 0
195
+ );
196
+ };
197
+
198
+ getTagProps = label => {
199
+ const { multi, value = [], disabled: _disabled } = this.props;
200
+ const val = Array.isArray(value) ? value : [value];
201
+ const matchingVal = val.find(op => op?.label === label);
202
+ const disabled = _disabled || (matchingVal && matchingVal.disabled);
203
+ const className = matchingVal && matchingVal.className;
204
+
205
+ return {
206
+ ...getTagColorStyle(multi && matchingVal && matchingVal.color),
207
+ intent: disabled ? "" : "primary",
208
+ minimal: true,
209
+ className: classNames(className, "tg-select-value", {
210
+ disabled
211
+ }),
212
+ onRemove: multi && !disabled ? this.handleTagRemove : null
213
+ };
214
+ };
215
+
216
+ getOptionRenderer = () => {
217
+ const { isTagSelect, optionRenderer, multi } = this.props;
218
+
219
+ if (isTagSelect && multi) {
220
+ return tagOptionRender;
221
+ }
222
+ return optionRenderer;
223
+ };
224
+ renderCreateNewOption = (query, active, handleClick) => {
225
+ if (this.props.renderCreateNewOption) {
226
+ return this.props.renderCreateNewOption(query, active, handleClick);
227
+ }
228
+ return (
229
+ <MenuItem
230
+ icon="add"
231
+ text={`Create "${query}"`}
232
+ active={active}
233
+ onClick={(...args) => {
234
+ const shouldStopEarly = this.props.onCreateNewOption?.(query);
235
+ if (shouldStopEarly) {
236
+ this.setOpenState(false);
237
+ } else {
238
+ handleClick(...args);
239
+ }
240
+ }}
241
+ shouldDismissPopover={false}
242
+ />
243
+ );
244
+ };
245
+
246
+ render() {
247
+ let {
248
+ multi,
249
+ asTag,
250
+ options,
251
+ unfilteredOptions,
252
+ value,
253
+ creatable,
254
+ optionRenderer, //pull this one out here so it doesn't get passsed along
255
+ tagInputProps,
256
+ autoFocus,
257
+ autoOpen,
258
+ mustHaveQueryToOpen,
259
+ noResultsText,
260
+ noResults: _noResults,
261
+ inputProps,
262
+ backgroundColor,
263
+ doNotFillWidth,
264
+ noToggle,
265
+ small,
266
+ placeholder,
267
+ isLoading,
268
+ disallowClear,
269
+ onBlur,
270
+ disabled,
271
+ popoverProps,
272
+ additionalRightEl,
273
+ resetOnSelect = true,
274
+ ...rest
275
+ } = this.props;
276
+ if (asTag) {
277
+ small = true;
278
+ placeholder = " ";
279
+ backgroundColor = "red";
280
+ disallowClear = true;
281
+ doNotFillWidth = true;
282
+ noToggle = true;
283
+ }
284
+ let noResults = _noResults;
285
+
286
+ // Null is also a valid value for a React Component, noResultsDefault should only be appplied when noResults is undefined
287
+ if (noResults === undefined) noResults = noResultsDefault;
288
+ const hasQuery = this.state.query?.length > 0;
289
+ const hasValue = Array.isArray(value)
290
+ ? value.length > 0
291
+ : !!value || value === 0;
292
+
293
+ const rightElement = isLoading ? (
294
+ <Button loading minimal />
295
+ ) : (
296
+ <span>
297
+ {additionalRightEl}
298
+ {hasValue && !disallowClear && !disabled && (
299
+ <Button
300
+ className="tg-select-clear-all"
301
+ icon="cross"
302
+ minimal
303
+ onClick={this.handleClear}
304
+ />
305
+ )}
306
+ {noResults !== null && !noToggle && (
307
+ <Button
308
+ onClick={e => {
309
+ if (this.state.isOpen) {
310
+ e.stopPropagation();
311
+
312
+ this.setOpenState(false);
313
+ }
314
+ }}
315
+ disabled={disabled}
316
+ className="tg-select-toggle"
317
+ minimal
318
+ icon={this.state.isOpen ? "caret-up" : "caret-down"}
319
+ />
320
+ )}
321
+ </span>
322
+ );
323
+
324
+ const maybeCreateNewItemFromQuery = creatable ? createNewOption : undefined;
325
+ const maybeCreateNewItemRenderer =
326
+ creatable && !this.queryHasExactOptionMatch()
327
+ ? this.renderCreateNewOption
328
+ : null;
329
+ const selectedItems = getValueArray(value).map(value => {
330
+ if (value && value.label) return value; //if the value has a label, just use that
331
+ //if not, look for an existing option to use that value
332
+ // unfilteredOptions will have every option included selected ones
333
+ return unfilteredOptions.find(
334
+ opt => opt && opt.value === ((value && value.value) || value)
335
+ );
336
+ });
337
+ const toRet = (
338
+ <MultiSelect
339
+ onActiveItemChange={this.handleActiveItemChange}
340
+ closeOnSelect={!multi}
341
+ resetOnSelect={resetOnSelect}
342
+ items={options || []}
343
+ activeItem={
344
+ this.state.activeItem ||
345
+ (options && options.filter(opt => !selectedItems.includes(opt))[0]) ||
346
+ null //it's important we pass null here instead of undefined if no active item is found
347
+ }
348
+ itemDisabled={itemDisabled}
349
+ query={this.state.query}
350
+ popoverProps={{
351
+ captureDismiss: true,
352
+ minimal: true,
353
+ className: classNames("tg-select", "tg-stop-dialog-form-enter", {
354
+ "tg-single-select": !multi,
355
+ "tg-select-as-tag": asTag,
356
+ "do-not-fill-width": doNotFillWidth,
357
+ "tg-small": small
358
+ }),
359
+ wrapperTagName: "div",
360
+ canEscapeKeyClose: true,
361
+ onInteraction: this.onInteraction,
362
+ isOpen: mustHaveQueryToOpen
363
+ ? hasQuery && this.state.isOpen
364
+ : this.state.isOpen,
365
+ modifiers: popoverOverflowModifiers,
366
+ ...popoverProps
367
+ }}
368
+ onItemSelect={this.handleItemSelect}
369
+ createNewItemFromQuery={maybeCreateNewItemFromQuery}
370
+ createNewItemRenderer={maybeCreateNewItemRenderer}
371
+ noResults={noResultsText || noResults}
372
+ onQueryChange={this.onQueryChange}
373
+ itemRenderer={this.itemRenderer}
374
+ itemListPredicate={this.itemListPredicate}
375
+ {...{
376
+ selectedItems,
377
+ tagRenderer: this.tagRenderer,
378
+ tagInputProps: {
379
+ inputRef: n => {
380
+ if (n) this.input = n;
381
+ },
382
+ placeholder:
383
+ placeholder || (creatable ? "Select/Create..." : "Select..."),
384
+ tagProps: this.getTagProps,
385
+ onRemove: multi ? this.handleTagInputRemove : null,
386
+ rightElement: rightElement,
387
+ disabled: disabled, // tg: adding isLoading will cause the input to be blurred when using generic select asReactSelect (don't do it),
388
+ ...tagInputProps, //spread additional tag input props here
389
+ intent: this.props.intent,
390
+ onKeyDown: e => {
391
+ const { which } = e;
392
+ e.persist();
393
+ if (which === Keys.ENTER) {
394
+ e.preventDefault();
395
+ // e.stopPropagation();
396
+ }
397
+ if (which === Keys.ESCAPE || which === Keys.TAB) {
398
+ // By default the escape key will not trigger a blur on the
399
+ // input element. It must be done explicitly.
400
+ if (this.input != null) {
401
+ this.input.blur();
402
+ }
403
+ this.setOpenState(false);
404
+ e.preventDefault();
405
+ e.stopPropagation(); //this prevents dialog's it is in from closing
406
+ } else if (
407
+ !(
408
+ which === Keys.BACKSPACE ||
409
+ which === Keys.ARROW_LEFT ||
410
+ which === Keys.ARROW_RIGHT
411
+ )
412
+ ) {
413
+ this.setOpenState(true);
414
+ }
415
+ },
416
+ inputProps: {
417
+ autoFocus: autoFocus || autoOpen,
418
+ onBlur,
419
+ ...(tagInputProps && tagInputProps.inputProps)
420
+ }
421
+ }
422
+ }}
423
+ {...rest}
424
+ />
425
+ );
426
+ return toRet;
427
+ }
428
+ }
429
+
430
+ const withAsyncOptions = Component => props => {
431
+ const { loadOptions, options, ...rest } = props;
432
+ const [asyncOptions, setAsyncOptions] = useState([]);
433
+ const [isLoading, setLoading] = useState(false);
434
+ useEffect(() => {
435
+ if (loadOptions) {
436
+ setLoading(true);
437
+ loadOptions().then(options => {
438
+ setAsyncOptions(options);
439
+ setLoading(false);
440
+ });
441
+ }
442
+ }, [loadOptions]);
443
+
444
+ return (
445
+ <Component
446
+ {...rest}
447
+ isLoading={isLoading || rest.isLoading}
448
+ options={loadOptions ? asyncOptions : options}
449
+ />
450
+ );
451
+ };
452
+
453
+ export default compose(
454
+ withAsyncOptions,
455
+ withProps(props => {
456
+ const { multi, value, options = [] } = props;
457
+ let optionsToRet = options;
458
+ // based on incoming value hide those selected options from the option list
459
+ if (multi && value) {
460
+ const valArray = getValueArray(value);
461
+ optionsToRet = options.filter(op => {
462
+ const isOptionSelected = valArray.some(val => {
463
+ if (!val) return false;
464
+ const matching = isEqual(val.value, op.value);
465
+ return matching;
466
+ });
467
+ return !isOptionSelected;
468
+ });
469
+ }
470
+ return {
471
+ // unfilteredOptions is needed for finding selected items
472
+ unfilteredOptions: options,
473
+ options: optionsToRet
474
+ };
475
+ })
476
+ )(TgSelect);
477
+
478
+ const itemDisabled = i => i.disabled;
479
+ const noResultsDefault = <div>No Results...</div>;
480
+
481
+ export function createNewOption(newValString) {
482
+ if (!newValString) return;
483
+ return {
484
+ userCreated: true,
485
+ label: newValString,
486
+ value: newValString
487
+ };
488
+ }
489
+
490
+ function getValueArray(value) {
491
+ return value || value === 0 ? (Array.isArray(value) ? value : [value]) : [];
492
+ }
493
+
494
+ //we export this here for use in createGenericSelect
495
+ export const itemListPredicate = (_queryString = "", items, isSimpleSearch) => {
496
+ const queryString = _queryString.toLowerCase();
497
+ const toSearchArr = (items || []).map(item => {
498
+ return {
499
+ item,
500
+ text: item.toLowerCase
501
+ ? item.toLowerCase()
502
+ : item.label
503
+ ? item.label.toLowerCase
504
+ ? item.label.toLowerCase()
505
+ : getTextFromEl(item.label).toLowerCase()
506
+ : (item.value &&
507
+ item.value.toLowerCase &&
508
+ item.value.toLowerCase()) ||
509
+ ""
510
+ };
511
+ });
512
+ let toRet = toSearchArr.filter(({ text }) =>
513
+ (isSimpleSearch ? simplesearch : fuzzysearch)(queryString, text)
514
+ );
515
+ toRet = toRet.sort(({ text }, { text: text2 }) => {
516
+ return getSort(text, queryString) - getSort(text2, queryString);
517
+ });
518
+
519
+ toRet = toRet.map(({ item }) => item);
520
+ return toRet;
521
+ };
522
+
523
+ export function simplesearch(needle, haystack) {
524
+ return (haystack || "").indexOf(needle) !== -1;
525
+ }
526
+ function tagOptionRender(vals) {
527
+ if (vals.noTagStyle) return vals.label;
528
+ return <Tag {...getTagProps(vals)}></Tag>;
529
+ }
530
+
531
+ function getSort(text, queryString) {
532
+ let ret;
533
+ if (text === queryString) ret = -1;
534
+ else if (text.includes(queryString)) ret = text.indexOf(queryString);
535
+ else ret = text.length;
536
+ return ret;
537
+ }