Skip to content

Commit 9676308

Browse files
Infra/tabbar typescript (#839)
* migrating to TS * adding style and testID props and fixing style types * update generateTypes * fix for optional props Co-authored-by: Ethan Sharabi <[email protected]>
1 parent dbe9e7a commit 9676308

File tree

8 files changed

+368
-253
lines changed

8 files changed

+368
-253
lines changed

demo/src/screens/componentScreens/TabBarScreen.js renamed to demo/src/screens/componentScreens/TabBarScreen.tsx

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {StyleSheet, ScrollView} from 'react-native';
4-
import {Colors, Typography, View, Button, TabBar} from 'react-native-ui-lib';
4+
import {Colors, View, Button, TabBar} from 'react-native-ui-lib';
55

66

77
const labelsArray = [
@@ -16,30 +16,30 @@ const themeColors = [Colors.violet30, Colors.green30, Colors.red30, Colors.blue3
1616

1717

1818
export default class TabBarScreen extends Component {
19-
constructor(props) {
20-
super(props);
21-
22-
this.state = {
23-
selectedIndex: 1,
24-
selectedIndex1: 1,
25-
labels: labelsArray[0],
26-
currentTabs: [],
27-
themeColor: themeColors[0]
28-
};
29-
30-
this.counter = 0;
31-
this.colorCounter = 0;
32-
}
19+
state = {
20+
selectedIndex: 1,
21+
selectedIndex1: 1,
22+
selectedIndex2: 1,
23+
labels: labelsArray[0],
24+
currentTabs: [],
25+
themeColor: themeColors[0]
26+
};
27+
28+
counter = 0;
29+
colorCounter = 0;
30+
tabbar: any = undefined;
3331

3432
/** Index change */
3533
changeIndex = () => {
3634
let index;
37-
38-
do {
39-
index = Math.floor(Math.random() * this.tabbar.props.children.length);
40-
} while (index === this.state.selectedIndex);
4135

42-
this.setState({selectedIndex: index});
36+
if (this.tabbar) {
37+
do {
38+
index = Math.floor(Math.random() * this.tabbar.props.children.length);
39+
} while (index === this.state.selectedIndex);
40+
41+
this.setState({selectedIndex: index});
42+
}
4343
};
4444

4545
/** Labels change */
@@ -74,8 +74,8 @@ export default class TabBarScreen extends Component {
7474
addTab = () => {
7575
const random = Math.floor(Math.random() * 100000);
7676
const newTabs = this.state.currentTabs;
77-
78-
newTabs.push({id: random, displayLabel: `tab #${this.state.currentTabs.length}`});
77+
const item = {id: random, displayLabel: `tab #${this.state.currentTabs.length}`};
78+
newTabs.push(item);
7979
this.setState({currentTabs: newTabs});
8080
};
8181

@@ -90,7 +90,7 @@ export default class TabBarScreen extends Component {
9090
};
9191

9292
/** Actions */
93-
getTabs(showAddTab) {
93+
getTabs(showAddTab: boolean) {
9494
const tabs = _.map(this.state.currentTabs, tab => this.renderTabs(tab));
9595

9696
if (showAddTab) {
@@ -104,7 +104,7 @@ export default class TabBarScreen extends Component {
104104
}
105105

106106
/** Renders */
107-
renderTabs(tab) {
107+
renderTabs(tab: any) {
108108
return <TabBar.Item key={tab.id} label={tab.displayLabel} onPress={tab.onPress}/>;
109109
}
110110

@@ -154,8 +154,8 @@ export default class TabBarScreen extends Component {
154154
</TabBar>
155155

156156
<View center row>
157-
<Button size={'small'} margin-20 label={`Add tabs`} onPress={this.addTab}/>
158-
<Button size={'small'} margin-20 label={`Remove tabs`} onPress={this.removeTab}/>
157+
<Button size={Button.sizes.small} margin-20 label={`Add tabs`} onPress={this.addTab}/>
158+
<Button size={Button.sizes.small} margin-20 label={`Remove tabs`} onPress={this.removeTab}/>
159159
</View>
160160
<TabBar style={styles.tabbar} selectedIndex={0} enableShadow>
161161
{this.getTabs(false)}
@@ -170,7 +170,7 @@ export default class TabBarScreen extends Component {
170170
</TabBar>
171171

172172
<Button
173-
size={'small'}
173+
size={Button.sizes.small}
174174
margin-20
175175
label={`Change index: ${this.state.selectedIndex}`}
176176
onPress={this.changeIndex}
@@ -188,13 +188,13 @@ export default class TabBarScreen extends Component {
188188
</TabBar>
189189

190190
<View row>
191-
<Button size={'small'} margin-20 label={`Change Labels`} onPress={this.changeLabels}/>
192-
<Button size={'small'} margin-20 label={`Change Color`} onPress={this.changeColors}/>
191+
<Button size={Button.sizes.small} margin-20 label={`Change Labels`} onPress={this.changeLabels}/>
192+
<Button size={Button.sizes.small} margin-20 label={`Change Color`} onPress={this.changeColors}/>
193193
</View>
194194
<TabBar
195195
style={styles.tabbar}
196196
selectedIndex={this.state.selectedIndex1}
197-
onChangeIndex={index => this.setState({selectedIndex1: index})}
197+
onChangeIndex={(index: number) => this.setState({selectedIndex1: index})}
198198
indicatorStyle={{backgroundColor: this.state.themeColor}}
199199
enableShadow
200200
>
@@ -205,7 +205,7 @@ export default class TabBarScreen extends Component {
205205
<TabBar
206206
style={styles.tabbar}
207207
selectedIndex={this.state.selectedIndex2}
208-
onChangeIndex={index => this.setState({selectedIndex2: index})}
208+
onChangeIndex={(index: number) => this.setState({selectedIndex2: index})}
209209
enableShadow
210210
>
211211
<TabBar.Item label={this.state.labels[0]}/>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
import { Animated, ViewStyle, TextStyle } from 'react-native';
3+
import { BaseComponentInjectedProps } from '../../commons/new';
4+
import { BadgeProps } from '../badge';
5+
export declare type TabBarItemProps = BaseComponentInjectedProps & {
6+
/**
7+
* icon of the tab
8+
*/
9+
icon?: number;
10+
/**
11+
* icon tint color
12+
*/
13+
iconColor?: string;
14+
/**
15+
* icon selected tint color
16+
*/
17+
iconSelectedColor?: string;
18+
/**
19+
* label of the tab
20+
*/
21+
label?: string;
22+
/**
23+
* custom label style
24+
*/
25+
labelStyle?: TextStyle;
26+
/**
27+
* Badge component props to display next the item label
28+
*/
29+
badge?: BadgeProps;
30+
/**
31+
* maximum number of lines the label can break
32+
*/
33+
maxLines?: number;
34+
/**
35+
* custom selected tab label style
36+
*/
37+
selectedLabelStyle: TextStyle;
38+
/**
39+
* whether the tab is selected or not
40+
*/
41+
selected?: boolean;
42+
/**
43+
* whether the tab will have a divider on its right
44+
*/
45+
showDivider?: boolean;
46+
/**
47+
* A fixed width for the item
48+
*/
49+
width?: number;
50+
/**
51+
* ignore of the tab
52+
*/
53+
ignore?: boolean;
54+
/**
55+
* callback for when pressing a tab
56+
*/
57+
onPress?: (props: any) => void;
58+
/**
59+
* whether to change the text to uppercase
60+
*/
61+
uppercase?: boolean;
62+
/**
63+
* Apply background color on press for TouchableOpacity
64+
*/
65+
activeBackgroundColor?: string;
66+
indicatorStyle?: ViewStyle;
67+
style?: ViewStyle;
68+
testID?: string;
69+
};
70+
export declare type State = {
71+
indicatorOpacity?: Animated.Value;
72+
};
73+
declare const _default: React.ComponentClass<TabBarItemProps, any> & State;
74+
export default _default;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import { Animated, ViewStyle } from 'react-native';
3+
import { BaseComponentInjectedProps } from '../../commons/new';
4+
import { ViewPropTypes } from '../view';
5+
import { TabBarItemProps } from './TabBarItem';
6+
export declare type TabBarProps = BaseComponentInjectedProps & ViewPropTypes & {
7+
/**
8+
* Show Tab Bar bottom shadow
9+
*/
10+
enableShadow?: boolean;
11+
/**
12+
* The minimum number of tabs to render
13+
*/
14+
minTabsForScroll?: number;
15+
/**
16+
* current selected tab index
17+
*/
18+
selectedIndex?: number;
19+
/**
20+
* callback for when index has change (will not be called on ignored items)
21+
*/
22+
onChangeIndex?: (props: any) => void;
23+
/**
24+
* callback for when tab selected
25+
*/
26+
onTabSelected?: (props: any) => void;
27+
/**
28+
* custom style for the selected indicator
29+
*/
30+
indicatorStyle?: ViewStyle;
31+
/**
32+
* Tab Bar height
33+
*/
34+
height?: number;
35+
children: React.ReactNode;
36+
style?: ViewStyle;
37+
testID?: string;
38+
};
39+
export declare type State = {
40+
gradientOpacity: Animated.Value;
41+
scrollEnabled?: boolean;
42+
currentIndex?: number;
43+
};
44+
declare const _default: React.ComponentClass<TabBarProps, any> & {
45+
Item: React.ComponentClass<TabBarItemProps, any> & import("./TabBarItem").State;
46+
};
47+
export default _default;

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export {default as Image, ImageProps} from './components/image';
1515
export {default as Overlay, OverlayTypes} from './components/overlay';
1616
export {default as RadioButton, RadioButtonPropTypes} from './components/radioButton/RadioButton';
1717
export {default as RadioGroup, RadioGroupPropTypes} from './components/radioButton/RadioGroup';
18+
export {default as TabBar} from './components/TabBar';
1819

1920
/* All components with manual typings */
2021
export {
@@ -47,7 +48,6 @@ export {
4748
GradientSlider,
4849
ColorSliderGroup,
4950
Stepper,
50-
TabBar,
5151
TagsInput,
5252
SharedTransition,
5353
StackAggregator,

0 commit comments

Comments
 (0)