UNPKG

react-native-segment-control-ui

Version:
183 lines (182 loc) 6.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); 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; } const SegmentControl = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => { let { segments = [], activeTab = 0, style, activeStyle, textStyle, onPress, textActiveStyle, duration = 300, iconField, labelField = 'title', testID, iconActiveField } = _ref; ``; const [segmentItemWidth, setSegmentItemWidth] = (0, _react.useState)(0); const [active, setActive] = (0, _react.useState)(activeTab); const widthSegment = (0, _react.useCallback)(function (segmentArr) { let width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _reactNative.Dimensions.get('window').width; if (typeof width === 'number') { return { width: width / segmentArr.length }; } else if (typeof width === 'string') { console.warn('=>>>SegmentControl<<<= : Segment not supported for size string'); return { width: 0 }; } else { return { width: _reactNative.Dimensions.get('window').width }; } }, []); const heightSegment = function () { let height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 60; if (typeof height === 'number') { return { height: height - 6 }; } else if (typeof height === 'string') { console.warn('=>>>SegmentControl<<<= : Segment not supported for size string'); return { height: 0 }; } else { return { height: 60 }; } }; const fadeAnim = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current; (0, _react.useImperativeHandle)(ref, () => ({ updateActive: function () { let index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; if (index <= segments.length - 1) { setActive(index); translateXAnim(index); } else { console.warn('=>>>SegmentControl<<<= : Active cannot exceed the length of the array'); } } })); const translateXAnim = (0, _react.useCallback)(index => { _reactNative.Animated.timing(fadeAnim, { toValue: segmentItemWidth * index, easing: _reactNative.Easing.out(_reactNative.Easing.quad), duration: duration, useNativeDriver: false }).start(); }, [duration, fadeAnim, segmentItemWidth]); _react.default.useEffect(() => { const { width } = widthSegment(segments, style === null || style === void 0 ? void 0 : style.width); setSegmentItemWidth(width); }, [segments, style === null || style === void 0 ? void 0 : style.width, widthSegment]); _react.default.useEffect(() => { if (activeTab <= segments.length - 1) { translateXAnim(activeTab); setActive(activeTab); } else { console.warn('=>>>SegmentControl<<<= : Active cannot exceed the length of the array'); } }, [activeTab, translateXAnim, segments]); return /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [styles.container, { ...style }], testID: testID }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.viewWrap }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.boxView }, segments === null || segments === void 0 ? void 0 : segments.map((s, i) => { return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableWithoutFeedback, { ref: ref, key: i, onPress: () => { const value = s; translateXAnim(i); setActive(i); typeof onPress === 'function' && onPress(value, i); } }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [styles.button, widthSegment(segments, style === null || style === void 0 ? void 0 : style.width), heightSegment(style === null || style === void 0 ? void 0 : style.height)] }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { key: i, style: styles.box }, iconActiveField ? active === i ? s[iconActiveField] : s[iconField] : s[iconField], /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: active === i ? textActiveStyle : textStyle }, s[labelField])))); })), /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, { style: [styles.animation, styles.boxAnimated, { transform: [{ translateX: fadeAnim }], width: segmentItemWidth - 10 }, activeStyle] }))); }); const styles = _reactNative.StyleSheet.create({ container: { backgroundColor: '#dddddd' }, viewWrap: { margin: 2 }, boxView: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', position: 'relative', elevation: 10, zIndex: 999 }, box: { flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', borderRadius: 50 }, boxAnimated: { backgroundColor: '#F1F2F6' }, button: { flexDirection: 'row' }, animation: { position: 'absolute', borderRadius: 7, top: 2, bottom: 2, right: 2, left: 2, borderWidth: 0.5, borderColor: 'rgba(0,0,0,0.04)', shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.22, shadowRadius: 2.22, elevation: 4, zIndex: 1 } }); var _default = SegmentControl; exports.default = _default; //# sourceMappingURL=index.js.map