perf(): prevent unnecesary event listener changes

This commit is contained in:
Manu Mtz.-Almeida
2018-10-09 16:03:14 -05:00
parent 545db2e4c4
commit a999c1f0a9
9 changed files with 82 additions and 86 deletions

View File

@ -108,20 +108,20 @@ export class Button implements ComponentInterface {
} }
} }
private onFocus() { private onFocus = () => {
this.ionFocus.emit(); this.ionFocus.emit();
} }
private onKeyUp() { private onKeyUp = () => {
this.keyFocus = true; this.keyFocus = true;
} }
private onBlur() { private onBlur = () => {
this.keyFocus = false; this.keyFocus = false;
this.ionBlur.emit(); this.ionBlur.emit();
} }
private onClick(ev: Event) { private onClick = (ev: Event) => {
if (this.type === 'button') { if (this.type === 'button') {
return openURL(this.win, this.href, ev, this.routerDirection); return openURL(this.win, this.href, ev, this.routerDirection);
@ -177,10 +177,10 @@ export class Button implements ComponentInterface {
{...attrs} {...attrs}
class="button-native" class="button-native"
disabled={this.disabled} disabled={this.disabled}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onKeyUp={this.onKeyUp.bind(this)} onKeyUp={this.onKeyUp}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onClick={this.onClick.bind(this)} onClick={this.onClick}
> >
<span class="button-inner"> <span class="button-inner">
<slot name="icon-only"></slot> <slot name="icon-only"></slot>

View File

@ -99,19 +99,19 @@ export class Checkbox implements ComponentInterface {
}); });
} }
private onChange() { private onChange = () => {
this.checked = !this.checked; this.checked = !this.checked;
} }
private onKeyUp() { private onKeyUp = () => {
this.keyFocus = true; this.keyFocus = true;
} }
private onFocus() { private onFocus = () => {
this.ionFocus.emit(); this.ionFocus.emit();
} }
private onBlur() { private onBlur = () => {
this.keyFocus = false; this.keyFocus = false;
this.ionBlur.emit(); this.ionBlur.emit();
} }
@ -140,10 +140,10 @@ export class Checkbox implements ComponentInterface {
type="checkbox" type="checkbox"
id={this.inputId} id={this.inputId}
aria-labelledby={this.labelId} aria-labelledby={this.labelId}
onChange={this.onChange.bind(this)} onChange={this.onChange}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onKeyUp={this.onKeyUp.bind(this)} onKeyUp={this.onKeyUp}
checked={this.checked} checked={this.checked}
name={this.name} name={this.name}
value={this.value} value={this.value}

View File

@ -527,7 +527,6 @@ export class Datetime implements ComponentInterface {
<button <button
type="button" type="button"
aria-haspopup="true" aria-haspopup="true"
// id={this.datetimeId}
aria-labelledby={this.labelId} aria-labelledby={this.labelId}
aria-disabled={this.disabled ? 'true' : null} aria-disabled={this.disabled ? 'true' : null}
onClick={this.open.bind(this)} onClick={this.open.bind(this)}

View File

@ -265,15 +265,15 @@ export class Input implements ComponentInterface {
}); });
} }
private onInput(ev: KeyboardEvent) { private onInput = (ev: Event) => {
const input = ev.target as HTMLInputElement | null; const input = ev.target as HTMLInputElement | null;
if (input) { if (input) {
this.value = input.value || ''; this.value = input.value || '';
} }
this.ionInput.emit(ev); this.ionInput.emit(ev as KeyboardEvent);
} }
private onBlur() { private onBlur = () => {
this.hasFocus = false; this.hasFocus = false;
this.focusChanged(); this.focusChanged();
this.emitStyle(); this.emitStyle();
@ -281,7 +281,7 @@ export class Input implements ComponentInterface {
this.ionBlur.emit(); this.ionBlur.emit();
} }
private onFocus() { private onFocus = () => {
this.hasFocus = true; this.hasFocus = true;
this.focusChanged(); this.focusChanged();
this.emitStyle(); this.emitStyle();
@ -289,17 +289,7 @@ export class Input implements ComponentInterface {
this.ionFocus.emit(); this.ionFocus.emit();
} }
private focusChanged() { private onKeydown = () => {
// If clearOnEdit is enabled and the input blurred but has a value, set a flag
if (this.clearOnEdit && !this.hasFocus && this.hasValue()) {
this.didBlurAfterEdit = true;
}
}
/**
* Check if we need to clear the text input if clearOnEdit is enabled
*/
private onKeydown() {
if (this.clearOnEdit) { if (this.clearOnEdit) {
// Did the input value change after it was blurred and edited? // Did the input value change after it was blurred and edited?
if (this.didBlurAfterEdit && this.hasValue()) { if (this.didBlurAfterEdit && this.hasValue()) {
@ -312,10 +302,17 @@ export class Input implements ComponentInterface {
} }
} }
private clearTextInput() { private clearTextInput = () => {
this.value = ''; this.value = '';
} }
private focusChanged() {
// If clearOnEdit is enabled and the input blurred but has a value, set a flag
if (this.clearOnEdit && !this.hasFocus && this.hasValue()) {
this.didBlurAfterEdit = true;
}
}
private hasValue(): boolean { private hasValue(): boolean {
return this.getValue().length > 0; return this.getValue().length > 0;
} }
@ -362,18 +359,18 @@ export class Input implements ComponentInterface {
size={this.size} size={this.size}
type={this.type} type={this.type}
value={this.getValue()} value={this.getValue()}
onInput={this.onInput.bind(this)} onInput={this.onInput}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onKeyDown={this.onKeydown.bind(this)} onKeyDown={this.onKeydown}
/>, />,
<slot></slot>, <slot></slot>,
(this.clearInput && !this.readonly && !this.disabled) && <button (this.clearInput && !this.readonly && !this.disabled) && <button
type="button" type="button"
class="input-clear-icon" class="input-clear-icon"
tabindex="-1" tabindex="-1"
onTouchStart={this.clearTextInput.bind(this)} onTouchStart={this.clearTextInput}
onMouseDown={this.clearTextInput.bind(this)} onMouseDown={this.clearTextInput}
/> />
]; ];
} }

View File

@ -146,24 +146,24 @@ export class Radio implements ComponentInterface {
}); });
} }
private onClick() { private onClick = () => {
this.checkedChanged(true); this.checkedChanged(true);
} }
private onChange() { private onChange = () => {
this.checked = true; this.checked = true;
this.nativeInput.focus(); this.nativeInput.focus();
} }
private onKeyUp() { private onKeyUp = () => {
this.keyFocus = true; this.keyFocus = true;
} }
private onFocus() { private onFocus = () => {
this.ionFocus.emit(); this.ionFocus.emit();
} }
private onBlur() { private onBlur = () => {
this.keyFocus = false; this.keyFocus = false;
this.ionBlur.emit(); this.ionBlur.emit();
} }
@ -188,11 +188,11 @@ export class Radio implements ComponentInterface {
</div>, </div>,
<input <input
type="radio" type="radio"
onClick={this.onClick.bind(this)} onClick={this.onClick}
onChange={this.onChange.bind(this)} onChange={this.onChange}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onKeyUp={this.onKeyUp.bind(this)} onKeyUp={this.onKeyUp}
id={this.inputId} id={this.inputId}
name={this.name} name={this.name}
value={this.value} value={this.value}

View File

@ -161,7 +161,7 @@ export class Searchbar implements ComponentInterface {
/** /**
* Clears the input field and triggers the control change. * Clears the input field and triggers the control change.
*/ */
private clearInput(ev?: Event) { private onClearInput = (ev?: Event) => {
this.ionClear.emit(); this.ionClear.emit();
if (ev) { if (ev) {
@ -185,13 +185,13 @@ export class Searchbar implements ComponentInterface {
* the clearInput function doesn't want the input to blur * the clearInput function doesn't want the input to blur
* then calls the custom cancel function if the user passed one in. * then calls the custom cancel function if the user passed one in.
*/ */
private cancelSearchbar(ev?: Event) { private onCancelSearchbar = (ev?: Event) => {
if (ev) { if (ev) {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
} }
this.ionCancel.emit(); this.ionCancel.emit();
this.clearInput(); this.onClearInput();
this.nativeInput.blur(); this.nativeInput.blur();
} }
@ -199,19 +199,19 @@ export class Searchbar implements ComponentInterface {
/** /**
* Update the Searchbar input value when the input changes * Update the Searchbar input value when the input changes
*/ */
private onInput(ev: KeyboardEvent) { private onInput = (ev: Event) => {
const input = ev.target as HTMLInputElement | null; const input = ev.target as HTMLInputElement | null;
if (input) { if (input) {
this.value = input.value; this.value = input.value;
} }
this.ionInput.emit(ev); this.ionInput.emit(ev as KeyboardEvent);
} }
/** /**
* Sets the Searchbar to not focused and checks if it should align left * Sets the Searchbar to not focused and checks if it should align left
* based on whether there is a value in the searchbar or not. * based on whether there is a value in the searchbar or not.
*/ */
private onBlur() { private onBlur = () => {
this.focused = false; this.focused = false;
this.ionBlur.emit(); this.ionBlur.emit();
this.positionElements(); this.positionElements();
@ -220,7 +220,7 @@ export class Searchbar implements ComponentInterface {
/** /**
* Sets the Searchbar to focused and active on input focus. * Sets the Searchbar to focused and active on input focus.
*/ */
private onFocus() { private onFocus = () => {
this.focused = true; this.focused = true;
this.ionFocus.emit(); this.ionFocus.emit();
this.positionElements(); this.positionElements();
@ -339,8 +339,8 @@ export class Searchbar implements ComponentInterface {
<button <button
type="button" type="button"
tabIndex={this.mode === 'ios' && !this.focused ? -1 : undefined} tabIndex={this.mode === 'ios' && !this.focused ? -1 : undefined}
onMouseDown={this.cancelSearchbar.bind(this)} onMouseDown={this.onCancelSearchbar}
onTouchStart={this.cancelSearchbar.bind(this)} onTouchStart={this.onCancelSearchbar}
class="searchbar-cancel-button" class="searchbar-cancel-button"
> >
{ this.mode === 'md' { this.mode === 'md'
@ -355,9 +355,9 @@ export class Searchbar implements ComponentInterface {
<input <input
ref={el => this.nativeInput = el as HTMLInputElement} ref={el => this.nativeInput = el as HTMLInputElement}
class="searchbar-input" class="searchbar-input"
onInput={this.onInput.bind(this)} onInput={this.onInput}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
placeholder={this.placeholder} placeholder={this.placeholder}
type={this.type} type={this.type}
value={this.value} value={this.value}
@ -374,8 +374,8 @@ export class Searchbar implements ComponentInterface {
type="button" type="button"
no-blur no-blur
class="searchbar-clear-button" class="searchbar-clear-button"
onMouseDown={this.clearInput.bind(this)} onMouseDown={this.onClearInput}
onTouchStart={this.clearInput.bind(this)} onTouchStart={this.onClearInput}
> >
<ion-icon mode={this.mode} icon={clearIcon} lazy={false} class="searchbar-clear-icon"></ion-icon> <ion-icon mode={this.mode} icon={clearIcon} lazy={false} class="searchbar-clear-icon"></ion-icon>
</button> </button>

View File

@ -425,15 +425,15 @@ export class Select implements ComponentInterface {
return overlay.dismiss(); return overlay.dismiss();
} }
onKeyUp() { private onKeyUp = () => {
this.keyFocus = true; this.keyFocus = true;
} }
onFocus() { private onFocus = () => {
this.ionFocus.emit(); this.ionFocus.emit();
} }
onBlur() { private onBlur = () => {
this.keyFocus = false; this.keyFocus = false;
this.ionBlur.emit(); this.ionBlur.emit();
} }
@ -500,9 +500,9 @@ export class Select implements ComponentInterface {
aria-expanded={this.isExpanded ? 'true' : null} aria-expanded={this.isExpanded ? 'true' : null}
aria-disabled={this.disabled ? 'true' : null} aria-disabled={this.disabled ? 'true' : null}
onClick={this.open.bind(this)} onClick={this.open.bind(this)}
onKeyUp={this.onKeyUp.bind(this)} onKeyUp={this.onKeyUp}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
class="select-cover" class="select-cover"
> >
<slot></slot> <slot></slot>

View File

@ -186,27 +186,27 @@ export class Textarea implements ComponentInterface {
}); });
} }
private onInput(ev: KeyboardEvent) { private onInput = (ev: Event) => {
this.value = this.nativeInput!.value; this.value = this.nativeInput!.value;
this.emitStyle(); this.emitStyle();
this.ionInput.emit(ev); this.ionInput.emit(ev as KeyboardEvent);
} }
private onFocus() { private onFocus = () => {
this.hasFocus = true; this.hasFocus = true;
this.focusChange(); this.focusChange();
this.ionFocus.emit(); this.ionFocus.emit();
} }
private onBlur() { private onBlur = () => {
this.hasFocus = false; this.hasFocus = false;
this.focusChange(); this.focusChange();
this.ionBlur.emit(); this.ionBlur.emit();
} }
private onKeyDown() { private onKeyDown = () => {
this.checkClearOnEdit(); this.checkClearOnEdit();
} }
@ -268,10 +268,10 @@ export class Textarea implements ComponentInterface {
cols={this.cols} cols={this.cols}
rows={this.rows} rows={this.rows}
wrap={this.wrap} wrap={this.wrap}
onInput={this.onInput.bind(this)} onInput={this.onInput}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onKeyDown={this.onKeyDown.bind(this)} onKeyDown={this.onKeyDown}
> >
{this.value} {this.value}
</textarea> </textarea>

View File

@ -154,19 +154,19 @@ export class Toggle implements ComponentInterface {
this.nativeInput.focus(); this.nativeInput.focus();
} }
private onChange() { private onChange = () => {
this.checked = !this.checked; this.checked = !this.checked;
} }
private onKeyUp() { private onKeyUp = () => {
this.keyFocus = true; this.keyFocus = true;
} }
private onFocus() { private onFocus = () => {
this.ionFocus.emit(); this.ionFocus.emit();
} }
private onBlur() { private onBlur = () => {
this.keyFocus = false; this.keyFocus = false;
this.ionBlur.emit(); this.ionBlur.emit();
} }
@ -194,10 +194,10 @@ export class Toggle implements ComponentInterface {
</div>, </div>,
<input <input
type="checkbox" type="checkbox"
onChange={this.onChange.bind(this)} onChange={this.onChange}
onFocus={this.onFocus.bind(this)} onFocus={this.onFocus}
onBlur={this.onBlur.bind(this)} onBlur={this.onBlur}
onKeyUp={this.onKeyUp.bind(this)} onKeyUp={this.onKeyUp}
checked={this.checked} checked={this.checked}
id={this.inputId} id={this.inputId}
name={this.name} name={this.name}