From b1cf9f6eeb133efed5bd44915e65377470637f8c Mon Sep 17 00:00:00 2001 From: tianjiaxing Date: Wed, 23 Jun 2021 19:34:51 +0800 Subject: [PATCH] feat(App): showActionSheet --- .../src/protocols/ui/showActionSheet.ts | 1 + .../uni-app-plus/src/service/api/index.ts | 1 + .../src/service/api/popup/showActionSheet.ts | 67 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 packages/uni-app-plus/src/service/api/popup/showActionSheet.ts diff --git a/packages/uni-api/src/protocols/ui/showActionSheet.ts b/packages/uni-api/src/protocols/ui/showActionSheet.ts index fee30c342..8c0109365 100644 --- a/packages/uni-api/src/protocols/ui/showActionSheet.ts +++ b/packages/uni-api/src/protocols/ui/showActionSheet.ts @@ -6,6 +6,7 @@ export const ShowActionSheetProtocol: ApiProtocol = type: Array, required: true, }, + title: 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 4aa565e02..cf229b441 100644 --- a/packages/uni-app-plus/src/service/api/index.ts +++ b/packages/uni-app-plus/src/service/api/index.ts @@ -19,3 +19,4 @@ export * from './context/createInnerAudioContext' export * from './location/getLocation' export * from './popup/showModal' +export * from './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 new file mode 100644 index 000000000..919df2bce --- /dev/null +++ b/packages/uni-app-plus/src/service/api/popup/showActionSheet.ts @@ -0,0 +1,67 @@ +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 +) -- GitLab