Skip to content

feat(select): Added ability to clear select #412

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions demo/src/app/pages/modules/select/select.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ <h4 class="ui header">Variations</h4>
<example-select-variations result></example-select-variations>
</demo-example>

<demo-example [code]="exampleClearableTemplate">
<div info>
<h4 class="ui header">Clearable select</h4>
<p>A select can have a "clear" icon to reset the selected value. It's useful if you want to use a <code>SuiSelect</code> as filter</p>
</div>
<example-clearable-select result></example-clearable-select>
</demo-example>

<demo-example [code]="exampleInMenuSearchTemplate">
<div info>
<h4 class="ui header">Search In-Menu</h4>
Expand Down
29 changes: 29 additions & 0 deletions demo/src/app/pages/modules/select/select.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ const exampleVariationsTemplate = `
</div>
`;

const exampleClearableTemplate = `
<sui-select class="selection"
[(ngModel)]="selectedOption"
[options]="filters"
[isClearable]="true"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this fix in old version ???

#select>
<sui-select-option *ngFor="let option of select.filteredOptions"
[value]="option">
</sui-select-option>
</sui-select>
`;

const exampleInMenuSearchTemplate = `
<sui-multi-select [(ngModel)]="selected"
[options]="options"
Expand Down Expand Up @@ -221,6 +233,12 @@ export class SelectPage {
description: "Sets whether the multi select is searchable.",
defaultValue: "false"
},
{
name: "isСlearable",
type: "boolean",
description: "Sets whether the select is clearable.",
defaultValue: "false"
},
{
name: "optionFormatter",
type: "(result:T, query?:string) => string",
Expand Down Expand Up @@ -408,6 +426,7 @@ export class SelectPage {
];
public exampleStandardTemplate:string = exampleStandardTemplate;
public exampleVariationsTemplate:string = exampleVariationsTemplate;
public exampleClearableTemplate:string = exampleClearableTemplate;
public exampleInMenuSearchTemplate:string = exampleInMenuSearchTemplate;
public exampleTemplateTemplate:string = exampleTemplateTemplate;
public formatterCode:string = `
Expand Down Expand Up @@ -468,6 +487,15 @@ export class SelectExampleVariations {
public filters:string[] = ["Important", "Announcement", "Discussion"];
}

@Component({
selector: "example-clearable-select",
template: exampleClearableTemplate
})
export class SelectClearableExample {
public selectedOption:string;
public filters:string[] = ["Important", "Announcement", "Discussion"];
}

@Component({
selector: "example-select-in-menu-search",
template: exampleInMenuSearchTemplate
Expand Down Expand Up @@ -520,6 +548,7 @@ export const SelectPageComponents = [

SelectExampleStandard,
SelectExampleVariations,
SelectClearableExample,
SelectExampleInMenuSearch,
SelectExampleTemplate,
SelectExampleLookupSearch
Expand Down
44 changes: 41 additions & 3 deletions src/modules/select/components/select.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ViewContainerRef, ViewChild, Output, EventEmitter, ElementRef, Directive, Input } from "@angular/core";
import { ICustomValueAccessorHost, customValueAccessorFactory, CustomValueAccessor } from "../../../misc/util/internal";
import { ICustomValueAccessorHost, customValueAccessorFactory, CustomValueAccessor, HandledEvent } from "../../../misc/util/internal";
import { SuiLocalizationService } from "../../../behaviors/localization/internal";
import { SuiSelectBase } from "../classes/select-base";
import { SuiSelectOption } from "./select-option";
Expand All @@ -20,7 +20,8 @@ import { SuiSelectOption } from "./select-option";
<span *ngIf="!optionTemplate && selectedOption != undefined" [innerHTML]="configuredFormatter(selectedOption)"></span>
</div>
<!-- Dropdown icon -->
<i class="{{ icon }} icon" (click)="onCaretClick($event)"></i>
<i *ngIf="selectedOption && !isSearching && isClearable" class="remove icon deselect" (click)="onRemoveClick($event)"></i>
<i *ngIf="!selectedOption || !isClearable" class="{{ icon }} icon" (click)="onCaretClick($event)"></i>
<!-- Select dropdown menu -->
<div class="menu"
suiDropdownMenu
Expand All @@ -33,7 +34,31 @@ import { SuiSelectOption } from "./select-option";
{{ localeValues.noResultsMessage }}
</div>
</div>
`
`,
styles: [`
:host .remove.icon.deselect {
position: absolute;
width: auto;
height: auto;
line-height: 1.21428571em;
top: .78571429em;
right: 1em;
margin: -.78571429em;
opacity: .6;
font-size: 1.07142857em;
padding: .6em;
-webkit-transition: opacity .1s ease;
transition: opacity .1s ease;
z-index: 3;
}
:host .remove.icon.deselect.larger {
padding: .91666667em;
font-size: .85714286em;
}
:host .remove.icon.deselect:hover {
opacity: 1;
}
`]
})
export class SuiSelect<T, U> extends SuiSelectBase<T, U> implements ICustomValueAccessorHost<U> {
public selectedOption?:T;
Expand All @@ -57,12 +82,25 @@ export class SuiSelect<T, U> extends SuiSelectBase<T, U> implements ICustomValue
this._placeholder = placeholder;
}

@Input()
public isClearable:boolean;

constructor(element:ElementRef, localizationService:SuiLocalizationService) {
super(element, localizationService);

this.isClearable = false;
this.selectedOptionChange = new EventEmitter<U>();
}

public onRemoveClick(e:HandledEvent):void {
if (!e.eventHandled) {
e.eventHandled = true;
this.selectedOption = undefined;
this.selectedOptionChange.emit(undefined);
this._renderedOptions.forEach(o => o.isActive = false);
}
}

protected optionsUpdateHook():void {
if (!this._writtenOption && this.selectedOption) {
// We need to check the option still exists.
Expand Down