Skip to content

Commit

Permalink
Merge pull request #235 from scshsy/develop
Browse files Browse the repository at this point in the history
fix(dialog): resolve issue #232
  • Loading branch information
LeeJim authored Mar 8, 2022
2 parents 94f872b + f8003b6 commit 3df9d2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
30 changes: 30 additions & 0 deletions example/pages/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ Page({
},
],
},
{
title: '命令调用',
btns: [
{
type: 'commandWithCancel',
text: '带取消按钮',
},
{
type: 'command',
text: '无取消按钮',
},
],
},
],
},

Expand Down Expand Up @@ -205,6 +218,23 @@ Page({
});
return;
}
case 'commandWithCancel': {
Dialog.confirm({
title: '弹窗标题',
content: '告知当前状态、信息和解决方法等内容。',
confirmBtn: '确认按钮',
cancelBtn: '取消按钮',
});
return;
}
case 'command': {
Dialog.confirm({
title: '弹窗标题',
content: '告知当前状态、信息和解决方法等内容。',
confirmBtn: '确认按钮',
});
return;
}
default: {
Dialog.alert({
title: `未知key: ${key}`,
Expand Down
25 changes: 21 additions & 4 deletions src/dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import props from './props';

type Context = WechatMiniprogram.Page.TrivialInstance | WechatMiniprogram.Component.TrivialInstance;

interface DialogAlertOptionsType {
Expand All @@ -9,6 +11,8 @@ interface DialogAlertOptionsType {
asyncClose?: boolean;
confirmButtonText?: string;
textAlign?: string;
cancelBtn?: string | object;
confirmBtn?: string | object;
}

interface DialogComfirmOptionsType extends DialogAlertOptionsType {
Expand All @@ -32,6 +36,19 @@ interface DialogActionOptionsType {
buttonLayout?: 'vertical' | 'horizontal'; // 多按钮排列方式,可选值:horizontal/vertical。
}

const defaultOptions = {
actions: false,
buttonLayout: props.buttonLayout.value,
cancelBtn: props.cancelBtn.value,
closeOnOverlayClick: props.closeOnOverlayClick.value,
confirmBtn: props.confirmBtn.value,
content: '',
preventScrollThrough: props.preventScrollThrough.value,
showOverlay: props.showOverlay.value,
title: '',
visible: props.visible.value,
};

const getDialogInstance = function (context?: Context, selector = '#t-dialog') {
if (!context) {
const pages = getCurrentPages();
Expand All @@ -48,7 +65,7 @@ const getDialogInstance = function (context?: Context, selector = '#t-dialog') {

export default {
alert(options: DialogAlertOptionsType) {
const { context, selector, ...otherOptions } = options;
const { context, selector, ...otherOptions } = { ...defaultOptions, ...options };
const instance = getDialogInstance(context, selector);
if (!instance) return Promise.reject();

Expand All @@ -62,7 +79,7 @@ export default {
});
},
confirm(options: DialogComfirmOptionsType) {
const { context, selector, ...otherOptions } = options;
const { context, selector, ...otherOptions } = { ...defaultOptions, ...options };
const instance = getDialogInstance(context, selector);
if (!instance) return Promise.reject();

Expand All @@ -84,10 +101,10 @@ export default {
return Promise.reject();
},
action(options: DialogActionOptionsType): Promise<{ index: number }> {
const { context, selector, actions, ...otherOptions } = options;
const { context, selector, actions, ...otherOptions } = { ...defaultOptions, ...options };
const instance = getDialogInstance(context, selector);
if (!instance) return Promise.reject();
if (!actions || actions.length === 0 || actions.length > 7) {
if (!actions || (typeof actions === 'object' && (actions.length === 0 || actions.length > 7))) {
console.warn('action 数量建议控制在1至7个');
}

Expand Down

0 comments on commit 3df9d2c

Please sign in to comment.