|
1 | | -import React, { useState, useEffect, ReactNode } from 'react' |
| 1 | +import React, { ReactNode, useEffect, useState, useCallback } from 'react' |
2 | 2 | import classNames from 'classnames' |
3 | 3 | import { ArrowLeft, ArrowRight, DoubleLeft, DoubleRight } from './icon' |
4 | 4 | import { BasicComponent, ComponentDefaults } from '@/utils/typings' |
@@ -34,6 +34,7 @@ export interface CalendarCardProps extends BasicComponent { |
34 | 34 | onPageChange: (data: CalendarCardMonth) => void |
35 | 35 | onChange: (value: CalendarCardValue) => void |
36 | 36 | } |
| 37 | + |
37 | 38 | const defaultProps = { |
38 | 39 | ...ComponentDefaults, |
39 | 40 | type: 'single', |
@@ -122,33 +123,36 @@ export const CalendarCard = React.forwardRef< |
122 | 123 | } |
123 | 124 | } |
124 | 125 |
|
125 | | - const getDays = (month: CalendarCardMonth) => { |
126 | | - const y = month.year |
127 | | - const m = month.month |
128 | | - const days = [ |
129 | | - ...getPrevMonthDays(y, m, firstDayOfWeek), |
130 | | - ...getCurrentMonthDays(y, m), |
131 | | - ] as CalendarCardDay[] |
132 | | - const size = days.length |
133 | | - const yearOfNextMonth = month.month === 12 ? month.year + 1 : month.year |
134 | | - const monthOfNextMonth = month.month === 12 ? 1 : month.month + 1 |
135 | | - // 补全 6 行 7 列视图 |
136 | | - for (let i = 1; i <= 42 - size; i++) { |
137 | | - days.push({ |
138 | | - type: 'next', |
139 | | - year: yearOfNextMonth, |
140 | | - month: monthOfNextMonth, |
141 | | - date: i, |
142 | | - }) |
143 | | - } |
144 | | - return days |
145 | | - } |
| 126 | + const getDays = useCallback( |
| 127 | + (month: CalendarCardMonth) => { |
| 128 | + const y = month.year |
| 129 | + const m = month.month |
| 130 | + const days = [ |
| 131 | + ...getPrevMonthDays(y, m, firstDayOfWeek), |
| 132 | + ...getCurrentMonthDays(y, m), |
| 133 | + ] as CalendarCardDay[] |
| 134 | + const size = days.length |
| 135 | + const yearOfNextMonth = month.month === 12 ? month.year + 1 : month.year |
| 136 | + const monthOfNextMonth = month.month === 12 ? 1 : month.month + 1 |
| 137 | + // 补全 6 行 7 列视图 |
| 138 | + for (let i = 1; i <= 42 - size; i++) { |
| 139 | + days.push({ |
| 140 | + type: 'next', |
| 141 | + year: yearOfNextMonth, |
| 142 | + month: monthOfNextMonth, |
| 143 | + date: i, |
| 144 | + }) |
| 145 | + } |
| 146 | + return days |
| 147 | + }, |
| 148 | + [firstDayOfWeek] |
| 149 | + ) |
146 | 150 |
|
147 | 151 | useEffect(() => { |
148 | 152 | const newDays = getDays(month) |
149 | 153 | setDays(newDays) |
150 | 154 | onPageChange?.(month) |
151 | | - }, [month]) |
| 155 | + }, [month, getDays, onPageChange, firstDayOfWeek]) |
152 | 156 |
|
153 | 157 | const isSameDay = (day1: CalendarCardDay, day2: CalendarCardDay) => { |
154 | 158 | return ( |
|
0 commit comments