Skip to content

Feat search empty query #359

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 9 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Thumbs.db

# output files
demo/dist
bundles
bundles
15 changes: 15 additions & 0 deletions demo/src/app/pages/modules/search/search.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { AlertModal } from "../../../modals/alert.modal";
const exampleStandardTemplate = `
<sui-search placeholder="Example Search..."
[hasIcon]="hasIcon"
[allowEmptyQuery]="allowEmptyQuery"
[options]="options"
[searchDelay]="0"
(resultSelected)="alertSelected($event)"></sui-search>

<div class="ui segment">
<sui-checkbox [(ngModel)]="hasIcon">Has icon?</sui-checkbox>
<sui-checkbox [(ngModel)]="allowEmptyQuery">Allow empty query?</sui-checkbox>
</div>
`;

Expand Down Expand Up @@ -57,6 +59,18 @@ export class SearchPage {
description: "Sets whether or not the search displays an icon.",
defaultValue: "true"
},
{
name: "allowEmptyQuery",
type: "boolean",
description: "Sets whether the search element display result with empty query.",
defaultValue: "false"
},
{
name: "resetQueryOnChange",
type: "boolean",
description: "Sets whether the query is reset if options change.",
defaultValue: "true"
},
{
name: "options",
type: "T[]",
Expand Down Expand Up @@ -165,6 +179,7 @@ export class SearchExampleStandard {
"Yellow", "Zebra"];

public hasIcon:boolean = true;
public allowEmptyQuery:boolean = true;

public get options():string[] {
return SearchExampleStandard.standardOptions;
Expand Down
23 changes: 21 additions & 2 deletions src/modules/search/components/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
@Input()
public hasIcon:boolean;

// Sets whether the query is reset if options change.
@Input()
public set resetQueryOnChange(resetQueryOnChange:boolean) {
this.searchService.resetQueryOnChange = resetQueryOnChange;
}

// Sets whether the search element display result with empty query.
@Input()
public set allowEmptyQuery(allowEmptyQuery:boolean) {
this._allowEmptyQuery = allowEmptyQuery;
this.searchService.allowEmptyQuery = allowEmptyQuery;
}
public get allowEmptyQuery():boolean {
return this._allowEmptyQuery;
}

private _allowEmptyQuery:boolean;
private _placeholder:string;

// Gets & sets the placeholder text displayed inside the text input.
Expand Down Expand Up @@ -102,7 +119,7 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
// Initialise a delayed search.
this.searchService.updateQueryDelayed(query, () =>
// Set the results open state depending on whether a query has been entered.
this.dropdownService.setOpenState(this.searchService.query.length > 0));
this.dropdownService.setOpenState(this.searchService.query.length > 0 || this.allowEmptyQuery));
}

@Input()
Expand Down Expand Up @@ -193,6 +210,8 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {

this._searchClasses = true;
this.hasIcon = true;
this.allowEmptyQuery = false;
this.resetQueryOnChange = true;
this.retainSelectedResult = true;
this.searchDelay = 200;
this.maxResults = 7;
Expand Down Expand Up @@ -238,7 +257,7 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
}

private open():void {
if (this.searchService.query.length > 0) {
if (this.searchService.query.length > 0 || this.allowEmptyQuery) {
// Only open on click when there is a query entered.
this.dropdownService.setOpenState(true);
}
Expand Down
10 changes: 9 additions & 1 deletion src/modules/search/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class SearchService<T, U> {
}

private _query:string;
// Allows the query to be empty when the options change.
public resetQueryOnChange:boolean;
// Allows the empty query to produce results.
public allowEmptyQuery:boolean;
// How long to delay the search for when using updateQueryDelayed. Stored in ms.
Expand Down Expand Up @@ -99,6 +101,8 @@ export class SearchService<T, U> {

// Set default values and reset.
this.allowEmptyQuery = allowEmptyQuery;
this._query = "";
this.resetQueryOnChange = true;
this.searchDelay = 0;
this.reset();
}
Expand Down Expand Up @@ -203,6 +207,10 @@ export class SearchService<T, U> {
this._results = [];
this._resultsCache = {};
this._isSearching = false;
this.updateQuery("");
if (this.resetQueryOnChange || !this.query) {
this.updateQuery("");
} else {
this.updateQuery(this.query);
}
}
}