@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
199 lines (164 loc) • 8.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _isNil = _interopRequireDefault(require("lodash/isNil"));
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _bottomBar = _interopRequireDefault(require("../bottom-bar"));
var _style = require("../button/style");
var _helpers = require("../helpers");
var _hooks = require("../hooks");
var _theme = _interopRequireDefault(require("../theme"));
var _style2 = require("./style");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const TabBar = _ref => {
var _options$;
let {
textColor,
iconColor,
activeTextColor,
activeIconColor,
options,
indicator = false,
indicatorWidth,
indicatorHeight = 3,
indicatorColor,
tabAlign = 'center',
height,
style,
...restProps
} = _ref;
const tabNum = options.length;
const isTabAdaption = tabAlign === 'center';
const isTabTextCompact = (0, _isNil.default)(indicatorWidth);
const isIndicatorWidthLayout = isTabTextCompact || indicatorWidth === 0;
const [value, onChange] = (0, _hooks.useControllableValue)(restProps, {
defaultValue: (_options$ = options[0]) === null || _options$ === void 0 ? void 0 : _options$.value
});
const TOKENS = _theme.default.useThemeTokens();
const CV = _theme.default.createVar(TOKENS, _style2.varCreator);
const CV_BUTTON = _theme.default.createVar(TOKENS, _style.varCreator);
const STYLES = _theme.default.createStyle(CV, _style2.styleCreator);
const [state, setState] = (0, _react.useState)({
layoutFinish: false
});
const layouts = (0, _react.useRef)(new Array(tabNum).fill({}));
const AnimatedIndicatorLeft = (0, _react.useRef)(new _reactNative.Animated.Value(0));
const AnimatedIndicatorWidth = (0, _react.useRef)(new _reactNative.Animated.Value(0));
const ScrollViewRef = (0, _react.useRef)(null);
const scrollViewWidth = (0, _react.useRef)(0);
if (indicator && (0, _isNil.default)(height)) {
height = 40;
}
textColor = (0, _helpers.getDefaultValue)(textColor, CV.tab_bar_text_color);
iconColor = (0, _helpers.getDefaultValue)(iconColor, CV.tab_bar_icon_color);
activeTextColor = (0, _helpers.getDefaultValue)(activeTextColor, CV.tab_bar_active_text_color);
activeIconColor = (0, _helpers.getDefaultValue)(activeIconColor, CV.tab_bar_active_icon_color);
indicatorColor = (0, _helpers.getDefaultValue)(indicatorColor, CV.tab_bar_indicator_color);
const navigateTo = (0, _react.useCallback)(n => {
const targetLayout = layouts.current[n];
const left = targetLayout.tab.x + (targetLayout.tab.width - (isIndicatorWidthLayout ? targetLayout.text.width : indicatorWidth)) / 2;
const width = isIndicatorWidthLayout ? targetLayout.text.width : indicatorWidth;
_reactNative.Animated.parallel([_reactNative.Animated.timing(AnimatedIndicatorLeft.current, {
toValue: left,
useNativeDriver: false,
duration: TOKENS.animation_duration_base
}), _reactNative.Animated.timing(AnimatedIndicatorWidth.current, {
toValue: width,
useNativeDriver: false,
duration: TOKENS.animation_duration_base
})]).start();
if (!isTabAdaption) {
const hh = scrollViewWidth.current / 2;
ScrollViewRef.current.scrollTo({
x: targetLayout.tab.x + targetLayout.tab.width / 2 - hh,
animated: true
});
}
}, [TOKENS.animation_duration_base, indicatorWidth, isIndicatorWidthLayout, isTabAdaption]);
const initIndicator = (0, _react.useCallback)(() => {
const layoutItems = layouts.current.filter(item => item.tab && item.text);
if (layoutItems.length === layouts.current.length) {
setState(s => ({ ...s,
layoutFinish: true
}));
}
}, []);
(0, _react.useEffect)(() => {
if (state.layoutFinish) {
const n = options.findIndex(item => item.value === value);
navigateTo(n);
}
}, [value, options, state.layoutFinish, navigateTo]);
const onLayoutScrollView = (0, _react.useCallback)(e => {
scrollViewWidth.current = e.nativeEvent.layout.width;
}, []);
const genOnPress = v => () => {
onChange(v);
};
const genOnLayoutTab = i => e => {
var _layouts$current$i;
layouts.current[i] = {
text: (_layouts$current$i = layouts.current[i]) === null || _layouts$current$i === void 0 ? void 0 : _layouts$current$i.text,
tab: e.nativeEvent.layout
};
initIndicator();
};
const genOnLayoutText = i => e => {
var _layouts$current$i2;
layouts.current[i] = {
tab: (_layouts$current$i2 = layouts.current[i]) === null || _layouts$current$i2 === void 0 ? void 0 : _layouts$current$i2.tab,
text: e.nativeEvent.layout
};
initIndicator();
};
const tabs = options.map((item, index) => {
var _item$iconRender;
const isActive = item.value === value;
return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
key: item.value,
style: [STYLES.item, isTabAdaption ? STYLES.item_adaption : null, item.iconRender ? null : STYLES.item_no_icon],
activeOpacity: CV_BUTTON.button_active_opacity,
onPress: isActive ? undefined : genOnPress(item.value),
onLayout: genOnLayoutTab(index)
}, (_item$iconRender = item.iconRender) === null || _item$iconRender === void 0 ? void 0 : _item$iconRender.call(item, isActive ? activeIconColor : iconColor), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [STYLES.item_text, isTabTextCompact ? null : STYLES.item_text_full, item.iconRender ? STYLES.item_text_icon : null, // eslint-disable-next-line react-native/no-inline-styles
{
color: isActive ? activeTextColor : textColor,
fontWeight: isActive && indicator ? '500' : 'normal'
}],
onLayout: genOnLayoutText(index)
}, item.label));
});
const indicatorJSX = indicator && state.layoutFinish ? /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, {
// eslint-disable-next-line react-native/no-inline-styles
style: {
height: indicatorHeight,
width: AnimatedIndicatorWidth.current,
backgroundColor: indicatorColor,
position: 'absolute',
left: AnimatedIndicatorLeft.current,
bottom: 0
}
}) : null;
return /*#__PURE__*/_react.default.createElement(_bottomBar.default, _extends({}, restProps, {
height: height,
style: [STYLES.tab_bar, style]
}), isTabAdaption ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, tabs, indicatorJSX) : /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, {
onLayout: onLayoutScrollView,
ref: ScrollViewRef,
horizontal: true,
bounces: false,
showsHorizontalScrollIndicator: false,
style: STYLES.tab_bar_scroll,
contentContainerStyle: STYLES.tab_bar_scroll_content
}, indicatorJSX, tabs));
};
var _default = /*#__PURE__*/(0, _react.memo)(TabBar);
exports.default = _default;
//# sourceMappingURL=index.js.map