Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: CalendarPicker组件无法自动滚动到选中日期 #6425

Open
Yueyanc opened this issue Nov 6, 2023 · 5 comments
Open

feature: CalendarPicker组件无法自动滚动到选中日期 #6425

Yueyanc opened this issue Nov 6, 2023 · 5 comments
Labels

Comments

@Yueyanc
Copy link

Yueyanc commented Nov 6, 2023

Version of antd-mobile

No response

What is this feature about?

希望日历组件能自动滚动到当前选中日期

@Yueyanc Yueyanc added the feature label Nov 6, 2023
@Yueyanc
Copy link
Author

Yueyanc commented Nov 6, 2023

暂时可以这样:

  useEffect(() => {
    if (visible) {
      const el = document.getElementById(dayjs(saveDate).format('YYYY-MM-DD'))
      setTimeout(() => {
        el?.scrollIntoView({ behavior: 'smooth', block: 'center' })
      }, 100)
    }
  }, [visible])


<CalendarPicker
renderDate={(date) => {
            return <div id={dayjs(date).format('YYYY-MM-DD')}>{date.getDate()}</div>
}}
/>

@Yueyanc Yueyanc closed this as completed Nov 6, 2023
@Yueyanc Yueyanc reopened this Nov 6, 2023
@crazyyoung1020
Copy link

牛的,完美解决问题

@sdwflmw
Copy link

sdwflmw commented Dec 18, 2023

暂时可以这样:

  useEffect(() => {
    if (visible) {
      const el = document.getElementById(dayjs(saveDate).format('YYYY-MM-DD'))
      setTimeout(() => {
        el?.scrollIntoView({ behavior: 'smooth', block: 'center' })
      }, 100)
    }
  }, [visible])


<CalendarPicker
renderDate={(date) => {
            return <div id={dayjs(date).format('YYYY-MM-DD')}>{date.getDate()}</div>
}}
/>

当设置了min 且min 与现在日期相差较大时,默认在min页,然后el 可能为空,就无法跳转过去了

@sdwflmw
Copy link

sdwflmw commented Dec 18, 2023

改造了一下

const [minDate,setMinDate]=useState(null)
useEffect(() => {
    if (visible) {
          setTimeout(() => {
              setMinDate(new Date('2023-01-01'));
              const el = document.getElementById(dayjs(saveDate).format('YYYY-MM-DD'))
              setTimeout(() => {
                el?.scrollIntoView({ behavior: 'smooth', block: 'center' })
              }, 100)
          }, 100);
    }else{
       setMinDate(null)
    }
  }, [visible])


<CalendarPicker
min={minDate}
renderDate={(date) => {
            return <div id={dayjs(date).format('YYYY-MM-DD')}>{date.getDate()}</div>
}}
/>

@yzqzy
Copy link

yzqzy commented Apr 18, 2024

改造了一下

const [minDate,setMinDate]=useState(null)
useEffect(() => {
    if (visible) {
          setTimeout(() => {
              setMinDate(new Date('2023-01-01'));
              const el = document.getElementById(dayjs(saveDate).format('YYYY-MM-DD'))
              setTimeout(() => {
                el?.scrollIntoView({ behavior: 'smooth', block: 'center' })
              }, 100)
          }, 100);
    }else{
       setMinDate(null)
    }
  }, [visible])


<CalendarPicker
min={minDate}
renderDate={(date) => {
            return <div id={dayjs(date).format('YYYY-MM-DD')}>{date.getDate()}</div>
}}
/>

6 6 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants