From 0fb7dcad8f0a3cc56bf08a6462d55a98f86f5d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9D=B0?= Date: Mon, 21 Oct 2024 11:48:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DDatePicker=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/pretty-snails-walk.md | 5 ++ .../components/DatePicker/useDatePicker.ts | 7 ++- .../src/picker/components/Normal/index.tsx | 48 +++++++++---------- 3 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 .changeset/pretty-snails-walk.md diff --git a/.changeset/pretty-snails-walk.md b/.changeset/pretty-snails-walk.md new file mode 100644 index 0000000000..f6e79b651b --- /dev/null +++ b/.changeset/pretty-snails-walk.md @@ -0,0 +1,5 @@ +--- +'@td-design/react-native-picker': patch +--- + +fix: 修复DatePicker的bug diff --git a/packages/react-native-picker/src/components/DatePicker/useDatePicker.ts b/packages/react-native-picker/src/components/DatePicker/useDatePicker.ts index 96a17f56f8..9d6c073dea 100644 --- a/packages/react-native-picker/src/components/DatePicker/useDatePicker.ts +++ b/packages/react-native-picker/src/components/DatePicker/useDatePicker.ts @@ -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'; @@ -19,6 +19,8 @@ export default function useDatePicker({ 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)) { @@ -39,7 +41,7 @@ export default function useDatePicker({ }; const getDate = () => { - return clipDate(value); + return clipDate(tempValue); }; const getMinYear = () => { @@ -249,6 +251,7 @@ export default function useDatePicker({ const onValueChange = (data: PickerData, index: number) => { const newDate = getNewDate(parseInt(data.value + '', 10), index); + setTempValue(newDate.toDate()); onChange?.(newDate.toDate(), newDate.format(format)); }; diff --git a/packages/react-native-picker/src/picker/components/Normal/index.tsx b/packages/react-native-picker/src/picker/components/Normal/index.tsx index 98cf60db50..1e78178713 100644 --- a/packages/react-native-picker/src/picker/components/Normal/index.tsx +++ b/packages/react-native-picker/src/picker/components/Normal/index.tsx @@ -47,31 +47,29 @@ function NormalPicker(props: NormalPickerProps) { if (displayType === 'modal') { return ( - { - - - - {cancelText} - - - - - {title} - - - - - {okText} - - - - } + + + + {cancelText} + + + + + {title} + + + + + {okText} + + + {PickerComp} );