-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsearch-select.js
305 lines (274 loc) · 12.9 KB
/
search-select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { task } from 'ember-concurrency';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { resolve } from 'rsvp';
import { filterOptions, defaultMatcher } from 'ember-power-select/utils/group-utils';
import { removeFromArray } from 'vault/helpers/remove-from-array';
import { addToArray } from 'vault/helpers/add-to-array';
import { assert } from '@ember/debug';
/**
* @module SearchSelect
* The `SearchSelect` is an implementation of the [ember-power-select](https://github.com/cibernox/ember-power-select) used for form elements where options come dynamically from the API.
*
* @example
* <SearchSelect @id="policy" @models={{array "policies/acl"}} @onChange={{this.onChange}} @inputValue={{get @model this.valuePath}} @wildcardLabel="role" @fallbackComponent="string-list" @selectLimit={{1}} @backend={{@model.backend}} @disallowNewItems={{true}} class={{if this.validationError "dropdown-has-error-border"}} />
*
// * component functionality
* @param {function} onChange - The onchange action for this form field. ** SEE EXAMPLE ** mfa-login-enforcement-form.js (onMethodChange) for example when selecting models from a hasMany relationship
* @param {array} [inputValue] - Array of strings corresponding to the input's initial value, e.g. an array of model ids that on edit will appear as selected items below the input
* @param {boolean} [disallowNewItems=false] - Controls whether or not the user can add a new item if none found
* @param {boolean} [shouldRenderName=false] - By default an item's id renders in the dropdown, `true` displays the name with its id in smaller text beside it *NOTE: the boolean flips automatically with 'identity' models or if this.idKey !== 'id'
* @param {string} [nameKey="name"] - if shouldRenderName=true, you can use this arg to specify which key to use for the rendered name. Defaults to "name".
* @param {array} [parentManageSelected] - Array of selected items if the parent is keeping track of selections, see mfa-login-enforcement-form.js
* @param {boolean} [passObject=false] - When true, the onChange callback returns an array of objects with id (string) and isNew (boolean) (and any params from objectKeys). By default - onChange returns an array of id strings.
* @param {array} [objectKeys] - Array of values that correlate to model attrs. Used to render attr other than 'id' beside the name if shouldRenderName=true. If passObject=true, objectKeys are added to the passed, selected object.
* @param {number} [selectLimit] - Sets select limit
// * query params for dropdown items
* @param {Array} models - An array of model types to fetch from the API.
* @param {string} [backend] - name of the backend if the query for options needs additional information (eg. secret backend)
* @param {object} [queryObject] - object passed as query options to this.store.query(). NOTE: will override @backend
// * template only/display args
* @param {string} id - The name of the form field
* @param {string} [label] - Label for this form field
* @param {string} [labelClass] - overwrite default label size (14px) from class="is-label"
* @param {string} [ariaLabel] - fallback accessible label if label is not provided
* @param {string} [subText] - Text to be displayed below the label
* @param {string} fallbackComponent - name of component to be rendered if the API call 403s
* @param {string} [helpText] - Text to be displayed in the info tooltip for this form field
* @param {string} [wildcardLabel] - string (singular) for rendering label tag beside a wildcard selection (i.e. 'role*'), for the number of items it includes, e.g. @wildcardLabel="role" -> "includes 4 roles"
* @param {string} [placeholder] - text you wish to replace the default "search" with
* @param {boolean} [displayInherit=false] - if you need the search select component to display inherit instead of box.
* @param {boolean} [renderInPlace] - pass `true` when power select renders in a modal
* @param {function} [renderInfoTooltip] - receives each inputValue string and list of dropdownOptions as args, so parent can determine when to render a tooltip beside a selectedOption and the tooltip text. see 'oidc/provider-form.js'
* @param {boolean} [disabled] - if true sets the disabled property on the ember-power-select component and makes it unusable.
*
// * advanced customization
* @param {Array} options - array of objects passed directly to the power-select component. If doing this, `models` should not also be passed as that will overwrite the
* passed options. ex: [{ name: 'namespace45', id: 'displayedName' }]. It's recommended the parent should manage the array of selected items if manually passing in options.
* @param {function} search - Customizes how the power-select component searches for matches - see the power-select docs for more information.
*
*/
export default class SearchSelect extends Component {
@service store;
@tracked shouldUseFallback = false;
@tracked selectedOptions = []; // array of selected options (initially set by @inputValue)
@tracked dropdownOptions = []; // options that will render in dropdown, updates as selections are added/discarded
@tracked allOptions = []; // both selected and unselected options, used for wildcard filter
constructor() {
super(...arguments);
assert(
'one of @id, @label, or @ariaLabel must be passed to search-select component',
this.args.id || this.args.label || this.args.ariaLabel
);
}
get hidePowerSelect() {
return this.selectedOptions.length >= this.args.selectLimit;
}
get idKey() {
// if objectKeys exists, use the first element of the array as the identifier
// make 'id' as the first element in objectKeys if you do not want to override the default of 'id'
return this.args.objectKeys ? this.args.objectKeys[0] : 'id';
}
get shouldRenderName() {
return this.args.models?.some((model) => model.includes('identity')) ||
this.idKey !== 'id' ||
this.args.shouldRenderName
? true
: false;
}
get nameKey() {
return this.args.nameKey || 'name';
}
get searchEnabled() {
if (typeof this.args.searchEnabled === 'boolean') return this.args.searchEnabled;
return true;
}
addSearchText(optionsToFormat) {
// maps over array of objects or response from query
return optionsToFormat.map((option) => {
const id = option[this.idKey] ? option[this.idKey] : option.id;
option.searchText = `${option[this.nameKey]} ${id}`;
return option;
});
}
formatInputAndUpdateDropdown(inputValues) {
// inputValues are initially an array of strings from @inputValue
// map over so selectedOptions are objects
return inputValues.map((option) => {
const matchingOption = this.dropdownOptions.find((opt) => opt[this.idKey] === option);
// tooltip text comes from return of parent function
const addTooltip = this.args.renderInfoTooltip
? this.args.renderInfoTooltip(option, this.dropdownOptions)
: false;
// remove any matches from dropdown list
this.dropdownOptions = removeFromArray(this.dropdownOptions, matchingOption);
return {
id: option,
name: matchingOption ? matchingOption[this.nameKey] : option,
searchText: matchingOption ? matchingOption.searchText : option,
addTooltip,
// add additional attrs if we're using a dynamic idKey
...(this.idKey !== 'id' && this.customizeObject(matchingOption)),
};
});
}
@task
*fetchOptions() {
this.dropdownOptions = []; // reset dropdown anytime we re-fetch
if (this.args.parentManageSelected) {
// works in tandem with parent passing in @options directly
this.selectedOptions = this.args.parentManageSelected;
}
if (!this.args.models) {
if (Array.isArray(this.args.options)) {
const { options } = this.args;
// if options are nested, let parent handle formatting - see path-filter-config-list.js
this.dropdownOptions = options.some((e) => Object.keys(e).includes('groupName'))
? options
: [...this.addSearchText(options)];
if (!this.args.parentManageSelected) {
// set selectedOptions and remove matches from dropdown list
this.selectedOptions = this.args.inputValue
? this.formatInputAndUpdateDropdown(this.args.inputValue)
: [];
}
}
return;
}
for (const modelType of this.args.models) {
try {
let queryParams = {};
if (this.args.backend) {
queryParams = { backend: this.args.backend };
}
if (this.args.queryObject) {
queryParams = this.args.queryObject;
}
// fetch options from the store
const options = yield this.store.query(modelType, queryParams);
// store both select + unselected options in tracked property used by wildcard filter
this.allOptions = [...this.allOptions, ...options.map((option) => option.id)];
// add to dropdown options
this.dropdownOptions = [...this.dropdownOptions, ...this.addSearchText(options)];
} catch (err) {
if (err.httpStatus === 404) {
// continue to query other models even if one 404s
// and so selectedOptions will be set after for loop
continue;
}
if (err.httpStatus === 403) {
this.shouldUseFallback = true;
return;
}
throw err;
}
}
// after all models are queried, set selectedOptions and remove matches from dropdown list
this.selectedOptions = this.args.inputValue
? this.formatInputAndUpdateDropdown(this.args.inputValue)
: [];
}
@action
handleChange() {
if (this.selectedOptions.length && typeof this.selectedOptions[0] === 'object') {
this.args.onChange(
Array.from(this.selectedOptions, (option) =>
this.args.passObject ? this.customizeObject(option) : option.id
)
);
} else {
this.args.onChange(this.selectedOptions);
}
}
shouldShowCreate(id, searchResults) {
if (searchResults && searchResults.length && searchResults[0].groupName) {
return !searchResults.some((group) => group.options.find((opt) => opt.id === id));
}
const existingOption =
this.dropdownOptions && this.dropdownOptions.find((opt) => opt.id === id || opt.name === id);
if (this.args.disallowNewItems && !existingOption) {
return false;
}
return !existingOption;
}
// ----- adapted from ember-power-select-with-create
addCreateOption(term, results) {
if (this.shouldShowCreate(term, results)) {
const name = `Click to add new item: ${term}`;
const suggestion = {
__isSuggestion__: true,
__value__: term,
name,
id: name,
};
results.unshift(suggestion);
}
}
filter(options, searchText) {
const matcher = (option, text) => defaultMatcher(option.searchText, text);
return filterOptions(options || [], searchText, matcher);
}
// -----
customizeObject(option) {
if (!option) return;
let additionalKeys;
if (this.args.objectKeys) {
// pull attrs corresponding to objectKeys from model record, add to the selection
additionalKeys = Object.fromEntries(this.args.objectKeys.map((key) => [key, option[key]]));
// filter any undefined attrs, which could mean the model was not hydrated,
// the record is new or the model doesn't have that attribute
Object.keys(additionalKeys).forEach((key) => {
if (additionalKeys[key] === undefined) {
delete additionalKeys[key];
}
});
}
return {
id: option.id,
isNew: !!option.new,
...additionalKeys,
};
}
@action
discardSelection(selected) {
this.selectedOptions = removeFromArray(this.selectedOptions, selected);
if (!selected.new) {
this.dropdownOptions = addToArray(this.dropdownOptions, selected);
}
this.handleChange();
}
// ----- adapted from ember-power-select-with-create
@action
searchAndSuggest(term, select) {
if (term.length === 0) {
return this.dropdownOptions;
}
if (this.args.search) {
return resolve(this.args.search(term, select)).then((results) => {
this.addCreateOption(term, results);
return results;
});
}
const newOptions = this.filter(this.dropdownOptions, term);
this.addCreateOption(term, newOptions);
return newOptions;
}
@action
selectOrCreate(selection) {
if (selection && selection.__isSuggestion__) {
const name = selection.__value__;
this.selectedOptions = addToArray(this.selectedOptions, { name, id: name, new: true });
} else {
this.selectedOptions = addToArray(this.selectedOptions, selection);
this.dropdownOptions = removeFromArray(this.dropdownOptions, selection);
}
this.handleChange();
}
// -----
}