Skip to content

Commit ec7721b

Browse files
refactor(simple-combo): make text selection work as expected
1 parent ca67a86 commit ec7721b

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { IgxComboBaseDirective, IGX_COMBO_COMPONENT } from '../combo/combo.commo
1313
import { IgxComboModule } from '../combo/combo.component';
1414
import { DisplayDensityToken, IDisplayDensityOptions } from '../core/displayDensity';
1515
import { IgxSelectionAPIService } from '../core/selection';
16-
import { CancelableEventArgs, IBaseEventArgs, PlatformUtil } from '../core/utils';
16+
import { CancelableEventArgs, IBaseCancelableBrowserEventArgs, IBaseEventArgs, PlatformUtil } from '../core/utils';
1717
import { IgxButtonModule } from '../directives/button/button.directive';
1818
import { IgxForOfModule } from '../directives/for-of/for_of.directive';
1919
import { IgxRippleModule } from '../directives/ripple/ripple.directive';
20-
import { IgxTextSelectionModule } from '../directives/text-selection/text-selection.directive';
20+
import { IgxTextSelectionDirective, IgxTextSelectionModule } from '../directives/text-selection/text-selection.directive';
2121
import { IgxToggleModule } from '../directives/toggle/toggle.directive';
2222
import { IgxDropDownModule } from '../drop-down/public_api';
2323
import { IgxIconModule, IgxIconService } from '../icon/public_api';
@@ -76,6 +76,9 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
7676
@Output()
7777
public selectionChanging = new EventEmitter<ISimpleComboSelectionChangingEventArgs>();
7878

79+
@ViewChild(IgxTextSelectionDirective, { static: true })
80+
private textSelection: IgxTextSelectionDirective;
81+
7982
/** @hidden @internal */
8083
public composing = false;
8184

@@ -163,14 +166,6 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
163166
this.dropdownContainer.nativeElement.focus();
164167
}
165168
});
166-
this.dropdown.closed.pipe(takeUntil(this.destroy$)).subscribe(() => {
167-
this.composing = false;
168-
});
169-
this.dropdown.closing.pipe(takeUntil(this.destroy$)).subscribe(() => {
170-
const selection = this.selectionService.first_item(this.id);
171-
this.comboInput.value = selection !== undefined && selection !== null ? selection : '';
172-
this._onChangeCallback(selection);
173-
});
174169

175170
super.ngAfterViewInit();
176171
}
@@ -183,7 +178,13 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
183178
this.open();
184179
this.dropdown.navigateFirst();
185180
}
181+
if (!this.comboInput.value.trim()) {
182+
// handle clearing of input by space
183+
this.clearSelection();
184+
this._onChangeCallback(null);
185+
}
186186
super.handleInputChange(event);
187+
this.composing = true;
187188
}
188189

189190
/** @hidden @internal */
@@ -197,6 +198,8 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
197198
event.preventDefault();
198199
event.stopPropagation();
199200
this.close();
201+
// manually trigger text selection as it will not be triggered during editing
202+
this.textSelection.trigger();
200203
return;
201204
}
202205
if (event.key === this.platformUtil.KEYMAP.BACKSPACE
@@ -205,10 +208,6 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
205208
this.clearSelection();
206209
}
207210
super.handleKeyDown(event);
208-
this.composing = event.key !== this.platformUtil.KEYMAP.ARROW_DOWN
209-
&& event.key !== this.platformUtil.KEYMAP.ARROW_LEFT
210-
&& event.key !== this.platformUtil.KEYMAP.ARROW_RIGHT
211-
&& event.key !== this.platformUtil.KEYMAP.TAB;
212211
}
213212

214213
/** @hidden @internal */
@@ -250,6 +249,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
250249

251250
this.comboInput.value = this.filterValue = this.searchValue = '';
252251
this.dropdown.focusedItem = null;
252+
this.composing = false;
253253
this.comboInput.focus();
254254
}
255255

@@ -260,6 +260,24 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
260260
this.opened.emit({ owner: this });
261261
}
262262

263+
/** @hidden @internal */
264+
public handleClosing(e: IBaseCancelableBrowserEventArgs) {
265+
const args: IBaseCancelableBrowserEventArgs = { owner: this, event: e.event, cancel: e.cancel };
266+
this.closing.emit(args);
267+
e.cancel = args.cancel;
268+
if (e.cancel) {
269+
return;
270+
}
271+
272+
this.composing = false;
273+
// explicitly update selection so that we don't have to force CD
274+
this.textSelection.selected = true;
275+
const selection = this.selectionService.first_item(this.id);
276+
this._value = selection !== undefined && selection !== null ? selection : '';
277+
this.comboInput.focus();
278+
this._onChangeCallback(selection);
279+
}
280+
263281
/** @hidden @internal */
264282
public focusSearchInput(opening?: boolean): void {
265283
if (opening) {
@@ -302,7 +320,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
302320
: [];
303321
this.selectionService.select_items(this.id, argsSelection, true);
304322
if (this._updateInput) {
305-
this._value = displayText !== args.displayText
323+
this.comboInput.value = this._value = displayText !== args.displayText
306324
? args.displayText
307325
: this.createDisplayText([args.newSelection], [args.oldSelection]);
308326
}

0 commit comments

Comments
 (0)