@angular/material
Version:
Angular Material
1,366 lines (1,352 loc) • 43.6 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChildren, Directive, ElementRef, EventEmitter, Input, NgModule, Optional, Output, Self, ViewEncapsulation } from '@angular/core';
import { ErrorStateMatcher, mixinColor, mixinDisabled, mixinErrorState } from '@angular/material/core';
import { FocusKeyManager } from '@angular/cdk/a11y';
import { Directionality } from '@angular/cdk/bidi';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { SelectionModel } from '@angular/cdk/collections';
import { BACKSPACE, DELETE, ENTER, SPACE } from '@angular/cdk/keycodes';
import { startWith } from 'rxjs/operators/startWith';
import { FormGroupDirective, NgControl, NgForm } from '@angular/forms';
import { MatFormFieldControl } from '@angular/material/form-field';
import { merge } from 'rxjs/observable/merge';
import { Subscription } from 'rxjs/Subscription';
import { Subject } from 'rxjs/Subject';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Represents an event fired on an individual `mat-chip`.
* @record
*/
/**
* Event object emitted by MatChip when selected or deselected.
*/
class MatChipSelectionChange {
/**
* @param {?} source
* @param {?} selected
* @param {?=} isUserInput
*/
constructor(source, selected, isUserInput = false) {
this.source = source;
this.selected = selected;
this.isUserInput = isUserInput;
}
}
/**
* \@docs-private
*/
class MatChipBase {
/**
* @param {?} _elementRef
*/
constructor(_elementRef) {
this._elementRef = _elementRef;
}
}
const _MatChipMixinBase = mixinColor(mixinDisabled(MatChipBase), 'primary');
/**
* Dummy directive to add CSS class to basic chips.
* \@docs-private
*/
class MatBasicChip {
}
MatBasicChip.decorators = [
{ type: Directive, args: [{
selector: `mat-basic-chip, [mat-basic-chip]`,
host: { 'class': 'mat-basic-chip' },
},] },
];
/** @nocollapse */
MatBasicChip.ctorParameters = () => [];
/**
* Material design styled Chip component. Used inside the MatChipList component.
*/
class MatChip extends _MatChipMixinBase {
/**
* @param {?} _elementRef
*/
constructor(_elementRef) {
super(_elementRef);
this._elementRef = _elementRef;
/**
* Whether the chip has focus.
*/
this._hasFocus = false;
this._selected = false;
this._selectable = true;
this._removable = true;
/**
* Emits when the chip is focused.
*/
this._onFocus = new Subject();
/**
* Emits when the chip is blured.
*/
this._onBlur = new Subject();
/**
* Emitted when the chip is selected or deselected.
*/
this.selectionChange = new EventEmitter();
/**
* Emitted when the chip is destroyed.
*/
this.destroyed = new EventEmitter();
/**
* Emitted when the chip is destroyed.
* @deprecated Use 'destroyed' instead.
* \@deletion-target 6.0.0
*/
this.destroy = this.destroyed;
/**
* Emitted when a chip is to be removed.
*/
this.removed = new EventEmitter();
/**
* Emitted when a chip is to be removed.
* @deprecated Use `removed` instead.
* \@deletion-target 6.0.0
*/
this.onRemove = this.removed;
}
/**
* Whether the chip is selected.
* @return {?}
*/
get selected() { return this._selected; }
/**
* @param {?} value
* @return {?}
*/
set selected(value) {
this._selected = coerceBooleanProperty(value);
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: value
});
}
/**
* The value of the chip. Defaults to the content inside `<mat-chip>` tags.
* @return {?}
*/
get value() {
return this._value != undefined
? this._value
: this._elementRef.nativeElement.textContent;
}
/**
* @param {?} value
* @return {?}
*/
set value(value) { this._value = value; }
/**
* Whether or not the chips are selectable. When a chip is not selectable,
* changes to it's selected state are always ignored.
* @return {?}
*/
get selectable() { return this._selectable; }
/**
* @param {?} value
* @return {?}
*/
set selectable(value) {
this._selectable = coerceBooleanProperty(value);
}
/**
* Determines whether or not the chip displays the remove styling and emits (remove) events.
* @return {?}
*/
get removable() { return this._removable; }
/**
* @param {?} value
* @return {?}
*/
set removable(value) {
this._removable = coerceBooleanProperty(value);
}
/**
* The ARIA selected applied to the chip.
* @return {?}
*/
get ariaSelected() {
return this.selectable ? this.selected.toString() : null;
}
/**
* @return {?}
*/
ngOnDestroy() {
this.destroyed.emit({ chip: this });
}
/**
* Selects the chip.
* @return {?}
*/
select() {
this._selected = true;
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: true
});
}
/**
* Deselects the chip.
* @return {?}
*/
deselect() {
this._selected = false;
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: false
});
}
/**
* Select this chip and emit selected event
* @return {?}
*/
selectViaInteraction() {
this._selected = true;
// Emit select event when selected changes.
this.selectionChange.emit({
source: this,
isUserInput: true,
selected: true
});
}
/**
* Toggles the current selected state of this chip.
* @param {?=} isUserInput
* @return {?}
*/
toggleSelected(isUserInput = false) {
this._selected = !this.selected;
this.selectionChange.emit({
source: this,
isUserInput,
selected: this._selected
});
return this.selected;
}
/**
* Allows for programmatic focusing of the chip.
* @return {?}
*/
focus() {
this._elementRef.nativeElement.focus();
this._onFocus.next({ chip: this });
}
/**
* Allows for programmatic removal of the chip. Called by the MatChipList when the DELETE or
* BACKSPACE keys are pressed.
*
* Informs any listeners of the removal request. Does not remove the chip from the DOM.
* @return {?}
*/
remove() {
if (this.removable) {
this.removed.emit({ chip: this });
}
}
/**
* Ensures events fire properly upon click.
* @param {?} event
* @return {?}
*/
_handleClick(event) {
// Check disabled
if (this.disabled) {
return;
}
event.preventDefault();
event.stopPropagation();
this.focus();
}
/**
* Handle custom key presses.
* @param {?} event
* @return {?}
*/
_handleKeydown(event) {
if (this.disabled) {
return;
}
switch (event.keyCode) {
case DELETE:
case BACKSPACE:
// If we are removable, remove the focused chip
this.remove();
// Always prevent so page navigation does not occur
event.preventDefault();
break;
case SPACE:
// If we are selectable, toggle the focused chip
if (this.selectable) {
this.toggleSelected(true);
}
// Always prevent space from scrolling the page since the list has focus
event.preventDefault();
break;
}
}
/**
* @return {?}
*/
_blur() {
this._hasFocus = false;
this._onBlur.next({ chip: this });
}
}
MatChip.decorators = [
{ type: Directive, args: [{
selector: `mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]`,
inputs: ['color', 'disabled'],
exportAs: 'matChip',
host: {
'class': 'mat-chip',
'[attr.tabindex]': 'disabled ? null : -1',
'role': 'option',
'[class.mat-chip-selected]': 'selected',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-selected]': 'ariaSelected',
'(click)': '_handleClick($event)',
'(keydown)': '_handleKeydown($event)',
'(focus)': '_hasFocus = true',
'(blur)': '_blur()',
},
},] },
];
/** @nocollapse */
MatChip.ctorParameters = () => [
{ type: ElementRef, },
];
MatChip.propDecorators = {
"selected": [{ type: Input },],
"value": [{ type: Input },],
"selectable": [{ type: Input },],
"removable": [{ type: Input },],
"selectionChange": [{ type: Output },],
"destroyed": [{ type: Output },],
"destroy": [{ type: Output },],
"removed": [{ type: Output },],
"onRemove": [{ type: Output, args: ['remove',] },],
};
/**
* Applies proper (click) support and adds styling for use with the Material Design "cancel" icon
* available at https://material.io/icons/#ic_cancel.
*
* Example:
*
* `<mat-chip>
* <mat-icon matChipRemove>cancel</mat-icon>
* </mat-chip>`
*
* You *may* use a custom icon, but you may need to override the `mat-chip-remove` positioning
* styles to properly center the icon within the chip.
*/
class MatChipRemove {
/**
* @param {?} _parentChip
*/
constructor(_parentChip) {
this._parentChip = _parentChip;
}
/**
* Calls the parent chip's public `remove()` method if applicable.
* @return {?}
*/
_handleClick() {
if (this._parentChip.removable) {
this._parentChip.remove();
}
}
}
MatChipRemove.decorators = [
{ type: Directive, args: [{
selector: '[matChipRemove]',
host: {
'class': 'mat-chip-remove',
'(click)': '_handleClick()',
}
},] },
];
/** @nocollapse */
MatChipRemove.ctorParameters = () => [
{ type: MatChip, },
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* \@docs-private
*/
class MatChipListBase {
/**
* @param {?} _defaultErrorStateMatcher
* @param {?} _parentForm
* @param {?} _parentFormGroup
* @param {?} ngControl
*/
constructor(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl) {
this._defaultErrorStateMatcher = _defaultErrorStateMatcher;
this._parentForm = _parentForm;
this._parentFormGroup = _parentFormGroup;
this.ngControl = ngControl;
}
}
const _MatChipListMixinBase = mixinErrorState(MatChipListBase);
// Increasing integer for generating unique ids for chip-list components.
let nextUniqueId = 0;
/**
* Change event object that is emitted when the chip list value has changed.
*/
class MatChipListChange {
/**
* @param {?} source
* @param {?} value
*/
constructor(source, value) {
this.source = source;
this.value = value;
}
}
/**
* A material design chips component (named ChipList for it's similarity to the List component).
*/
class MatChipList extends _MatChipListMixinBase {
/**
* @param {?} _elementRef
* @param {?} _changeDetectorRef
* @param {?} _dir
* @param {?} _parentForm
* @param {?} _parentFormGroup
* @param {?} _defaultErrorStateMatcher
* @param {?} ngControl
*/
constructor(_elementRef, _changeDetectorRef, _dir, _parentForm, _parentFormGroup, _defaultErrorStateMatcher, /** @docs-private */
ngControl) {
super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
this._elementRef = _elementRef;
this._changeDetectorRef = _changeDetectorRef;
this._dir = _dir;
this.ngControl = ngControl;
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
*/
this.controlType = 'mat-chip-list';
/**
* When a chip is destroyed, we track the index so we can focus the appropriate next chip.
*/
this._lastDestroyedIndex = null;
/**
* Track which chips we're listening to for focus/destruction.
*/
this._chipSet = new WeakMap();
/**
* Subscription to tabbing out from the chip list.
*/
this._tabOutSubscription = Subscription.EMPTY;
/**
* Uid of the chip list
*/
this._uid = `mat-chip-list-${nextUniqueId++}`;
/**
* Tab index for the chip list.
*/
this._tabIndex = 0;
/**
* User defined tab index.
* When it is not null, use user defined tab index. Otherwise use _tabIndex
*/
this._userTabIndex = null;
/**
* Function when touched
*/
this._onTouched = () => { };
/**
* Function when changed
*/
this._onChange = () => { };
this._multiple = false;
this._compareWith = (o1, o2) => o1 === o2;
this._required = false;
this._disabled = false;
/**
* Orientation of the chip list.
*/
this.ariaOrientation = 'horizontal';
this._selectable = true;
/**
* Event emitted when the selected chip list value has been changed by the user.
*/
this.change = new EventEmitter();
/**
* Event that emits whenever the raw value of the chip-list changes. This is here primarily
* to facilitate the two-way binding for the `value` input.
* \@docs-private
*/
this.valueChange = new EventEmitter();
if (this.ngControl) {
this.ngControl.valueAccessor = this;
}
}
/**
* The array of selected chips inside chip list.
* @return {?}
*/
get selected() {
return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0];
}
/**
* The ARIA role applied to the chip list.
* @return {?}
*/
get role() { return this.empty ? null : 'listbox'; }
/**
* Whether the user should be allowed to select multiple chips.
* @return {?}
*/
get multiple() { return this._multiple; }
/**
* @param {?} value
* @return {?}
*/
set multiple(value) {
this._multiple = coerceBooleanProperty(value);
}
/**
* A function to compare the option values with the selected values. The first argument
* is a value from an option. The second is a value from the selection. A boolean
* should be returned.
* @return {?}
*/
get compareWith() { return this._compareWith; }
/**
* @param {?} fn
* @return {?}
*/
set compareWith(fn) {
this._compareWith = fn;
if (this._selectionModel) {
// A different comparator means the selection could change.
this._initializeSelection();
}
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get value() { return this._value; }
/**
* @param {?} value
* @return {?}
*/
set value(value) {
this.writeValue(value);
this._value = value;
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get id() { return this._id || this._uid; }
/**
* @param {?} value
* @return {?}
*/
set id(value) {
this._id = value;
this.stateChanges.next();
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get required() { return this._required; }
/**
* @param {?} value
* @return {?}
*/
set required(value) {
this._required = coerceBooleanProperty(value);
this.stateChanges.next();
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get placeholder() {
return this._chipInput ? this._chipInput.placeholder : this._placeholder;
}
/**
* @param {?} value
* @return {?}
*/
set placeholder(value) {
this._placeholder = value;
this.stateChanges.next();
}
/**
* Whether any chips or the matChipInput inside of this chip-list has focus.
* @return {?}
*/
get focused() {
return this.chips.some(chip => chip._hasFocus) ||
(this._chipInput && this._chipInput.focused);
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get empty() {
return (!this._chipInput || this._chipInput.empty) && this.chips.length === 0;
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get shouldLabelFloat() { return !this.empty || this.focused; }
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
get disabled() { return this.ngControl ? !!this.ngControl.disabled : this._disabled; }
/**
* @param {?} value
* @return {?}
*/
set disabled(value) { this._disabled = coerceBooleanProperty(value); }
/**
* Whether or not this chip is selectable. When a chip is not selectable,
* its selected state is always ignored.
* @return {?}
*/
get selectable() { return this._selectable; }
/**
* @param {?} value
* @return {?}
*/
set selectable(value) { this._selectable = coerceBooleanProperty(value); }
/**
* @param {?} value
* @return {?}
*/
set tabIndex(value) {
this._userTabIndex = value;
this._tabIndex = value;
}
/**
* Combined stream of all of the child chips' selection change events.
* @return {?}
*/
get chipSelectionChanges() {
return merge(...this.chips.map(chip => chip.selectionChange));
}
/**
* Combined stream of all of the child chips' focus change events.
* @return {?}
*/
get chipFocusChanges() {
return merge(...this.chips.map(chip => chip._onFocus));
}
/**
* Combined stream of all of the child chips' blur change events.
* @return {?}
*/
get chipBlurChanges() {
return merge(...this.chips.map(chip => chip._onBlur));
}
/**
* Combined stream of all of the child chips' remove change events.
* @return {?}
*/
get chipRemoveChanges() {
return merge(...this.chips.map(chip => chip.destroy));
}
/**
* @return {?}
*/
ngAfterContentInit() {
this._keyManager = new FocusKeyManager(this.chips)
.withWrap()
.withVerticalOrientation()
.withHorizontalOrientation(this._dir ? this._dir.value : 'ltr');
// Prevents the chip list from capturing focus and redirecting
// it back to the first chip when the user tabs out.
this._tabOutSubscription = this._keyManager.tabOut.subscribe(() => {
this._tabIndex = -1;
setTimeout(() => this._tabIndex = this._userTabIndex || 0);
});
// When the list changes, re-subscribe
this._changeSubscription = this.chips.changes.pipe(startWith(null)).subscribe(() => {
this._resetChips();
// Reset chips selected/deselected status
this._initializeSelection();
// Check to see if we need to update our tab index
this._updateTabIndex();
// Check to see if we have a destroyed chip and need to refocus
this._updateFocusForDestroyedChips();
});
}
/**
* @return {?}
*/
ngOnInit() {
this._selectionModel = new SelectionModel(this.multiple, undefined, false);
this.stateChanges.next();
}
/**
* @return {?}
*/
ngDoCheck() {
if (this.ngControl) {
// We need to re-evaluate this on every change detection cycle, because there are some
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
// that whatever logic is in here has to be super lean or we risk destroying the performance.
this.updateErrorState();
}
}
/**
* @return {?}
*/
ngOnDestroy() {
this._tabOutSubscription.unsubscribe();
if (this._changeSubscription) {
this._changeSubscription.unsubscribe();
}
this._dropSubscriptions();
this.stateChanges.complete();
}
/**
* Associates an HTML input element with this chip list.
* @param {?} inputElement
* @return {?}
*/
registerInput(inputElement) {
this._chipInput = inputElement;
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @param {?} ids
* @return {?}
*/
setDescribedByIds(ids) { this._ariaDescribedby = ids.join(' '); }
/**
* @param {?} value
* @return {?}
*/
writeValue(value) {
if (this.chips) {
this._setSelectionByValue(value, false);
}
}
/**
* @param {?} fn
* @return {?}
*/
registerOnChange(fn) {
this._onChange = fn;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnTouched(fn) {
this._onTouched = fn;
}
/**
* @param {?} isDisabled
* @return {?}
*/
setDisabledState(isDisabled) {
this.disabled = isDisabled;
this._elementRef.nativeElement.disabled = isDisabled;
this.stateChanges.next();
}
/**
* Implemented as part of MatFormFieldControl.
* \@docs-private
* @return {?}
*/
onContainerClick() { this.focus(); }
/**
* Focuses the the first non-disabled chip in this chip list, or the associated input when there
* are no eligible chips.
* @return {?}
*/
focus() {
// TODO: ARIA says this should focus the first `selected` chip if any are selected.
// Focus on first element if there's no chipInput inside chip-list
if (this._chipInput && this._chipInput.focused) {
// do nothing
}
else if (this.chips.length > 0) {
this._keyManager.setFirstItemActive();
this.stateChanges.next();
}
else {
this._focusInput();
this.stateChanges.next();
}
}
/**
* Attempt to focus an input if we have one.
* @return {?}
*/
_focusInput() {
if (this._chipInput) {
this._chipInput.focus();
}
}
/**
* Pass events to the keyboard manager. Available here for tests.
* @param {?} event
* @return {?}
*/
_keydown(event) {
const /** @type {?} */ target = /** @type {?} */ (event.target);
// If they are on an empty input and hit backspace, focus the last chip
if (event.keyCode === BACKSPACE && this._isInputEmpty(target)) {
this._keyManager.setLastItemActive();
event.preventDefault();
}
else if (target && target.classList.contains('mat-chip')) {
this._keyManager.onKeydown(event);
this.stateChanges.next();
}
}
/**
* Check the tab index as you should not be allowed to focus an empty list.
* @return {?}
*/
_updateTabIndex() {
// If we have 0 chips, we should not allow keyboard focus
this._tabIndex = this._userTabIndex || (this.chips.length === 0 ? -1 : 0);
}
/**
* Update key manager's active item when chip is deleted.
* If the deleted chip is the last chip in chip list, focus the new last chip.
* Otherwise focus the next chip in the list.
* Save `_lastDestroyedIndex` so we can set the correct focus.
* @param {?} chip
* @return {?}
*/
_updateKeyManager(chip) {
let /** @type {?} */ chipIndex = this.chips.toArray().indexOf(chip);
if (this._isValidIndex(chipIndex)) {
if (chip._hasFocus) {
// Check whether the chip is not the last item
if (chipIndex < this.chips.length - 1) {
this._keyManager.setActiveItem(chipIndex);
}
else if (chipIndex - 1 >= 0) {
this._keyManager.setActiveItem(chipIndex - 1);
}
}
if (this._keyManager.activeItemIndex === chipIndex) {
this._lastDestroyedIndex = chipIndex;
}
}
}
/**
* Checks to see if a focus chip was recently destroyed so that we can refocus the next closest
* one.
* @return {?}
*/
_updateFocusForDestroyedChips() {
let /** @type {?} */ chipsArray = this.chips;
if (this._lastDestroyedIndex != null && chipsArray.length > 0) {
// Check whether the destroyed chip was the last item
const /** @type {?} */ newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
this._keyManager.setActiveItem(newFocusIndex);
let /** @type {?} */ focusChip = this._keyManager.activeItem;
// Focus the chip
if (focusChip) {
focusChip.focus();
}
}
// Reset our destroyed index
this._lastDestroyedIndex = null;
}
/**
* Utility to ensure all indexes are valid.
*
* @param {?} index The index to be checked.
* @return {?} True if the index is valid for our list of chips.
*/
_isValidIndex(index) {
return index >= 0 && index < this.chips.length;
}
/**
* @param {?} element
* @return {?}
*/
_isInputEmpty(element) {
if (element && element.nodeName.toLowerCase() === 'input') {
let /** @type {?} */ input = /** @type {?} */ (element);
return !input.value;
}
return false;
}
/**
* @param {?} value
* @param {?=} isUserInput
* @return {?}
*/
_setSelectionByValue(value, isUserInput = true) {
this._clearSelection();
this.chips.forEach(chip => chip.deselect());
if (Array.isArray(value)) {
value.forEach(currentValue => this._selectValue(currentValue, isUserInput));
this._sortValues();
}
else {
const /** @type {?} */ correspondingChip = this._selectValue(value, isUserInput);
// Shift focus to the active item. Note that we shouldn't do this in multiple
// mode, because we don't know what chip the user interacted with last.
if (correspondingChip) {
const /** @type {?} */ correspondingChipIndex = this.chips.toArray().indexOf(correspondingChip);
if (isUserInput) {
this._keyManager.setActiveItem(correspondingChipIndex);
}
else {
this._keyManager.updateActiveItemIndex(correspondingChipIndex);
}
}
}
}
/**
* Finds and selects the chip based on its value.
* @param {?} value
* @param {?=} isUserInput
* @return {?} Chip that has the corresponding value.
*/
_selectValue(value, isUserInput = true) {
const /** @type {?} */ correspondingChip = this.chips.find(chip => {
return chip.value != null && this._compareWith(chip.value, value);
});
if (correspondingChip) {
isUserInput ? correspondingChip.selectViaInteraction() : correspondingChip.select();
this._selectionModel.select(correspondingChip);
}
return correspondingChip;
}
/**
* @return {?}
*/
_initializeSelection() {
// Defer setting the value in order to avoid the "Expression
// has changed after it was checked" errors from Angular.
Promise.resolve().then(() => {
if (this.ngControl || this._value) {
this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value, false);
this.stateChanges.next();
}
});
}
/**
* Deselects every chip in the list.
* @param {?=} skip Chip that should not be deselected.
* @return {?}
*/
_clearSelection(skip) {
this._selectionModel.clear();
this.chips.forEach(chip => {
if (chip !== skip) {
chip.deselect();
}
});
this.stateChanges.next();
}
/**
* Sorts the model values, ensuring that they keep the same
* order that they have in the panel.
* @return {?}
*/
_sortValues() {
if (this._multiple) {
this._selectionModel.clear();
this.chips.forEach(chip => {
if (chip.selected) {
this._selectionModel.select(chip);
}
});
this.stateChanges.next();
}
}
/**
* Emits change event to set the model value.
* @param {?=} fallbackValue
* @return {?}
*/
_propagateChanges(fallbackValue) {
let /** @type {?} */ valueToEmit = null;
if (Array.isArray(this.selected)) {
valueToEmit = this.selected.map(chip => chip.value);
}
else {
valueToEmit = this.selected ? this.selected.value : fallbackValue;
}
this._value = valueToEmit;
this.change.emit(new MatChipListChange(this, valueToEmit));
this.valueChange.emit(valueToEmit);
this._onChange(valueToEmit);
this._changeDetectorRef.markForCheck();
}
/**
* When blurred, mark the field as touched when focus moved outside the chip list.
* @return {?}
*/
_blur() {
if (!this.disabled) {
if (this._chipInput) {
// If there's a chip input, we should check whether the focus moved to chip input.
// If the focus is not moved to chip input, mark the field as touched. If the focus moved
// to chip input, do nothing.
// Timeout is needed to wait for the focus() event trigger on chip input.
setTimeout(() => {
if (!this.focused) {
this._markAsTouched();
}
});
}
else {
// If there's no chip input, then mark the field as touched.
this._markAsTouched();
}
}
}
/**
* Mark the field as touched
* @return {?}
*/
_markAsTouched() {
this._onTouched();
this._changeDetectorRef.markForCheck();
this.stateChanges.next();
}
/**
* @return {?}
*/
_resetChips() {
this._dropSubscriptions();
this._listenToChipsFocus();
this._listenToChipsSelection();
this._listenToChipsRemoved();
}
/**
* @return {?}
*/
_dropSubscriptions() {
if (this._chipFocusSubscription) {
this._chipFocusSubscription.unsubscribe();
this._chipFocusSubscription = null;
}
if (this._chipBlurSubscription) {
this._chipBlurSubscription.unsubscribe();
this._chipBlurSubscription = null;
}
if (this._chipSelectionSubscription) {
this._chipSelectionSubscription.unsubscribe();
this._chipSelectionSubscription = null;
}
}
/**
* Listens to user-generated selection events on each chip.
* @return {?}
*/
_listenToChipsSelection() {
this._chipSelectionSubscription = this.chipSelectionChanges.subscribe(event => {
event.source.selected
? this._selectionModel.select(event.source)
: this._selectionModel.deselect(event.source);
// For single selection chip list, make sure the deselected value is unselected.
if (!this.multiple) {
this.chips.forEach(chip => {
if (!this._selectionModel.isSelected(chip) && chip.selected) {
chip.deselect();
}
});
}
if (event.isUserInput) {
this._propagateChanges();
}
});
}
/**
* Listens to user-generated selection events on each chip.
* @return {?}
*/
_listenToChipsFocus() {
this._chipFocusSubscription = this.chipFocusChanges.subscribe(event => {
let /** @type {?} */ chipIndex = this.chips.toArray().indexOf(event.chip);
if (this._isValidIndex(chipIndex)) {
this._keyManager.updateActiveItemIndex(chipIndex);
}
this.stateChanges.next();
});
this._chipBlurSubscription = this.chipBlurChanges.subscribe(() => {
this._blur();
this.stateChanges.next();
});
}
/**
* @return {?}
*/
_listenToChipsRemoved() {
this._chipRemoveSubscription = this.chipRemoveChanges.subscribe(event => {
this._updateKeyManager(event.chip);
});
}
}
MatChipList.decorators = [
{ type: Component, args: [{selector: 'mat-chip-list',
template: `<div class="mat-chip-list-wrapper"><ng-content></ng-content></div>`,
exportAs: 'matChipList',
host: {
'[attr.tabindex]': '_tabIndex',
'[attr.aria-describedby]': '_ariaDescribedby || null',
'[attr.aria-required]': 'required.toString()',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-invalid]': 'errorState',
'[attr.aria-multiselectable]': 'multiple',
'[attr.role]': 'role',
'[class.mat-chip-list-disabled]': 'disabled',
'[class.mat-chip-list-invalid]': 'errorState',
'[class.mat-chip-list-required]': 'required',
'[attr.aria-orientation]': 'ariaOrientation',
'class': 'mat-chip-list',
'(focus)': 'focus()',
'(blur)': '_blur()',
'(keydown)': '_keydown($event)'
},
providers: [{ provide: MatFormFieldControl, useExisting: MatChipList }],
styles: [".mat-chip-list-wrapper{display:flex;flex-direction:row;flex-wrap:wrap;align-items:baseline}.mat-chip:not(.mat-basic-chip){transition:box-shadow 280ms cubic-bezier(.4,0,.2,1);display:inline-flex;padding:7px 12px;border-radius:24px;align-items:center;cursor:default}.mat-chip:not(.mat-basic-chip)+.mat-chip:not(.mat-basic-chip){margin:0 0 0 8px}[dir=rtl] .mat-chip:not(.mat-basic-chip)+.mat-chip:not(.mat-basic-chip){margin:0 8px 0 0}.mat-form-field-prefix .mat-chip:not(.mat-basic-chip):last-child{margin-right:8px}[dir=rtl] .mat-form-field-prefix .mat-chip:not(.mat-basic-chip):last-child{margin-left:8px}.mat-chip:not(.mat-basic-chip) .mat-chip-remove.mat-icon{width:1em;height:1em}.mat-chip:not(.mat-basic-chip):focus{box-shadow:0 3px 3px -2px rgba(0,0,0,.2),0 3px 4px 0 rgba(0,0,0,.14),0 1px 8px 0 rgba(0,0,0,.12);outline:0}@media screen and (-ms-high-contrast:active){.mat-chip:not(.mat-basic-chip){outline:solid 1px}}.mat-chip-list-stacked .mat-chip-list-wrapper{display:block}.mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip){display:block;margin:0;margin-bottom:8px}[dir=rtl] .mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip){margin:0;margin-bottom:8px}.mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip):last-child,[dir=rtl] .mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip):last-child{margin-bottom:0}.mat-form-field-prefix .mat-chip-list-wrapper{margin-bottom:8px}.mat-chip-remove{margin-right:-4px;margin-left:6px;cursor:pointer}[dir=rtl] .mat-chip-remove{margin-right:6px;margin-left:-4px}input.mat-chip-input{width:150px;margin:3px;flex:1 0 150px}"],
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush
},] },
];
/** @nocollapse */
MatChipList.ctorParameters = () => [
{ type: ElementRef, },
{ type: ChangeDetectorRef, },
{ type: Directionality, decorators: [{ type: Optional },] },
{ type: NgForm, decorators: [{ type: Optional },] },
{ type: FormGroupDirective, decorators: [{ type: Optional },] },
{ type: ErrorStateMatcher, },
{ type: NgControl, decorators: [{ type: Optional }, { type: Self },] },
];
MatChipList.propDecorators = {
"errorStateMatcher": [{ type: Input },],
"multiple": [{ type: Input },],
"compareWith": [{ type: Input },],
"value": [{ type: Input },],
"id": [{ type: Input },],
"required": [{ type: Input },],
"placeholder": [{ type: Input },],
"disabled": [{ type: Input },],
"ariaOrientation": [{ type: Input, args: ['aria-orientation',] },],
"selectable": [{ type: Input },],
"tabIndex": [{ type: Input },],
"change": [{ type: Output },],
"valueChange": [{ type: Output },],
"chips": [{ type: ContentChildren, args: [MatChip,] },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Represents an input event on a `matChipInput`.
* @record
*/
/**
* Directive that adds chip-specific behaviors to an input element inside `<mat-form-field>`.
* May be placed inside or outside of an `<mat-chip-list>`.
*/
class MatChipInput {
/**
* @param {?} _elementRef
*/
constructor(_elementRef) {
this._elementRef = _elementRef;
/**
* Whether the control is focused.
*/
this.focused = false;
this._addOnBlur = false;
/**
* The list of key codes that will trigger a chipEnd event.
*
* Defaults to `[ENTER]`.
*/
this.separatorKeyCodes = [ENTER];
/**
* Emitted when a chip is to be added.
*/
this.chipEnd = new EventEmitter();
/**
* The input's placeholder text.
*/
this.placeholder = '';
this._inputElement = /** @type {?} */ (this._elementRef.nativeElement);
}
/**
* Register input for chip list
* @param {?} value
* @return {?}
*/
set chipList(value) {
if (value) {
this._chipList = value;
this._chipList.registerInput(this);
}
}
/**
* Whether or not the chipEnd event will be emitted when the input is blurred.
* @return {?}
*/
get addOnBlur() { return this._addOnBlur; }
/**
* @param {?} value
* @return {?}
*/
set addOnBlur(value) { this._addOnBlur = coerceBooleanProperty(value); }
/**
* Whether the input is empty.
* @return {?}
*/
get empty() { return !this._inputElement.value; }
/**
* Utility method to make host definition/tests more clear.
* @param {?=} event
* @return {?}
*/
_keydown(event) {
this._emitChipEnd(event);
}
/**
* Checks to see if the blur should emit the (chipEnd) event.
* @return {?}
*/
_blur() {
if (this.addOnBlur) {
this._emitChipEnd();
}
this.focused = false;
// Blur the chip list if it is not focused
if (!this._chipList.focused) {
this._chipList._blur();
}
this._chipList.stateChanges.next();
}
/**
* @return {?}
*/
_focus() {
this.focused = true;
this._chipList.stateChanges.next();
}
/**
* Checks to see if the (chipEnd) event needs to be emitted.
* @param {?=} event
* @return {?}
*/
_emitChipEnd(event) {
if (!this._inputElement.value && !!event) {
this._chipList._keydown(event);
}
if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) {
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
if (event) {
event.preventDefault();
}
}
}
/**
* @return {?}
*/
_onInput() {
// Let chip list know whenever the value changes.
this._chipList.stateChanges.next();
}
/**
* Focuses the input.
* @return {?}
*/
focus() { this._inputElement.focus(); }
}
MatChipInput.decorators = [
{ type: Directive, args: [{
selector: 'input[matChipInputFor]',
exportAs: 'matChipInput, matChipInputFor',
host: {
'class': 'mat-chip-input mat-input-element',
'(keydown)': '_keydown($event)',
'(blur)': '_blur()',
'(focus)': '_focus()',
'(input)': '_onInput()',
}
},] },
];
/** @nocollapse */
MatChipInput.ctorParameters = () => [
{ type: ElementRef, },
];
MatChipInput.propDecorators = {
"chipList": [{ type: Input, args: ['matChipInputFor',] },],
"addOnBlur": [{ type: Input, args: ['matChipInputAddOnBlur',] },],
"separatorKeyCodes": [{ type: Input, args: ['matChipInputSeparatorKeyCodes',] },],
"chipEnd": [{ type: Output, args: ['matChipInputTokenEnd',] },],
"placeholder": [{ type: Input },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class MatChipsModule {
}
MatChipsModule.decorators = [
{ type: NgModule, args: [{
imports: [],
exports: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
declarations: [MatChipList, MatChip, MatChipInput, MatChipRemove, MatChipRemove, MatBasicChip],
providers: [ErrorStateMatcher]
},] },
];
/** @nocollapse */
MatChipsModule.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Generated bundle index. Do not edit.
*/
export { MatChipsModule, MatChipListBase, _MatChipListMixinBase, MatChipListChange, MatChipList, MatChipSelectionChange, MatChipBase, _MatChipMixinBase, MatBasicChip, MatChip, MatChipRemove, MatChipInput };
//# sourceMappingURL=chips.js.map