@@ -3,24 +3,25 @@ import { ViewStyle, TextStyle } from 'react-native';
33import ButtonGroup , { ButtonGroupProps } from '../ButtonGroup' ;
44import Button from '../Button' ;
55
6- export interface SegmentedControlProps extends ButtonGroupProps {
7- value ?: string [ ] ;
6+ export interface SegmentedControlProps < T > extends ButtonGroupProps {
7+ value ?: string [ ] | T [ ] ;
88 selectedIndex ?: number ;
9+ renderItem ?: ( label : string | T , selectedIndex : number , props : ButtonGroupProps ) => JSX . Element ;
910 onValueChange ?: ( selectedIndex : number ) => void ;
1011}
1112
1213export interface SegmentedControlState {
1314 selectedIndex : number ;
1415}
1516
16- export default class SegmentedControl extends Component < SegmentedControlProps , SegmentedControlState > {
17- constructor ( props : SegmentedControlProps ) {
17+ export default class SegmentedControl < T > extends Component < SegmentedControlProps < T > , SegmentedControlState > {
18+ constructor ( props : SegmentedControlProps < T > ) {
1819 super ( props ) ;
1920 this . state = {
2021 selectedIndex : props . selectedIndex || 0 ,
2122 } ;
2223 }
23- static defaultProps : SegmentedControlProps = {
24+ static defaultProps : SegmentedControlProps < { } > = {
2425 value : [ ] ,
2526 size : 'small' ,
2627 selectedIndex : 0 ,
@@ -33,23 +34,26 @@ export default class SegmentedControl extends Component<SegmentedControlProps, S
3334 } ) ;
3435 }
3536 render ( ) {
36- const { value, selectedIndex, ...otherProps } = this . props ;
37+ const { value, selectedIndex, renderItem , ...otherProps } = this . props ;
3738 return (
3839 < ButtonGroup { ...otherProps } >
39- { value ! . map ( ( label , key ) => {
40+ { value && ( value as ( string | T ) [ ] ) . map ( ( label : string | T , key : number ) => {
4041 const styl : ViewStyle = { } ;
4142 const textStyle : TextStyle = { } ;
4243 if ( this . state . selectedIndex !== key + 1 ) {
4344 styl . backgroundColor = '#fff' ;
4445 textStyle . color = otherProps . color ;
4546 }
46- return React . cloneElement ( < Button /> , {
47- key,
47+ const props : ButtonGroupProps = {
4848 type : 'primary' ,
4949 style : [ styl , otherProps . textStyle ] ,
5050 textStyle : [ textStyle ] ,
5151 onPress : this . handlePress . bind ( this , key + 1 ) ,
52- } , label ) ;
52+ }
53+ if ( renderItem ) {
54+ return renderItem ( label , key + 1 , props ) ;
55+ }
56+ return React . cloneElement ( < Button key = { key } /> , { ...props } , label ) ;
5357 } ) }
5458 </ ButtonGroup >
5559 ) ;
0 commit comments