mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 03:36:36 +08:00
fix(date-picker): handle string date to avoid crash (#25522)
Co-authored-by: 刘佳佳 <liujiajia@nanjingwanhui.com> Co-authored-by: crazywoola <427733928@qq.com>
This commit is contained in:
parent
17b5309e47
commit
69aad38d03
@ -42,7 +42,14 @@ const DatePicker = ({
|
|||||||
const [view, setView] = useState(ViewType.date)
|
const [view, setView] = useState(ViewType.date)
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
const isInitial = useRef(true)
|
const isInitial = useRef(true)
|
||||||
const inputValue = useRef(value ? value.tz(timezone) : undefined).current
|
|
||||||
|
// Normalize the value to ensure that all subsequent uses are Day.js objects.
|
||||||
|
const normalizedValue = useMemo(() => {
|
||||||
|
if (!value) return undefined
|
||||||
|
return dayjs.isDayjs(value) ? value.tz(timezone) : dayjs(value).tz(timezone)
|
||||||
|
}, [value, timezone])
|
||||||
|
|
||||||
|
const inputValue = useRef(normalizedValue).current
|
||||||
const defaultValue = useRef(getDateWithTimezone({ timezone })).current
|
const defaultValue = useRef(getDateWithTimezone({ timezone })).current
|
||||||
|
|
||||||
const [currentDate, setCurrentDate] = useState(inputValue || defaultValue)
|
const [currentDate, setCurrentDate] = useState(inputValue || defaultValue)
|
||||||
@ -68,8 +75,8 @@ const DatePicker = ({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
clearMonthMapCache()
|
clearMonthMapCache()
|
||||||
if (value) {
|
if (normalizedValue) {
|
||||||
const newValue = getDateWithTimezone({ date: value, timezone })
|
const newValue = getDateWithTimezone({ date: normalizedValue, timezone })
|
||||||
setCurrentDate(newValue)
|
setCurrentDate(newValue)
|
||||||
setSelectedDate(newValue)
|
setSelectedDate(newValue)
|
||||||
onChange(newValue)
|
onChange(newValue)
|
||||||
@ -88,9 +95,9 @@ const DatePicker = ({
|
|||||||
}
|
}
|
||||||
setView(ViewType.date)
|
setView(ViewType.date)
|
||||||
setIsOpen(true)
|
setIsOpen(true)
|
||||||
if (value) {
|
if (normalizedValue) {
|
||||||
setCurrentDate(value)
|
setCurrentDate(normalizedValue)
|
||||||
setSelectedDate(value)
|
setSelectedDate(normalizedValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +199,7 @@ const DatePicker = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const timeFormat = needTimePicker ? t('time.dateFormats.displayWithTime') : t('time.dateFormats.display')
|
const timeFormat = needTimePicker ? t('time.dateFormats.displayWithTime') : t('time.dateFormats.display')
|
||||||
const displayValue = value?.format(timeFormat) || ''
|
const displayValue = normalizedValue?.format(timeFormat) || ''
|
||||||
const displayTime = selectedDate?.format('hh:mm A') || '--:-- --'
|
const displayTime = selectedDate?.format('hh:mm A') || '--:-- --'
|
||||||
const placeholderDate = isOpen && selectedDate ? selectedDate.format(timeFormat) : (placeholder || t('time.defaultPlaceholder'))
|
const placeholderDate = isOpen && selectedDate ? selectedDate.format(timeFormat) : (placeholder || t('time.defaultPlaceholder'))
|
||||||
|
|
||||||
@ -204,7 +211,7 @@ const DatePicker = ({
|
|||||||
>
|
>
|
||||||
<PortalToFollowElemTrigger className={triggerWrapClassName}>
|
<PortalToFollowElemTrigger className={triggerWrapClassName}>
|
||||||
{renderTrigger ? (renderTrigger({
|
{renderTrigger ? (renderTrigger({
|
||||||
value,
|
value: normalizedValue,
|
||||||
selectedDate,
|
selectedDate,
|
||||||
isOpen,
|
isOpen,
|
||||||
handleClear,
|
handleClear,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user