-
Notifications
You must be signed in to change notification settings - Fork 6
/
polaris-select.js
359 lines (316 loc) · 7.11 KB
/
polaris-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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import Component from '@ember/component';
import { computed } from '@ember/object';
import { bool } from '@ember/object/computed';
import { guidFor } from '@ember/object/internals';
import { errorId, helpTextId } from '@smile-io/ember-polaris/utils/id';
import { isGroup } from '@smile-io/ember-polaris/helpers/polaris-select/is-group';
import layout from '../templates/components/polaris-select';
const PLACEHOLDER_VALUE = '';
export default Component.extend({
tagName: '',
layout,
/**
* List of options or option groups to choose from
*
* Options may be either simple string values, or
* objects with the following properties:
*
* value: String. Machine value of the option; this is the value passed to `onChange`
* label: String. Human-readable text for the option
* disabled: Boolean. Option will be visible, but not selectable
*
* Groups are represented by objects with these properties:
*
* title: String. Title for the group
* options: (String|Object)[]. List of options
*
* @property options
* @type {(String|Object)[]}
* @default null
* @public
*/
options: null,
/**
* Label for the select
*
* @property label
* @type {String}
* @default null
* @public
*/
label: null,
/**
* Adds an action to the label
*
* Object with the following optional properties:
*
* text: String. Content the action displays
* onAction: Function. Callback when an action takes place
*
* @property labelAction
* @type {Object}
* @default null
* @public
*/
labelAction: null,
/**
* Visually hide the label
*
* @property labelHidden
* @type {Boolean}
* @default false
* @public
*/
labelHidden: false,
/**
* Show the label to the left of the value, inside the control
*
* @property labelInline
* @type {Boolean}
* @default false
* @public
*/
labelInline: false,
/**
* Disable input
*
* @property disabled
* @type {Boolean}
* @default false
* @public
*/
disabled: false,
/**
* Additional text to aide in use
*
* @property helpText
* @type {String|Component|Object}
* @default null
* @public
*/
helpText: null,
/**
* Example text to display as placeholder
*
* @property placeholder
* @type {String}
* @default null
* @public
*/
placeholder: null,
/* eslint smile-ember/order-in-components: 'off' */
/**
* ID for form input
*
* @property id
* @type {String}
* @default null
* @public
*/
id: null,
/**
* Name for form input
*
* @property name
* @type {String}
* @default null
* @public
*/
name: null,
/**
* Value for form input
*
* @property value
* @type {String}
* @default ''
* @public
*/
value: '',
/**
* Display an error state
*
* @property error
* @type {String|Component|Boolean}
* @default null
* @public
*/
error: null,
/**
* Callback when selection is changed
*
* @property onChange
* @type {Function}
* @default noop
* @public
*/
onChange() {},
/**
* Callback when select is focussed
*
* @property onFocus
* @type {Function}
* @default noop
* @public
*/
onFocus() {},
/**
* Callback when focus is removed
*
* @property onBlur
* @type {Function}
* @default noop
* @public
*/
onBlur() {},
/**
* Flag indicating whether an error is present
*
* @property hasError
* @type {Boolean}
* @private
*/
hasError: bool('error').readOnly(),
/**
* Internal ID for form input, with a default value.
*
* @property id
* @type {String}
* @private
*/
// eslint disable smile-ember/order-in-components
_id: computed('id', function() {
return this.get('id') || guidFor(this);
}).readOnly(),
/**
* Class names for select element
*
* @property className
* @type {String}
* @private
*/
className: computed('error', 'disabled', function() {
let classNames = ['Polaris-Select'];
if (this.get('error')) {
classNames.push('Polaris-Select--error');
}
if (this.get('disabled')) {
classNames.push('Polaris-Select--disabled');
}
return classNames.join(' ');
}).readOnly(),
/**
* Aria described-by field for accessibility
*
* @property describedBy
* @type {String}
* @private
*/
describedBy: computed('error', 'helpText', function() {
let { error, helpText, _id: id } = this.getProperties(
'error',
'helpText',
'_id'
);
let describedBy = [];
if (helpText) {
describedBy.push(helpTextId(id));
}
if (error) {
describedBy.push(errorId(id));
}
return describedBy.length ? describedBy.join(' ') : undefined;
}).readOnly(),
/**
* Options processed into a renderable state.
*
* @property normalizedOptions
* @type {Object[]}
* @private
*/
normalizedOptions: computed('options.[]', 'placeholder', function() {
let options = this.get('options') || [];
let normalizedOptions = options.map(normalizeOption);
let placeholder = this.get('placeholder');
if (placeholder) {
normalizedOptions = [
{
label: placeholder,
value: PLACEHOLDER_VALUE,
disabled: true,
},
...normalizedOptions,
];
}
return normalizedOptions;
}).readOnly(),
/**
* Gets the text to display in the UI, for the currently selected option
*
* @property selectedOption
* @type {String}
* @private
*/
selectedOption: computed(
'normalizedOptions.@each.value',
'value',
function() {
let { normalizedOptions: options, value } = this.getProperties(
'normalizedOptions',
'value'
);
let flatOptions = flattenOptions(options);
let selectedOption = flatOptions.find((option) => value === option.value);
if (selectedOption === undefined) {
// Get the first visible option (not the hidden placeholder)
selectedOption = flatOptions.find((option) => !option.hidden);
}
return selectedOption ? selectedOption.label : '';
}
).readOnly(),
actions: {
handleChange(e) {
this.onChange(e.currentTarget.value, this.get('_id'));
},
},
});
function isString(option) {
return typeof option === 'string';
}
function normalizeStringOption(option) {
return {
label: option,
value: option,
};
}
/**
* Converts a string option (and each string option in a Group) into
* an Option object.
*/
function normalizeOption(option) {
if (isString(option)) {
return normalizeStringOption(option);
} else if (isGroup(option)) {
let { title, options } = option;
return {
title,
options: options.map((option) => {
return isString(option) ? normalizeStringOption(option) : option;
}),
};
}
return option;
}
/**
* Ungroups an options array
*/
function flattenOptions(options) {
let flatOptions = [];
options.forEach((optionOrGroup) => {
if (isGroup(optionOrGroup)) {
flatOptions = flatOptions.concat(optionOrGroup.options);
} else {
flatOptions.push(optionOrGroup);
}
});
return flatOptions;
}