|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {Directive} from '@angular/core'; |
| 9 | +export type OpenAction = 'focus' | 'click' | 'downKey' | 'toggle'; |
| 10 | +export type OpenActionInput = OpenAction | OpenAction[] | string | null | undefined; |
| 11 | + |
| 12 | +import { |
| 13 | + AfterContentInit, |
| 14 | + Directive, |
| 15 | + ElementRef, |
| 16 | + EventEmitter, |
| 17 | + Input, |
| 18 | + OnDestroy, |
| 19 | + Optional, |
| 20 | + Output, ViewContainerRef |
| 21 | +} from '@angular/core'; |
| 22 | +import {CdkComboboxPanel, AriaHasPopupValue} from './combobox-panel'; |
| 23 | +import {TemplatePortal} from '@angular/cdk/portal'; |
| 24 | +import { |
| 25 | + ConnectedPosition, |
| 26 | + FlexibleConnectedPositionStrategy, |
| 27 | + Overlay, |
| 28 | + OverlayConfig, |
| 29 | + OverlayRef |
| 30 | +} from '@angular/cdk/overlay'; |
| 31 | +import {Directionality} from '@angular/cdk/bidi'; |
| 32 | +import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion'; |
| 33 | + |
10 | 34 |
|
11 | 35 | @Directive({
|
12 | 36 | selector: '[cdkCombobox]',
|
13 | 37 | exportAs: 'cdkCombobox',
|
14 | 38 | host: {
|
15 |
| - 'role': 'combobox' |
| 39 | + 'role': 'combobox', |
| 40 | + '(click)': 'toggle()', |
| 41 | + '[attr.aria-disabled]': 'disabled', |
| 42 | + '[attr.aria-controls]': 'contentId', |
| 43 | + '[attr.aria-haspopup]': 'contentType' |
16 | 44 | }
|
17 | 45 | })
|
18 |
| -export class CdkCombobox<T = unknown> { |
| 46 | +export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit { |
| 47 | + @Input('cdkComboboxTriggerFor') |
| 48 | + get panel(): CdkComboboxPanel<T> | undefined { return this._panel; } |
| 49 | + set panel(panel: CdkComboboxPanel<T> | undefined) { this._panel = panel; } |
| 50 | + private _panel: CdkComboboxPanel<T> | undefined; |
| 51 | + |
| 52 | + @Input() |
| 53 | + value: T; |
| 54 | + |
| 55 | + @Input() |
| 56 | + get disabled(): boolean { return this._disabled; } |
| 57 | + set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value); } |
| 58 | + private _disabled: boolean = false; |
| 59 | + |
| 60 | + @Input() |
| 61 | + get openAction(): OpenAction[] { |
| 62 | + return this._openActions; |
| 63 | + } |
| 64 | + set openAction(action: OpenAction[]) { |
| 65 | + this._openActions = this._coerceOpenActionProperty(action); |
| 66 | + } |
| 67 | + private _openActions: OpenAction[] = ['click']; |
| 68 | + |
| 69 | + @Output('comboboxPanelOpened') readonly opened: EventEmitter<void> = new EventEmitter<void>(); |
| 70 | + @Output('comboboxPanelClosed') readonly closed: EventEmitter<void> = new EventEmitter<void>(); |
| 71 | + @Output('panelValueChanged') readonly panelValueChanged: EventEmitter<T> = new EventEmitter<T>(); |
| 72 | + |
| 73 | + private _overlayRef: OverlayRef; |
| 74 | + private _panelContent: TemplatePortal; |
| 75 | + contentId: string = ''; |
| 76 | + contentType: AriaHasPopupValue; |
| 77 | + |
| 78 | + constructor( |
| 79 | + private readonly _elementRef: ElementRef<HTMLElement>, |
| 80 | + private readonly _overlay: Overlay, |
| 81 | + protected readonly _viewContainerRef: ViewContainerRef, |
| 82 | + @Optional() private readonly _directionality?: Directionality |
| 83 | + ) {} |
| 84 | + |
| 85 | + ngAfterContentInit() { |
| 86 | + this._panel?.valueUpdated.subscribe(data => { |
| 87 | + this._setComboboxValue(data); |
| 88 | + this.close(); |
| 89 | + }); |
| 90 | + |
| 91 | + this._panel?.contentIdUpdated.subscribe(id => { |
| 92 | + this.contentId = id; |
| 93 | + }); |
| 94 | + |
| 95 | + this._panel?.contentTypeUpdated.subscribe(type => { |
| 96 | + this.contentType = type; |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + ngOnDestroy() { |
| 101 | + this.opened.complete(); |
| 102 | + this.closed.complete(); |
| 103 | + this.panelValueChanged.complete(); |
| 104 | + } |
| 105 | + |
| 106 | + /** Toggles the open state of the panel. */ |
| 107 | + toggle() { |
| 108 | + if (this.hasPanel()) { |
| 109 | + this.isOpen() ? this.close() : this.open(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** If the combobox is closed and not disabled, opens the panel. */ |
| 114 | + open() { |
| 115 | + if (!this.isOpen() && !this.disabled) { |
| 116 | + this.opened.next(); |
| 117 | + this._overlayRef = this._overlayRef || this._overlay.create(this._getOverlayConfig()); |
| 118 | + this._overlayRef.attach(this._getPanelContent()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** If the combobox is open and not disabled, closes the panel. */ |
| 123 | + close() { |
| 124 | + if (this.isOpen() && !this.disabled) { |
| 125 | + this.closed.next(); |
| 126 | + this._overlayRef.detach(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** Returns true if panel is currently opened. */ |
| 131 | + isOpen(): boolean { |
| 132 | + return this._overlayRef ? this._overlayRef.hasAttached() : false; |
| 133 | + } |
| 134 | + |
| 135 | + /** Returns true if combobox has a child panel. */ |
| 136 | + hasPanel(): boolean { |
| 137 | + return !!this.panel; |
| 138 | + } |
| 139 | + |
| 140 | + private _setComboboxValue(value: T) { |
| 141 | + const valueChanged = (this.value !== value); |
| 142 | + this.value = value; |
| 143 | + |
| 144 | + if (valueChanged) { |
| 145 | + this.panelValueChanged.emit(value); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private _getOverlayConfig() { |
| 150 | + return new OverlayConfig({ |
| 151 | + positionStrategy: this._getOverlayPositionStrategy(), |
| 152 | + scrollStrategy: this._overlay.scrollStrategies.block(), |
| 153 | + direction: this._directionality, |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + private _getOverlayPositionStrategy(): FlexibleConnectedPositionStrategy { |
| 158 | + return this._overlay |
| 159 | + .position() |
| 160 | + .flexibleConnectedTo(this._elementRef) |
| 161 | + .withPositions(this._getOverlayPositions()); |
| 162 | + } |
| 163 | + |
| 164 | + private _getOverlayPositions(): ConnectedPosition[] { |
| 165 | + return [ |
| 166 | + {originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'}, |
| 167 | + {originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom'}, |
| 168 | + {originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top'}, |
| 169 | + {originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom'}, |
| 170 | + ]; |
| 171 | + } |
| 172 | + |
| 173 | + private _getPanelContent() { |
| 174 | + const hasPanelChanged = this._panel?._templateRef !== this._panelContent?.templateRef; |
| 175 | + if (this._panel && (!this._panel || hasPanelChanged)) { |
| 176 | + this._panelContent = new TemplatePortal(this._panel._templateRef, this._viewContainerRef); |
| 177 | + } |
| 178 | + |
| 179 | + return this._panelContent; |
| 180 | + } |
| 181 | + |
| 182 | + private _coerceOpenActionProperty(input: string | OpenAction[]): OpenAction[] { |
| 183 | + let actions: OpenAction[] = []; |
| 184 | + if (typeof input === 'string') { |
| 185 | + actions.push(input as OpenAction); |
| 186 | + } else { |
| 187 | + actions = input; |
| 188 | + } |
| 189 | + return actions; |
| 190 | + } |
19 | 191 |
|
| 192 | + static ngAcceptInputType_disabled: BooleanInput; |
| 193 | + static ngAcceptInputType_openActions: OpenActionInput; |
20 | 194 | }
|
0 commit comments