1
- import React , { Component } from 'react' ;
2
- import {
3
- View ,
4
- ViewPropTypes ,
5
- TouchableOpacity ,
6
- StyleSheet ,
7
- Text
8
- } from 'react-native' ;
1
+ import React , { PureComponent } from 'react' ;
9
2
import PropTypes from 'prop-types' ;
3
+ import { View , ViewPropTypes , StyleSheet , Text } from 'react-native' ;
4
+ import TabOption from './TabOption' ;
10
5
6
+ const styles = StyleSheet . create ( {
7
+ tabsContainerStyle : {
8
+ backgroundColor : 'transparent' ,
9
+ flexDirection : 'row' ,
10
+ } ,
11
+ tabStyle : {
12
+ paddingVertical : 5 ,
13
+ flex : 1 ,
14
+ justifyContent : 'center' ,
15
+ alignItems : 'center' ,
16
+ borderColor : '#0076FF' ,
17
+ borderWidth : 1 ,
18
+ backgroundColor : 'white' ,
19
+ } ,
20
+ } ) ;
11
21
const handleTabPress = ( index , multiple , selectedIndex , onTabPress ) => {
12
- if ( multiple ) {
13
- onTabPress ( index ) ;
14
- }
15
- else if ( selectedIndex !== index ) {
16
- onTabPress ( index ) ;
17
- }
22
+ if ( multiple ) {
23
+ onTabPress ( index ) ;
24
+ } else if ( selectedIndex !== index ) {
25
+ onTabPress ( index ) ;
26
+ }
18
27
} ;
19
28
20
- const TabOption = ( {
21
- isTabActive, index, badge, text,
22
- firstTabStyle, lastTabStyle,
23
- tabStyle, activeTabStyle,
24
- tabTextStyle, activeTabTextStyle,
25
- tabBadgeContainerStyle, activeTabBadgeContainerStyle,
26
- tabBadgeStyle, activeTabBadgeStyle,
27
- onTabPress, textNumberOfLines,
28
- allowFontScaling,
29
- accessible,
30
- activeTabOpacity,
31
- accessibilityLabel,
32
- enabled,
33
- } ) => {
34
- return (
35
- < TouchableOpacity style = { [
36
- styles . tabStyle ,
37
- tabStyle ,
38
- isTabActive ? [ styles . activeTabStyle , activeTabStyle ] : { } ,
39
- firstTabStyle ,
40
- lastTabStyle ] }
41
- accessible = { accessible }
42
- accessibilityLabel = { accessibilityLabel }
43
- accessibilityTraits = { isTabActive ? "selected" : "button" }
44
- accessibilityComponentType = { "button" }
45
- onPress = { ( ) => onTabPress ( index ) }
46
- disabled = { ! enabled }
47
- activeOpacity = { activeTabOpacity } >
48
- < View style = { { flexDirection : "row" } } >
49
- < Text style = { [
50
- styles . tabTextStyle ,
51
- tabTextStyle ,
52
- isTabActive ? [ styles . activeTabTextStyle , activeTabTextStyle ] : { } ] }
53
- numberOfLines = { textNumberOfLines }
54
- allowFontScaling = { allowFontScaling }
55
- ellipsizeMode = "tail" >
56
- { text }
57
- </ Text >
58
- {
59
- Boolean ( badge ) ?
60
- < View style = { [
61
- styles . tabBadgeContainerStyle ,
62
- tabBadgeContainerStyle ,
63
- isTabActive ? [ styles . activeTabBadgeContainerStyle , activeTabBadgeContainerStyle ] : { } ] } >
64
- < Text style = { [
65
- styles . tabBadgeStyle ,
66
- tabBadgeStyle ,
67
- isTabActive ? [ styles . activeTabBadgeStyle , activeTabBadgeStyle ] : { } ] }
68
- allowFontScaling = { allowFontScaling } >
69
- { badge }
70
- </ Text >
71
- </ View > : false
72
- }
73
- </ View >
74
- </ TouchableOpacity >
75
- ) ;
76
- }
77
-
78
- const getAccessibilityLabelByIndex = ( accessibilityLabels , index ) => {
79
- return accessibilityLabels && accessibilityLabels . length > 0 && accessibilityLabels [ index ] ? accessibilityLabels [ index ] : undefined
80
- }
81
-
82
- const SegmentedControlTab = ( {
83
- multiple, selectedIndex, selectedIndices, values,
84
- badges, borderRadius, tabsContainerStyle, tabsContainerDisableStyle,
85
- tabStyle, activeTabStyle,
86
- tabTextStyle, activeTabTextStyle,
87
- tabBadgeContainerStyle, activeTabBadgeContainerStyle,
88
- tabBadgeStyle, activeTabBadgeStyle,
89
- onTabPress, textNumberOfLines,
90
- allowFontScaling,
91
- accessible,
92
- accessibilityLabels,
93
- activeTabOpacity,
94
- enabled
95
- } ) => {
96
-
97
- const firstTabStyle = [ { borderRightWidth : values . length == 2 ? 1 : 0 , borderTopLeftRadius : borderRadius , borderBottomLeftRadius : borderRadius } ]
98
- const lastTabStyle = [ { borderLeftWidth : 0 , borderTopRightRadius : borderRadius , borderBottomRightRadius : borderRadius } ]
99
-
100
- const tabsContainerStyles = [ styles . tabsContainerStyle , tabsContainerStyle ]
101
- if ( ! enabled ) {
102
- tabsContainerStyles . push ( tabsContainerDisableStyle )
103
- }
104
-
105
- return (
106
- < View
107
- style = { tabsContainerStyles }
108
- removeClippedSubviews = { false } >
109
- {
110
- values . map ( ( item , index ) => {
111
- const accessibilityText = getAccessibilityLabelByIndex ( accessibilityLabels , index )
112
- return (
113
- < TabOption
114
- key = { index }
115
- index = { index }
116
- badge = { badges && badges [ index ] ? badges [ index ] : false }
117
- isTabActive = { multiple ? selectedIndices . includes ( index ) : selectedIndex === index }
118
- text = { item }
119
- textNumberOfLines = { textNumberOfLines }
120
- onTabPress = { ( index ) => handleTabPress ( index , multiple , selectedIndex , onTabPress ) }
121
- firstTabStyle = { index === 0 ? [ { borderRightWidth : 0 } , firstTabStyle ] : { } }
122
- lastTabStyle = { index === values . length - 1 ? [ { borderLeftWidth : 0 } , lastTabStyle ] : { } }
123
- tabStyle = { [ tabStyle , index !== 0 && index !== values . length - 1 ? { marginLeft : - 1 } : { } ] }
124
- activeTabStyle = { activeTabStyle }
125
- tabTextStyle = { tabTextStyle }
126
- activeTabTextStyle = { activeTabTextStyle }
127
- tabBadgeContainerStyle = { tabBadgeContainerStyle }
128
- activeTabBadgeContainerStyle = { activeTabBadgeContainerStyle }
129
- tabBadgeStyle = { tabBadgeStyle }
130
- activeTabBadgeStyle = { activeTabBadgeStyle }
131
- allowFontScaling = { allowFontScaling }
132
- activeTabOpacity = { activeTabOpacity }
133
- accessible = { accessible }
134
- accessibilityLabel = { accessibilityText ? accessibilityText : item }
135
- enabled = { enabled }
136
- />
137
- ) ;
138
- } )
139
- }
140
- </ View >
141
- ) ;
142
- } ;
29
+ const getAccessibilityLabelByIndex = ( accessibilityLabels , index ) =>
30
+ accessibilityLabels &&
31
+ accessibilityLabels . length > 0 &&
32
+ accessibilityLabels [ index ]
33
+ ? accessibilityLabels [ index ]
34
+ : undefined ;
143
35
144
- SegmentedControlTab . propTypes = {
36
+ export default class SegmentedControlTab extends PureComponent {
37
+ static propTypes = {
145
38
values : PropTypes . array ,
146
39
badges : PropTypes . array ,
147
40
multiple : PropTypes . bool ,
@@ -164,20 +57,20 @@ SegmentedControlTab.propTypes = {
164
57
accessible : PropTypes . bool ,
165
58
accessibilityLabels : PropTypes . array ,
166
59
activeTabOpacity : PropTypes . number ,
167
- enabled : PropTypes . bool
168
- } ;
60
+ enabled : PropTypes . bool ,
61
+ } ;
169
62
170
- SegmentedControlTab . defaultProps = {
63
+ static defaultProps = {
171
64
values : [ 'One' , 'Two' , 'Three' ] ,
172
65
accessible : true ,
173
66
accessibilityLabels : [ ] ,
174
67
badges : [ '' , '' , '' ] ,
175
68
multiple : false ,
176
69
selectedIndex : 0 ,
177
70
selectedIndices : [ 0 ] ,
178
- onTabPress : ( ) => { } ,
71
+ onTabPress : ( ) => { } ,
179
72
tabsContainerStyle : { } ,
180
- tabsContainerDisableStyle : { opacity :0.6 } ,
73
+ tabsContainerDisableStyle : { opacity : 0.6 } ,
181
74
tabStyle : { } ,
182
75
activeTabStyle : { } ,
183
76
tabTextStyle : { } ,
@@ -191,50 +84,105 @@ SegmentedControlTab.defaultProps = {
191
84
allowFontScaling : true ,
192
85
activeTabOpacity : 1 ,
193
86
enabled : true ,
194
- } ;
87
+ } ;
195
88
196
- const styles = StyleSheet . create ( {
197
- tabsContainerStyle : {
198
- backgroundColor : 'transparent' ,
199
- flexDirection : 'row' ,
200
- } ,
201
- tabStyle : {
202
- paddingVertical : 5 ,
203
- flex : 1 ,
204
- justifyContent : 'center' ,
205
- alignItems : 'center' ,
206
- borderColor : '#0076FF' ,
207
- borderWidth : 1 ,
208
- backgroundColor : 'white' ,
209
- } ,
210
- activeTabStyle : {
211
- backgroundColor : '#0076FF'
212
- } ,
213
- tabTextStyle : {
214
- color : '#0076FF'
215
- } ,
216
- activeTabTextStyle : {
217
- color : 'white'
218
- } ,
219
- tabBadgeContainerStyle : {
220
- borderRadius : 20 ,
221
- backgroundColor : 'red' ,
222
- paddingLeft : 5 ,
223
- paddingRight : 5 ,
224
- marginLeft : 5 ,
225
- marginBottom : 3
226
- } ,
227
- activeTabBadgeContainerStyle : {
228
- backgroundColor : 'white'
229
- } ,
230
- tabBadgeStyle : {
231
- color : 'white' ,
232
- fontSize : 11 ,
233
- fontWeight : 'bold'
234
- } ,
235
- activeTabBadgeStyle : {
236
- color : 'black'
237
- }
238
- } ) ;
89
+ render ( ) {
90
+ const {
91
+ multiple,
92
+ selectedIndex,
93
+ selectedIndices,
94
+ values,
95
+ badges,
96
+ borderRadius,
97
+ tabsContainerStyle,
98
+ tabsContainerDisableStyle,
99
+ tabStyle,
100
+ activeTabStyle,
101
+ tabTextStyle,
102
+ activeTabTextStyle,
103
+ tabBadgeContainerStyle,
104
+ activeTabBadgeContainerStyle,
105
+ tabBadgeStyle,
106
+ activeTabBadgeStyle,
107
+ onTabPress,
108
+ textNumberOfLines,
109
+ allowFontScaling,
110
+ accessible,
111
+ accessibilityLabels,
112
+ activeTabOpacity,
113
+ enabled,
114
+ } = this . props ;
115
+ const firstTabStyle = [
116
+ {
117
+ borderRightWidth : values . length === 2 ? 1 : 0 ,
118
+ borderTopLeftRadius : borderRadius ,
119
+ borderBottomLeftRadius : borderRadius ,
120
+ } ,
121
+ ] ;
122
+ const lastTabStyle = [
123
+ {
124
+ borderLeftWidth : 0 ,
125
+ borderTopRightRadius : borderRadius ,
126
+ borderBottomRightRadius : borderRadius ,
127
+ } ,
128
+ ] ;
239
129
240
- export default SegmentedControlTab
130
+ const tabsContainerStyles = [ styles . tabsContainerStyle , tabsContainerStyle ] ;
131
+ if ( ! enabled ) {
132
+ tabsContainerStyles . push ( tabsContainerDisableStyle ) ;
133
+ }
134
+ return (
135
+ < View style = { tabsContainerStyles } removeClippedSubviews = { false } >
136
+ { values . map ( ( item , index ) => {
137
+ const accessibilityText = getAccessibilityLabelByIndex (
138
+ accessibilityLabels ,
139
+ index ,
140
+ ) ;
141
+ return (
142
+ < TabOption
143
+ key = { `${ index } ${ item } ` }
144
+ index = { index }
145
+ badge = { badges && badges [ index ] ? badges [ index ] : false }
146
+ isTabActive = {
147
+ multiple
148
+ ? selectedIndices . includes ( index )
149
+ : selectedIndex === index
150
+ }
151
+ text = { item }
152
+ textNumberOfLines = { textNumberOfLines }
153
+ onTabPress = { indexs =>
154
+ handleTabPress ( indexs , multiple , selectedIndex , onTabPress )
155
+ }
156
+ firstTabStyle = {
157
+ index === 0 ? [ { borderRightWidth : 0 } , firstTabStyle ] : { }
158
+ }
159
+ lastTabStyle = {
160
+ index === values . length - 1
161
+ ? [ { borderLeftWidth : 0 } , lastTabStyle ]
162
+ : { }
163
+ }
164
+ tabStyle = { [
165
+ tabStyle ,
166
+ index !== 0 && index !== values . length - 1
167
+ ? { marginLeft : - 1 }
168
+ : { } ,
169
+ ] }
170
+ activeTabStyle = { activeTabStyle }
171
+ tabTextStyle = { tabTextStyle }
172
+ activeTabTextStyle = { activeTabTextStyle }
173
+ tabBadgeContainerStyle = { tabBadgeContainerStyle }
174
+ activeTabBadgeContainerStyle = { activeTabBadgeContainerStyle }
175
+ tabBadgeStyle = { tabBadgeStyle }
176
+ activeTabBadgeStyle = { activeTabBadgeStyle }
177
+ allowFontScaling = { allowFontScaling }
178
+ activeTabOpacity = { activeTabOpacity }
179
+ accessible = { accessible }
180
+ accessibilityLabel = { accessibilityText || item }
181
+ enabled = { enabled }
182
+ />
183
+ ) ;
184
+ } ) }
185
+ </ View >
186
+ ) ;
187
+ }
188
+ }
0 commit comments