Skip to content

Commit 54c2e5e

Browse files
authored
Feat/ TabController.TabBar style (#1331)
* wideIndicator prop * spreadItems prop * move spreadItems to defaultProps
1 parent 24c1f35 commit 54c2e5e

File tree

3 files changed

+26
-7
lines changed
  • demo/src/screens/componentScreens/TabControllerScreen
  • generatedTypes/components/tabController
  • src/components/tabController

3 files changed

+26
-7
lines changed

demo/src/screens/componentScreens/TabControllerScreen/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class TabControllerScreen extends Component<{}, State> {
146146
key={key}
147147
// uppercase
148148
// indicatorStyle={{backgroundColor: 'green', height: 3}}
149+
// wideIndicator
150+
// spreadItems={false}
149151
// labelColor={'green'}
150152
// selectedLabelColor={'red'}
151153
// labelStyle={{fontSize: 20}}

generatedTypes/components/tabController/TabBar.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export interface TabControllerBarProps {
66
* The list of tab bar items
77
*/
88
items?: TabControllerItemProps[];
9+
/**
10+
* Whether the tabBar should be spread (default: true)
11+
*/
12+
spreadItems?: boolean;
913
/**
1014
* Tab Bar height
1115
*/
@@ -22,6 +26,10 @@ export interface TabControllerBarProps {
2226
* custom style for the selected indicator
2327
*/
2428
indicatorStyle?: StyleProp<ViewStyle>;
29+
/**
30+
* Whether the indicator should be wide (as the item)
31+
*/
32+
wideIndicator?: boolean;
2533
/**
2634
* custom label style
2735
*/

src/components/tabController/TabBar.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const {Code, Value, interpolate: _interpolate, interpolateNode, block, set} = Re
2020
const interpolate = interpolateNode || _interpolate;
2121

2222
const DEFAULT_HEIGHT = 48;
23-
const INDICATOR_INSET = Spacings.s4;
2423
const DEFAULT_BACKGROUND_COLOR = Colors.white;
2524

2625
const DEFAULT_LABEL_STYLE = {
@@ -40,6 +39,10 @@ export interface TabControllerBarProps {
4039
* The list of tab bar items
4140
*/
4241
items?: TabControllerItemProps[];
42+
/**
43+
* Whether the tabBar should be spread (default: true)
44+
*/
45+
spreadItems?: boolean;
4346
/**
4447
* Tab Bar height
4548
*/
@@ -60,6 +63,10 @@ export interface TabControllerBarProps {
6063
* custom style for the selected indicator
6164
*/
6265
indicatorStyle?: StyleProp<ViewStyle>;
66+
/**
67+
* Whether the indicator should be wide (as the item)
68+
*/
69+
wideIndicator?: boolean;
6370
/**
6471
* custom label style
6572
*/
@@ -128,11 +135,13 @@ interface Props extends TabControllerBarProps, BaseComponentInjectedProps, Forwa
128135
const TabBar = (props: Props) => {
129136
const {
130137
items: propsItems,
138+
spreadItems,
131139
height,
132140
enableShadow,
133141
shadowStyle: propsShadowStyle,
134142
// minTabsForScroll,
135143
indicatorStyle,
144+
wideIndicator,
136145
labelStyle,
137146
selectedLabelStyle,
138147
labelColor,
@@ -149,6 +158,7 @@ const TabBar = (props: Props) => {
149158
children: propsChildren
150159
} = props;
151160

161+
const indicatorInset = wideIndicator ? 0 : Spacings.s4;
152162
const context = useContext(TabBarContext);
153163
// @ts-ignore // TODO: typescript
154164
const {itemStates, items: contextItems, currentPage, targetPage, registerTabItems, selectedIndex} = context;
@@ -307,7 +317,7 @@ const TabBar = (props: Props) => {
307317

308318
const selectedIndicator =
309319
itemsWidths && itemsWidths.length > 0 ? (
310-
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
320+
<Reanimated.View style={[styles.selectedIndicator, {marginHorizontal: indicatorInset}, indicatorStyle, _indicatorTransitionStyle]}/>
311321
) : undefined;
312322

313323
const renderCodeBlock = _.memoize(() => {
@@ -321,7 +331,7 @@ const TabBar = (props: Props) => {
321331
nodes.push(set(_indicatorWidth,
322332
interpolate(currentPage, {
323333
inputRange: itemsWidths.map((_v, i) => i),
324-
outputRange: itemsWidths.map(v => v - 2 * INDICATOR_INSET)
334+
outputRange: itemsWidths.map(v => v - 2 * indicatorInset)
325335
})));
326336

327337
nodes.push(Reanimated.onChange(targetPage, Reanimated.call([targetPage], focusIndex as any)));
@@ -339,7 +349,7 @@ const TabBar = (props: Props) => {
339349
}, [shadowStyle, containerWidth, containerStyle]);
340350

341351
const indicatorContainerStyle = useMemo(() => {
342-
return [styles.tabBar, !_.isUndefined(height) && {height}, {backgroundColor}];
352+
return [styles.tabBar, {flex: spreadItems ? 1 : undefined}, !_.isUndefined(height) && {height}, {backgroundColor}];
343353
}, [height, backgroundColor]);
344354

345355
const scrollViewContainerStyle = useMemo(() => {
@@ -371,7 +381,8 @@ TabBar.displayName = 'TabController.TabBar';
371381
TabBar.defaultProps = {
372382
labelStyle: DEFAULT_LABEL_STYLE,
373383
selectedLabelStyle: DEFAULT_SELECTED_LABEL_STYLE,
374-
backgroundColor: DEFAULT_BACKGROUND_COLOR
384+
backgroundColor: DEFAULT_BACKGROUND_COLOR,
385+
spreadItems: true
375386

376387
// containerWidth: Constants.screenWidth
377388
};
@@ -381,7 +392,6 @@ const styles = StyleSheet.create({
381392
zIndex: 100
382393
},
383394
tabBar: {
384-
flex: 1,
385395
height: DEFAULT_HEIGHT,
386396
flexDirection: 'row',
387397
justifyContent: 'space-between'
@@ -400,7 +410,6 @@ const styles = StyleSheet.create({
400410
left: 0,
401411
width: 70,
402412
height: 2,
403-
marginHorizontal: INDICATOR_INSET,
404413
backgroundColor: Colors.primary
405414
},
406415
containerShadow: {

0 commit comments

Comments
 (0)