From abed621a18e5e4c78e6183f4b9152c8eb92ad459 Mon Sep 17 00:00:00 2001 From: tianjiaxing Date: Thu, 24 Jun 2021 17:15:54 +0800 Subject: [PATCH] fix(App): showModal, showActionSheet --- package.json | 2 +- .../src/protocols/ui/showActionSheet.ts | 1 + .../uni-app-plus/src/service/api/index.ts | 4 +- .../src/service/api/popup/showActionSheet.ts | 67 ---------------- .../src/service/api/popup/showModal.ts | 78 ------------------- .../service/api/ui/popup/showActionSheet.ts | 52 +++++++++++++ .../src/service/api/ui/popup/showModal.ts | 45 +++++++++++ .../src/service/api/ui/popup/actionSheet.tsx | 13 +++- yarn.lock | 8 +- 9 files changed, 115 insertions(+), 155 deletions(-) delete mode 100644 packages/uni-app-plus/src/service/api/popup/showActionSheet.ts delete mode 100644 packages/uni-app-plus/src/service/api/popup/showModal.ts create mode 100644 packages/uni-app-plus/src/service/api/ui/popup/showActionSheet.ts create mode 100644 packages/uni-app-plus/src/service/api/ui/popup/showModal.ts diff --git a/package.json b/package.json index 2d237e03b..4bb6454bb 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "node": ">=10.0.0" }, "devDependencies": { - "@dcloudio/types": "^2.2.13", + "@dcloudio/types": "^2.2.14", "@microsoft/api-extractor": "^7.13.2", "@rollup/plugin-alias": "^3.1.1", "@rollup/plugin-commonjs": "^17.0.0", diff --git a/packages/uni-api/src/protocols/ui/showActionSheet.ts b/packages/uni-api/src/protocols/ui/showActionSheet.ts index 8c0109365..28fe8631f 100644 --- a/packages/uni-api/src/protocols/ui/showActionSheet.ts +++ b/packages/uni-api/src/protocols/ui/showActionSheet.ts @@ -7,6 +7,7 @@ export const ShowActionSheetProtocol: ApiProtocol = required: true, }, title: String, + alertText: String, itemColor: String, popover: Object, } diff --git a/packages/uni-app-plus/src/service/api/index.ts b/packages/uni-app-plus/src/service/api/index.ts index 680892bff..23ed288ad 100644 --- a/packages/uni-app-plus/src/service/api/index.ts +++ b/packages/uni-app-plus/src/service/api/index.ts @@ -24,5 +24,5 @@ export * from './context/getBackgroundAudioManager' export * from './location/getLocation' -export * from './popup/showModal' -export * from './popup/showActionSheet' +export * from './ui/popup/showModal' +export * from './ui/popup/showActionSheet' diff --git a/packages/uni-app-plus/src/service/api/popup/showActionSheet.ts b/packages/uni-app-plus/src/service/api/popup/showActionSheet.ts deleted file mode 100644 index 919df2bce..000000000 --- a/packages/uni-app-plus/src/service/api/popup/showActionSheet.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { - API_TYPE_SHOW_ACTION_SHEET, - API_SHOW_ACTION_SHEET, - ShowActionSheetProtocol, - ShowActionSheetOptions, - defineAsyncApi, -} from '@dcloudio/uni-api' -import { extend } from '@vue/shared' -import { nextTick, reactive } from 'vue' -import { Props } from '../../../../../uni-h5/src/service/api/ui/popup/actionSheet' -import { useI18n } from '@dcloudio/uni-core' - -let showActionSheetState: Props - -export const showActionSheet = defineAsyncApi( - API_SHOW_ACTION_SHEET, - ( - { itemList = [], itemColor = '#000000', title = '', popover }, - { resolve, reject } - ) => { - const { t } = useI18n() - if (!showActionSheetState) { - showActionSheetState = reactive({ - itemList, - itemColor, - title, - popover, - } as Props) - const options = { - title, - cancel: t('uni.showActionSheet.cancel'), - buttons: itemList.map((item) => ({ - title: item, - color: itemColor, - })), - } - if (title) { - options.title = title - } - plus.nativeUI.actionSheet( - extend(options, { - popover, - }), - (e) => { - if (e.index > 0) { - resolve({ - tapIndex: e.index - 1, - }) - } else { - reject('showActionSheet:fail cancel') - } - } - ) - nextTick(() => (showActionSheetState.visible = true)) - } else { - extend(showActionSheetState, { - itemList, - itemColor, - title, - popover, - }) - showActionSheetState.visible = true - } - }, - ShowActionSheetProtocol, - ShowActionSheetOptions -) diff --git a/packages/uni-app-plus/src/service/api/popup/showModal.ts b/packages/uni-app-plus/src/service/api/popup/showModal.ts deleted file mode 100644 index e7f2d1d9b..000000000 --- a/packages/uni-app-plus/src/service/api/popup/showModal.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { extend } from '@vue/shared' -import { nextTick, reactive } from 'vue' -import { - API_SHOW_MODAL, - API_TYPE_SHOW_MODAL, - defineAsyncApi, - ShowModalOptions, - ShowModalProtocol, -} from '@dcloudio/uni-api' - -import { ModalProps } from '../../../../../uni-h5/src/service/api/ui/popup/modal' - -let showModalState: ModalProps - -let currentShowModalResolve: UniApp.ShowModalOptions['success'] - -export const showModal = defineAsyncApi( - API_SHOW_MODAL, - ( - { - title = '', - content = '', - showCancel = true, - cancelText, - cancelColor, - confirmText, - confirmColor, - } = {}, - { resolve } - ) => { - currentShowModalResolve = resolve - if (!showModalState) { - showModalState = reactive({ - title, - content, - showCancel, - cancelText, - cancelColor, - confirmText, - confirmColor, - } as ModalProps) - content = content || ' ' - plus.nativeUI.confirm( - content, - (e) => { - if (showCancel) { - resolve({ - confirm: e.index === 1, - cancel: e.index === 0 || e.index === -1, - }) - } else { - resolve({ - confirm: e.index === 0, - cancel: false, - }) - } - }, - title as PlusNativeUIConfirmStyles, - showCancel ? [cancelText, confirmText] : [confirmText] - ) - //下一帧执行,确保首次显示时有动画效果 - nextTick(() => (showModalState.visible = true)) - } else { - extend(showModalState, { - title, - content, - showCancel, - cancelText, - cancelColor, - confirmText, - confirmColor, - }) - showModalState.visible = true - } - }, - ShowModalProtocol, - ShowModalOptions -) diff --git a/packages/uni-app-plus/src/service/api/ui/popup/showActionSheet.ts b/packages/uni-app-plus/src/service/api/ui/popup/showActionSheet.ts new file mode 100644 index 000000000..e1565b793 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/ui/popup/showActionSheet.ts @@ -0,0 +1,52 @@ +import { + API_TYPE_SHOW_ACTION_SHEET, + API_SHOW_ACTION_SHEET, + ShowActionSheetProtocol, + ShowActionSheetOptions, + defineAsyncApi, +} from '@dcloudio/uni-api' +import { extend } from '@vue/shared' +import { useI18n } from '@dcloudio/uni-core' + +export const showActionSheet = defineAsyncApi( + API_SHOW_ACTION_SHEET, + ( + { + itemList = [], + itemColor = '#000000', + title = '', + alertText = '', + popover, + }, + { resolve, reject } + ) => { + const { t } = useI18n() + const options = { + title, + cancel: t('uni.showActionSheet.cancel'), + buttons: itemList.map((item) => ({ + title: item, + color: itemColor, + })), + } + if (title || alertText) { + options.title = alertText || title + } + plus.nativeUI.actionSheet( + extend(options, { + popover, + }), + (e) => { + if (e.index > 0) { + resolve({ + tapIndex: e.index - 1, + }) + } else { + reject('showActionSheet:fail cancel') + } + } + ) + }, + ShowActionSheetProtocol, + ShowActionSheetOptions +) diff --git a/packages/uni-app-plus/src/service/api/ui/popup/showModal.ts b/packages/uni-app-plus/src/service/api/ui/popup/showModal.ts new file mode 100644 index 000000000..65803f97d --- /dev/null +++ b/packages/uni-app-plus/src/service/api/ui/popup/showModal.ts @@ -0,0 +1,45 @@ +import { + API_SHOW_MODAL, + API_TYPE_SHOW_MODAL, + defineAsyncApi, + ShowModalOptions, + ShowModalProtocol, +} from '@dcloudio/uni-api' + +export const showModal = defineAsyncApi( + API_SHOW_MODAL, + ( + { + title = '', + content = '', + showCancel = true, + cancelText, + cancelColor, + confirmText, + confirmColor, + } = {}, + { resolve } + ) => { + content = content || ' ' + plus.nativeUI.confirm( + content, + (e) => { + if (showCancel) { + resolve({ + confirm: e.index === 1, + cancel: e.index === 0 || e.index === -1, + }) + } else { + resolve({ + confirm: e.index === 0, + cancel: false, + }) + } + }, + title as PlusNativeUIConfirmStyles, + showCancel ? [cancelText, confirmText] : [confirmText] + ) + }, + ShowModalProtocol, + ShowModalOptions +) diff --git a/packages/uni-h5/src/service/api/ui/popup/actionSheet.tsx b/packages/uni-h5/src/service/api/ui/popup/actionSheet.tsx index 55f6b6918..0941bd1bf 100644 --- a/packages/uni-h5/src/service/api/ui/popup/actionSheet.tsx +++ b/packages/uni-h5/src/service/api/ui/popup/actionSheet.tsx @@ -9,6 +9,7 @@ import { watchEffect, nextTick, Transition, + computed, } from 'vue' import { usePopupStyle } from '../../../../helpers/usePopupStyle' import { useKeyboard } from '../../../../helpers/useKeyboard' @@ -29,6 +30,10 @@ const props = { type: String, default: '', }, + alertText: { + type: String, + default: '', + }, itemList: { type: Array, default() { @@ -135,12 +140,14 @@ export default /*#__PURE__*/ defineComponent({ $event.preventDefault() } + const fixTitle = computed(() => props.title || props.alertText) + watch( () => props.visible, () => { nextTick(() => { // title 占位 - if (props.title) { + if (fixTitle.value) { titleHeight.value = ( document.querySelector('.uni-actionsheet__title') as HTMLElement ).offsetHeight @@ -182,13 +189,13 @@ export default /*#__PURE__*/ defineComponent({ onWheel={_handleWheel} > {/* title占位 */} - {props.title ? ( + {fixTitle.value ? ( <>
-
{props.title}
+
{fixTitle.value}
) : ( '' diff --git a/yarn.lock b/yarn.lock index bc86aa6d2..8be75f2b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -357,10 +357,10 @@ exec-sh "^0.3.2" minimist "^1.2.0" -"@dcloudio/types@^2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.2.13.tgz#eecbcde36baf12a869e4c93cc49cc1932734e93c" - integrity sha512-rGalNMnv6XXy9/GDU/FjhkuVZBCYJd1or5j6iQ+Ub1Q+gEQG61MgJjIPFxFNiMxlbMMSq4X+AqqUEfwTuDrgeQ== +"@dcloudio/types@^2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.2.14.tgz#551b3207d7531bc48b0cce9b237b2436fa11c384" + integrity sha512-DondVwxDmO+lopqu8IlC5uAOw11d2+1dbby0oyMZrhAQzRFzBWT2OEGBw9/btK4G85h10hPTondbQa3yi218hA== "@eslint/eslintrc@^0.4.2": version "0.4.2" -- GitLab