UNPKG

vantui-edit

Version:

一套适用于Taro3及React的vantui组件库

136 lines (124 loc) 5.23 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["active", "activeColor", "inactiveColor", "border", "fixed", "safeAreaInsetBottom", "zIndex", "placeholder", "onChange", "style", "className", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import { nextTick } from '@tarojs/taro'; import { useState, isValidElement, cloneElement, useEffect, useCallback, useMemo } from 'react'; import toArray from 'rc-util/lib/Children/toArray'; import { View } from '@tarojs/components'; import * as utils from '../wxs/utils'; import { getRect } from '../common/utils'; import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; function parseTabList(children) { return toArray(children).map(function (node) { if ( /*#__PURE__*/isValidElement(node)) { var key = node.key !== undefined ? String(node.key) : undefined; return _objectSpread(_objectSpread({ key: key }, node.props), {}, { node: node }); } return null; }).filter(function (tab) { return tab; }); } export function Tabbar(props) { var _useState = useState({ height: 50, current: 0 }), _useState2 = _slicedToArray(_useState, 2), state = _useState2[0], setState = _useState2[1]; var height = state.height, current = state.current; var active = props.active, activeColor = props.activeColor, inactiveColor = props.inactiveColor, _props$border = props.border, border = _props$border === void 0 ? true : _props$border, _props$fixed = props.fixed, fixed = _props$fixed === void 0 ? true : _props$fixed, _props$safeAreaInsetB = props.safeAreaInsetBottom, safeAreaInsetBottom = _props$safeAreaInsetB === void 0 ? true : _props$safeAreaInsetB, zIndex = props.zIndex, placeholder = props.placeholder, onChange = props.onChange, style = props.style, className = props.className, children = props.children, others = _objectWithoutProperties(props, _excluded); var _change = useCallback(function (data) { setState(function (pre) { return _objectSpread(_objectSpread({}, pre), {}, { current: data }); }); onChange === null || onChange === void 0 ? void 0 : onChange({ detail: data }); }, [onChange]); var newChildren = useMemo(function () { var tabs = parseTabList(children); return tabs.map(function (tab, index) { return /*#__PURE__*/cloneElement(tab.node, { key: index, index: index, active: current, activeColor: activeColor, inactiveColor: inactiveColor, onChange: _change }); }); }, [children, current, activeColor, inactiveColor, _change]); useEffect(function () { setState(function (pre) { return _objectSpread(_objectSpread({}, pre), {}, { current: active }); }); }, [active]); useEffect(function () { if (!fixed || !placeholder) { return; } nextTick(function () { getRect(null, '.van-tabbar').then(function (res) { setState(function (pre) { return _objectSpread(_objectSpread({}, pre), {}, { height: res.height }); }); }); }); }, [fixed, placeholder]); // useEffect( // function () { // if (!Array.isArray(children) || !children.length) { // return // } // children.forEach((child) => child.updateFromParent()) // }, // // eslint-disable-next-line react-hooks/exhaustive-deps // [active, activeColor, inactiveColor], // ) return /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(View, _objectSpread(_objectSpread({ className: (border ? 'van-hairline--top-bottom' : '') + ' ' + utils.bem('tabbar', { fixed: fixed, safe: safeAreaInsetBottom }) + ' custom-class' + " ".concat(className || ''), style: utils.style([zIndex ? 'zIndex: ' + zIndex : '', style]) }, others), {}, { children: newChildren })), fixed && placeholder && /*#__PURE__*/_jsx(View, { style: 'height: ' + height + 'px;' })] }); } export default Tabbar;