Skip to content

Commit 7ed3d39

Browse files
authored
Merge pull request #886 from thundersdata-frontend/rn-issue
fix: 修复日期区间选择组件在清除日期后的bug
2 parents f282ee7 + 41725ad commit 7ed3d39

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@td-design/react-native-picker': patch
3+
---
4+
5+
fix: 修复日期区间选择组件在清除日期后的bug

packages/react-native-picker/src/date-period-input/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const DatePeriodInput: FC<DatePeriodInputProps> = ({
6262
handleStartPress,
6363
handleEndPress,
6464
handleChange,
65-
handleInputClear1,
66-
handleInputClear2,
65+
clearStartDate,
66+
clearEndDate,
6767
} = useDatePeriodInput({ value, onChange, format });
6868

6969
const styles = StyleSheet.create({
@@ -97,7 +97,7 @@ const DatePeriodInput: FC<DatePeriodInputProps> = ({
9797
</Text>
9898
</Flex>
9999
{!disabled && allowClear && dates[0] && (
100-
<Pressable activeOpacity={1} onPress={handleInputClear1} hitOffset={10} style={styles.icon}>
100+
<Pressable activeOpacity={1} onPress={clearStartDate} hitOffset={10} style={styles.icon}>
101101
<SvgIcon name="closecircleo" color={theme.colors.icon} />
102102
</Pressable>
103103
)}
@@ -115,7 +115,7 @@ const DatePeriodInput: FC<DatePeriodInputProps> = ({
115115
</Text>
116116
</Flex>
117117
{!disabled && allowClear && dates[1] && (
118-
<Pressable activeOpacity={1} onPress={handleInputClear2} hitOffset={10} style={styles.icon}>
118+
<Pressable activeOpacity={1} onPress={clearEndDate} hitOffset={10} style={styles.icon}>
119119
<SvgIcon name="closecircleo" color={theme.colors.icon} />
120120
</Pressable>
121121
)}

packages/react-native-picker/src/date-period-input/useDatePeriodInput.ts

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ export default function useDatePeriodInput({
1414
const [currentIndex, setCurrentIndex] = useSafeState(0);
1515
const [dates, setDates] = useSafeState<[Date | undefined, Date | undefined]>(value ?? [undefined, undefined]);
1616
const [visible, { setTrue, setFalse }] = useBoolean(false);
17-
const [minDate, setMinDate] = useSafeState<string | undefined>(undefined); // 对第二个日期输入框来说,它的最小值就是第一个日期输入框的值
18-
const [maxDate, setMaxDate] = useSafeState<string | undefined>(undefined); // 对第一个日期输入框来说,它的最大值就是第二个日期输入框的值
17+
const [minDate, setMinDate] = useSafeState<string | undefined>(undefined); // 对结束时间来说,它的最小值就是开始时间的值
18+
const [maxDate, setMaxDate] = useSafeState<string | undefined>(undefined); // 对开始时间来说,它的最大值就是结束时间的值
1919

2020
useEffect(() => {
2121
value && setDates(value);
2222
}, [value]);
2323

2424
const handleChange = (date?: Date) => {
25-
const [firstDate, secondDate] = dates;
26-
setDates(draft => {
27-
draft[currentIndex] = date;
28-
return draft;
29-
});
30-
if (currentIndex === 0) {
31-
onChange?.([date!, secondDate]);
25+
const [startDate, endDate] = dates;
26+
if (onChange) {
27+
onChange(currentIndex === 0 ? [date!, endDate] : [startDate, date!]);
3228
} else {
33-
onChange?.([firstDate, date!]);
29+
setDates(draft => {
30+
draft[currentIndex] = date;
31+
return draft;
32+
});
3433
}
3534
};
3635

36+
/** 点开开始时间选择器 */
3737
const handleStartPress = () => {
3838
Keyboard.dismiss();
3939
setCurrentIndex(0);
@@ -44,6 +44,7 @@ export default function useDatePeriodInput({
4444
setTrue();
4545
};
4646

47+
/** 点开结束时间选择器 */
4748
const handleEndPress = () => {
4849
Keyboard.dismiss();
4950
setCurrentIndex(1);
@@ -54,18 +55,41 @@ export default function useDatePeriodInput({
5455
setTrue();
5556
};
5657

57-
const handleInputClear1 = () => {
58-
const [, secondDate] = value ?? [, undefined];
59-
60-
setDates(draft => [undefined, draft[1]]);
61-
onChange?.([undefined, secondDate]);
58+
/**
59+
* 清除开始时间
60+
* 不光是要把date改掉,同时还需要判断结束时间是否有值
61+
* 如果有值,需要设置结束时间为最大时间
62+
* 如果没有值,则最大时间
63+
*/
64+
const clearStartDate = () => {
65+
const [, endDate] = value ?? [, undefined];
66+
if (onChange) {
67+
onChange([undefined, endDate]);
68+
} else {
69+
setDates(draft => [undefined, draft[1]]);
70+
}
71+
setMinDate(undefined);
72+
if (endDate) {
73+
setMaxDate(dayjs(endDate).format(format));
74+
} else {
75+
setMaxDate(undefined);
76+
}
6277
};
6378

64-
const handleInputClear2 = () => {
65-
const [firstDate] = value ?? [undefined];
66-
67-
setDates(draft => [draft[0], undefined]);
68-
onChange?.([firstDate, undefined]);
79+
/** 清除结束时间 */
80+
const clearEndDate = () => {
81+
const [startDate] = value ?? [undefined];
82+
if (onChange) {
83+
onChange([startDate, undefined]);
84+
} else {
85+
setDates(draft => [draft[0], undefined]);
86+
}
87+
setMaxDate(undefined);
88+
if (startDate) {
89+
setMinDate(dayjs(startDate).format(format));
90+
} else {
91+
setMinDate(undefined);
92+
}
6993
};
7094

7195
return {
@@ -78,7 +102,7 @@ export default function useDatePeriodInput({
78102
handleStartPress: useMemoizedFn(handleStartPress),
79103
handleEndPress: useMemoizedFn(handleEndPress),
80104
handleChange: useMemoizedFn(handleChange),
81-
handleInputClear1: useMemoizedFn(handleInputClear1),
82-
handleInputClear2: useMemoizedFn(handleInputClear2),
105+
clearStartDate: useMemoizedFn(clearStartDate),
106+
clearEndDate: useMemoizedFn(clearEndDate),
83107
};
84108
}

0 commit comments

Comments
 (0)