11import React , { Component } from 'react' ;
2- import { ViewStyle , TextStyle } from 'react-native' ;
2+ import { ViewStyle , TextStyle , Text } from 'react-native' ;
33import ButtonGroup , { ButtonGroupProps } from '../ButtonGroup' ;
44import Button from '../Button' ;
55
@@ -8,6 +8,7 @@ export interface SegmentedControlProps<T> extends ButtonGroupProps {
88 selectedIndex ?: number ;
99 renderItem ?: ( label : string | T , selectedIndex : number , props : ButtonGroupProps ) => JSX . Element ;
1010 onValueChange ?: ( label : string | T , selectedIndex : number ) => void ;
11+ textColor ?: [ string , string ] ;
1112}
1213
1314export interface SegmentedControlState {
@@ -35,16 +36,24 @@ export default class SegmentedControl<T> extends Component<SegmentedControlProps
3536 } ;
3637 render ( ) {
3738 // eslint-disable-next-line @typescript-eslint/no-unused-vars
38- const { value, selectedIndex, renderItem, ...otherProps } = this . props ;
39+ const {
40+ value,
41+ selectedIndex,
42+ renderItem,
43+ textColor = [ '#fff' , this . props . color ?? '#108ee9' ] ,
44+ ...otherProps
45+ } = this . props ;
3946 return (
4047 < ButtonGroup { ...otherProps } >
4148 { value &&
4249 ( value as ( string | T ) [ ] ) . map ( ( label : string | T , key : number ) => {
4350 const styl : ViewStyle = { } ;
4451 const textStyle : TextStyle = { } ;
52+ let textStyleColor : string = textColor [ 0 ] ;
4553 if ( this . state . selectedIndex !== key + 1 ) {
4654 styl . backgroundColor = '#fff' ;
4755 textStyle . color = otherProps . color ;
56+ textStyleColor = textColor [ 1 ] ;
4857 }
4958 const props : ButtonGroupProps = {
5059 type : 'primary' ,
@@ -55,7 +64,11 @@ export default class SegmentedControl<T> extends Component<SegmentedControlProps
5564 if ( renderItem ) {
5665 return renderItem ( label , key + 1 , props ) ;
5766 }
58- return React . cloneElement ( < Button key = { key } /> , { ...props } , label ) ;
67+ return React . cloneElement (
68+ < Button key = { key } /> ,
69+ { ...props } ,
70+ < Text style = { { color : textStyleColor } } > { label } </ Text > ,
71+ ) ;
5972 } ) }
6073 </ ButtonGroup >
6174 ) ;
0 commit comments