diff --git a/src/dialog/Dialog.js b/src/dialog/Dialog.js index 75777458f..379795117 100644 --- a/src/dialog/Dialog.js +++ b/src/dialog/Dialog.js @@ -1,25 +1,32 @@ +import { reactive } from 'vue'; + // Utils import { createNamespace, addUnit, pick } from '../utils'; import { BORDER_TOP, BORDER_LEFT } from '../utils/constant'; // Components -import Popup from '../popup'; +import Popup, { popupSharedProps } from '../popup'; import Button from '../button'; import ActionBar from '../action-bar'; import ActionBarButton from '../action-bar-button'; const [createComponent, bem, t] = createNamespace('dialog'); +const popupKeys = [ + ...Object.keys(popupSharedProps), + 'transition', + 'closeOnPopstate', +]; + export default createComponent({ props: { - show: Boolean, + ...popupSharedProps, title: String, theme: String, width: [Number, String], message: String, - className: null, callback: Function, - lazyRender: Boolean, + className: null, beforeClose: Function, messageAlign: String, showCancelButton: Boolean, @@ -28,10 +35,6 @@ export default createComponent({ confirmButtonText: String, confirmButtonColor: String, closeOnClickOverlay: Boolean, - overlay: { - type: Boolean, - default: true, - }, allowHtml: { type: Boolean, default: true, @@ -52,180 +55,160 @@ export default createComponent({ emits: ['confirm', 'cancel', 'update:show'], - data() { - return { - loading: { - confirm: false, - cancel: false, - }, + setup(props, { emit, slots }) { + const loading = reactive({ + confirm: false, + cancel: false, + }); + + const onUpdateShow = (value) => { + emit('update:show', value); }; - }, - methods: { - onClickOverlay() { - this.handleAction('overlay'); - }, + const close = (action) => { + onUpdateShow(false); + if (props.callback) { + props.callback(action); + } + }; - handleAction(action) { - this.$emit(action); - - // show not trigger close event when hidden - if (!this.show) { + const handleAction = (action) => { + // should not trigger close event when hidden + if (!props.show) { return; } - if (this.beforeClose) { - this.loading[action] = true; - this.beforeClose(action, (state) => { - if (state !== false && this.loading[action]) { - this.onClose(action); - } + emit(action); - this.loading.confirm = false; - this.loading.cancel = false; + if (props.beforeClose) { + loading[action] = true; + props.beforeClose(action, (result) => { + if (result !== false && loading[action]) { + close(action); + } + loading[action] = false; }); } else { - this.onClose(action); + close(action); } - }, - - onClose(action) { - this.onUpdateShow(false); - - if (this.callback) { - this.callback(action); - } - }, - - onUpdateShow(value) { - this.$emit('update:show', value); - }, - - genRoundButtons() { - return ( - - {this.showCancelButton && ( - { - this.handleAction('cancel'); - }} - /> - )} - {this.showConfirmButton && ( - { - this.handleAction('confirm'); - }} - /> - )} - - ); - }, - - genButtons() { - const multiple = this.showCancelButton && this.showConfirmButton; - - return ( -
- {this.showCancelButton && ( -
- ); - }, - - genContent(hasTitle) { - if (this.$slots.default) { - return
{this.$slots.default()}
; - } - - const { message, messageAlign } = this; - if (message) { - const data = { - class: bem('message', { - 'has-title': hasTitle, - [messageAlign]: messageAlign, - }), - [this.allowHtml ? 'innerHTML' : 'textContent']: message, - }; + }; + const renderTitle = () => { + const title = slots.title ? slots.title() : props.title; + if (title) { return ( -
-
+
+ {title}
); } - }, - }, + }; - render() { - const { message } = this; - const title = this.$slots.title ? this.$slots.title() : this.title; - const Title = title && ( -
- {title} + const renderContent = () => { + if (slots.default) { + return
{slots.default()}
; + } + + const { title, message, allowHtml, messageAlign } = props; + if (message) { + return ( +
+
+
+ ); + } + }; + + const renderButtons = () => ( +
+ {props.showCancelButton && ( +
); - return ( - - {Title} - {this.genContent(title)} - {this.theme === 'round-button' - ? this.genRoundButtons() - : this.genButtons()} - + const renderRoundButtons = () => ( + + {props.showCancelButton && ( + { + handleAction('cancel'); + }} + /> + )} + {props.showConfirmButton && ( + { + handleAction('confirm'); + }} + /> + )} + ); + + return () => { + const { width, title, theme, message, className } = props; + return ( + + {renderTitle()} + {renderContent()} + {theme === 'round-button' ? renderRoundButtons() : renderButtons()} + + ); + }; }, }); diff --git a/src/dialog/index.js b/src/dialog/index.js index b51d4f90e..9631bdf5d 100644 --- a/src/dialog/index.js +++ b/src/dialog/index.js @@ -8,15 +8,7 @@ function initInstance() { const Wrapper = { setup() { const { state, toggle } = usePopupState(); - return () => ( - - ); + return () => ; }, };