|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {coerceBooleanProperty} from '@angular/cdk/coercion'; |
| 10 | +import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core'; |
| 11 | +import {hasModifierKey, TAB} from '@angular/cdk/keycodes'; |
| 12 | +import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options'; |
| 13 | +import {MatChipList} from './chip-set'; |
| 14 | +import {MatChipTextControl} from './chip-text-control'; |
| 15 | + |
| 16 | +/** Represents an input event on a `matChipInput`. */ |
| 17 | +export interface MatChipInputEvent { |
| 18 | + /** The native `<input>` element that the event is being fired for. */ |
| 19 | + input: HTMLInputElement; |
| 20 | + |
| 21 | + /** The value of the input. */ |
| 22 | + value: string; |
| 23 | +} |
| 24 | + |
| 25 | +// Increasing integer for generating unique ids. |
| 26 | +let nextUniqueId = 0; |
| 27 | + |
| 28 | +/** |
| 29 | + * Directive that adds chip-specific behaviors to an input element inside `<mat-form-field>`. |
| 30 | + * May be placed inside or outside of an `<mat-chip-list>`. |
| 31 | + */ |
| 32 | +@Directive({ |
| 33 | + selector: 'input[matChipInputFor]', |
| 34 | + exportAs: 'matChipInput, matChipInputFor', |
| 35 | + host: { |
| 36 | + 'class': 'mat-chip-input mat-input-element', |
| 37 | + '(keydown)': '_keydown($event)', |
| 38 | + '(blur)': '_blur()', |
| 39 | + '(focus)': '_focus()', |
| 40 | + '(input)': '_onInput()', |
| 41 | + '[id]': 'id', |
| 42 | + '[attr.disabled]': 'disabled || null', |
| 43 | + '[attr.placeholder]': 'placeholder || null', |
| 44 | + '[attr.aria-invalid]': '_chipList && _chipList.ngControl ? _chipList.ngControl.invalid : null', |
| 45 | + } |
| 46 | +}) |
| 47 | +export class MatChipInput implements MatChipTextControl, OnChanges { |
| 48 | + /** Whether the control is focused. */ |
| 49 | + focused: boolean = false; |
| 50 | + _chipList: MatChipList; |
| 51 | + |
| 52 | + /** Register input for chip list */ |
| 53 | + @Input('matChipInputFor') |
| 54 | + set chipList(value: MatChipList) { |
| 55 | + if (value) { |
| 56 | + this._chipList = value; |
| 57 | + this._chipList.registerInput(this); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Whether or not the chipEnd event will be emitted when the input is blurred. |
| 63 | + */ |
| 64 | + @Input('matChipInputAddOnBlur') |
| 65 | + get addOnBlur(): boolean { return this._addOnBlur; } |
| 66 | + set addOnBlur(value: boolean) { this._addOnBlur = coerceBooleanProperty(value); } |
| 67 | + _addOnBlur: boolean = false; |
| 68 | + |
| 69 | + /** |
| 70 | + * The list of key codes that will trigger a chipEnd event. |
| 71 | + * |
| 72 | + * Defaults to `[ENTER]`. |
| 73 | + */ |
| 74 | + @Input('matChipInputSeparatorKeyCodes') |
| 75 | + separatorKeyCodes: number[] | Set<number> = this._defaultOptions.separatorKeyCodes; |
| 76 | + |
| 77 | + /** Emitted when a chip is to be added. */ |
| 78 | + @Output('matChipInputTokenEnd') |
| 79 | + chipEnd: EventEmitter<MatChipInputEvent> = new EventEmitter<MatChipInputEvent>(); |
| 80 | + |
| 81 | + /** The input's placeholder text. */ |
| 82 | + @Input() placeholder: string = ''; |
| 83 | + |
| 84 | + /** Unique id for the input. */ |
| 85 | + @Input() id: string = `mat-chip-list-input-${nextUniqueId++}`; |
| 86 | + |
| 87 | + /** Whether the input is disabled. */ |
| 88 | + @Input() |
| 89 | + get disabled(): boolean { return this._disabled || (this._chipList && this._chipList.disabled); } |
| 90 | + set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value); } |
| 91 | + private _disabled: boolean = false; |
| 92 | + |
| 93 | + /** Whether the input is empty. */ |
| 94 | + get empty(): boolean { return !this._inputElement.value; } |
| 95 | + |
| 96 | + /** The native input element to which this directive is attached. */ |
| 97 | + protected _inputElement: HTMLInputElement; |
| 98 | + |
| 99 | + constructor( |
| 100 | + protected _elementRef: ElementRef<HTMLInputElement>, |
| 101 | + @Inject(MAT_CHIPS_DEFAULT_OPTIONS) private _defaultOptions: MatChipsDefaultOptions) { |
| 102 | + this._inputElement = this._elementRef.nativeElement as HTMLInputElement; |
| 103 | + } |
| 104 | + |
| 105 | + ngOnChanges() { |
| 106 | + this._chipList.stateChanges.next(); |
| 107 | + } |
| 108 | + |
| 109 | + /** Utility method to make host definition/tests more clear. */ |
| 110 | + _keydown(event?: KeyboardEvent) { |
| 111 | + // Allow the user's focus to escape when they're tabbing forward. Note that we don't |
| 112 | + // want to do this when going backwards, because focus should go back to the first chip. |
| 113 | + if (event && event.keyCode === TAB && !hasModifierKey(event, 'shiftKey')) { |
| 114 | + this._chipList._allowFocusEscape(); |
| 115 | + } |
| 116 | + |
| 117 | + this._emitChipEnd(event); |
| 118 | + } |
| 119 | + |
| 120 | + /** Checks to see if the blur should emit the (chipEnd) event. */ |
| 121 | + _blur() { |
| 122 | + if (this.addOnBlur) { |
| 123 | + this._emitChipEnd(); |
| 124 | + } |
| 125 | + this.focused = false; |
| 126 | + // Blur the chip list if it is not focused |
| 127 | + if (!this._chipList.focused) { |
| 128 | + this._chipList._blur(); |
| 129 | + } |
| 130 | + this._chipList.stateChanges.next(); |
| 131 | + } |
| 132 | + |
| 133 | + _focus() { |
| 134 | + this.focused = true; |
| 135 | + this._chipList.stateChanges.next(); |
| 136 | + } |
| 137 | + |
| 138 | + /** Checks to see if the (chipEnd) event needs to be emitted. */ |
| 139 | + _emitChipEnd(event?: KeyboardEvent) { |
| 140 | + if (!this._inputElement.value && !!event) { |
| 141 | + this._chipList._keydown(event); |
| 142 | + } |
| 143 | + if (!event || this._isSeparatorKey(event)) { |
| 144 | + this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value }); |
| 145 | + |
| 146 | + if (event) { |
| 147 | + event.preventDefault(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + _onInput() { |
| 153 | + // Let chip list know whenever the value changes. |
| 154 | + this._chipList.stateChanges.next(); |
| 155 | + } |
| 156 | + |
| 157 | + /** Focuses the input. */ |
| 158 | + focus(): void { |
| 159 | + this._inputElement.focus(); |
| 160 | + } |
| 161 | + |
| 162 | + /** Checks whether a keycode is one of the configured separators. */ |
| 163 | + private _isSeparatorKey(event: KeyboardEvent) { |
| 164 | + if (hasModifierKey(event)) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + const separators = this.separatorKeyCodes; |
| 169 | + const keyCode = event.keyCode; |
| 170 | + return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode); |
| 171 | + } |
| 172 | +} |
| 173 | + |
0 commit comments