Skip to content

Commit

Permalink
fix: empty line when one row filled by tag and optimize #ACP-2796
Browse files Browse the repository at this point in the history
  • Loading branch information
Guodong Zang committed Jul 1, 2020
1 parent 8da8d4d commit 437cfb3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/input/tags-input/mixin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
width: 0;
padding: 0;
margin: 0;
position: absolute;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/select/multi-select/multi-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
(keydown)="onKeyDown($event)"
(input)="onInput($event)"
/>
<span #inputValueMirror [class]="bem.element('mirror')">
{{ inputValue }}
</span>
</div>

<ng-template #templateRef>
Expand Down
48 changes: 35 additions & 13 deletions src/select/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { TagClassFn } from '../select.types';
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
/* eslint-disable no-use-before-define */
providers: [
{
provide: NG_VALUE_ACCESSOR,
Expand Down Expand Up @@ -68,6 +69,11 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
@ViewChild('inputRef', { static: true })
inputRef: ElementRef<HTMLInputElement>;

@ViewChild('inputValueMirror', { static: true })
inputValueMirror: ElementRef<HTMLElement>;

inputValue = '';

get rootClass() {
const size = this.size || ComponentSize.Medium;
return `aui-input ${this.bem.block(size)} ${
Expand Down Expand Up @@ -102,7 +108,7 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
focused = false;
trackByValue = (_: number, item: OptionComponent) => this.trackFn(item.value);

constructor(cdr: ChangeDetectorRef, private readonly renderer: Renderer2) {
constructor(cdr: ChangeDetectorRef, private readonly _renderer: Renderer2) {
super(cdr);
this.values$.pipe(takeUntil(this.destroy$$)).subscribe(values => {
this.selectedValues = values;
Expand Down Expand Up @@ -163,18 +169,12 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
onHideOptions() {
super.onHideOptions();
this.inputRef.nativeElement.value = '';
this.renderer.removeStyle(this.inputRef.nativeElement, 'width');
this._renderer.removeStyle(this.inputRef.nativeElement, 'width');
}

onInput(event: Event) {
super.onInput(event);
// TODO: optimize performance
this.renderer.removeStyle(this.inputRef.nativeElement, 'width');
this.renderer.setStyle(
this.inputRef.nativeElement,
'width',
this.inputRef.nativeElement.scrollWidth + 'px',
);
this._setInputWidth();
this.tooltipRef.updatePosition();
}

Expand All @@ -184,6 +184,7 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>

onInputBlur() {
this.focused = false;
this.inputValue = '';
this.closeOption();
}

Expand Down Expand Up @@ -214,7 +215,7 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>

writeValue(val: any[]) {
this.value$$.next(val || []);
this.resetInput();
this._resetInput();
requestAnimationFrame(() => {
this.tooltipRef.updatePosition();
});
Expand All @@ -238,7 +239,7 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
const values = this.selectedValues.concat(value);
this.emitValueChange(values);
if (this.onChange) {
this.resetInput();
this._resetInput();
requestAnimationFrame(() => {
this.tooltipRef.updatePosition();
});
Expand All @@ -251,7 +252,7 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
);
this.emitValueChange(values);
if (this.onChange) {
this.resetInput();
this._resetInput();
requestAnimationFrame(() => {
this.tooltipRef.updatePosition();
});
Expand All @@ -264,8 +265,29 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
event.preventDefault();
}

private resetInput() {
private _resetInput() {
this.inputRef.nativeElement.value = '';
this._setInputWidth();
this.filterString = '';
}

// calculate input element width according to its value
private _setInputWidth() {
const { value } = this.inputRef.nativeElement;
if (!value.length) {
this.inputValue = '';
requestAnimationFrame(() => {
this._renderer.removeStyle(this.inputRef.nativeElement, 'width');
});
} else {
this.inputValue = value;
requestAnimationFrame(() => {
this._renderer.setStyle(
this.inputRef.nativeElement,
'width',
`${this.inputValueMirror.nativeElement.scrollWidth}px`,
);
});
}
}
}

0 comments on commit 437cfb3

Please sign in to comment.