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'
224 changes: 224 additions & 0 deletions packages/taro-harmony/src/apis/interactive/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import { isString, isNumber, isArray } from '@tarojs/shared'
import {
getParameterError, unsupport, noop, callAsyncSuccess
} from '../utils'

const prompt = require('@system.prompt')

const resCallback = (res) => {
return { errMsg: `${res}:ok` }
}

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',
success: noop,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些方法在 callAsyncSuccess 里已经有判空逻辑,不需要默认值

complete: noop
}

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'
}))
}

const toastOptions = {
message: title,
duration,
bottom
}

return new Promise(resolve => {
prompt.showToast(toastOptions)
callAsyncSuccess(resolve, resCallback('showToast'), options)
})
}

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

options = { ..._default, ...options }

const {
title, content, cancelText, confirmText,
cancelColor, confirmColor, showCancel, cancel
} = options

const buttons: any = []

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

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

return new Promise(resolve => {
const modalOptions = {
title,
message: content,
buttons: buttons,
success: (data) => {
if (data.index === 1) {
return resolve(options.success(
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
{
...resCallback('showModal'),
confirm: true,
cancel: false,
content: null
}
))
} else {
return resolve(options.success(
{
...resCallback('showModal'),
confirm: false,
cancel: true
}
))
}
},
// 鸿蒙没有失败方法,只有取消
cancel: (data) => {
return cancel({ errMsg: `showModal:fail ${data}` })
}
}

prompt.showDialog(modalOptions)
})
}

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

options = { ..._default, ...options }

const { title, itemList, itemColor } = options

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

if (!isArray(itemList)) {
return console.error(getParameterError({
name: 'showActionSheet',
correct: 'Array',
wrong: 'itemList'
}))
}

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

return new Promise((resolve) => {
const actionSheetOptions = {
title,
buttons,
success: (data) => {
return resolve(options.success({
...data,
...resCallback('showActionSheet')
}))
},
// 取消方法,并非失败
fail: (data) => {
return resolve(options.fail({
...data,
errMsg: data.errMsg.replace('showActionMenu', 'showActionSheet')
}))
},
complete: (data) => {
return resolve(options.complete({
...data,
errMsg: data.errMsg.replace('showActionMenu', 'showActionSheet')
}))
}
}

prompt.showActionMenu(actionSheetOptions)
})
}

export function hideToast (options) {
const _default = {
success: noop,
complete: noop
}
options = { ..._default, ...options }
return new Promise(resolve => {
prompt.showToast({
message: '关闭中',
duration: 10,
bottom: '9999px'
})
callAsyncSuccess(resolve, resCallback('hideToast'), options)
})
}

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