fix(select): object as value

fixes: #11413
This commit is contained in:
Manuel Mtz-Almeida
2017-04-27 21:27:52 +02:00
parent b11442716b
commit 4c8efc22e1

View File

@ -9,7 +9,7 @@ import { Config } from '../../config/config';
import { DeepLinker } from '../../navigation/deep-linker'; import { DeepLinker } from '../../navigation/deep-linker';
import { Form } from '../../util/form'; import { Form } from '../../util/form';
import { BaseInput } from '../../util/base-input'; import { BaseInput } from '../../util/base-input';
import { isCheckedProperty, isTrueProperty, deepCopy, deepEqual } from '../../util/util'; import { isCheckedProperty, isTrueProperty, deepCopy, deepEqual, assert } from '../../util/util';
import { Item } from '../item/item'; import { Item } from '../item/item';
import { NavController } from '../../navigation/nav-controller'; import { NavController } from '../../navigation/nav-controller';
import { Option } from '../option/option'; import { Option } from '../option/option';
@ -147,13 +147,12 @@ import { SelectPopover, SelectPopoverOption } from './select-popover-component';
providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: Select, multi: true } ], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: Select, multi: true } ],
encapsulation: ViewEncapsulation.None, encapsulation: ViewEncapsulation.None,
}) })
export class Select extends BaseInput<string[]|string> implements AfterViewInit, OnDestroy { export class Select extends BaseInput<any> implements AfterViewInit, OnDestroy {
_multi: boolean = false; _multi: boolean = false;
_options: QueryList<Option>; _options: QueryList<Option>;
_texts: string[] = []; _texts: string[] = [];
_text: string = ''; _text: string = '';
_values: string[] = [];
/** /**
* @input {string} The text to display on the cancel button. Default: `Cancel`. * @input {string} The text to display on the cancel button. Default: `Cancel`.
@ -222,6 +221,15 @@ export class Select extends BaseInput<string[]|string> implements AfterViewInit,
this.open(); this.open();
} }
/**
* @hidden
*/
getValues(): any[] {
const values = Array.isArray(this._value) ? this._value : [this._value];
assert(!this._multi && values.length <= 1, 'single only can have one value');
return values;
}
/** /**
* Open the select interface. * Open the select interface.
*/ */
@ -394,8 +402,8 @@ export class Select extends BaseInput<string[]|string> implements AfterViewInit,
@ContentChildren(Option) @ContentChildren(Option)
set options(val: QueryList<Option>) { set options(val: QueryList<Option>) {
this._options = val; this._options = val;
const values = this.getValues();
if (this._values.length === 0) { if (values.length === 0) {
// there are no values set at this point // there are no values set at this point
// so check to see who should be selected // so check to see who should be selected
// we use writeValue() because we don't want to update ngModel // we use writeValue() because we don't want to update ngModel
@ -422,12 +430,11 @@ export class Select extends BaseInput<string[]|string> implements AfterViewInit,
*/ */
_inputUpdated() { _inputUpdated() {
this._texts.length = 0; this._texts.length = 0;
this._values = Array.isArray(this._value) ? this._value : [this._value + ''];
if (this._options) { if (this._options) {
this._options.forEach(option => { this._options.forEach(option => {
// check this option if the option's value is in the values array // check this option if the option's value is in the values array
option.selected = this._values.some(selectValue => { option.selected = this.getValues().some(selectValue => {
return isCheckedProperty(selectValue, option.value); return isCheckedProperty(selectValue, option.value);
}); });