@@ -74,6 +74,8 @@ const Picker: React.FC<DatePickerProps> = ({
7474 onConfirm,
7575 onClose,
7676 defaultValue,
77+ controlledValue,
78+ onColumnChange,
7779 prefixes,
7880 mode = 'bottom' ,
7981 style,
@@ -97,19 +99,39 @@ const Picker: React.FC<DatePickerProps> = ({
9799 : commonColors . darkGray ;
98100 } , [ themeName ] ) ;
99101 const [ pickerValue , setPickerValue ] = useState < ( string | number ) [ ] > ( [ ] ) ;
102+ // 用 ref 追踪上一次值,用于检测哪列发生了变化
103+ const prevPickerValue = React . useRef < ( string | number ) [ ] > ( [ ] ) ;
104+
100105 const title = useMemo (
101106 ( ) => titleDisplayLogic ( pickerValue , data ) ,
102107 [ pickerValue , data ]
103108 ) ;
104109 const isBottomMode = useMemo ( ( ) => {
105110 return mode !== 'middle' ;
106111 } , [ mode ] ) ;
107- // 默认选择逻辑
112+ // 默认选择逻辑(仅 mount 时)
108113 useEffect ( ( ) => {
109- handlePick ( defaultValue ? defaultValue : data . map ( item => item [ 0 ] . value ) ) ;
114+ const initial = defaultValue ?? data . map ( item => item [ 0 ] . value ) ;
115+ prevPickerValue . current = initial ;
116+ setPickerValue ( initial ) ;
110117 } , [ ] ) ;
118+ // 外部 controlledValue 变化时同步内部状态(用于级联重置)
119+ useEffect ( ( ) => {
120+ if ( ! controlledValue ) return ;
121+ const serialized = JSON . stringify ( controlledValue ) ;
122+ if ( serialized === JSON . stringify ( prevPickerValue . current ) ) return ;
123+ prevPickerValue . current = [ ...controlledValue ] ;
124+ setPickerValue ( [ ...controlledValue ] ) ;
125+ } , [ JSON . stringify ( controlledValue ) ] ) ;
126+
111127 const handlePick = ( pickedValue : ( string | number ) [ ] ) => {
128+ const prev = prevPickerValue . current ;
129+ const changedIndex = pickedValue . findIndex ( ( v , i ) => v !== prev [ i ] ) ;
130+ prevPickerValue . current = [ ...pickedValue ] ;
112131 setPickerValue ( pickedValue ) ;
132+ if ( onColumnChange && changedIndex >= 0 ) {
133+ onColumnChange ( pickedValue , changedIndex ) ;
134+ }
113135 } ;
114136 const handleConfirm = ( ) => {
115137 if ( onConfirm ) onConfirm ( pickerValue . map ( item => String ( item ) ) ) ;
0 commit comments