Skip to content

Commit 1fdf7e1

Browse files
committed
feat: 修改scrollTo 调用时机
1 parent 6f83cda commit 1fdf7e1

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

Diff for: src/components/calendar-picker-view/calendar-picker-view.tsx

+22-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
useImperativeHandle,
55
useMemo,
66
useRef,
7+
useEffect,
78
} from 'react'
89
import type { ReactNode } from 'react'
910
import { NativeProps, withNativeProps } from '../../utils/native-props'
@@ -29,7 +30,7 @@ const classPrefix = 'adm-calendar-picker-view'
2930
export type CalendarPickerViewRef = {
3031
jumpTo: (page: Page | ((page: Page) => Page)) => void
3132
jumpToToday: () => void
32-
scrollTo: (date: Date) => void
33+
scrollTo: (page: Page) => void
3334
getDateRange: () => DateRange
3435
}
3536

@@ -108,6 +109,21 @@ export const CalendarPickerView = forwardRef<
108109
const [current, setCurrent] = useState(() =>
109110
dayjs(dateRange ? dateRange[0] : today).date(1)
110111
)
112+
useEffect(() => {
113+
if (dateRange) {
114+
const curr = dayjs(dateRange[0])
115+
scrollTo({ year: curr.year(), month: curr.month() + 1 })
116+
}
117+
}, [dateRange])
118+
119+
const scrollTo = (page: Page) => {
120+
const cell = rootRef.current?.querySelector(
121+
`[data-date="${convertPageToDayjs(page).format('YYYY-MM')}"`
122+
)
123+
if (cell) {
124+
cell.scrollIntoView()
125+
}
126+
}
111127

112128
useImperativeHandle(ref, () => ({
113129
jumpTo: pageOrPageGenerator => {
@@ -121,19 +137,15 @@ export const CalendarPickerView = forwardRef<
121137
page = pageOrPageGenerator
122138
}
123139
setCurrent(convertPageToDayjs(page))
140+
scrollTo(page)
124141
},
125142
jumpToToday: () => {
126-
setCurrent(dayjs().date(1))
143+
const curr = dayjs().date(1)
144+
setCurrent(curr)
145+
scrollTo({ year: curr.year(), month: curr.month() + 1 })
127146
},
128147
getDateRange: () => dateRange,
129-
scrollTo: (date: Date) => {
130-
const cell = rootRef.current?.querySelector(
131-
`[data-date="${dayjs(date).format('YYYY-MM')}"`
132-
)
133-
if (cell) {
134-
cell.scrollIntoView()
135-
}
136-
},
148+
scrollTo: scrollTo,
137149
}))
138150

139151
const header = (

Diff for: src/components/calendar-picker/calendar-picker.tsx

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, useRef, useEffect } from 'react'
1+
import React, { forwardRef, useRef } from 'react'
22
import { withNativeProps } from '../../utils/native-props'
33
import classNames from 'classnames'
44
import Button from '../button'
@@ -11,7 +11,6 @@ import CalendarPickerView, {
1111
CalendarPickerViewProps,
1212
CalendarPickerViewRef,
1313
} from '../calendar-picker-view'
14-
import { sleep } from '../../utils/sleep'
1514

1615
const classPrefix = 'adm-calendar-picker'
1716

@@ -74,14 +73,7 @@ export const CalendarPicker = forwardRef<
7473
getContainer,
7574
...calendarViewProps
7675
} = props
77-
useEffect(() => {
78-
sleep(0).then(() => {
79-
const dateRange = calendarRef.current?.getDateRange() ?? null
80-
if (dateRange && dateRange[0]) {
81-
calendarRef.current?.scrollTo(dateRange[0])
82-
}
83-
})
84-
}, [visible])
76+
8577
const footer = (
8678
<div className={`${classPrefix}-footer`}>
8779
<Divider />

0 commit comments

Comments
 (0)