Skip to content

feat: adding a clear selected button to select option #1488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions projects/components/src/select/select.component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,33 @@ describe('Select Component', () => {
expect(onChange).not.toHaveBeenCalled();
flush();
}));

test('should show clear selected button', fakeAsync(() => {
spectator = hostFactory(
`
<ht-select [selected]="selected">
<ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value" [selectedLabel]="option.selectedLabel">
</ht-select-option>
</ht-select>`,
{
hostProps: {
options: selectionOptions,
selected: selectionOptions[1].value
}
}
);

spectator.tick();
spectator.click('.trigger-content');

const clearSelectedButton = spectator.query('.clear-selected', { root: true });
expect(clearSelectedButton).toExist();
spectator.click(clearSelectedButton!);

spectator.tick();
expect(spectator.component.selected).toBe(undefined);
expect(spectator.query('.clear-selected', { root: true })).not.toExist();

flush();
}));
});
15 changes: 15 additions & 0 deletions projects/components/src/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LoggerService, queryListAndChanges$, SubscriptionLifecycle, TypedSimple
import { isEqual } from 'lodash-es';
import { EMPTY, merge, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ButtonRole, ButtonSize, ButtonStyle } from '../button/button';
import { IconSize } from '../icon/icon-size';
import { SearchBoxDisplayMode } from '../search-box/search-box.component';
import { SelectControlOptionComponent, SelectControlOptionPosition } from './select-control-option.component';
Expand Down Expand Up @@ -134,6 +135,16 @@ import { SelectSize } from './select-size';
</ht-event-blocker>
<ht-divider class="divider"></ht-divider>
</ng-container>
<ht-button
class="clear-selected"
*ngIf="this.selected !== undefined"
role="${ButtonRole.Primary}"
display="${ButtonStyle.Text}"
size="${ButtonSize.ExtraSmall}"
label="Clear Selected"
(click)="this.onClearSelected()"
></ht-button>

<ng-container *htLetAsync="this.topControlItems$ as topControlItems">
<div *ngIf="topControlItems?.length !== 0">
<ng-container
Expand Down Expand Up @@ -311,6 +322,10 @@ export class SelectComponent<V> implements ControlValueAccessor, AfterContentIni
this.propagateValueChangeToFormControl(this.selected);
}

public onClearSelected(): void {
this.setSelection();
}

private setSelection(value?: V): void {
this.selected = value;
this.selected$ = this.buildObservableOfSelected();
Expand Down
4 changes: 3 additions & 1 deletion projects/components/src/select/select.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MemoizeModule } from '@hypertrace/common';
import { ButtonModule } from '../button/button.module';
import { DividerModule } from '../divider/divider.module';
import { EventBlockerModule } from '../event-blocker/event-blocker.module';
import { IconModule } from '../icon/icon.module';
Expand All @@ -28,7 +29,8 @@ import { SelectComponent } from './select.component';
DividerModule,
MemoizeModule,
TraceSearchBoxModule,
EventBlockerModule
EventBlockerModule,
ButtonModule
],
declarations: [
SelectComponent,
Expand Down