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

feat: calendar picker view scroll #6521

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/components/calendar-picker-view/calendar-picker-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
useState,
useImperativeHandle,
useMemo,
useRef,
} from 'react'
import type { ReactNode } from 'react'
import { NativeProps, withNativeProps } from '../../utils/native-props'
Expand All @@ -28,6 +29,7 @@ const classPrefix = 'adm-calendar-picker-view'
export type CalendarPickerViewRef = {
jumpTo: (page: Page | ((page: Page) => Page)) => void
jumpToToday: () => void
scrollTo: (date: Date) => void
getDateRange: () => DateRange
}

Expand Down Expand Up @@ -76,6 +78,7 @@ export const CalendarPickerView = forwardRef<
CalendarPickerViewRef,
CalendarPickerViewProps
>((p, ref) => {
const rootRef = useRef<HTMLDivElement>(null)
const today = dayjs()
const props = mergeProps(defaultProps, p)
const { locale } = useConfig()
Expand Down Expand Up @@ -123,6 +126,14 @@ export const CalendarPickerView = forwardRef<
setCurrent(dayjs().date(1))
},
getDateRange: () => dateRange,
scrollTo: (date: Date) => {
const cell = rootRef.current?.querySelector(
`.${classPrefix}-cell-${dayjs(date).format('YYYY-MM-DD')}`
)
if (cell) {
cell.scrollIntoView({ block: 'center' })
}
},
}))

const header = (
Expand Down Expand Up @@ -286,7 +297,12 @@ export const CalendarPickerView = forwardRef<
<div className={`${classPrefix}-cell-top`}>
{renderTop()}
</div>
<div className={`${classPrefix}-cell-date`}>
<div
className={classNames(
`${classPrefix}-cell-date`,
`${classPrefix}-cell-${d.format('YYYY-MM-DD')}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要加到 className 里,可以加个 data-date

)}
>
{props.renderDate
? props.renderDate(d.toDate())
: d.date()}
Expand Down Expand Up @@ -320,7 +336,7 @@ export const CalendarPickerView = forwardRef<

return withNativeProps(
props,
<div className={classPrefix}>
<div ref={rootRef} className={classPrefix}>
{header}
{mark}
{body}
Expand Down
Loading
Loading