UNPKG

taro-hooks

Version:
83 lines 3.23 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } import { showTabBar, hideTabBar, showTabBarRedDot, hideTabBarRedDot, setTabBarStyle, setTabBarBadge, removeTabBarBadge, setTabBarItem } from '@tarojs/taro'; import { useRef } from '@taro-hooks/core'; import { isNumber, isObject } from '@taro-hooks/shared'; import usePromise from '../usePromise'; import useApp from '../useApp'; import { generateGeneralFail } from '../utils/tool'; function useTabBar() { var memoRedDot = useRef({}); var memoVisible = useRef(true); var _useApp = useApp(), app = _useApp.app; var showRedDotAsync = usePromise(showTabBarRedDot); var hideRedDotAsync = usePromise(hideTabBarRedDot); var toggleRedDot = function toggleRedDot(index) { // first is undefined. so need set visible var memoIndexRedDotState = memoRedDot.current[index]; var toggleAction = memoIndexRedDotState ? hideRedDotAsync : showRedDotAsync; return toggleAction({ index: index }).then(function (res) { memoRedDot.current[index] = !memoIndexRedDotState; return res; }); }; var showBadgeAsync = usePromise(setTabBarBadge); var hideBadgeAsync = usePromise(removeTabBarBadge); var toggleBadge = function toggleBadge(index, text) { if (text != null && text.length) return showBadgeAsync({ index: index, text: text }); return hideBadgeAsync({ index: index }); }; var showTabBarAsync = usePromise(showTabBar); var hideTabBarAsync = usePromise(hideTabBar); // must give animation define default value. h5 will error in undefined var toggle = function toggle(animation) { if (animation === void 0) { animation = false; } var toggleAction = memoVisible.current ? hideTabBarAsync : showTabBarAsync; return toggleAction({ animation: animation }).then(function (res) { memoVisible.current = !memoVisible.current; return res; }); }; var setStyle = usePromise(setTabBarStyle); var setItemAsync = usePromise(setTabBarItem); var setItem = function setItem(option, index) { var _app$config, _app$config$tabBar; if (isObject(option) && isNumber(index)) { return setItemAsync(_extends({}, option, { index: index })); } // if not index, set all tabbbaritem var tabbarList = app == null ? void 0 : (_app$config = app.config) == null ? void 0 : (_app$config$tabBar = _app$config.tabBar) == null ? void 0 : _app$config$tabBar.list; if (tabbarList != null && tabbarList.length) { return Promise.all(tabbarList.map(function (v, i) { return setItemAsync(_extends({}, option, { index: i })); })).then(function (_ref) { var res = _ref[0]; return res; }); } return Promise.reject(generateGeneralFail('setItem', 'you must set config')); }; return { toggleRedDot: toggleRedDot, toggleBadge: toggleBadge, setStyle: setStyle, setItem: setItem, toggle: toggle }; } export default useTabBar;