3lang3 / react-vant

React mobile UI Components base on Vant
https://react-vant.3lang.dev
MIT License
1.32k stars 189 forks source link

🐛 [Bug]: DateTimePicker 设置了popup 属性后参数defaultValue失效了 #608

Closed spoem9527 closed 1 year ago

spoem9527 commented 1 year ago

Which React Vant packages are impacted?

What version of React Vant are you using?

3.1.1

Reproduce live demo(codesandebox or stackblitz)?

No response

Descripition

代码样例 import React, { useState, useEffect, useRef } from "react"; import { DatetimePicker, Field } from "react-vant"; const Demo = () => { const [value, setValue] = useState(null); return ( <DatetimePicker popup={{ round: true, }} type="date" title="选择年月日" minDate={new Date(2021, 0, 1)} maxDate={new Date(2025, 10, 1)} value={value} defaultValue={new Date()} onConfirm={setValue}

{(val, _, actions) => { return ( <Field readOnly clickable label="选择年月日" value={val} placeholder="请选择日期" onClick={() => actions.open()} /> ); }} ); };

export default Demo;

注意看图片中,默认选择的日期 设置了popup属性 image

没有设置popup属性 image

可能问题出现在DatePicker.js的154行const value = props.popup ? formatValue(props.value) : currentDateRef.current; 传递给formatValue的值没有defaultValue 应该参照 DatePicker.js的33行const [currentDate, setCurrentDate, currentDateRef] = useRefState(() => formatValue(value || defaultValue)) 这样给formatValue传值