Skip to content

Commit

Permalink
refactor(messageBox): optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
wuls committed Dec 24, 2020
1 parent 2a4cf07 commit 6858619
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 573 deletions.
37 changes: 17 additions & 20 deletions packages/element3/packages/message-box/__tests__/MessageBox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('MessageBox.js', () => {
expect(instanceProprety.message).toBeTruthy()
expect(callback).toHaveBeenCalled()
})
test('alert', async () => {
test('confirm', async () => {
messageBox.confirm({
type: 'warning',
title: '消息',
Expand All @@ -65,26 +65,23 @@ describe('MessageBox.js', () => {
})
test('kind of prompt', async () => {
let v = ''
// const callback = jest.fn(({ value }) => {
// v = value
// })
messageBox
.prompt('请输入邮箱', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
confirmButtonClass: 'mmm',
inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
inputErrorMessage: '邮箱格式不正确'
})
.then(({ action }) => {
v = action
})
await sleep()
const callback = jest.fn(({ value }) => {
v = value
})
const instance = messageBox.prompt('请输入邮箱', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
confirmButtonClass: 'mmm',
inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
inputErrorMessage: '邮箱格式不正确'
})
instance.then(callback)
await nextTick()
const btn = document.querySelector('.mmm')
btn.click()
instance.instance.proxy.inputValue = '[email protected]'
await btn.click()
await sleep()
console.log('>>>>', v)
// await nextTick()
// expect(callback).toHaveBeenCalled()
expect(callback).toHaveBeenCalled()
expect(v).toBeTruthy()
})
})
12 changes: 7 additions & 5 deletions packages/element3/packages/message-box/__tests__/Prop.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import porps from '../src/prop/prop'
import props from '../src/prop/prop'
describe('porps', () => {
test('props', () => {
let o = { title: '23232', type: 'info' }
const b = Object.assign(porps, o)
expect(b.title).toBe('23232')
test('type', () => {
expect(props.type.validator('success')).toBeTruthy()
expect(props.type.validator()).toBeFalsy()
})
test('category', () => {
expect(props.category.validator('alert')).toBeTruthy()
})
})
80 changes: 28 additions & 52 deletions packages/element3/packages/message-box/src/MessageBox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// #todo
import { isVNode } from 'vue'
import { createComponent } from '../../../src/use/component'
import msgboxVue from './MessageBox.vue'
Expand Down Expand Up @@ -82,7 +81,7 @@ const MessageBox = function (options) {
}
}

MessageBox.alert = (message, title, options) => {
const MergeCondition = (message, title, options) => {
if (typeof title === 'object') {
options = title
title = ''
Expand All @@ -93,69 +92,46 @@ MessageBox.alert = (message, title, options) => {
options = message
message = ''
}
return Object.assign(
{
title: title,
message: message,
confirmButtonText: '确认',
cancelButtonText: '取消'
},
options
)
}

MessageBox.alert = (message, title, options) => {
const defaultVal = {
type: null,
category: 'alert'
}
return MessageBox(
Object.assign(
{
type: '',
title: title,
message: message,
category: 'alert',
confirmButtonText: '确认',
cancelButtonText: '取消'
},
options
)
Object.assign(defaultVal, MergeCondition(message, title, options))
)
}

MessageBox.confirm = (message, title, options) => {
if (typeof title === 'object') {
options = title
title = ''
} else if (title === undefined) {
title = ''
}
if (typeof message === 'object') {
options = message
message = ''
const defaultVal = {
type: 'info',
category: 'confirm'
}
return MessageBox(
Object.assign(
{
type: 'info',
title: title,
message: message,
category: 'confirm',
showCancelButton: true
},
options
)
Object.assign(defaultVal, MergeCondition(message, title, options))
)
}

MessageBox.prompt = (message, title, options) => {
if (typeof title === 'object') {
options = title
title = ''
} else if (title === undefined) {
title = ''
}
if (typeof message === 'object') {
options = message
const defaultVal = {
type: null,
showInput: true,
category: 'prompt',
inputErrorMessage: '输入的数据不合法!'
}
return MessageBox(
Object.assign(
{
type: '',
title: title,
message: message,
showCancelButton: true,
showInput: true,
category: 'prompt',
inputErrorMessage: '输入的数据不合法!'
},
options
)
Object.assign(defaultVal, MergeCondition(message, title, options))
)
}

Expand Down
Loading

0 comments on commit 6858619

Please sign in to comment.