Skip to content

Commit

Permalink
fix: tag input not hide placeholder when update (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Guodong Zang <[email protected]>
  • Loading branch information
zangguodong and Guodong Zang authored Jul 14, 2020
1 parent 4b05ac3 commit 5289daa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/input/tags-input/mixin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
position: absolute;
margin-left: 1px;

&[hidden] {
display: none;
Expand Down
5 changes: 4 additions & 1 deletion src/input/tags-input/tags-input.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
(mousedown)="$event.preventDefault()"
(click)="inputRef.focus()"
>
<span [hidden]="!displayPlaceholder" [class]="bem.element('placeholder')">
<span
[hidden]="(value$ | async).length || inputRef.value.length"
[class]="bem.element('placeholder')"
>
{{ placeholder }}
</span>
<aui-tag
Expand Down
8 changes: 5 additions & 3 deletions src/input/tags-input/tags-input.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ describe('TagsInputComponent', () => {
expect(ins.value).toEqual(['service']);
}));

it('should placeholder show or hidden correctly', () => {
it('should placeholder show or hidden correctly', fakeAsync(() => {
ins.placeholder = 'placeholder';
fixture.detectChanges();
const placeholderEl = el.querySelector('.aui-tags-input__placeholder');
expect(placeholderEl.innerHTML).toContain('placeholder');
expect(placeholderEl.getAttribute('hidden')).toBeNull();
el.dispatchEvent(new Event('click'));
ins.value = ['app', 'service'];
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(placeholderEl.getAttribute('hidden')).toBe('');
});
}));
});

@Component({
Expand Down
32 changes: 17 additions & 15 deletions src/input/tags-input/tags-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
const size = this.size || ComponentSize.Medium;
return `aui-input ${this.bem.block(size)} ${
this.disabled ? 'isDisabled' : ''
} ${
this.focused ? 'isFocused' : ''
} ${this.clearable.toString()} ? 'isClearable' : ''`;
} ${this.focused ? 'isFocused' : ''} ${
this.clearable ? 'isClearable' : ''
}`;
}

get tagSize() {
Expand All @@ -97,14 +97,10 @@ export class TagsInputComponent extends CommonFormControl<string[]> {

get inputClass() {
return `${this.bem.element('input', {
hidden: this.disabled || this.displayPlaceholder,
hidden: this.disabled || (!this.snapshot.value.length && !this.focused),
})} aui-tag aui-tag--${this.tagSize}`;
}

get displayPlaceholder() {
return !this.snapshot.value.length && !this.focused;
}

constructor(cdr: ChangeDetectorRef, private readonly renderer: Renderer2) {
super(cdr);
}
Expand All @@ -120,13 +116,19 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
onInput() {
this.inputValue = this.inputRef.nativeElement.value;
// make sure value sync to span element
requestAnimationFrame(() => {
this.renderer.setStyle(
this.inputRef.nativeElement,
'width',
this.inputValueMirror.nativeElement.scrollWidth + 'px',
);
});
if (!this.inputValue.length) {
requestAnimationFrame(() => {
this.renderer.removeStyle(this.inputRef.nativeElement, 'width');
});
} else {
requestAnimationFrame(() => {
this.renderer.setStyle(
this.inputRef.nativeElement,
'width',
this.inputValueMirror.nativeElement.scrollWidth + 'px',
);
});
}
}

onKeyDown(event: KeyboardEvent) {
Expand Down
5 changes: 4 additions & 1 deletion src/select/multi-select/multi-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
[icon]="loading ? 'spinner' : 'caret_down_s'"
></aui-icon>
</div>
<span [hidden]="!displayPlaceholder" [class]="bem.element('placeholder')">
<span
[hidden]="selectedValues.length || inputRef.value.length"
[class]="bem.element('placeholder')"
>
{{ placeholder }}
</span>
<aui-tag
Expand Down
4 changes: 0 additions & 4 deletions src/select/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ export class MultiSelectComponent extends BaseSelect<unknown[]>
})} aui-tag aui-tag--${this.tagSize}`;
}

get displayPlaceholder() {
return !this.selectedValues.length && this.inputReadonly;
}

get displayClearBtn() {
return !this.disabled && this.clearable && this.selectedValues.length;
}
Expand Down

0 comments on commit 5289daa

Please sign in to comment.