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

fix(CalendarPicker): fix jumpTo & jumpToToday not trigger panel scroll #6660

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 53 additions & 24 deletions src/components/calendar-picker-view/calendar-picker-view.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import classNames from 'classnames'
import dayjs from 'dayjs'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import isoWeek from 'dayjs/plugin/isoWeek'
import type { ReactNode } from 'react'
import React, {
forwardRef,
useState,
useContext,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react'
import type { ReactNode } from 'react'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import dayjs from 'dayjs'
import classNames from 'classnames'
import { usePropsValue } from '../../utils/use-props-value'
import { mergeProps } from '../../utils/with-default-props'
import { useConfig } from '../config-provider'
import isoWeek from 'dayjs/plugin/isoWeek'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import { usePropsValue } from '../../utils/use-props-value'
import {
convertValueToRange,
convertPageToDayjs,
DateRange,
Page,
convertPageToDayjs,
convertValueToRange,
} from './convert'
import useSyncScroll from './useSyncScroll'

dayjs.extend(isoWeek)
dayjs.extend(isSameOrBefore)

const classPrefix = 'adm-calendar-picker-view'

export const Context = React.createContext<{
visible: boolean
}>({
visible: false,
})

export type CalendarPickerViewRef = {
jumpTo: (page: Page | ((page: Page) => Page)) => void
jumpToToday: () => void
Expand Down Expand Up @@ -76,9 +85,11 @@ export const CalendarPickerView = forwardRef<
CalendarPickerViewRef,
CalendarPickerViewProps
>((p, ref) => {
const bodyRef = useRef<HTMLDivElement>(null)
const today = dayjs()
const props = mergeProps(defaultProps, p)
const { locale } = useConfig()

const markItems = [...locale.Calendar.markItems]
if (props.weekStartsOn === 'Sunday') {
const item = markItems.pop()
Expand Down Expand Up @@ -106,6 +117,21 @@ export const CalendarPickerView = forwardRef<
dayjs(dateRange ? dateRange[0] : today).date(1)
)

// =============================== Scroll ===============================
const context = useContext(Context)
const scrollTo = useSyncScroll(current, context.visible, bodyRef)

// ============================== Boundary ==============================
const maxDay = useMemo(
() => (props.max ? dayjs(props.max) : current.add(6, 'month')),
[props.max, current]
)
const minDay = useMemo(
() => (props.min ? dayjs(props.min) : current),
[props.min, current]
)

// ================================ Refs ================================
useImperativeHandle(ref, () => ({
jumpTo: pageOrPageGenerator => {
let page: Page
Expand All @@ -117,14 +143,19 @@ export const CalendarPickerView = forwardRef<
} else {
page = pageOrPageGenerator
}
setCurrent(convertPageToDayjs(page))
const next = convertPageToDayjs(page)
setCurrent(next)
scrollTo(next)
},
jumpToToday: () => {
setCurrent(dayjs().date(1))
const next = dayjs().date(1)
setCurrent(next)
scrollTo(next)
},
getDateRange: () => dateRange,
}))

// =============================== Render ===============================
const header = (
<div className={`${classPrefix}-header`}>
<div className={`${classPrefix}-title`}>
Expand All @@ -133,29 +164,23 @@ export const CalendarPickerView = forwardRef<
</div>
)

const maxDay = useMemo(
() => (props.max ? dayjs(props.max) : current.add(6, 'month')),
[props.max, current]
)
const minDay = useMemo(
() => (props.min ? dayjs(props.min) : current),
[props.min, current]
)

function renderBody() {
const cells: ReactNode[] = []
let monthIterator = minDay
// 遍历月份
while (monthIterator.isSameOrBefore(maxDay, 'month')) {
const year = monthIterator.year()
const month = monthIterator.month()
const month = monthIterator.month() + 1

const renderMap = {
year,
month: month + 1,
month,
}

const yearMonth = `${year}-${month}`

cells.push(
<div key={`${year}-${month}`}>
<div key={yearMonth} data-year-month={yearMonth}>
<div className={`${classPrefix}-title`}>
{locale.Calendar.yearAndMonth?.replace(
/\${(.*?)}/g,
Expand Down Expand Up @@ -306,7 +331,11 @@ export const CalendarPickerView = forwardRef<

return cells
}
const body = <div className={`${classPrefix}-body`}>{renderBody()}</div>
const body = (
<div className={`${classPrefix}-body`} ref={bodyRef}>
{renderBody()}
</div>
)

const mark = (
<div className={`${classPrefix}-mark`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ exports[`Calendar custom top 1`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2023-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -620,7 +622,9 @@ exports[`Calendar jump to a day 1`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2021-1"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -1111,7 +1115,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-2"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -1545,7 +1551,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-3"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -2024,7 +2032,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-4"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -2497,7 +2507,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -2991,7 +3003,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-6"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -3458,7 +3472,9 @@ exports[`Calendar jump to a day 1`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2021-7"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -4013,7 +4029,9 @@ exports[`Calendar jump to a day 2`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2023-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -4494,7 +4512,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-6"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -4967,7 +4987,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-7"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -5461,7 +5483,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-8"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -5943,7 +5967,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-9"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -6419,7 +6445,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-10"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -6916,7 +6944,9 @@ exports[`Calendar jump to a day 2`] = `
</div>
</div>
</div>
<div>
<div
data-year-month="2023-11"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -7447,7 +7477,9 @@ exports[`Calendar range mode 1`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2023-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -7993,7 +8025,9 @@ exports[`Calendar single mode 1`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2023-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down Expand Up @@ -8535,7 +8569,9 @@ exports[`Calendar week start on Monday 1`] = `
<div
class="adm-calendar-picker-view-body"
>
<div>
<div
data-year-month="2023-5"
>
<div
class="adm-calendar-picker-view-title"
>
Expand Down
Loading
Loading