diff --git a/example/app.json b/example/app.json index 84630033..72ebe923 100644 --- a/example/app.json +++ b/example/app.json @@ -81,6 +81,7 @@ "van-col-demo": "./dist/col/demo/index", "van-count-down": "./dist/count-down/index", "van-dialog": "./dist/dialog/index", + "van-dialog-demo": "./dist/dialog/demo/index", "van-divider": "./dist/divider/index", "van-empty": "./dist/empty/index", "van-field": "./dist/field/index", diff --git a/example/pages/dialog/index.js b/example/pages/dialog/index.js index 40f45eb9..cc11dfda 100644 --- a/example/pages/dialog/index.js +++ b/example/pages/dialog/index.js @@ -1,79 +1,3 @@ import Page from '../../common/page'; -import Dialog from '../../dist/dialog/dialog'; -const message = '代码是写出来给人看的,附带能在机器上运行'; - -Page({ - data: { - show: false, - }, - - showCustomDialog() { - this.setData({ show: true }); - }, - - getUserInfo(event) { - console.log(event.detail); - }, - - onClickThemeAlert() { - Dialog.alert({ - title: '标题', - theme: 'round-button', - message, - }); - }, - - onClickThemeAlert2() { - Dialog.alert({ - theme: 'round-button', - message, - }); - }, - - onClickAlert() { - Dialog.alert({ - title: '标题', - message, - }); - }, - - onClickAlert2() { - Dialog.alert({ - message, - }); - }, - - onClickConfirm() { - Dialog.confirm({ - title: '标题', - message, - }); - }, - - onClickAsyncClose() { - const beforeClose = (action) => - new Promise((resolve) => { - setTimeout(() => { - if (action === 'confirm') { - resolve(true); - } else { - // 拦截取消操作 - resolve(false); - } - }, 1000); - }); - - Dialog.confirm({ - title: '标题', - message, - beforeClose, - }); - }, - - onClose() { - this.setData({ - show: false, - }); - }, -}); +Page(); diff --git a/example/pages/dialog/index.wxml b/example/pages/dialog/index.wxml index 14442bf9..6921745b 100644 --- a/example/pages/dialog/index.wxml +++ b/example/pages/dialog/index.wxml @@ -1,35 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/packages/dialog/demo/index.json b/packages/dialog/demo/index.json new file mode 100644 index 00000000..e6e29d32 --- /dev/null +++ b/packages/dialog/demo/index.json @@ -0,0 +1,8 @@ +{ + "component": true, + "usingComponents": { + "van-cell": "../../cell/index", + "van-dialog": "../../dialog/index", + "demo-block": "../../../example/components/demo-block/index" + } +} diff --git a/example/pages/dialog/index.wxss b/packages/dialog/demo/index.less similarity index 100% rename from example/pages/dialog/index.wxss rename to packages/dialog/demo/index.less diff --git a/packages/dialog/demo/index.ts b/packages/dialog/demo/index.ts new file mode 100644 index 00000000..7f80bf70 --- /dev/null +++ b/packages/dialog/demo/index.ts @@ -0,0 +1,87 @@ +import { VantComponent } from '../../common/component'; +import Dialog, { Action } from '../../dialog/dialog'; + +const message = '代码是写出来给人看的,附带能在机器上运行'; + +VantComponent({ + data: { + show: false, + }, + + methods: { + showCustomDialog() { + this.setData({ show: true }); + }, + + getUserInfo(event) { + console.log(event.detail); + }, + + onClickThemeAlert() { + Dialog.alert({ + context: this, + title: '标题', + theme: 'round-button', + message, + }); + }, + + onClickThemeAlert2() { + Dialog.alert({ + context: this, + theme: 'round-button', + message, + }); + }, + + onClickAlert() { + Dialog.alert({ + context: this, + title: '标题', + message, + }); + }, + + onClickAlert2() { + Dialog.alert({ + context: this, + message, + }); + }, + + onClickConfirm() { + Dialog.confirm({ + context: this, + title: '标题', + message, + }); + }, + + onClickAsyncClose() { + const beforeClose = (action: Action): Promise => + new Promise((resolve) => { + setTimeout(() => { + if (action === 'confirm') { + resolve(true); + } else { + // 拦截取消操作 + resolve(false); + } + }, 1000); + }); + + Dialog.confirm({ + context: this, + title: '标题', + message, + beforeClose, + }); + }, + + onClose() { + this.setData({ + show: false, + }); + }, + }, +}); diff --git a/packages/dialog/demo/index.wxml b/packages/dialog/demo/index.wxml new file mode 100644 index 00000000..14442bf9 --- /dev/null +++ b/packages/dialog/demo/index.wxml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/dialog/dialog.ts b/packages/dialog/dialog.ts index 35cc3b47..effa0037 100644 --- a/packages/dialog/dialog.ts +++ b/packages/dialog/dialog.ts @@ -22,7 +22,7 @@ interface DialogOptions { * @deprecated use beforeClose instead */ asyncClose?: boolean; - beforeClose?: null | ((action: Action) => Promise | void); + beforeClose?: null | ((action: Action) => Promise | void); businessId?: number; sessionFrom?: string; overlayStyle?: string; diff --git a/packages/dialog/test/__snapshots__/demo.spec.ts.snap b/packages/dialog/test/__snapshots__/demo.spec.ts.snap new file mode 100644 index 00000000..940ea89a --- /dev/null +++ b/packages/dialog/test/__snapshots__/demo.spec.ts.snap @@ -0,0 +1,298 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render demo and match snapshot 1`] = ` +
+ + + + 提示弹窗 + + + + + + 提示弹窗 + + + + + + + + + + + 提示弹窗(无标题) + + + + + + + + + + + 确认弹窗 + + + + + + + + + + + + + + 圆角按钮样式 + + + + + + 提示弹窗 + + + + + + + + + + + 提示弹窗(无标题) + + + + + + + + + + + + + + 异步关闭 + + + + + + 异步关闭 + + + + + + + + + + + 组件调用 + + + + + + 组件调用 + + + + + + + + + + + + + + + + + + + + + +
+`; diff --git a/packages/dialog/test/demo.spec.ts b/packages/dialog/test/demo.spec.ts new file mode 100644 index 00000000..4c3798cb --- /dev/null +++ b/packages/dialog/test/demo.spec.ts @@ -0,0 +1,11 @@ +import path from 'path'; +import simulate from 'miniprogram-simulate'; + +test('should render demo and match snapshot', () => { + const id = simulate.load(path.resolve(__dirname, '../demo/index'), { + rootPath: path.resolve(__dirname, '../../'), + }); + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + expect(comp.toJSON()).toMatchSnapshot(); +});