Skip to content

Commit ba44780

Browse files
committed
fix(select): stores string | string[]
fixes #11337
1 parent 3b32b8e commit ba44780

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/components/select/select.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ import { SelectPopover, SelectPopoverOption } from './select-popover-component';
147147
providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: Select, multi: true } ],
148148
encapsulation: ViewEncapsulation.None,
149149
})
150-
export class Select extends BaseInput<string[]> implements AfterViewInit, OnDestroy {
150+
export class Select extends BaseInput<string[]|string> implements AfterViewInit, OnDestroy {
151151

152152
_multi: boolean = false;
153153
_options: QueryList<Option>;
154154
_texts: string[] = [];
155155
_text: string = '';
156+
_values: string[] = [];
156157

157158
/**
158159
* @input {string} The text to display on the cancel button. Default: `Cancel`.
@@ -394,7 +395,7 @@ export class Select extends BaseInput<string[]> implements AfterViewInit, OnDest
394395
set options(val: QueryList<Option>) {
395396
this._options = val;
396397

397-
if (this._value.length === 0) {
398+
if (this._values.length === 0) {
398399
// there are no values set at this point
399400
// so check to see who should be selected
400401
// we use writeValue() because we don't want to update ngModel
@@ -404,14 +405,7 @@ export class Select extends BaseInput<string[]> implements AfterViewInit, OnDest
404405
}
405406
}
406407

407-
_inputNormalize(val: any): string[] {
408-
if (Array.isArray(val)) {
409-
return val;
410-
}
411-
return [val + ''];
412-
}
413-
414-
_inputShouldChange(val: string[]): boolean {
408+
_inputShouldChange(val: string[]|string): boolean {
415409
return !deepEqual(this._value, val);
416410
}
417411

@@ -420,11 +414,12 @@ export class Select extends BaseInput<string[]> implements AfterViewInit, OnDest
420414
*/
421415
_inputUpdated() {
422416
this._texts.length = 0;
417+
this._values = Array.isArray(this._value) ? this._value : [this._value + ''];
423418

424419
if (this._options) {
425420
this._options.forEach(option => {
426421
// check this option if the option's value is in the values array
427-
option.selected = this._value.some(selectValue => {
422+
option.selected = this._values.some(selectValue => {
428423
return isCheckedProperty(selectValue, option.value);
429424
});
430425

src/components/select/test/select.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Select', () => {
2121
corpus: [
2222
[['hola'], ['hola']],
2323
[null, []],
24-
['hola', ['hola']],
24+
['hola', 'hola'],
2525
[['hola', 'adios'], ['hola', 'adios']]
2626
]
2727
});

src/util/base-input.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
238238
return this._isFocus;
239239
}
240240

241+
/**
242+
* @hidden
243+
*/
244+
hasValue(): boolean {
245+
const val = this._value;
246+
return isArray(val)
247+
? val.length > 0
248+
: isPresent(val);
249+
}
250+
241251
/**
242252
* @hidden
243253
*/
@@ -260,12 +270,7 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
260270
if (!this._item) {
261271
return;
262272
}
263-
264-
const hasValue = isArray(val)
265-
? val.length > 0
266-
: isPresent(val);
267-
268-
this._item.setElementClass('input-has-value', hasValue);
273+
this._item.setElementClass('input-has-value', this.hasValue());
269274
}
270275

271276
/**

0 commit comments

Comments
 (0)