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

Feat/harmony interactive api #10453

Merged
merged 14 commits into from
Nov 9, 2021
Merged
1 change: 1 addition & 0 deletions packages/taro-harmony/src/apis/apis.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './router'
export * from './storage'
export * from './interactive'
export * from './navbar'
export * from './tabbar'
173 changes: 173 additions & 0 deletions packages/taro-harmony/src/apis/interactive/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { isString, isNumber } from '@tarojs/shared'
import { getParameterError, unsupport } from '../utils'
const prompt = require('@system.prompt')

const noop = () => {}
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved

export function showToast (options) {
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
const _default = {
title: '',
icon: 'success',
image: '',
duration: 1500,
mask: false,
bottom: '100px'
}
options = { ..._default, ...options }

const { title, duration, bottom } = options

if (!isString(title)) {
return console.error(getParameterError({
name: 'showToast',
correct: 'String',
wrong: 'title'
}))
}

if (!isNumber(duration)) {
return console.error(getParameterError({
name: 'showToast',
correct: 'Number',
wrong: 'duration'
}))
}

if (!isString(bottom)) {
return console.error(getParameterError({
name: 'showToast',
correct: 'String',
wrong: 'bottom'
}))
}
return new Promise(resolve => {
prompt.showToast({
message: title,
duration,
bottom
})
resolve(null)
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
})
}

export function showModal (options) {
const _default = {
title: '',
content: '',
showCancel: true,
cancelText: '取消',
cancelColor: '#000000',
confirmText: '确定',
confirmColor: '#3CC51F',
success: noop,
fail: noop,
complete: noop
}
options = { ..._default, ...options }

const {
title, content, success, fail,
cancelText, confirmText, cancelColor, confirmColor
} = options

const buttons: any = []

if (cancelText !== '') {
buttons.push({
text: cancelText,
color: cancelColor
})
}

if (confirmText !== '') {
buttons.push({
text: confirmText,
color: confirmColor
})
}

const modalOptions = {
title,
message: content,
buttons: buttons,
success: (data) => {
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
if (data.index === 1) {
return success({ confirm: true, cancel: null })
} else {
return success({ confirm: null, cancel: true })
}
},
cancel: function () {
fail()
}
}

return new Promise(resolve => {
prompt.showDialog(modalOptions)
resolve(null)
})
}

export function showActionSheet (options) {
const _default = {
title: '',
itemList: [],
itemColor: '#000000',
success: noop,
fail: noop,
complete: noop
}
options = { ..._default, ...options }

const { itemList, itemColor, success, fail } = options

if (!Array.isArray(itemList)) {
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
return console.error(getParameterError({
name: 'showActionSheet',
correct: 'Array',
wrong: 'itemList'
}))
}

const buttons = itemList.map(res => {
return {
text: res,
color: itemColor
}
})

const actionSheetOptions = {
title: 'Title Info',
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
buttons: buttons,
success: function (data) {
success(data)
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
},
fail: function (data) {
fail(data)
}
}

return new Promise(resolve => {
prompt.showActionMenu(actionSheetOptions)
resolve(null)
})
}

export function hideToast () {
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
return new Promise(resolve => {
prompt.showToast({
message: '关闭中',
duration: 10,
bottom: '9999px'
})
resolve(null)
huozhongyi123 marked this conversation as resolved.
Show resolved Hide resolved
})
}

export function showLoading () {
process.env.NODE_ENV !== 'production' && unsupport('showLoading')
}

export function hideLoading () {
process.env.NODE_ENV !== 'production' && unsupport('hideLoading')
}
1 change: 1 addition & 0 deletions packages/taro-harmony/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default class Harmony extends TaroPlatformBase {
externals: {
'@system.app': 'commonjs @system.app',
'@system.router': 'commonjs @system.router',
'@system.prompt': 'commonjs @system.prompt',
'@ohos.data.storage': 'commonjs @ohos.data.storage'
}
})
Expand Down