Skip to content

Commit 5ef418f

Browse files
committed
fix: remove Calendar year picker
1 parent 6bf2f15 commit 5ef418f

File tree

5 files changed

+3
-227
lines changed

5 files changed

+3
-227
lines changed

packages/bui-core/src/Calendar/Calendar.tsx

+2-61
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@ import { useDidMountEffect, useValue } from '@bifrostui/utils';
33
import clsx from 'clsx';
44
import dayjs from 'dayjs';
55
import isoWeek from 'dayjs/plugin/isoWeek';
6-
import React, {
7-
Suspense,
8-
lazy,
9-
SyntheticEvent,
10-
useMemo,
11-
useState,
12-
} from 'react';
6+
import React, { SyntheticEvent, useMemo, useState } from 'react';
137
import { CalendarProps, ICalendarInstance } from './Calendar.types';
148
import { formatDate, isRange, isSame } from './utils';
159
import { useLocaleText } from '../locales';
1610
import './Calendar.less';
1711

18-
const Picker = lazy(() => import('../Picker'));
19-
2012
dayjs.extend(isoWeek);
2113

2214
const classes = {
@@ -41,12 +33,10 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
4133
headerBarLeftIcon,
4234
headerBarRightIcon,
4335
disabledDate,
44-
enableSelectYear,
4536
highlightDate,
4637
dateRender,
4738
weekRender,
4839
onMonthChange,
49-
onYearChange,
5040
onChange,
5141
...others
5242
} = props;
@@ -72,8 +62,6 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
7262
maxDate,
7363
);
7464

75-
// 控制年份选择picker
76-
const [openPicker, setOpenPicker] = useState<boolean>(false);
7765
// 头部操作栏月份
7866
const [renderMonth, setRenderMonth] = useState(() => {
7967
const initMonth =
@@ -186,21 +174,6 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
186174
return list;
187175
};
188176

189-
const getYearsList = () => {
190-
const result = [];
191-
// 使用传入参数的时间
192-
let startTime = new Date(minDate).getFullYear();
193-
const endTime = new Date(maxDate).getFullYear();
194-
while (endTime - startTime >= 0) {
195-
result.push({
196-
label: startTime,
197-
value: startTime,
198-
});
199-
startTime += 1;
200-
}
201-
return result;
202-
};
203-
204177
const getDayClassName = ({ day: itemDate, disabled }) => {
205178
let result = '';
206179
if (disabled) return result;
@@ -334,27 +307,6 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
334307
}
335308
};
336309

337-
/**
338-
* 点击顶部日期
339-
*/
340-
const onClickDate = (e) => {
341-
if (!enableSelectYear) {
342-
return;
343-
}
344-
e.stopPropagation();
345-
setOpenPicker(true);
346-
getYearsList();
347-
};
348-
const onClosePicker = (e, data) => {
349-
const selectYear = data.value[0];
350-
e.stopPropagation();
351-
setRenderMonth(dayjs(renderMonth).set('year', selectYear).toDate());
352-
onYearChange?.(e, {
353-
year: selectYear,
354-
});
355-
setOpenPicker(false);
356-
};
357-
358310
let data: Record<string, string> = {};
359311
if (isRangeMode) {
360312
data = {
@@ -382,7 +334,7 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
382334
<div onClick={onClickPrev} className={`${classes.handler}-btn`}>
383335
{headerBarIcon.left}
384336
</div>
385-
<div className={`${classes.handler}-text`} onClick={onClickDate}>
337+
<div className={`${classes.handler}-text`}>
386338
{dayjs(renderMonth).format(headerBarFormat)}
387339
</div>
388340
<div onClick={onClickNext} className={`${classes.handler}-btn`}>
@@ -404,16 +356,6 @@ const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(
404356
</div>
405357

406358
<div className={clsx(`${classes.root}-month`)}>{renderDayList()}</div>
407-
{enableSelectYear && (
408-
<Suspense fallback={null}>
409-
<Picker
410-
options={[getYearsList()]}
411-
open={openPicker}
412-
value={[dayjs(renderMonth).year()]}
413-
onClose={onClosePicker}
414-
/>
415-
</Suspense>
416-
)}
417359
</div>
418360
);
419361
},
@@ -423,7 +365,6 @@ Calendar.displayName = 'BuiCalendar';
423365
Calendar.defaultProps = {
424366
hideDaysOutsideCurrentMonth: false,
425367
headerBarFormat: 'YYYY/MM',
426-
enableSelectYear: false,
427368
mode: 'single',
428369
minDate: dayjs(dayjs().format('YYYYMMDD')).add(0, 'month').toDate(),
429370
maxDate: dayjs(dayjs().format('YYYYMMDD')).add(11, 'month').toDate(),

packages/bui-core/src/Calendar/Calendar.types.ts

-15
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export interface ICalendarMonthChangeData {
1414
/** 操作类型,prev: 点击上个月 next: 点击下个月 */
1515
type: 'prev' | 'next';
1616
}
17-
export interface ICalendarYearChangeData {
18-
/** 切换后的年份 */
19-
year: number;
20-
}
2117

2218
export interface ICustomIconProps {
2319
/** 是否为可选范围内的最小月份 */
@@ -96,17 +92,6 @@ export type CalendarProps<
9692
* 自定义周单元格的内容
9793
*/
9894
weekRender?: (week: string) => React.ReactNode;
99-
/**
100-
* 是否开启选择年
101-
*/
102-
enableSelectYear?: boolean;
103-
/**
104-
* 年份发生变化的回调
105-
*/
106-
onYearChange?: (
107-
e: React.SyntheticEvent,
108-
data: ICalendarYearChangeData,
109-
) => void;
11095
/**
11196
* 月份发生变化的回调
11297
*/

packages/bui-core/src/Calendar/__tests__/Calendar.test.tsx

+1-87
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dayjs from 'dayjs';
22
import React from 'react';
3-
import { act, screen, fireEvent, isConformant, render } from 'testing';
3+
import { screen, fireEvent, isConformant, render } from 'testing';
44
import { Calendar } from '..';
55

66
describe('Calendar', () => {
@@ -175,92 +175,6 @@ describe('Calendar', () => {
175175
expect(fakeMonthChange).toReturnWith('next');
176176
});
177177

178-
it('should render Picker when `enableSelectYear`', async () => {
179-
const fakeYearChange = jest.fn((e, data) => data.type);
180-
const { container } = render(
181-
<Calendar
182-
mode="single"
183-
enableSelectYear
184-
value={dayjs('20240402').toDate()}
185-
minDate={dayjs('20230401').toDate()}
186-
maxDate={dayjs('20261001').toDate()}
187-
onYearChange={fakeYearChange}
188-
/>,
189-
);
190-
const texts = container.querySelector(`.${rootClass}-handler-text`);
191-
192-
fireEvent.click(texts);
193-
194-
await act(async () => {
195-
await jest.runAllTimers();
196-
});
197-
const picker = document.querySelector(`.bui-picker-confirm`);
198-
expect(picker).toHaveTextContent('确认');
199-
});
200-
201-
it('should be called when onYearChange change yaer', async () => {
202-
const fakeYearChange = jest.fn((e, data) => data.type);
203-
const { container } = render(
204-
<Calendar
205-
mode="single"
206-
enableSelectYear
207-
value={dayjs('20240402').toDate()}
208-
minDate={dayjs('20230401').toDate()}
209-
maxDate={dayjs('20261001').toDate()}
210-
onYearChange={fakeYearChange}
211-
/>,
212-
);
213-
const texts = container.querySelector(`.${rootClass}-handler-text`);
214-
215-
fireEvent.click(texts);
216-
217-
await act(async () => {
218-
await jest.runAllTimers();
219-
});
220-
221-
const [panel1] = document.querySelectorAll(`.bui-picker-panel`);
222-
const [roller1] = document.querySelectorAll(`.bui-picker-panel-roller`);
223-
const confirmBtn1 = document.querySelector(`.bui-picker-confirm`);
224-
fireEvent.touchStart(panel1, {
225-
touches: [
226-
{
227-
clientX: 0,
228-
clientY: 0,
229-
},
230-
],
231-
cancelable: true,
232-
bubbles: true,
233-
});
234-
fireEvent.touchMove(panel1, {
235-
touches: [
236-
{
237-
clientX: 0,
238-
clientY: -36,
239-
},
240-
],
241-
cancelable: true,
242-
bubbles: true,
243-
});
244-
fireEvent.touchEnd(panel1, {
245-
touches: [
246-
{
247-
clientX: 0,
248-
clientY: -30,
249-
},
250-
],
251-
cancelable: true,
252-
bubbles: true,
253-
});
254-
fireEvent.transitionEnd(roller1);
255-
fireEvent.click(confirmBtn1);
256-
257-
await act(async () => {
258-
await jest.runAllTimers();
259-
});
260-
261-
expect(fakeYearChange).toBeCalled();
262-
});
263-
264178
it('should render handler bar by `headerBarFormat`', () => {
265179
const { container } = render(
266180
<Calendar

packages/bui-core/src/Calendar/index.en-US.md

-32
Original file line numberDiff line numberDiff line change
@@ -175,38 +175,6 @@ export default () => {
175175
};
176176
```
177177

178-
### Enable direct year switching function
179-
180-
Enable 'enableSelectYear' by clicking on the date text area to open the year floating layer and switch years.
181-
182-
```tsx
183-
import { Calendar, Stack } from '@bifrostui/react';
184-
import dayjs from 'dayjs/esm/index';
185-
import React, { useState } from 'react';
186-
187-
export default () => {
188-
const [value, setValue] = useState(dayjs().toDate());
189-
const handleChange = (e, res) => {
190-
console.log('date change:', res);
191-
setValue(res.value);
192-
};
193-
194-
return (
195-
<Stack>
196-
<div style={{ width: '320px' }}>
197-
<Calendar
198-
minDate={dayjs().subtract(5, 'year').toDate()}
199-
maxDate={dayjs().add(5, 'year').toDate()}
200-
enableSelectYear
201-
value={value}
202-
onChange={handleChange}
203-
/>
204-
</div>
205-
</Stack>
206-
);
207-
};
208-
```
209-
210178
### Customize disable date
211179

212180
You can customize the disable date through 'disabled date'.

packages/bui-core/src/Calendar/index.zh-CN.md

-32
Original file line numberDiff line numberDiff line change
@@ -175,38 +175,6 @@ export default () => {
175175
};
176176
```
177177

178-
### 开启直接切换年份功能
179-
180-
启用 `enableSelectYear` 通过点击日期文本区域打开年份浮层切换年份。
181-
182-
```tsx
183-
import { Calendar, Stack } from '@bifrostui/react';
184-
import dayjs from 'dayjs/esm/index';
185-
import React, { useState } from 'react';
186-
187-
export default () => {
188-
const [value, setValue] = useState(dayjs().toDate());
189-
const handleChange = (e, res) => {
190-
console.log('date change:', res);
191-
setValue(res.value);
192-
};
193-
194-
return (
195-
<Stack>
196-
<div style={{ width: '320px' }}>
197-
<Calendar
198-
minDate={dayjs().subtract(5, 'year').toDate()}
199-
maxDate={dayjs().add(5, 'year').toDate()}
200-
enableSelectYear
201-
value={value}
202-
onChange={handleChange}
203-
/>
204-
</div>
205-
</Stack>
206-
);
207-
};
208-
```
209-
210178
### 自定义禁用日期
211179

212180
通过 `disabledDate` 可自定义禁用日期。

0 commit comments

Comments
 (0)