mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 12:29:55 +08:00
refactor(action-sheet): adds proper TS types
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { Component, ElementRef, HostListener, Renderer, ViewEncapsulation } from '@angular/core';
|
||||
|
||||
import { ActionSheetOptions, ActionSheetButton } from './action-sheet-options';
|
||||
import { assert } from '../../util/util';
|
||||
import { BlockerDelegate, GestureController, BLOCK_ALL } from '../../gestures/gesture-controller';
|
||||
import { Config } from '../../config/config';
|
||||
@ -26,10 +27,10 @@ import { ViewController } from '../../navigation/view-controller';
|
||||
'{{b.text}}' +
|
||||
'</button>' +
|
||||
'</div>' +
|
||||
'<div class="action-sheet-group" *ngIf="d.cancelButton">' +
|
||||
'<button ion-button="action-sheet-button" (click)="click(d.cancelButton)" class="action-sheet-cancel disable-hover" [attr.icon-left]="d.cancelButton.icon ? \'\' : null" [ngClass]="d.cancelButton.cssClass">' +
|
||||
'<ion-icon [name]="d.cancelButton.icon" *ngIf="d.cancelButton.icon" class="action-sheet-icon"></ion-icon>' +
|
||||
'{{d.cancelButton.text}}' +
|
||||
'<div class="action-sheet-group" *ngIf="cancelButton">' +
|
||||
'<button ion-button="action-sheet-button" (click)="click(cancelButton)" class="action-sheet-cancel disable-hover" [attr.icon-left]="cancelButton.icon ? \'\' : null" [ngClass]="cancelButton.cssClass">' +
|
||||
'<ion-icon [name]="cancelButton.icon" *ngIf="cancelButton.icon" class="action-sheet-icon"></ion-icon>' +
|
||||
'{{cancelButton.text}}' +
|
||||
'</button>' +
|
||||
'</div>' +
|
||||
'</div>' +
|
||||
@ -41,15 +42,11 @@ import { ViewController } from '../../navigation/view-controller';
|
||||
},
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
})
|
||||
|
||||
export class ActionSheetCmp {
|
||||
d: {
|
||||
title?: string;
|
||||
subTitle?: string;
|
||||
cssClass?: string;
|
||||
buttons?: Array<any>;
|
||||
enableBackdropDismiss?: boolean;
|
||||
cancelButton: any;
|
||||
};
|
||||
|
||||
d: ActionSheetOptions;
|
||||
cancelButton: ActionSheetButton;
|
||||
descId: string;
|
||||
enabled: boolean;
|
||||
hdrId: string;
|
||||
@ -89,30 +86,26 @@ export class ActionSheetCmp {
|
||||
|
||||
ionViewDidLoad() {
|
||||
// normalize the data
|
||||
let buttons: any[] = [];
|
||||
|
||||
this.d.buttons.forEach((button: any) => {
|
||||
this.d.buttons = this.d.buttons.map(button => {
|
||||
if (typeof button === 'string') {
|
||||
button = { text: button };
|
||||
}
|
||||
if (!button.cssClass) {
|
||||
button.cssClass = '';
|
||||
}
|
||||
|
||||
if (button.role === 'cancel') {
|
||||
this.d.cancelButton = button;
|
||||
|
||||
} else {
|
||||
if (button.role === 'destructive') {
|
||||
switch (button.role) {
|
||||
case 'cancel':
|
||||
this.cancelButton = button;
|
||||
return null;
|
||||
case 'destructive':
|
||||
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive';
|
||||
} else if (button.role === 'selected') {
|
||||
break;
|
||||
case 'selected':
|
||||
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected';
|
||||
break;
|
||||
}
|
||||
buttons.push(button);
|
||||
}
|
||||
});
|
||||
|
||||
this.d.buttons = buttons;
|
||||
return button;
|
||||
}).filter(button => button !== null);
|
||||
}
|
||||
|
||||
ionViewWillEnter() {
|
||||
@ -141,7 +134,7 @@ export class ActionSheetCmp {
|
||||
}
|
||||
}
|
||||
|
||||
click(button: any) {
|
||||
click(button: ActionSheetButton) {
|
||||
if (!this.enabled) {
|
||||
return;
|
||||
}
|
||||
@ -163,8 +156,8 @@ export class ActionSheetCmp {
|
||||
|
||||
bdClick() {
|
||||
if (this.enabled && this.d.enableBackdropDismiss) {
|
||||
if (this.d.cancelButton) {
|
||||
this.click(this.d.cancelButton);
|
||||
if (this.cancelButton) {
|
||||
this.click(this.cancelButton);
|
||||
|
||||
} else {
|
||||
this.dismiss('backdrop');
|
||||
@ -172,7 +165,7 @@ export class ActionSheetCmp {
|
||||
}
|
||||
}
|
||||
|
||||
dismiss(role: any): Promise<any> {
|
||||
dismiss(role: string): Promise<any> {
|
||||
const opts: NavOptions = {
|
||||
minClickBlockDuration: 400
|
||||
};
|
||||
@ -181,7 +174,7 @@ export class ActionSheetCmp {
|
||||
|
||||
ngOnDestroy() {
|
||||
assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
|
||||
this.d = null;
|
||||
this.d = this.cancelButton = null;
|
||||
this.gestureBlocker.destroy();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,14 @@ export interface ActionSheetOptions {
|
||||
title?: string;
|
||||
subTitle?: string;
|
||||
cssClass?: string;
|
||||
buttons?: Array<any>;
|
||||
buttons?: (ActionSheetButton|string)[];
|
||||
enableBackdropDismiss?: boolean;
|
||||
}
|
||||
|
||||
export interface ActionSheetButton {
|
||||
text?: string;
|
||||
role?: string;
|
||||
icon?: string;
|
||||
cssClass?: string;
|
||||
handler?: () => boolean|void;
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ActionSheetCmp } from './action-sheet-component';
|
||||
import { ActionSheetOptions } from './action-sheet-options';
|
||||
import { ActionSheetOptions, ActionSheetButton } from './action-sheet-options';
|
||||
import { ActionSheetSlideIn, ActionSheetMdSlideIn, ActionSheetSlideOut, ActionSheetMdSlideOut, ActionSheetWpSlideIn, ActionSheetWpSlideOut } from './action-sheet-transitions';
|
||||
import { App } from '../app/app';
|
||||
import { Config } from '../../config/config';
|
||||
@ -34,7 +34,7 @@ export class ActionSheet extends ViewController {
|
||||
* @hidden
|
||||
*/
|
||||
getTransitionName(direction: string): string {
|
||||
let key = 'actionSheet' + (direction === 'back' ? 'Leave' : 'Enter');
|
||||
const key = 'actionSheet' + (direction === 'back' ? 'Leave' : 'Enter');
|
||||
return this._nav && this._nav.config.get(key);
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ export class ActionSheet extends ViewController {
|
||||
/**
|
||||
* @param {object} button Action sheet button
|
||||
*/
|
||||
addButton(button: any): ActionSheet {
|
||||
addButton(button: ActionSheetButton|string): ActionSheet {
|
||||
this.data.buttons.push(button);
|
||||
return this;
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ export class AlertCmp {
|
||||
}
|
||||
}
|
||||
|
||||
dismiss(role: any): Promise<any> {
|
||||
dismiss(role: string): Promise<any> {
|
||||
const opts: NavOptions = {
|
||||
minClickBlockDuration: 400
|
||||
};
|
||||
|
@ -158,7 +158,7 @@ export class PageOne {
|
||||
this.testPromptOpen = true;
|
||||
});
|
||||
|
||||
alert.onDidDismiss((data: any, role: any) => {
|
||||
alert.onDidDismiss((data, role) => {
|
||||
console.log('onDidDismiss, data:', data, 'role:', role);
|
||||
});
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ export class LoadingCmp {
|
||||
|
||||
}
|
||||
|
||||
dismiss(role: any): Promise<any> {
|
||||
dismiss(role: string): Promise<any> {
|
||||
if (this.durationTimeout) {
|
||||
clearTimeout(this.durationTimeout);
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ export class PickerCmp {
|
||||
}
|
||||
}
|
||||
|
||||
dismiss(role: any): Promise<any> {
|
||||
dismiss(role: string): Promise<any> {
|
||||
return this._viewCtrl.dismiss(this.getSelected(), role);
|
||||
}
|
||||
|
||||
|
@ -370,7 +370,7 @@ export class Select extends BaseInput<any> implements OnDestroy {
|
||||
|
||||
this._fireFocus();
|
||||
|
||||
overlay.onDidDismiss((value: any, role: string) => {
|
||||
overlay.onDidDismiss(() => {
|
||||
this._fireBlur();
|
||||
this._overlay = undefined;
|
||||
});
|
||||
@ -381,7 +381,7 @@ export class Select extends BaseInput<any> implements OnDestroy {
|
||||
/**
|
||||
* Close the select interface.
|
||||
*/
|
||||
close() {
|
||||
close(): Promise<any> {
|
||||
if (!this._overlay || !this.isFocus()) {
|
||||
return;
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ export class ToastCmp implements AfterViewInit {
|
||||
}
|
||||
}
|
||||
|
||||
dismiss(role: any): Promise<any> {
|
||||
dismiss(role: string): Promise<any> {
|
||||
clearTimeout(this.dismissTimeout);
|
||||
this.dismissTimeout = undefined;
|
||||
return this._viewCtrl.dismiss(null, role, {disableApp: false});
|
||||
|
@ -42,7 +42,7 @@ export class OverlayProxy {
|
||||
}
|
||||
}
|
||||
|
||||
dismiss(data?: any, role?: any, navOptions?: NavOptions): Promise<any> {
|
||||
dismiss(data?: any, role?: string, navOptions?: NavOptions): Promise<any> {
|
||||
if (this.overlay) {
|
||||
return this.overlay.dismiss();
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import { NavOptions } from './nav-util';
|
||||
|
||||
export interface Overlay {
|
||||
present(opts?: NavOptions): Promise<any>;
|
||||
dismiss(data?: any, role?: any, navOptions?: NavOptions): Promise<any>;
|
||||
dismiss(data?: any, role?: string, navOptions?: NavOptions): Promise<any>;
|
||||
onDidDismiss(callback: Function): void;
|
||||
onWillDismiss(callback: Function): void;
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ export class ViewController {
|
||||
* @param {NavOptions} NavOptions Options for the dismiss navigation.
|
||||
* @returns {any} data Returns the data passed in, if any.
|
||||
*/
|
||||
dismiss(data?: any, role?: any, navOptions: NavOptions = {}): Promise<any> {
|
||||
dismiss(data?: any, role?: string, navOptions: NavOptions = {}): Promise<any> {
|
||||
if (!this._nav) {
|
||||
assert(this._state === STATE_DESTROYED, 'ViewController does not have a valid _nav');
|
||||
return Promise.resolve(false);
|
||||
|
@ -591,7 +591,7 @@ export function mockNgModuleLoader(): NgModuleLoader {
|
||||
export function mockOverlay() {
|
||||
return {
|
||||
present: (opts?: NavOptions) => { return Promise.resolve(); },
|
||||
dismiss: (data?: any, role?: any, navOptions?: NavOptions) => { return Promise.resolve(); },
|
||||
dismiss: (data?: any, role?: string, navOptions?: NavOptions) => { return Promise.resolve(); },
|
||||
onDidDismiss: (callback: Function) => { },
|
||||
onWillDismiss: (callback: Function) => { }
|
||||
};
|
||||
|
Reference in New Issue
Block a user