File

src/search/search.component.ts

Description

See demo

../../iframe.html?id=search--basic

Implements

ControlValueAccessor

Metadata

providers { provide: NG_VALUE_ACCESSOR, useExisting: Search, multi: true }
selector ibm-search
templateUrl search.component.html

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners
Accessors

Constructor

constructor(elementRef: ElementRef, i18n: I18n)

Creates an instance of Search.

Parameters :
Name Type Optional Description
elementRef ElementRef No
i18n I18n No

The i18n translations.

Inputs

active

Set to true to expand the toolbar search component.

Default value : false

autocomplete

Sets the autocomplete attribute on the input element. For refernece: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#Values

Default value : "on"

clearButtonTitle

Used to set the title attribute of the clear button.

Default value : this.i18n.get().SEARCH.CLEAR_BUTTON

disabled

Set to true for a disabled search input.

Default value : false

id

The unique id for the search component.

Default value : `search-${Search.searchCount}`

label

Sets the text inside the label tag.

Default value : this.i18n.get().SEARCH.LABEL

name

Sets the name attribute on the input element.

Type : string

placeholder

Sets the placeholder attribute on the input element.

Default value : this.i18n.get().SEARCH.PLACEHOLDER

required

Reflects the required attribute of the input element.

Type : boolean

size

Size of the search field.

skeleton

Set to true for a loading search component.

Default value : false

tableSearch

Specifies whether the search component is used in the table toolbar.

Default value : false

theme

light or dark search theme.

Type : "light" | "dark"

Default value : "dark"

toolbar

Set to true for a toolbar search component.

Default value : false

value

Sets the value attribute on the input element.

Default value : ""

Outputs

change $event Type: EventEmitter
clear

Emits an event when the clear button is clicked.

$event Type: EventEmitter
open $event Type: EventEmitter
valueChange

Emits an event when value is changed.

$event Type: EventEmitter

HostBindings

class.bx--form-item
class.bx--form-item:

HostListeners

focusout
Arguments : '$event'
focusout(event)
keydown
Arguments : '$event'
keydown(event: KeyboardEvent)

Methods

clearSearch
clearSearch()

Called when clear button is clicked.

Returns : void
doValueChange
doValueChange()
Returns : void
onSearch
onSearch(search: string)

Called when text is written in the input.

Parameters :
Name Type Optional Description
search string No

The input text.

Returns : void
openSearch
openSearch()
Returns : void
Public registerOnChange
registerOnChange(fn: any)

Sets a method in order to propagate changes back to the form.

Parameters :
Name Type Optional
fn any No
Returns : void
Public registerOnTouched
registerOnTouched(fn: any)

Registers a callback to be triggered when the control has been touched.

Parameters :
Name Type Optional Description
fn any No

Callback to be triggered when the search input is touched.

Returns : void
Public writeValue
writeValue(value: any)

This is the initial value set to the component

Parameters :
Name Type Optional Description
value any No

The input value.

Returns : void

Properties

Protected _size
_size: "sm" | "xl"
Type : "sm" | "xl"
Default value : "xl"
inputRef
inputRef: ElementRef
Type : ElementRef
Decorators :
@ViewChild('input')
onTouched
onTouched: function
Type : function
Default value : () => {}

Called when search input is blurred. Needed to properly implement ControlValueAccessor.

propagateChange
propagateChange:
Default value : (_: any) => {}

Method set in registerOnChange to propagate changes back to the form.

Static searchCount
searchCount: number
Type : number
Default value : 0

Variable used for creating unique ids for search components.

Accessors

size
getsize()
setsize(value)

Size of the search field.

Parameters :
Name Optional
value No
Returns : void
import {
	Component,
	Input,
	EventEmitter,
	Output,
	HostBinding,
	ElementRef,
	HostListener,
	ViewChild
} from "@angular/core";
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
import { I18n } from "../i18n/i18n.module";

/**
 * @deprecated in favor of `valueChange`, to be removed in the next major carbon-components-angular version
 * Used to emit changes performed on search components.
 */
export class SearchChange {
	/**
	 * Contains the `Search` that has been changed.
	 */
	source: Search;
	/**
	 * The value of the `Search` field encompassed in the `SearchChange` class.
	 */
	value: string;
}

/**
 * [See demo](../../?path=/story/search--basic)
 *
 * <example-url>../../iframe.html?id=search--basic</example-url>
 */
@Component({
	selector: "ibm-search",
	templateUrl: "search.component.html",
	providers: [
		{
			provide: NG_VALUE_ACCESSOR,
			useExisting: Search,
			multi: true
		}
	]
})
export class Search implements ControlValueAccessor {
	/**
	 * Variable used for creating unique ids for search components.
	 */
	static searchCount = 0;

	@HostBinding("class.bx--form-item") get containerClass() { return !this.toolbar; }

	/**
	 * `light` or `dark` search theme.
	 */
	@Input() theme: "light" | "dark" = "dark";
	/**
	 * Size of the search field.
	 */
	@Input() set size(value: "sm" | "lg" | "xl") {
		if (value === "lg") {
			console.warn("size `lg` has been deprecated and replaced by `xl`");
			value = "xl";
		}
		this._size = value;
	}

	get size(): "sm" | "lg" | "xl" {
		return this._size;
	}
	/**
	 * Set to `true` for a disabled search input.
	 */
	@Input() disabled = false;
	/**
	 * Set to `true` for a toolbar search component.
	 */
	@Input() toolbar = false;
	/**
	 * Set to `true` for a loading search component.
	 */
	@Input() skeleton = false;
	/**
	 * Set to `true` to expand the toolbar search component.
	 */
	@Input() active = false;
	/**
	 * Specifies whether the search component is used in the table toolbar.
	 */
	@Input() tableSearch = false;
	/**
	 * Sets the name attribute on the `input` element.
	 */
	@Input() name: string;
	/**
	 * The unique id for the search component.
	 */
	@Input() id = `search-${Search.searchCount}`;
	/**
	 * Reflects the required attribute of the `input` element.
	 */
	@Input() required: boolean;
	/**
	 * Sets the value attribute on the `input` element.
	 */
	@Input() value = "";
	/**
	 * Sets the autocomplete attribute on the `input` element.
	 * For refernece: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#Values
	 */
	@Input() autocomplete = "on";
	/**
	 * Sets the text inside the `label` tag.
	 */
	@Input() label = this.i18n.get().SEARCH.LABEL;
	/**
	 * Sets the placeholder attribute on the `input` element.
	 */
	@Input() placeholder = this.i18n.get().SEARCH.PLACEHOLDER;
	/**
	 * Used to set the `title` attribute of the clear button.
	 */
	@Input() clearButtonTitle = this.i18n.get().SEARCH.CLEAR_BUTTON;
	/**
	 * @deprecated in favor of `valueChange`, to be removed in the next major carbon-components-angular version
	 * Emits event notifying other classes when a change in state occurs in the input.
	 */
	@Output() change = new EventEmitter<SearchChange>();
	/**
	 * Emits an event when value is changed.
	 */
	@Output() valueChange = new EventEmitter<string>();
	@Output() open = new EventEmitter<boolean>();
	/**
	 * Emits an event when the clear button is clicked.
	 */
	@Output() clear = new EventEmitter();

	@ViewChild("input") inputRef: ElementRef;

	protected _size: "sm" | "xl" = "xl";

	/**
	 * Creates an instance of `Search`.
	 * @param i18n The i18n translations.
	 */
	constructor(protected elementRef: ElementRef, protected i18n: I18n) {
		Search.searchCount++;
	}

	/**
	 * This is the initial value set to the component
	 * @param value The input value.
	 */
	public writeValue(value: any) {
		this.value = value;
	}

	/**
	 * Sets a method in order to propagate changes back to the form.
	 */
	public registerOnChange(fn: any) {
		this.propagateChange = fn;
	}

	/**
	 * Registers a callback to be triggered when the control has been touched.
	 * @param fn Callback to be triggered when the search input is touched.
	 */
	public registerOnTouched(fn: any) {
		this.onTouched = fn;
	}

	/**
	 * Called when search input is blurred. Needed to properly implement `ControlValueAccessor`.
	 */
	onTouched: () => any = () => {};

	/**
	 * Method set in `registerOnChange` to propagate changes back to the form.
	 */
	propagateChange = (_: any) => {};

	/**
	 * Called when text is written in the input.
	 * @param search The input text.
	 */
	onSearch(search: string) {
		this.value = search;
		this.doValueChange();
	}

	/**
	 * Called when clear button is clicked.
	 */
	clearSearch(): void {
		this.value = "";
		this.clear.emit();
		this.propagateChange(this.value);
	}

	doValueChange() {
		let event = new SearchChange();
		event.source = this;
		event.value = this.value;
		this.change.emit(event);
		this.valueChange.emit(this.value);
		this.propagateChange(this.value);
	}

	openSearch() {
		this.active = true;
		this.open.emit(this.active);
		setTimeout(() => this.inputRef.nativeElement.focus());
	}

	@HostListener("keydown", ["$event"])
	keyDown(event: KeyboardEvent) {
		if (this.toolbar) {
			if (event.key === "Escape") {
				this.active = false;
			} else if (event.key === "Enter") {
				this.openSearch();
			}
		}
	}

	@HostListener("focusout", ["$event"])
	focusOut(event) {
		if (this.toolbar &&
			this.inputRef.nativeElement.value === "" &&
			event.relatedTarget === null) {
			this.active = false;
			this.open.emit(this.active);
		}
	}
}
<div
	class="bx--search"
	[ngClass]="{
		'bx--search--sm': size === 'sm',
		'bx--search--xl': size === 'xl',
		'bx--search--light': theme === 'light',
		'bx--skeleton': skeleton,
		'bx--toolbar-search': toolbar,
		'bx--toolbar-search--active': toolbar && active
	}"
	role="search">
	<label class="bx--label" [for]="id">{{label}}</label>

	<div *ngIf="skeleton; else enableInput" class="bx--search-input"></div>
	<ng-template #enableInput>
		<input
			#input
			*ngIf="!toolbar || active || value !== ''"
			class="bx--search-input"
			[type]="tableSearch || !toolbar ? 'text' : 'search'"
			[id]="id"
			[value]="value"
			[autocomplete]="autocomplete"
			[placeholder]="placeholder"
			[disabled]="disabled"
			[required]="required"
			(input)="onSearch($event.target.value)"/>
		<button *ngIf="!tableSearch && toolbar" class="bx--toolbar-search__btn" (click)="openSearch()">
			<ibm-icon-search16 class="bx--search-magnifier"></ibm-icon-search16>
		</button>
		<ibm-icon-search16 *ngIf="tableSearch || !toolbar" (click)="openSearch()" class="bx--search-magnifier"></ibm-icon-search16>
	</ng-template>

	<button
		*ngIf="tableSearch || !toolbar"
		class="bx--search-close"
		[ngClass]="{
			'bx--search-close--hidden': !value || value.length === 0
		}"
		[title]="clearButtonTitle"
		[attr.aria-label]="clearButtonTitle"
		(click)="clearSearch()">
		<ibm-icon-close16></ibm-icon-close16>
	</button>
</div>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""