refactor(searchbar): moving logic out of directive and back into searchbar

This commit is contained in:
Brandy Carney
2015-12-18 19:20:01 -05:00
parent df035003f7
commit c40d71ae19
2 changed files with 54 additions and 44 deletions

View File

@ -14,52 +14,18 @@ import {Button} from '../button/button';
selector: '.searchbar-input', selector: '.searchbar-input',
}) })
export class SearchbarInput { export class SearchbarInput {
@HostListener('keyup', ['$event']) @HostListener('input', ['$event'])
/** /**
* @private * @private
* Update the Searchbar input value when the input changes * Update the Searchbar input value when the input changes
*/ */
private inputChanged(ev) { private inputChanged(ev) {
this.writeValue(ev.target.value); ev.preventDefault();
this.onChange(ev.target.value);
} }
constructor(ngControl: NgControl, elementRef: ElementRef) { constructor(elementRef: ElementRef) {
this.elementRef = elementRef; this.elementRef = elementRef;
if (!ngControl) return;
this.ngControl = ngControl;
this.ngControl.valueAccessor = this;
} }
/**
* @private
* Write a new value to the element.
*/
public writeValue(value: any) {
this.value = value;
}
public onChange = (_:any) => {};
public onTouched = () => {};
/**
* @private
* Set the function to be called when the control receives a change event.
*/
public registerOnChange(fn:(_:any) => {}):void {
this.onChange = fn;
}
/**
* @private
* Set the function to be called when the control receives a touch event.
*/
public registerOnTouched(fn:() => {}):void {
this.onTouched = fn;
}
} }
@ -92,7 +58,7 @@ export class SearchbarInput {
'<icon arrow-back></icon>' + '<icon arrow-back></icon>' +
'</button>' + '</button>' +
'<div class="searchbar-search-icon"></div>' + '<div class="searchbar-search-icon"></div>' +
'<input [value]="value" (blur)="inputBlurred()" (focus)="inputFocused()" class="searchbar-input" type="search" [attr.placeholder]="placeholder">' + '<input [value]="value" (keyup)="inputChanged($event)" (blur)="inputBlurred()" (focus)="inputFocused()" class="searchbar-input" type="search" [attr.placeholder]="placeholder">' +
'<button clear *ngIf="value" class="searchbar-clear-icon" (click)="clearInput()" (mousedown)="clearInput()"></button>' + '<button clear *ngIf="value" class="searchbar-clear-icon" (click)="clearInput()" (mousedown)="clearInput()"></button>' +
'</div>' + '</div>' +
'<button clear (click)="cancelSearchbar()" (mousedown)="cancelSearchbar()" [hidden]="hideCancelButton" class="searchbar-ios-cancel">{{cancelButtonText}}</button>', '<button clear (click)="cancelSearchbar()" (mousedown)="cancelSearchbar()" [hidden]="hideCancelButton" class="searchbar-ios-cancel">{{cancelButtonText}}</button>',
@ -110,18 +76,62 @@ export class Searchbar extends Ion {
@Output() cancel: EventEmitter<Searchbar> = new EventEmitter(); @Output() cancel: EventEmitter<Searchbar> = new EventEmitter();
@Output() clear: EventEmitter<Searchbar> = new EventEmitter(); @Output() clear: EventEmitter<Searchbar> = new EventEmitter();
value: string = '';
blurInput = true;
@HostBinding('class.searchbar-focused') isFocused; @HostBinding('class.searchbar-focused') isFocused;
@HostBinding('class.searchbar-left-aligned') shouldLeftAlign; @HostBinding('class.searchbar-left-aligned') shouldLeftAlign;
value: string = ''; @HostListener('keyup', ['$event'])
blurInput = true; /**
* @private
* Update the Searchbar input value when the input changes
*/
private inputChanged(ev) {
this.value = ev.target.value;
this.onChange(this.value);
this.input.emit(this.value);
}
constructor( constructor(
elementRef: ElementRef, elementRef: ElementRef,
config: Config, config: Config,
ngControl: NgControl
) { ) {
super(elementRef, config); super(elementRef, config);
if (ngControl) {
this.ngControl = ngControl;
this.ngControl.valueAccessor = this;
}
}
/**
* @private
* Write a new value to the element.
*/
public writeValue(value: any) {
this.value = value;
}
public onChange = (_:any) => {};
public onTouched = () => {};
/**
* @private
* Set the function to be called when the control receives a change event.
*/
public registerOnChange(fn:(_:any) => {}):void {
this.onChange = fn;
}
/**
* @private
* Set the function to be called when the control receives a touch event.
*/
public registerOnTouched(fn:() => {}):void {
this.onTouched = fn;
} }
/** /**
@ -159,7 +169,6 @@ export class Searchbar extends Ion {
// blurInput determines if it should blur // blurInput determines if it should blur
// if we are clearing the input we still want to stay focused in the input // if we are clearing the input we still want to stay focused in the input
if (this.blurInput == false) { if (this.blurInput == false) {
console.log(this.searchbarInput);
this.searchbarInput.elementRef.nativeElement.focus(); this.searchbarInput.elementRef.nativeElement.focus();
this.blurInput = true; this.blurInput = true;
return; return;
@ -174,6 +183,7 @@ export class Searchbar extends Ion {
*/ */
clearInput() { clearInput() {
this.value = ''; this.value = '';
this.onChange(this.value);
this.blurInput = false; this.blurInput = false;
this.clear.emit(this); this.clear.emit(this);
} }

View File

@ -19,14 +19,14 @@ class E2EApp {
} }
onClearSearchbar(searchbar) { onClearSearchbar(searchbar) {
// console.log("Clicked clear input on", searchbar.value); console.log("Clicked clear input on", searchbar);
} }
onCancelSearchbar(searchbar) { onCancelSearchbar(searchbar) {
console.log("Clicked cancel button with", searchbar.value); console.log("Clicked cancel button with", searchbar);
} }
triggerInput(searchbar) { triggerInput(searchbar) {
// console.log("Triggered input", searchbar.value); console.log("Triggered input", searchbar);
} }
} }