提交 6b8598d1 编写于 作者: richard_1015's avatar richard_1015

chore: dialog

上级 ecebe956
<template> <template>
<div class="demo"> <div class="demo">
<!-- <nut-cell title="基础弹框" @click="baseClick"></nut-cell> --> <nut-cell title="基础弹框" @click="baseClick"></nut-cell>
<nut-cell title="标签弹框" @click="noTitleClick"></nut-cell> <nut-cell title="无标题弹框" @click="noTitleClick"></nut-cell>
<nut-cell title="提示弹框" @click="tipsClick"></nut-cell>
<nut-cell title="组件调用" @click="componentClick"></nut-cell>
<nut-dialog <nut-dialog
title="标签式使用" title="组件调用"
:close-on-click-overlay="false" content="如果需要在弹窗内嵌入组件或其他自定义内容,可以使用组件调用的方式。"
:content="content"
v-model:visible="visible" v-model:visible="visible"
> >
</nut-dialog> </nut-dialog>
<!-- <template v-slot:header>
</template> -->
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { ref, getCurrentInstance } from 'vue'; import { ref } from 'vue';
import { createComponent } from '@/utils/create'; import { createComponent } from '@/utils/create';
const { createDemo } = createComponent('dialog'); const { createDemo } = createComponent('dialog');
import { Dialog } from '@/nutui';
export default createDemo({ export default createDemo({
props: {},
setup() { setup() {
const { proxy } = getCurrentInstance();
const content = ref(
'模态对话框,在浮层中显示,引导用户进行相关操作,支持图片对话框。'
);
const visible = ref(false); const visible = ref(false);
const baseClick = () => { const onCancel = () => {
proxy.$dialog({ console.log('event cancel');
};
const onOk = () => {
console.log('event ok');
};
const baseClick = (): void => {
Dialog({
title: '基础弹框', title: '基础弹框',
content: '基础弹框内容' content: '支持函数调用和组件调用两种方式。',
onCancel,
onOk
}); });
}; };
const noTitleClick = () => { const noTitleClick = () => {
Dialog({
content: '无标题弹框',
onCancel,
onOk
});
};
const tipsClick = () => {
Dialog({
title: '温馨提示',
content: '支持函数调用和组件调用两种方式。',
onCancel,
onOk
});
};
const componentClick = () => {
visible.value = true; visible.value = true;
}; };
return { return {
content,
visible, visible,
baseClick, baseClick,
noTitleClick noTitleClick,
componentClick,
tipsClick
}; };
} }
}); });
......
...@@ -26,7 +26,7 @@ app.use(Dialog); ...@@ -26,7 +26,7 @@ app.use(Dialog);
``` javascript ``` javascript
import { ref } from 'vue'; import { ref } from 'vue';
import { Dialog } from '@nutui/nutui';
export default { export default {
setup() { setup() {
const visible = ref(true); const visible = ref(true);
...@@ -38,30 +38,60 @@ export default { ...@@ -38,30 +38,60 @@ export default {
}; };
``` ```
## API
| 字段 | 说明 | 类型 | 默认值 |
|---------------------|---------------------------------------|----------|----------|
| title | 标题 | String | - |
| content | 内容,支持HTML | String | - |
| closeOnClickOverlay | 点击蒙层是否关闭对话框 | Boolean | false |
| noFooter | 是否隐藏底部按钮栏 | Boolean | false |
| noOkBtn | 是否隐藏确定按钮 | Boolean | false |
| noCancelBtn | 是否隐藏取消按钮 | Boolean | false |
| cancelText | 取消按钮文案 | String | ”取消“ |
| okText | 确定按钮文案 | String | ”确定“ |
| okBtnDisabled | 禁用确定按钮 | Boolean | false |
| cancelAutoClose | 取消按钮是否默认关闭弹窗 | Boolean | true |
| textAlign | 文字对齐方向,可选值同css的text-align | String | "center" |
| closeOnPopstate | 是否在页面回退时自动关闭 | Boolean | false |
| onUpdate | 更新 | Boolean | false |
| onOk | 确定按钮回调 | Function | - |
| onCancel | 取消按钮回调 | Function | - |
| onOpen | 背景是否锁定 | Function | - |
| onClosed | 关闭回调,任何情况关闭弹窗都会触发 | Function | - |
## Props ## Props
| 字段 | 说明 | 类型 | 默认值 | 字段 | 说明 | 类型 | 默认值 |
|----- | ----- | ----- | ----- |------------------------|---------------------------------------|---------|----------|
| title | 标题 | String | - | title | 标题 | String | - |
| content | 内容,支持HTML | String | - | content | 内容,支持HTML | String | - |
| close-on-click-overlay | 点击蒙层是否关闭对话框 | Boolean | true | close-on-click-overlay | 点击蒙层是否关闭对话框 | Boolean | false |
| noFooter | 是否隐藏底部按钮栏 | Boolean | false | no-footer | 是否隐藏底部按钮栏 | Boolean | false |
| noOkBtn | 是否隐藏确定按钮 | Boolean | false | no-ok-btn | 是否隐藏确定按钮 | Boolean | false |
| noCancelBtn | 是否隐藏取消按钮 | Boolean | false | no-cancel-btn | 是否隐藏取消按钮 | Boolean | false |
| cancelText | 取消按钮文案 | String | ”取消“ | cancel-text | 取消按钮文案 | String | ”取消“ |
| okText | 确定按钮文案 | String | ”确 定“ | ok-text | 确定按钮文案 | String | ”确 定“ |
| okBtnDisabled | 禁用确定按钮 | Boolean | false | ok-btn-disabled | 禁用确定按钮 | Boolean | false |
| cancelAutoClose | 取消按钮是否默认关闭弹窗 | Boolean | true | cancel-auto-close | 取消按钮是否默认关闭弹窗 | Boolean | true |
| textAlign | 文字对齐方向,可选值同css的text-align | String | "center" | text-align | 文字对齐方向,可选值同css的text-align | String | "center" |
| closeOnPopstate | 是否在页面回退时自动关闭 | Boolean | false | close-on-popstate | 是否在页面回退时自动关闭 | Boolean | false |
| lock-scroll | 背景是否锁定 | Boolean | false | lock-scroll | 背景是否锁定 | Boolean | false |
## Events ## Events
| 字段 | 说明 | 类型 | 默认值 | 字段 | 说明 | 类型 | 默认值 |
|----- | ----- | ----- | ----- |--------|------------------------------------|----------|--------|
| ok | 确定按钮回调 | Function | - | ok | 确定按钮回调 | Function | - |
| cancel | 取消按钮回调 | Function | - | cancel | 取消按钮回调 | Function | - |
| open | 关闭按钮回调 | Function | - | closed | 关闭回调,任何情况关闭弹窗都会触发 | Function | - |
| closed | 关闭回调,任何情况关闭弹窗都会触发 | Function | -
\ No newline at end of file
## Slots
| 名称 | 说明 |
|---------|--------------------|
| header | 自定义标题区域 |
| default | 自定义内容 |
| footer | 自定义底部按钮区域 |
\ No newline at end of file
import dialogInstance from './index.vue'; import Dialog from './index.vue';
import { render, createVNode, ref } from 'vue'; import { render, createVNode, h } from 'vue';
export const show = ref(false);
export class DialogOptions { export class DialogOptions {
title: string = ''; title?: string = '';
content: string = ''; content?: string = '';
cancelText: string = '取消'; cancelText?: string = '取消';
okText: string = '确定'; okText?: string = '确定';
textAlign: string = 'center'; textAlign?: string = 'center';
teleport: String | Element = 'body'; teleport?: String = 'body';
// function // function
private onUpdate: Function = (value: boolean) => { onUpdate?: Function = (value: boolean) => {};
show.value = value; onOk?: Function = () => {};
}; onCancel?: Function = () => {};
onOk: Function = () => {}; onClose?: Function = () => {};
onCancel: Function = () => {}; onClosed?: Function = () => {};
onClose: Function = () => {};
onClosed: Function = () => {}; visible?: boolean = true;
noFooter?: boolean = false;
noFooter: boolean = false; noOkBtn?: boolean = false;
noOkBtn: boolean = false; noCancelBtn?: boolean = false;
noCancelBtn: boolean = false; okBtnDisabled?: boolean = false;
okBtnDisabled: boolean = false; closeOnPopstate?: boolean = false;
closeOnPopstate: boolean = false; lockScroll?: boolean = false;
lockScroll: boolean = false;
} }
class Dialog { class DialogFunction {
options: DialogOptions = new DialogOptions(); options: DialogOptions = new DialogOptions();
constructor(_options: DialogOptions) { constructor(_options: DialogOptions) {
Object.assign(this.options, _options); let options = Object.assign(this.options, _options);
show.value = true; const root = document.createElement('view');
const instance: any = createVNode(dialogInstance, this.options as any); root.id = 'dialog-' + new Date().getTime();
render(instance, document.body); const Wrapper = {
setup() {
options.onUpdate = (val: boolean) => {
if (val == false) {
document.body.removeChild(root);
}
};
options.teleport = `#${root.id}`;
return () => {
return h(Dialog, options);
};
}
};
const instance: any = createVNode(Wrapper);
document.body.appendChild(root);
render(instance, root);
} }
close = () => { close = () => {
...@@ -52,10 +65,11 @@ class Dialog { ...@@ -52,10 +65,11 @@ class Dialog {
} }
const _Dialog = function(options: DialogOptions) { const _Dialog = function(options: DialogOptions) {
return new Dialog(options); return new DialogFunction(options);
}; };
_Dialog.install = (app: any) => { _Dialog.install = (app: any) => {
app.use(dialogInstance); app.use(Dialog);
app.config.globalProperties.$dialog = _Dialog; app.config.globalProperties.$dialog = _Dialog;
}; };
export { Dialog };
export default _Dialog; export default _Dialog;
<template> <template>
<nut-popup <nut-popup
name="pop"
:teleport="teleport" :teleport="teleport"
v-model:visible="showPopup" v-model:visible="showPopup"
:close-on-click-overlay="closeOnClickOverlay" :close-on-click-overlay="closeOnClickOverlay"
...@@ -50,11 +49,11 @@ ...@@ -50,11 +49,11 @@
</nut-popup> </nut-popup>
</template> </template>
<script lang="ts"> <script lang="ts">
import { onMounted, computed, watch, onUnmounted, ref, toRefs } from 'vue'; import { onMounted, computed, watch, ref } from 'vue';
import { createComponent } from '@/utils/create'; import { createComponent } from '@/utils/create';
const { componentName, create } = createComponent('dialog'); const { componentName, create } = createComponent('dialog');
import { Button, Popup } from '@/nutui'; import Popup, { popupProps } from '@/packages/popup/index.vue';
import { show } from './index'; import { Button } from '@/nutui';
export default create({ export default create({
inheritAttrs: false, inheritAttrs: false,
children: [Popup, Button], children: [Popup, Button],
...@@ -63,8 +62,8 @@ export default create({ ...@@ -63,8 +62,8 @@ export default create({
'nut-button': Button 'nut-button': Button
}, },
props: { props: {
...Popup.popupProps, ...popupProps,
visible: { closeOnClickOverlay: {
type: Boolean, type: Boolean,
default: false default: false
}, },
...@@ -140,9 +139,7 @@ export default create({ ...@@ -140,9 +139,7 @@ export default create({
'closed' 'closed'
], ],
setup(props, { emit }) { setup(props, { emit }) {
const showPopup = ref(false); const showPopup = ref(props.visible);
showPopup.value = show.value;
onMounted(() => { onMounted(() => {
if (props.closeOnPopstate) { if (props.closeOnPopstate) {
window.addEventListener('popstate', function() { window.addEventListener('popstate', function() {
...@@ -151,10 +148,6 @@ export default create({ ...@@ -151,10 +148,6 @@ export default create({
} }
}); });
watch(show, value => {
showPopup.value = value;
});
watch( watch(
() => props.visible, () => props.visible,
value => { value => {
...@@ -195,7 +188,6 @@ export default create({ ...@@ -195,7 +188,6 @@ export default create({
classes, classes,
onCancel, onCancel,
onOk, onOk,
show,
showPopup showPopup
}; };
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册