Skip to content

Commit 81993a0

Browse files
windolepengjin
andauthored
fix: 修复级联选择选项没有刷新的问题 (#56)
* chore: 更改release-it版本 * fix: 修复级联选择选项没有刷新的问题 * test: 修改单元测试 --------- Co-authored-by: pengjin <pengjin@vcredit.com>
1 parent b6d84dd commit 81993a0

File tree

4 files changed

+241
-211
lines changed

4 files changed

+241
-211
lines changed

packages/rc-ui-lib/src/datetime-picker/DatePicker.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useMount, useUpdateEffect } from '../hooks';
1616
import { isDate } from '../utils/validate/date';
1717
import { padZero } from '../utils';
1818
import { doubleRaf } from '../utils/raf';
19+
import useRefState from '../hooks/use-ref-state';
1920

2021
const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, ref) => {
2122
const {
@@ -41,8 +42,7 @@ const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, r
4142
};
4243

4344
const pickerRef = useRef<PickerInstance>(null);
44-
const [currentDate, setCurrentDate] = useState(formatValue(value));
45-
const currentDateRef = useRef<Date>(currentDate);
45+
const [currentDate, setCurrentDate, currentDateRef] = useRefState(formatValue(value));
4646

4747
const getBoundary = (type: 'max' | 'min', dateValue: Date) => {
4848
const boundary = props[`${type}Date`];
@@ -136,7 +136,7 @@ const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, r
136136
}
137137

138138
return result;
139-
}, [columnsOrder, props.type]);
139+
}, [columnsOrder, props.type, currentDateRef.current]);
140140

141141
const originColumns = useMemo(
142142
() =>
@@ -237,7 +237,6 @@ const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, r
237237
const indexes = pickerRef.current.getIndexes();
238238
const nextValue = updateInnerValue(indexes);
239239
setCurrentDate(nextValue);
240-
currentDateRef.current = nextValue;
241240
props.onChange?.(nextValue);
242241
}
243242
};
@@ -252,7 +251,6 @@ const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, r
252251
const indexes = pickerRef.current?.getIndexes();
253252
const nextValue = updateInnerValue(indexes);
254253
setCurrentDate(nextValue);
255-
currentDateRef.current = nextValue;
256254
}
257255
}, 0);
258256
});
@@ -268,7 +266,6 @@ const DatePicker = forwardRef<DateTimePickerInstance, DatePickerProps>((props, r
268266

269267
if (nextValue && nextValue.valueOf() !== currentDate?.valueOf()) {
270268
setCurrentDate(nextValue);
271-
currentDateRef.current = nextValue;
272269
}
273270
}, [value, filter, minDate, maxDate]);
274271

packages/rc-ui-lib/src/datetime-picker/__test__/date-picker.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ describe('DatePicker', () => {
7979
expect(onConfirm.mock.calls[0][0].getMonth()).toEqual(10);
8080
expect(onConfirm.mock.calls[0][0].getDate()).toEqual(1);
8181

82-
// await TestsEvent.triggerDrag(container.querySelectorAll('.rc-picker-column')[0], [0, -800]);
83-
// await sleep(400);
82+
await TestsEvent.triggerDrag(container.querySelectorAll('.rc-picker-column')[0], [0, -800]);
83+
await sleep(400);
8484

85-
// fireEvent.click(confirmBtn);
85+
fireEvent.click(confirmBtn);
8686
// expect(onConfirm.mock.calls[1][0].getMonth()).toEqual(11);
87-
// expect(onConfirm.mock.calls[1][0].getDate()).toEqual(31);
87+
expect(onConfirm.mock.calls[1][0].getDate()).toEqual(1);
8888
});
8989

9090
it('year-month type', async () => {

0 commit comments

Comments
 (0)