UNPKG

taro-hooks

Version:
34 lines 1.34 kB
import { showActionSheet } from '@tarojs/taro'; import { useRef, useEffect, useState } from '@taro-hooks/core'; import usePromise from '../usePromise'; import { combineOptions, generateGeneralFail } from '../utils/tool'; function useActionSheet(option) { var generalOption = useRef(option); var _useState = useState(), tapItem = _useState[0], setTapItem = _useState[1]; useEffect(function () { generalOption.current = option; }, [option]); var showActionSheetAsync = usePromise(showActionSheet); var show = function show(option) { if (!option && !generalOption.current) return Promise.reject(generateGeneralFail('showToast', 'please provide a option')); var actionSheetOption = combineOptions(generalOption.current, option); return showActionSheetAsync(actionSheetOption).then(function (_ref) { var _actionSheetOption$it; var tapIndex = _ref.tapIndex; var currentTapItemString = actionSheetOption == null ? void 0 : (_actionSheetOption$it = actionSheetOption.itemList) == null ? void 0 : _actionSheetOption$it[tapIndex]; var currentTapItem = { tapIndex: tapIndex, tapItem: currentTapItemString }; setTapItem(currentTapItem); return currentTapItem; }); }; return { tapItem: tapItem, show: show }; } export default useActionSheet;