mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-23 18:00:27 +08:00
[improvement] Dialog: jsx (#2511)
This commit is contained in:
parent
c68edd388b
commit
53b1942d08
124
packages/dialog/Dialog.js
Normal file
124
packages/dialog/Dialog.js
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
import { use } from '../utils';
|
||||||
|
import Button from '../button';
|
||||||
|
import Popup from '../mixins/popup';
|
||||||
|
|
||||||
|
const [sfc, bem, t] = use('dialog');
|
||||||
|
|
||||||
|
export default sfc({
|
||||||
|
mixins: [Popup],
|
||||||
|
|
||||||
|
props: {
|
||||||
|
title: String,
|
||||||
|
message: String,
|
||||||
|
callback: Function,
|
||||||
|
className: [String, Object, Array],
|
||||||
|
beforeClose: Function,
|
||||||
|
messageAlign: String,
|
||||||
|
confirmButtonText: String,
|
||||||
|
cancelButtonText: String,
|
||||||
|
showCancelButton: Boolean,
|
||||||
|
showConfirmButton: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
|
},
|
||||||
|
overlay: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
|
},
|
||||||
|
closeOnClickOverlay: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
loading: {
|
||||||
|
confirm: false,
|
||||||
|
cancel: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
handleAction(action) {
|
||||||
|
if (this.beforeClose) {
|
||||||
|
this.loading[action] = true;
|
||||||
|
this.beforeClose(action, state => {
|
||||||
|
if (state !== false) {
|
||||||
|
this.onClose(action);
|
||||||
|
}
|
||||||
|
this.loading[action] = false;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.onClose(action);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onClose(action) {
|
||||||
|
this.$emit('input', false);
|
||||||
|
this.$emit(action);
|
||||||
|
this.callback && this.callback(action);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
render(h) {
|
||||||
|
if (!this.shouldRender) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { title, message, messageAlign } = this;
|
||||||
|
const children = this.$slots.default;
|
||||||
|
|
||||||
|
const Title = title && (
|
||||||
|
<div class={bem('header', { isolated: !message && !children })}>{title}</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const Content = (children || message) && (
|
||||||
|
<div class={bem('content')}>
|
||||||
|
{children || (
|
||||||
|
<div
|
||||||
|
domPropsInnerHTML={message}
|
||||||
|
class={bem('message', { 'has-title': title, [messageAlign]: messageAlign })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const hasButtons = this.showCancelButton && this.showConfirmButton;
|
||||||
|
const ButtonGroup = (
|
||||||
|
<div class={['van-hairline--top', bem('footer', { buttons: hasButtons })]}>
|
||||||
|
<Button
|
||||||
|
v-show={this.showCancelButton}
|
||||||
|
size="large"
|
||||||
|
class={bem('cancel')}
|
||||||
|
loading={this.loading.cancel}
|
||||||
|
text={this.cancelButtonText || t('cancel')}
|
||||||
|
onClick={() => {
|
||||||
|
this.handleAction('cancel');
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
v-show={this.showConfirmButton}
|
||||||
|
size="large"
|
||||||
|
class={[bem('confirm'), { 'van-hairline--left': hasButtons }]}
|
||||||
|
loading={this.loading.confirm}
|
||||||
|
text={this.confirmButtonText || t('confirm')}
|
||||||
|
onClick={() => {
|
||||||
|
this.handleAction('confirm');
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<transition name="van-dialog-bounce">
|
||||||
|
<div v-show={this.value} class={[bem(), this.className]}>
|
||||||
|
{Title}
|
||||||
|
{Content}
|
||||||
|
{ButtonGroup}
|
||||||
|
</div>
|
||||||
|
</transition>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
@ -1,119 +0,0 @@
|
|||||||
<template>
|
|
||||||
<transition name="van-dialog-bounce">
|
|
||||||
<div
|
|
||||||
v-if="shouldRender"
|
|
||||||
v-show="value"
|
|
||||||
:class="[b(), className]"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
v-if="title"
|
|
||||||
v-text="title"
|
|
||||||
:class="b('header', { isolated: !message && !$slots.default })"
|
|
||||||
/>
|
|
||||||
<div
|
|
||||||
:class="b('content')"
|
|
||||||
v-if="message || $slots.default"
|
|
||||||
>
|
|
||||||
<slot>
|
|
||||||
<div
|
|
||||||
v-if="message"
|
|
||||||
v-html="message"
|
|
||||||
:class="b('message', { 'has-title': title, [messageAlign]: messageAlign })"
|
|
||||||
/>
|
|
||||||
</slot>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
class="van-hairline--top"
|
|
||||||
:class="b('footer', { 'buttons': showCancelButton && showConfirmButton })"
|
|
||||||
>
|
|
||||||
<van-button
|
|
||||||
v-show="showCancelButton"
|
|
||||||
size="large"
|
|
||||||
:class="b('cancel')"
|
|
||||||
:loading="loading.cancel"
|
|
||||||
:text="cancelButtonText || $t('cancel')"
|
|
||||||
@click="handleAction('cancel')"
|
|
||||||
/>
|
|
||||||
<van-button
|
|
||||||
v-show="showConfirmButton"
|
|
||||||
size="large"
|
|
||||||
:class="[b('confirm'), { 'van-hairline--left': showCancelButton && showConfirmButton }]"
|
|
||||||
:loading="loading.confirm"
|
|
||||||
:text="confirmButtonText || $t('confirm')"
|
|
||||||
@click="handleAction('confirm')"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</transition>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import create from '../utils/create';
|
|
||||||
import VanButton from '../button';
|
|
||||||
import Popup from '../mixins/popup';
|
|
||||||
|
|
||||||
export default create({
|
|
||||||
name: 'dialog',
|
|
||||||
|
|
||||||
components: {
|
|
||||||
VanButton
|
|
||||||
},
|
|
||||||
|
|
||||||
mixins: [Popup],
|
|
||||||
|
|
||||||
props: {
|
|
||||||
title: String,
|
|
||||||
message: String,
|
|
||||||
callback: Function,
|
|
||||||
className: [String, Object, Array],
|
|
||||||
beforeClose: Function,
|
|
||||||
messageAlign: String,
|
|
||||||
confirmButtonText: String,
|
|
||||||
cancelButtonText: String,
|
|
||||||
showCancelButton: Boolean,
|
|
||||||
showConfirmButton: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true
|
|
||||||
},
|
|
||||||
overlay: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true
|
|
||||||
},
|
|
||||||
closeOnClickOverlay: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
loading: {
|
|
||||||
confirm: false,
|
|
||||||
cancel: false
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
handleAction(action) {
|
|
||||||
if (this.beforeClose) {
|
|
||||||
this.loading[action] = true;
|
|
||||||
this.beforeClose(action, state => {
|
|
||||||
if (state !== false) {
|
|
||||||
this.onClose(action);
|
|
||||||
}
|
|
||||||
this.loading[action] = false;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.onClose(action);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onClose(action) {
|
|
||||||
this.$emit('input', false);
|
|
||||||
this.$emit(action);
|
|
||||||
this.callback && this.callback(action);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
@ -14,7 +14,6 @@ exports[`renders demo correctly 1`] = `
|
|||||||
高级用法
|
高级用法
|
||||||
</span></button>
|
</span></button>
|
||||||
<div name="van-dialog-bounce" class="van-dialog" style="display:none;">
|
<div name="van-dialog-bounce" class="van-dialog" style="display:none;">
|
||||||
<!---->
|
|
||||||
<div class="van-dialog__content">
|
<div class="van-dialog__content">
|
||||||
<div placeholder="请输入用户名" class="van-cell van-field">
|
<div placeholder="请输入用户名" class="van-cell van-field">
|
||||||
<div class="van-cell__title"><span>用户名</span></div>
|
<div class="van-cell__title"><span>用户名</span></div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user