@@ -88,13 +88,20 @@ const InternalSegmentedOption: React.FC<{
88
88
) ;
89
89
} ;
90
90
91
+ interface ThumbMoveStatus {
92
+ from : React . CSSProperties | null ;
93
+ to : React . CSSProperties | null ;
94
+ }
95
+
91
96
const Segmented = React . forwardRef < HTMLDivElement , SegmentedProps > (
92
97
( props , ref ) => {
93
98
const {
94
99
prefixCls = 'rc-segmented' ,
95
100
direction,
96
101
options,
97
102
disabled,
103
+ defaultValue,
104
+ value,
98
105
onChange,
99
106
className = '' ,
100
107
motionName = 'thumb-motion' ,
@@ -104,9 +111,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
104
111
const containerRef = React . useRef < HTMLDivElement > ( null ) ;
105
112
const mergedRef = composeRef < HTMLDivElement > ( containerRef , ref ) ;
106
113
107
- const thumbMoveStyles = React . useRef <
108
- Record < 'from' | 'to' , React . CSSProperties | null >
109
- > ( {
114
+ const thumbMoveStatus = React . useRef < ThumbMoveStatus > ( {
110
115
from : null ,
111
116
to : null ,
112
117
} ) ;
@@ -115,51 +120,52 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
115
120
return normalizeOptions ( options ) ;
116
121
} , [ options ] ) ;
117
122
118
- const [ selected , setSelected ] = useMergedState (
119
- props . defaultValue || segmentedOptions [ 0 ] ?. value ,
120
- ) ;
123
+ const [ selected , setSelected ] = useMergedState ( segmentedOptions [ 0 ] ?. value , {
124
+ value,
125
+ defaultValue,
126
+ } ) ;
121
127
122
128
const [ visualSelected , setVisualSelected ] = React . useState <
123
129
SegmentedRawOption | undefined
124
130
> ( selected ) ;
125
131
126
132
const [ thumbShow , setThumbShow ] = React . useState ( false ) ;
127
133
128
- const calcThumbMoveStyle = ( event : React . ChangeEvent < HTMLInputElement > ) => {
134
+ const calcThumbMoveStatus = (
135
+ event : React . ChangeEvent < HTMLInputElement > ,
136
+ ) => {
129
137
const toElement = event . target . closest ( `.${ prefixCls } -item` ) ;
130
138
131
139
const fromElement = containerRef . current ?. querySelector (
132
140
`.${ prefixCls } -item-selected` ,
133
141
) ;
134
142
135
- if ( fromElement && toElement && thumbMoveStyles . current ) {
136
- thumbMoveStyles . current . from = calcThumbStyle (
143
+ if ( fromElement && toElement && thumbMoveStatus . current ) {
144
+ thumbMoveStatus . current . from = calcThumbStyle (
137
145
fromElement as HTMLElement ,
138
146
) ;
139
- thumbMoveStyles . current . to = calcThumbStyle ( toElement as HTMLElement ) ;
147
+ thumbMoveStatus . current . to = calcThumbStyle ( toElement as HTMLElement ) ;
140
148
141
149
setThumbShow ( true ) ;
142
150
}
143
151
} ;
144
152
145
153
const handleChange = (
146
154
event : React . ChangeEvent < HTMLInputElement > ,
147
- value : SegmentedRawOption ,
155
+ val : SegmentedRawOption ,
148
156
) => {
149
157
if ( disabled ) {
150
158
return ;
151
159
}
152
160
153
- if ( value !== selected ) {
154
- calcThumbMoveStyle ( event ) ;
155
- }
161
+ calcThumbMoveStatus ( event ) ;
156
162
157
- setSelected ( value ) ;
163
+ setSelected ( val ) ;
158
164
159
165
if ( onChange ) {
160
166
const mutatedTarget = Object . create ( event . target , {
161
167
value : {
162
- value,
168
+ value : val ,
163
169
} ,
164
170
} ) ;
165
171
const mutatedEvent = Object . create ( event , {
@@ -173,15 +179,15 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
173
179
174
180
// --- motion event handlers for thumb move
175
181
const handleThumbEnterStart = ( ) => {
176
- const fromStyle = thumbMoveStyles . current . from ;
182
+ const fromStyle = thumbMoveStatus . current . from ;
177
183
if ( fromStyle ) {
178
184
setVisualSelected ( undefined ) ;
179
185
return fromStyle ;
180
186
}
181
187
} ;
182
188
183
189
const handleThumbEnterActive = ( ) => {
184
- const toStyle = thumbMoveStyles . current . to ;
190
+ const toStyle = thumbMoveStatus . current . to ;
185
191
if ( toStyle ) {
186
192
return toStyle ;
187
193
}
@@ -191,8 +197,8 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
191
197
setThumbShow ( false ) ;
192
198
setVisualSelected ( selected ) ;
193
199
194
- if ( thumbMoveStyles . current ) {
195
- thumbMoveStyles . current = {
200
+ if ( thumbMoveStatus . current ) {
201
+ thumbMoveStatus . current = {
196
202
from : null ,
197
203
to : null ,
198
204
} ;
@@ -251,6 +257,8 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
251
257
252
258
Segmented . displayName = 'Segmented' ;
253
259
254
- Segmented . defaultProps = { } ;
260
+ Segmented . defaultProps = {
261
+ options : [ ] ,
262
+ } ;
255
263
256
264
export default Segmented ;
0 commit comments