Skip to content

Commit aacd62b

Browse files
authored
fix(module:tree-select): fix search box exception when Chinese search (#8324)
* fix(module:tree-select): fix search box exception when Chinese search * chore: 添加单元测试 * chore: chore * chore: 优化tree-select测试 * chore: 调整测试 * chore: chore
1 parent 936317e commit aacd62b

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

components/tree-select/tree-select.component.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import {
3333
ViewChild
3434
} from '@angular/core';
3535
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
36-
import { merge, of as observableOf, Subject } from 'rxjs';
37-
import { distinctUntilChanged, filter, map, takeUntil, tap, withLatestFrom } from 'rxjs/operators';
36+
import { combineLatest, merge, of as observableOf, Subject } from 'rxjs';
37+
import { distinctUntilChanged, filter, map, startWith, takeUntil, tap, withLatestFrom } from 'rxjs/operators';
3838

3939
import { slideMotion } from 'ng-zorro-antd/core/animation';
4040
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
@@ -169,7 +169,7 @@ const listOfPositions = [
169169
[nzId]="nzId"
170170
[showInput]="nzShowSearch"
171171
(keydown)="onKeyDownInput($event)"
172-
(isComposingChange)="isComposing = $event"
172+
(isComposingChange)="isComposingChange($event)"
173173
(valueChange)="setInputValue($event)"
174174
[value]="inputValue"
175175
[mirrorSync]="isMultiple"
@@ -352,6 +352,9 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
352352
private destroy$ = new Subject<void>();
353353
private isNzDisableFirstChange: boolean = true;
354354

355+
private isComposingChange$ = new Subject<boolean>();
356+
private searchValueChange$ = new Subject<string>();
357+
355358
onChange: OnChangeType = _value => {};
356359
onTouched: OnTouchedType = () => {};
357360

@@ -419,6 +422,17 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
419422
this.cdr.markForCheck();
420423
}
421424
});
425+
426+
// setInputValue method executed earlier than isComposingChange
427+
combineLatest([this.searchValueChange$, this.isComposingChange$.pipe(startWith(false))])
428+
.pipe(takeUntil(this.destroy$))
429+
.subscribe(([searchValue, isComposing]) => {
430+
this.isComposing = isComposing;
431+
if (!isComposing) {
432+
this.inputValue = searchValue;
433+
this.updatePosition();
434+
}
435+
});
422436
}
423437

424438
ngOnDestroy(): void {
@@ -429,7 +443,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
429443
}
430444

431445
isComposingChange(isComposing: boolean): void {
432-
this.isComposing = isComposing;
446+
this.isComposingChange$.next(isComposing);
433447
}
434448

435449
setDisabledState(isDisabled: boolean): void {
@@ -568,10 +582,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
568582
}
569583

570584
setInputValue(value: string): void {
571-
if (!this.isComposing) {
572-
this.inputValue = value;
573-
this.updatePosition();
574-
}
585+
this.searchValueChange$.next(value);
575586
}
576587

577588
removeSelected(node: NzTreeNode, emit: boolean = true): void {

components/tree-select/tree-select.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ describe('tree-select component', () => {
349349
fixture.detectChanges();
350350
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
351351
}));
352+
353+
it('should isComposing/inputValue is correct', fakeAsync(() => {
354+
treeSelectComponent.inputValue = '';
355+
treeSelectComponent.isComposingChange(true);
356+
treeSelectComponent.setInputValue('1011');
357+
flush();
358+
expect(treeSelectComponent.isComposing).toBe(true);
359+
expect(treeSelectComponent.inputValue).toBe('');
360+
}));
352361
});
353362

354363
describe('checkable', () => {

0 commit comments

Comments
 (0)