UNPKG

@ionic/core

Version:
214 lines (213 loc) • 7.69 kB
import { BACKDROP, dismiss, eventMethod, isCancel, present } from '../../utils/overlays'; import { getClassMap } from '../../utils/theme'; import { iosEnterAnimation } from './animations/ios.enter'; import { iosLeaveAnimation } from './animations/ios.leave'; import { mdEnterAnimation } from './animations/md.enter'; import { mdLeaveAnimation } from './animations/md.leave'; export class ActionSheet { constructor() { this.presented = false; this.keyboardClose = true; this.backdropDismiss = true; this.translucent = false; this.animated = true; } onBackdropTap() { this.dismiss(undefined, BACKDROP); } dispatchCancelHandler(ev) { const role = ev.detail.role; if (isCancel(role)) { const cancelButton = this.getButtons().find(b => b.role === 'cancel'); this.callButtonHandler(cancelButton); } } present() { return present(this, 'actionSheetEnter', iosEnterAnimation, mdEnterAnimation); } dismiss(data, role) { return dismiss(this, data, role, 'actionSheetLeave', iosLeaveAnimation, mdLeaveAnimation); } onDidDismiss() { return eventMethod(this.el, 'ionActionSheetDidDismiss'); } onWillDismiss() { return eventMethod(this.el, 'ionActionSheetWillDismiss'); } async buttonClick(button) { const role = button.role; if (isCancel(role)) { return this.dismiss(undefined, role); } const shouldDismiss = await this.callButtonHandler(button); if (shouldDismiss) { return this.dismiss(undefined, button.role); } return Promise.resolve(); } async callButtonHandler(button) { if (button && button.handler) { try { const rtn = await button.handler(); if (rtn === false) { return false; } } catch (e) { console.error(e); } } return true; } getButtons() { return this.buttons.map(b => { return (typeof b === 'string') ? { text: b } : b; }); } hostData() { return { 'role': 'dialog', 'aria-modal': 'true', style: { zIndex: 20000 + this.overlayIndex, }, class: Object.assign({}, getClassMap(this.cssClass), { 'action-sheet-translucent': this.translucent }) }; } render() { const allButtons = this.getButtons(); const cancelButton = allButtons.find(b => b.role === 'cancel'); const buttons = allButtons.filter(b => b.role !== 'cancel'); return [ h("ion-backdrop", { tappable: this.backdropDismiss }), h("div", { class: "action-sheet-wrapper", role: "dialog" }, h("div", { class: "action-sheet-container" }, h("div", { class: "action-sheet-group" }, this.header !== undefined && h("div", { class: "action-sheet-title" }, this.header, this.subHeader && h("div", { class: "action-sheet-sub-title" }, this.subHeader)), buttons.map(b => h("button", { type: "button", "ion-activatable": true, class: buttonClass(b), onClick: () => this.buttonClick(b) }, h("span", { class: "action-sheet-button-inner" }, b.icon && h("ion-icon", { icon: b.icon, lazy: false, class: "action-sheet-icon" }), b.text), this.mode === 'md' && h("ion-ripple-effect", null)))), cancelButton && h("div", { class: "action-sheet-group action-sheet-group-cancel" }, h("button", { type: "button", class: buttonClass(cancelButton), onClick: () => this.buttonClick(cancelButton) }, h("span", { class: "action-sheet-button-inner" }, cancelButton.icon && h("ion-icon", { icon: cancelButton.icon, lazy: false, class: "action-sheet-icon" }), cancelButton.text))))) ]; } static get is() { return "ion-action-sheet"; } static get encapsulation() { return "scoped"; } static get properties() { return { "animated": { "type": Boolean, "attr": "animated" }, "backdropDismiss": { "type": Boolean, "attr": "backdrop-dismiss" }, "buttons": { "type": "Any", "attr": "buttons" }, "config": { "context": "config" }, "cssClass": { "type": String, "attr": "css-class" }, "dismiss": { "method": true }, "el": { "elementRef": true }, "enterAnimation": { "type": "Any", "attr": "enter-animation" }, "header": { "type": String, "attr": "header" }, "keyboardClose": { "type": Boolean, "attr": "keyboard-close" }, "leaveAnimation": { "type": "Any", "attr": "leave-animation" }, "mode": { "type": String, "attr": "mode" }, "onDidDismiss": { "method": true }, "onWillDismiss": { "method": true }, "overlayIndex": { "type": Number, "attr": "overlay-index" }, "present": { "method": true }, "subHeader": { "type": String, "attr": "sub-header" }, "translucent": { "type": Boolean, "attr": "translucent" } }; } static get events() { return [{ "name": "ionActionSheetDidPresent", "method": "didPresent", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionActionSheetWillPresent", "method": "willPresent", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionActionSheetWillDismiss", "method": "willDismiss", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionActionSheetDidDismiss", "method": "didDismiss", "bubbles": true, "cancelable": true, "composed": true }]; } static get listeners() { return [{ "name": "ionBackdropTap", "method": "onBackdropTap" }, { "name": "ionActionSheetWillDismiss", "method": "dispatchCancelHandler" }]; } static get style() { return "/**style-placeholder:ion-action-sheet:**/"; } static get styleMode() { return "/**style-id-placeholder:ion-action-sheet:**/"; } } function buttonClass(button) { return Object.assign({ 'action-sheet-button': true, 'ion-activatable': true, [`action-sheet-${button.role}`]: button.role !== undefined }, getClassMap(button.cssClass)); }