Skip to content

Commit

Permalink
feat: added possibility to render icon in tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0trpietruszewski committed Sep 25, 2020
1 parent d8437bc commit 5c48814
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/StickyParallaxHeader.js
@@ -1,6 +1,5 @@
import React, { Component } from 'react';
import {
arrayOf,
bool,
func,
node,
Expand Down Expand Up @@ -466,7 +465,7 @@ StickyParallaxHeader.propTypes = {
tabTextContainerActiveStyle: shape({}),
tabTextContainerStyle: shape({}),
tabTextStyle: shape({}),
tabs: arrayOf(shape({})),
tabs: ScrollableTabBar.propTypes.tabs,
tabsContainerBackgroundColor: string,
tabWrapperStyle: ViewPropTypes.style,
tabsContainerStyle: ViewPropTypes.style,
Expand Down
15 changes: 13 additions & 2 deletions src/components/ScrollableTabBar/ScrollableTabBar.js
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-return-assign */
import React from 'react';
import { Animated, Text, TouchableOpacity, View, ScrollView, ViewPropTypes } from 'react-native';
import { array, func, number, object, shape, string } from 'prop-types';
import { arrayOf, func, number, object, shape, string, element, oneOfType } from 'prop-types';
import { constants } from '../../constants';
import styles from './ScrollableTabBar.styles';

Expand Down Expand Up @@ -73,6 +73,16 @@ class ScrollableTabBar extends React.PureComponent {
return goToPage(page);
};

renderIcon = (icon, page) => {
const { activeTab } = this.props;
const isActive = activeTab === page;
if (typeof icon === 'function') {
return icon(isActive);
}

return icon;
};

render() {
const {
activeTab,
Expand Down Expand Up @@ -140,6 +150,7 @@ class ScrollableTabBar extends React.PureComponent {
tabTextContainerStyle,
isTabActive && tabTextContainerActiveStyle,
]}>
{this.renderIcon(tab.icon, page)}
<Text
// eslint-disable-next-line no-return-assign
onLayout={({
Expand Down Expand Up @@ -182,7 +193,7 @@ ScrollableTabBar.propTypes = {
activeTab: number,
goToPage: func,
scrollValue: object,
tabs: array,
tabs: arrayOf(shape({ content: element, title: string, icon: oneOfType([element, func]) })),
tabTextStyle: shape({}),
tabTextActiveStyle: shape({}),
tabTextContainerStyle: shape({}),
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Expand Up @@ -31,6 +31,7 @@ export interface OnChangeTabArguments {
export interface Tab {
content: ReactElement;
title: string;
icon?: ReactElement | ((isActive: boolean) => ReactElement);
}
export interface SharedProps {
backgroundImage?: ImageSourcePropType;
Expand Down

0 comments on commit 5c48814

Please sign in to comment.