Skip to content

Commit

Permalink
Merge pull request #913 from thundersdata-frontend/rn-issue
Browse files Browse the repository at this point in the history
fix: 修复DatePicker的bug
  • Loading branch information
chj-damon authored Oct 21, 2024
2 parents 548b310 + 0fb7dca commit 8a3e964
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-snails-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-picker': patch
---

fix: 修复DatePicker的bug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react';

import { useMemoizedFn } from '@td-design/rn-hooks';
import { useMemoizedFn, useSafeState } from '@td-design/rn-hooks';
import dayjs, { Dayjs } from 'dayjs';

import { PickerData } from '../WheelPicker/type';
Expand All @@ -19,6 +19,8 @@ export default function useDatePicker<T>({
const minDayjs = useMemo(() => dayjs(minDate), [minDate]);
const maxDayjs = useMemo(() => dayjs(maxDate), [maxDate]);

const [tempValue, setTempValue] = useSafeState(value);

const clipDate = (date: Date) => {
if (mode === 'datetime') {
if (dayjs(date).isBefore(minDayjs)) {
Expand All @@ -39,7 +41,7 @@ export default function useDatePicker<T>({
};

const getDate = () => {
return clipDate(value);
return clipDate(tempValue);
};

const getMinYear = () => {
Expand Down Expand Up @@ -249,6 +251,7 @@ export default function useDatePicker<T>({

const onValueChange = (data: PickerData<T>, index: number) => {
const newDate = getNewDate(parseInt(data.value + '', 10), index);
setTempValue(newDate.toDate());
onChange?.(newDate.toDate(), newDate.format(format));
};

Expand Down
48 changes: 23 additions & 25 deletions packages/react-native-picker/src/picker/components/Normal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,29 @@ function NormalPicker<T>(props: NormalPickerProps<T>) {
if (displayType === 'modal') {
return (
<Modal position="bottom" animationType="slide" visible={visible} onClose={handleClose}>
{
<Flex
height={px(50)}
borderBottomWidth={ONE_PIXEL}
borderBottomColor="border"
backgroundColor="white"
paddingHorizontal="x3"
>
<Pressable activeOpacity={activeOpacity} onPress={handleClose} style={styles.cancel}>
<Text variant="p0" color="primary200">
{cancelText}
</Text>
</Pressable>
<Flex.Item alignItems="center">
<Text variant="p0" color="text">
{title}
</Text>
</Flex.Item>
<Pressable activeOpacity={activeOpacity} onPress={handleOk} style={styles.submit}>
<Text variant="p0" color="primary200">
{okText}
</Text>
</Pressable>
</Flex>
}
<Flex
height={px(50)}
borderBottomWidth={ONE_PIXEL}
borderBottomColor="border"
backgroundColor="white"
paddingHorizontal="x3"
>
<Pressable activeOpacity={activeOpacity} onPress={handleClose} style={styles.cancel}>
<Text variant="p0" color="primary200">
{cancelText}
</Text>
</Pressable>
<Flex.Item alignItems="center">
<Text variant="p0" color="text">
{title}
</Text>
</Flex.Item>
<Pressable activeOpacity={activeOpacity} onPress={handleOk} style={styles.submit}>
<Text variant="p0" color="primary200">
{okText}
</Text>
</Pressable>
</Flex>
{PickerComp}
</Modal>
);
Expand Down

0 comments on commit 8a3e964

Please sign in to comment.