Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dialog): resolve issue #232 #235

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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