1- import React , { Component } from 'react' ;
1+ import React , { useState } from 'react' ;
22import { ViewStyle , TextStyle , Text } from 'react-native' ;
33import ButtonGroup , { ButtonGroupProps } from '../ButtonGroup' ;
44import Button from '../Button' ;
@@ -19,68 +19,64 @@ export interface SegmentedControlState {
1919 selectedIndex : number ;
2020}
2121
22- export default class SegmentedControl < T extends React . ReactPortal > extends Component <
23- SegmentedControlProps < T > ,
24- SegmentedControlState
25- > {
26- constructor ( props : SegmentedControlProps < T > ) {
27- super ( props ) ;
28- this . state = {
29- selectedIndex : props . selectedIndex || 0 ,
30- } ;
31- }
32- static defaultProps : SegmentedControlProps < { } > = {
33- value : [ ] ,
34- size : 'small' ,
35- selectedIndex : 0 ,
36- color : '#108ee9' ,
37- } ;
38- handlePress = ( label : string | T , selectedIndex : number ) => {
39- const { onValueChange } = this . props ;
40- this . setState ( { selectedIndex } , ( ) => {
41- onValueChange && onValueChange ( label , selectedIndex ) ;
42- } ) ;
22+ export default function SegmentedControl < T extends React . ReactPortal > ( props : SegmentedControlProps < T > ) {
23+ const [ state , setState ] = useState ( {
24+ selectedIndex : props . selectedIndex || 0 ,
25+ } ) ;
26+
27+ const handlePress = ( label : string | T , selectedIndex : number ) => {
28+ setState ( { selectedIndex } ) ;
29+
30+ const { onValueChange } = props ;
31+ onValueChange && onValueChange ( label , selectedIndex ) ;
32+ return undefined ;
4333 } ;
44- render ( ) {
45- // eslint-disable-next-line @typescript-eslint/no-unused-vars
46- const {
47- value,
48- selectedIndex,
49- renderItem,
50- textColor = {
51- actived : '#fff' ,
52- unactived : this . props . color ?? '#108ee9' ,
53- } ,
54- ...otherProps
55- } = this . props ;
56- return (
57- < ButtonGroup { ...otherProps } >
58- { value &&
59- ( value as ( string | T ) [ ] ) . map ( ( label : string | T , key : number ) => {
60- const styl : ViewStyle = { } ;
61- const textStyle : TextStyle = { } ;
62- let textStyleColor : string = textColor . actived ! ;
63- if ( this . state . selectedIndex !== key + 1 ) {
64- styl . backgroundColor = '#fff' ;
65- textStyle . color = otherProps . color ;
66- textStyleColor = textColor . unactived ! ;
67- }
68- const props : ButtonGroupProps = {
69- type : 'primary' ,
70- style : [ styl , otherProps . textStyle ] ,
71- textStyle : [ textStyle ] ,
72- onPress : this . handlePress . bind ( this , label , key + 1 ) ,
73- } ;
74- if ( renderItem ) {
75- return renderItem ( label , key + 1 , props ) ;
76- }
77- return React . cloneElement (
78- < Button key = { key } /> ,
79- { ...props } ,
80- < Text style = { { color : textStyleColor } } > { label } </ Text > ,
81- ) ;
82- } ) }
83- </ ButtonGroup >
84- ) ;
85- }
34+
35+ const {
36+ value,
37+ selectedIndex,
38+ renderItem,
39+ textColor = {
40+ actived : '#fff' ,
41+ unactived : props . color ?? '#108ee9' ,
42+ } ,
43+ ...otherProps
44+ } = props ;
45+
46+ return (
47+ < ButtonGroup { ...otherProps } >
48+ { value &&
49+ ( value as ( string | T ) [ ] ) . map ( ( label : string | T , key : number ) => {
50+ const styl : ViewStyle = { } ;
51+ const textStyle : TextStyle = { } ;
52+ let textStyleColor : string = textColor . actived ! ;
53+ if ( state . selectedIndex !== key + 1 ) {
54+ styl . backgroundColor = '#fff' ;
55+ textStyle . color = otherProps . color ;
56+ textStyleColor = textColor . unactived ! ;
57+ }
58+ const props : ButtonGroupProps = {
59+ type : 'primary' ,
60+ style : [ styl , otherProps . textStyle ] ,
61+ textStyle : [ textStyle ] ,
62+ onPress : ( ) => handlePress ( label , key + 1 ) ,
63+ } ;
64+ if ( renderItem ) {
65+ return renderItem ( label , key + 1 , props ) ;
66+ }
67+ return React . cloneElement (
68+ < Button key = { key } /> ,
69+ { ...props } ,
70+ < Text style = { { color : textStyleColor } } > { label } </ Text > ,
71+ ) ;
72+ } ) }
73+ </ ButtonGroup >
74+ ) ;
8675}
76+
77+ SegmentedControl . defaultProps = {
78+ value : [ ] ,
79+ size : 'small' ,
80+ selectedIndex : 0 ,
81+ color : '#108ee9' ,
82+ } as SegmentedControlProps < { } > ;
0 commit comments