Skip to content

Commit

Permalink
feat: tooltip add animation; fix: autocomplete init position
Browse files Browse the repository at this point in the history
  • Loading branch information
igauch committed May 8, 2023
1 parent 335b946 commit 66ba4d1
Show file tree
Hide file tree
Showing 41 changed files with 491 additions and 196 deletions.
6 changes: 6 additions & 0 deletions .changeset/chatty-dolphins-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@alauda/ui': patch
---

feat: tooltip add animation
fix: autocomplete init position
19 changes: 18 additions & 1 deletion src/autocomplete/autocomplete.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,23 @@ export class AutocompleteComponent implements AfterContentInit {

hasVisibleSuggestion$: Observable<boolean>;
hasContent$: Observable<boolean>;
visibles$: Observable<boolean[]>;

directive$$ = new ReplaySubject<AutoCompleteDirective>(1);

constructor(private readonly cdr: ChangeDetectorRef) {}

ngAfterContentInit() {
this.hasVisibleSuggestion$ = this.suggestions.changes.pipe(
this.visibles$ = this.suggestions.changes.pipe(
startWith(this.suggestions),
switchMap((suggestions: QueryList<SuggestionComponent>) =>
suggestions.length > 0
? combineLatest(suggestions.map(suggestion => suggestion.visible$))
: of([] as boolean[]),
),
debounceTime(0),
);
this.hasVisibleSuggestion$ = this.visibles$.pipe(
map(visible => visible.some(Boolean)),
withLatestFrom(this.directive$$),
map(([hasVisibleSuggestion, directive]) => {
Expand Down Expand Up @@ -105,5 +108,19 @@ export class AutocompleteComponent implements AfterContentInit {
}
}),
);

this.visibles$
.pipe(
distinctUntilChanged(
(prev, cur) => JSON.stringify(prev) === JSON.stringify(cur),
),
)
.subscribe(() => {
this.directive$$.pipe(first()).subscribe(directive => {
window.requestAnimationFrame(() => {
directive.overlayRef?.updatePosition();
});
});
});
}
}
26 changes: 10 additions & 16 deletions src/autocomplete/autocomplete.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@ export class AutoCompleteDirective

declare innerSelector: string;

@Output('auiAutocompleteShow')
override show = new EventEmitter<void>();

@Output('auiAutocompleteHide')
override hide = new EventEmitter<void>();
@Output('auiAutocompleteVisibleChange')
override visibleChange = new EventEmitter<boolean>();

@Output('auiAutocompleteSelected')
selected = new EventEmitter<string>();
Expand Down Expand Up @@ -135,11 +132,8 @@ export class AutoCompleteDirective
}

ngOnInit() {
this.show.subscribe(() => {
this.updateSuggestionsContext();
});
this.hide.subscribe(() => {
this.resetFocusedSuggestion();
this.visibleChange.pipe(takeUntil(this.destroy$)).subscribe(visible => {
this[visible ? 'updateSuggestionsContext' : 'resetFocusedSuggestion']();
});

if (this.ngControl) {
Expand Down Expand Up @@ -196,7 +190,7 @@ export class AutoCompleteDirective
onInput(event: Event) {
const value = (event.target as HTMLInputElement).value;
this.inputValue$$.next(value);
this.createTooltip();
this.show();
}

onKeyDown(event: KeyboardEvent) {
Expand All @@ -219,7 +213,7 @@ export class AutoCompleteDirective
}
break;
case 'Escape':
this.disposeTooltip();
this.hide();
event.stopPropagation();
event.preventDefault();
break;
Expand Down Expand Up @@ -249,11 +243,11 @@ export class AutoCompleteDirective
this.inputValue$$.next(isArrCtrl ? '' : value);

this.selected.emit(value);
this.disposeTooltip();
this.hide();
}

override createTooltip() {
super.createTooltip();
override show() {
super.show();
this.autoFocusFirstSuggestion();
}

Expand Down Expand Up @@ -281,7 +275,7 @@ export class AutoCompleteDirective

private focusSuggestionDir(dir: 'down' | 'up') {
if (!this.isCreated) {
this.createTooltip();
this.show();
return;
}
const visibleSuggestions = this.autocomplete.suggestions.filter(
Expand Down
9 changes: 8 additions & 1 deletion src/date-picker/date-picker/date-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class DatePickerComponent
placeholder: string;

@Output()
openChange = new EventEmitter<boolean>();
visibleChange = new EventEmitter<boolean>();

value: Dayjs;
DatePickerType = DatePickerType;
Expand Down Expand Up @@ -117,4 +117,11 @@ export class DatePickerComponent
this.value = null;
this.emitValue(null);
}

tooltipVisibleChange(visible: boolean) {
this.visibleChange.next(visible);
if (!visible) {
this.emitValue(this.value);
}
}
}
6 changes: 3 additions & 3 deletions src/date-picker/date-picker/date-picker.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[format]="format"
style="display: flex"
[auiTooltip]="template"
[auiDisableAnimation]="true"
auiTooltipType="info"
auiTooltipClass="date-picker-wrapper"
[disabled]="!!disabled"
Expand All @@ -12,8 +13,7 @@
[auiTooltipDisabled]="!!disabled"
[auiTooltipHideOnClick]="true"
[placeholder]="placeholder"
(auiTooltipHide)="openChange.next(false); emitValue(value)"
(auiTooltipShow)="openChange.next(true)"
(auiTooltipVisibleChange)="tooltipVisibleChange($event)"
(clear)="clearValue()"
#tooltip="auiTooltip"
></aui-date-picker-trigger>
Expand All @@ -36,6 +36,6 @@
[maxDate]="maxDate"
(click)="$event.stopImmediatePropagation()"
(clear)="clearValue()"
(confirm)="tooltip.disposeTooltip(); emitValue(value)"
(confirm)="tooltip.hide(); emitValue(value)"
></aui-date-picker-panel>
</ng-template>
11 changes: 9 additions & 2 deletions src/date-picker/range-picker/range-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import dayjs, { ConfigType, Dayjs } from 'dayjs';

import { CommonFormControl } from '../../form/common-form';
import { CommonFormControl } from '../../form';
import { ComponentSize } from '../../types';
import { DisabledDateFn, DisabledTimeFn } from '../date-picker.type';

Expand Down Expand Up @@ -69,7 +69,7 @@ export class RangePickerComponent extends CommonFormControl<
size: ComponentSize;

@Output()
openChange = new EventEmitter<boolean>();
visibleChange = new EventEmitter<boolean>();

value: [Dayjs, Dayjs];

Expand All @@ -87,4 +87,11 @@ export class RangePickerComponent extends CommonFormControl<
this.value = null;
this.emitValue(null);
}

tooltipVisibleChange(visible: boolean) {
this.visibleChange.next(visible);
if (!visible) {
this.emitValue(this.value);
}
}
}
6 changes: 3 additions & 3 deletions src/date-picker/range-picker/range-picker.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[size]="size"
[value]="value"
[auiTooltip]="template"
[auiDisableAnimation]="true"
[disabled]="!!disabled"
style="display: flex"
auiTooltipType="info"
Expand All @@ -12,8 +13,7 @@
auiTooltipTrigger="click"
[auiTooltipHideOnClick]="true"
[auiTooltipDisabled]="!!disabled"
(auiTooltipHide)="openChange.next(false); emitValue(value)"
(auiTooltipShow)="openChange.next(true)"
(auiTooltipVisibleChange)="tooltipVisibleChange($event)"
(clear)="clearValue()"
#tooltip="auiTooltip"
></aui-date-picker-trigger>
Expand All @@ -33,6 +33,6 @@
[maxDate]="maxDate"
(click)="$event.stopImmediatePropagation()"
(clear)="clearValue()"
(confirm)="tooltip.disposeTooltip(); emitValue(value)"
(confirm)="tooltip.hide(); emitValue(value)"
></aui-date-range-picker-panel>
</ng-template>
27 changes: 12 additions & 15 deletions src/dropdown/dropdown.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Renderer2,
ViewContainerRef,
} from '@angular/core';
import { debounceTime } from 'rxjs';
import { takeUntil } from 'rxjs';

import { BaseTooltip, TooltipTrigger, TooltipType } from '../tooltip';

Expand Down Expand Up @@ -54,11 +54,8 @@ export class DropdownDirective extends BaseTooltip implements OnInit {
@Input('auiDropdownHideOnClick')
override hideOnClick = true;

@Output('auiDropdownShow')
override show = new EventEmitter<void>();

@Output('auiDropdownHide')
override hide = new EventEmitter<void>();
@Output('auiDropdownVisibleChange')
override visibleChange = new EventEmitter<boolean>();

private _menu: MenuComponent;

Expand All @@ -78,16 +75,16 @@ export class DropdownDirective extends BaseTooltip implements OnInit {
}

ngOnInit() {
this.show.pipe(debounceTime(0)).subscribe(() => {
if (this.menu.lazyContent) {
this.menu.lazyContent.attach(this.lazyContentContext);
this.updatePosition();
}
});

this.hide.subscribe(() => {
this.visibleChange.pipe(takeUntil(this.destroy$)).subscribe(visible => {
if (this.menu.lazyContent) {
this.menu.lazyContent.detach();
if (visible) {
setTimeout(() => {
this.menu.lazyContent.attach(this.lazyContentContext);
this.updatePosition();
});
} else {
this.menu.lazyContent.detach();
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/dropdown/submenu/submenu.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<button
[class]="className"
[auiTooltip]="menu.template"
[auiDisableAnimation]="true"
[auiTooltipDisabled]="disabled"
[auiTooltipTrigger]="trigger"
[auiTooltipHideOnClick]="true"
Expand Down
4 changes: 2 additions & 2 deletions src/select/base-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ export abstract class BaseSelect<T, V = T>
}

openOption() {
this.tooltipRef.createTooltip();
this.tooltipRef.show();
}

closeOption() {
this.tooltipRef.disposeTooltip();
this.tooltipRef.hide();
}

onShowOptions() {
Expand Down
4 changes: 2 additions & 2 deletions src/select/multi-select/multi-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#selectRef
#tooltipRef="auiTooltip"
[auiTooltip]="templateRef"
[auiDisableAnimation]="true"
[auiTooltipDisabled]="disabled"
auiTooltipTrigger="click"
auiTooltipPosition="bottom start"
auiTooltipType="plain"
(auiTooltipShow)="onShowOptions()"
(auiTooltipHide)="onHideOptions()"
(auiTooltipVisibleChange)="onVisibleOptions($event)"
(mousedown)="$event.preventDefault()"
[style.max-height]="maxHeight"
>
Expand Down
4 changes: 3 additions & 1 deletion src/select/multi-select/multi-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MultiSelectComponent, SelectModule } from '..';
import { ComponentSize } from '../../types';
Expand All @@ -22,7 +23,7 @@ describe('multiSelectComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [SelectModule, FormsModule],
imports: [SelectModule, FormsModule, BrowserAnimationsModule],
declarations: [TestComponent],
});
fixture = TestBed.createComponent(TestComponent);
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('multiSelectComponent', () => {
).toBe(2);
el.dispatchEvent(new Event('click'));
fixture.detectChanges();
tick();
ocEl
.querySelectorAll('.aui-option')
.item(2)
Expand Down
18 changes: 9 additions & 9 deletions src/select/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ export class MultiSelectComponent<T = unknown>
);
}

override onShowOptions() {
super.onShowOptions();
this.inputRef.nativeElement.focus();
}

override onHideOptions() {
super.onHideOptions();
this.inputRef.nativeElement.value = '';
this.renderer.removeStyle(this.inputRef.nativeElement, 'width');
onVisibleOptions(visible: boolean) {
if (visible) {
super.onShowOptions();
this.inputRef.nativeElement.focus();
} else {
super.onHideOptions();
this.inputRef.nativeElement.value = '';
this.renderer.removeStyle(this.inputRef.nativeElement, 'width');
}
}

override onInput(event: Event) {
Expand Down
4 changes: 2 additions & 2 deletions src/select/select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#selectRef
#tooltipRef="auiTooltip"
[auiTooltip]="templateRef"
[auiDisableAnimation]="true"
[auiTooltipDisabled]="disabled"
auiTooltipTrigger="click"
auiTooltipPosition="bottom start"
auiTooltipType="plain"
(auiTooltipShow)="onShowOptions()"
(auiTooltipHide)="onHideOptions()"
(auiTooltipVisibleChange)="onVisibleOptions($event)"
>
<aui-input-group>
<input
Expand Down
3 changes: 2 additions & 1 deletion src/select/select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Component, DebugElement, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { ComponentSize } from '../types';

Expand All @@ -19,7 +20,7 @@ describe('SelectComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [SelectModule, FormsModule],
imports: [SelectModule, FormsModule, BrowserAnimationsModule],
declarations: [TestComponent],
});
fixture = TestBed.createComponent(TestComponent);
Expand Down
Loading

0 comments on commit 66ba4d1

Please sign in to comment.