UNPKG

@arpitbhalla/rneui-base-dev

Version:
174 lines (173 loc) 8.55 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TabBase = void 0; var react_1 = __importDefault(require("react")); var react_native_1 = require("react-native"); var helpers_1 = require("../helpers"); var TabBase = function (_a) { var _b, _c; var _d = _a.theme, theme = _d === void 0 ? helpers_1.defaultTheme : _d, children = _a.children, _e = _a.value, value = _e === void 0 ? 0 : _e, _f = _a.scrollable, scrollable = _f === void 0 ? false : _f, _g = _a.onChange, onChange = _g === void 0 ? function () { } : _g, indicatorStyle = _a.indicatorStyle, disableIndicator = _a.disableIndicator, _h = _a.variant, variant = _h === void 0 ? 'default' : _h, style = _a.style, dense = _a.dense, iconPosition = _a.iconPosition, buttonStyle = _a.buttonStyle, titleStyle = _a.titleStyle, containerStyle = _a.containerStyle, rest = __rest(_a, ["theme", "children", "value", "scrollable", "onChange", "indicatorStyle", "disableIndicator", "variant", "style", "dense", "iconPosition", "buttonStyle", "titleStyle", "containerStyle"]); var animationRef = react_1.default.useRef(new react_native_1.Animated.Value(0)); var scrollViewRef = react_1.default.useRef(null); var scrollViewPosition = react_1.default.useRef(0); var validChildren = react_1.default.useMemo(function () { return react_1.default.Children.toArray(children); }, [children]); var tabItemPositions = react_1.default.useRef([]); var _j = react_1.default.useState(0), tabContainerWidth = _j[0], setTabContainerWidth = _j[1]; var scrollHandler = react_1.default.useCallback(function (currValue) { if (tabItemPositions.current.length > currValue) { var itemStartPosition = currValue === 0 ? 0 : tabItemPositions.current[currValue - 1].position; var itemEndPosition = tabItemPositions.current[currValue].position; var scrollCurrentPosition = scrollViewPosition.current; var tabContainerCurrentWidth = tabContainerWidth; var scrollX_1 = scrollCurrentPosition; if (itemStartPosition < scrollCurrentPosition) { scrollX_1 += itemStartPosition - scrollCurrentPosition; } else if (scrollCurrentPosition + tabContainerCurrentWidth < itemEndPosition) { scrollX_1 += itemEndPosition - (scrollCurrentPosition + tabContainerCurrentWidth); } scrollViewRef.current.scrollTo({ x: scrollX_1, y: 0, animated: true, }); } }, [tabContainerWidth]); react_1.default.useEffect(function () { react_native_1.Animated.timing(animationRef.current, { toValue: value, useNativeDriver: true, duration: 170, }).start(); scrollable && requestAnimationFrame(function () { return scrollHandler(value); }); }, [animationRef, scrollHandler, value, scrollable]); var onScrollHandler = react_1.default.useCallback(function (event) { scrollViewPosition.current = event.nativeEvent.contentOffset.x; }, []); var indicatorTransitionInterpolate = react_1.default.useMemo(function () { var countItems = validChildren.length; if (countItems < 2 || !tabItemPositions.current.length) { return 0; } var inputRange = Array.from(Array(countItems + 1).keys()); var outputRange = tabItemPositions.current.map(function (_a) { var position = _a.position; return position; }); if (inputRange.length - 1 !== outputRange.length) { return 0; } return animationRef.current.interpolate({ inputRange: inputRange, outputRange: __spreadArray([0], outputRange, true), }); }, [animationRef, validChildren, tabItemPositions.current.length]); var WIDTH = react_1.default.useMemo(function () { var _a; return (_a = tabItemPositions.current[value]) === null || _a === void 0 ? void 0 : _a.width; }, [value, tabItemPositions.current.length]); return (react_1.default.createElement(react_native_1.View, __assign({}, rest, { accessibilityRole: "tablist", style: [ variant === 'primary' && { backgroundColor: (_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.primary, }, styles.viewStyle, style, ], onLayout: function (_a) { var layout = _a.nativeEvent.layout; setTabContainerWidth(layout.width); } }), react_1.default.createElement(scrollable ? react_native_1.ScrollView : react_1.default.Fragment, __assign(__assign({}, (scrollable && { horizontal: true, ref: scrollViewRef, onScroll: onScrollHandler, showsHorizontalScrollIndicator: false, })), { children: (react_1.default.createElement(react_1.default.Fragment, null, validChildren.map(function (child, index) { return react_1.default.cloneElement(child, { onPress: function () { return onChange(index); }, onLayout: function (event) { var _a; var width = event.nativeEvent.layout.width; var previousItemPosition = ((_a = tabItemPositions.current[index - 1]) === null || _a === void 0 ? void 0 : _a.position) || 0; tabItemPositions.current[index] = { position: previousItemPosition + width, width: width, }; }, active: index === value, variant: variant, _parentProps: { dense: dense, iconPosition: iconPosition, buttonStyle: buttonStyle, containerStyle: containerStyle, titleStyle: titleStyle, }, }); }), !disableIndicator && (react_1.default.createElement(react_native_1.Animated.View, { style: [ styles.indicator, { backgroundColor: (_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.secondary, transform: [ { translateX: indicatorTransitionInterpolate, }, ], width: WIDTH, }, indicatorStyle, ] })))) })))); }; exports.TabBase = TabBase; var styles = react_native_1.StyleSheet.create({ viewStyle: { flexDirection: 'row', position: 'relative', }, indicator: { display: 'flex', position: 'absolute', height: 2, bottom: 0, }, }); exports.TabBase.displayName = 'Tab';