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

Daterangepicker limit start year #3163

Merged
merged 3 commits into from
Apr 20, 2023
Merged
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
3 changes: 2 additions & 1 deletion docs/pages/components/date-picker/en-US/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ Learn more in [Accessibility](/guide/accessibility).
| hideMinutes | (minute:number, date:Date) => boolean | Hidden minutes |
| hideSeconds | (second:number, date:Date) => boolean | Hidden seconds |
| isoWeek | boolean | ISO 8601 standard, each calendar week begins on Monday and Sunday on the seventh day |
| limitEndYear | number `(1000)` | Set the lower limit of the available year relative to the current selection date |
| limitEndYear | number `(1000)` | Set the upper limit of the available year relative to the current selection date |
| limitStartYear | number | Set the lower limit of the available year relative to the current selection date |
| locale | [CalendarLocaleType](/guide/i18n/#calendar) | Locale text |
| onChange | (date: Date) => void | Callback fired when value changed |
| onChangeCalendarDate | (date: Date, event) => void | Callback function that changes the calendar date. |
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/components/date-range-picker/en-US/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ Learn more in [Accessibility](/guide/accessibility).
| format | string `('yyyy-MM-dd')` | Format date [refer to date-fns format](https://date-fns.org/v2.24.0/docs/format) |
| hoverRange | unions: 'week', 'month' or (date: Date) => [ValueType](#code-ts-value-type-code) | The date range that will be selected when you click on the date |
| isoWeek | boolean | ISO 8601 standard, each calendar week begins on Monday and Sunday on the seventh day |
| limitEndYear | number `(1000)` | Sets the lower limit of the available year relative to the current selection date |
| limitEndYear | number `(1000)` | Sets the upper limit of the available year relative to the current selection date |
| limitStartYear | number | Sets the lower limit of the available year relative to the current selection date |
| locale | [CalendarLocaleType](/guide/i18n/#calendar) | Locale text |
| onChange | (value: [ValueType](#code-ts-value-type-code)) => void | Callback fired when value changed |
| onClean | (event) => void | Callback fired when value clean |
Expand Down
6 changes: 6 additions & 0 deletions src/Calendar/CalendarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export interface CalendarProps
/** Limit showing how many years in the future */
limitEndYear?: number;

/** Limit showing how many years in the past */
limitStartYear?: number;

/** Custom locale */
locale: CalendarLocale;

Expand Down Expand Up @@ -118,6 +121,7 @@ const CalendarContainer: RsRefForwardingComponent<'div', CalendarProps> = React.
hoverRangeValue,
isoWeek = false,
limitEndYear,
limitStartYear,
locale,
onChangeMonth,
onChangeTime,
Expand Down Expand Up @@ -274,6 +278,7 @@ const CalendarContainer: RsRefForwardingComponent<'div', CalendarProps> = React.
<MonthDropdown
show={showMonth}
limitEndYear={limitEndYear}
limitStartYear={limitStartYear}
disabledMonth={isDisabledDate}
/>
)}
Expand Down Expand Up @@ -310,6 +315,7 @@ CalendarContainer.propTypes = {
hideSeconds: PropTypes.func,
isoWeek: PropTypes.bool,
limitEndYear: PropTypes.number,
limitStartYear: PropTypes.number,
locale: PropTypes.object,
onChangeMonth: PropTypes.func,
onChangeTime: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion src/Calendar/MonthDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const MonthDropdown: RsRefForwardingComponent<'div', MonthDropdownProps> = React
const { date = new Date() } = useCalendarContext();
const { prefix, merge, withClassPrefix } = useClassNames(classPrefix);
const thisYear = DateUtils.getYear(new Date());
const startYear = limitStartYear ? thisYear - limitStartYear : 1900;
const startYear = limitStartYear ? thisYear - limitStartYear + 1 : 1900;

const rowCount = useMemo(() => {
const endYear = thisYear + limitEndYear;
Expand Down
67 changes: 67 additions & 0 deletions src/Calendar/test/CalendarMonthDropdownSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fireEvent, render } from '@testing-library/react';
import sinon from 'sinon';
import MonthDropdown from '../MonthDropdown';
import CalendarContext from '../CalendarContext';
import { DateUtils } from '../../utils';

describe('Calendar-MonthDropdown', () => {
it('Should output year and month ', () => {
Expand All @@ -23,6 +24,72 @@ describe('Calendar-MonthDropdown', () => {
expect(getAllByRole('gridcell', { hidden: true })[0].childNodes).to.be.lengthOf(12);
});

it('Should output year and month of current year', () => {
const { getAllByRole } = render(
<CalendarContext.Provider
value={{
date: new Date(),
locale: {},
isoWeek: false
}}
>
<MonthDropdown show limitStartYear={1} limitEndYear={1} />
</CalendarContext.Provider>
);
const currentYear = DateUtils.getYear(new Date());
expect(getAllByRole('row', { hidden: true })).to.be.lengthOf(1);
expect(getAllByRole('rowheader', { hidden: true })[0].innerText).to.be.eq(
currentYear.toString()
);
});

it('Should output year and month of two previous years', () => {
const { getAllByRole } = render(
<CalendarContext.Provider
value={{
date: new Date(),
locale: {},
isoWeek: false
}}
>
<MonthDropdown show limitStartYear={3} limitEndYear={0} />
</CalendarContext.Provider>
);
const currentYear = DateUtils.getYear(new Date());
expect(getAllByRole('row', { hidden: true })).to.be.lengthOf(2);
expect(getAllByRole('rowheader', { hidden: true })[0].innerText).to.be.eq(
(currentYear - 2).toString()
);
expect(getAllByRole('rowheader', { hidden: true })[1].innerText).to.be.eq(
(currentYear - 1).toString()
);
});

it('Should output a range of year and month between previous and next year', () => {
const { getAllByRole } = render(
<CalendarContext.Provider
value={{
date: new Date(),
locale: {},
isoWeek: false
}}
>
<MonthDropdown show limitStartYear={2} limitEndYear={2} />
</CalendarContext.Provider>
);
const currentYear = DateUtils.getYear(new Date());
const nextYear = currentYear + 1;
const previousYear = currentYear - 1;
expect(getAllByRole('row', { hidden: true })).to.be.lengthOf(3);
expect(getAllByRole('rowheader', { hidden: true })[0].innerText).to.be.eq(
previousYear.toString()
);
expect(getAllByRole('rowheader', { hidden: true })[1].innerText).to.be.eq(
currentYear.toString()
);
expect(getAllByRole('rowheader', { hidden: true })[2].innerText).to.be.eq(nextYear.toString());
});

it('Should call `onChangeMonth` callback ', () => {
const onChangeMonthSpy = sinon.spy();
const { getByRole } = render(
Expand Down
8 changes: 7 additions & 1 deletion src/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ export interface DatePickerProps
/** ISO 8601 standard, each calendar week begins on Monday and Sunday on the seventh day */
isoWeek?: boolean;

/** Set the lower limit of the available year relative to the current selection date */
/** Set the upper limit of the available year relative to the current selection date */
limitEndYear?: number;

/** Set the lower limit of the available year relative to the current selection date */
limitStartYear?: number;

/** Whether to show week numbers */
showWeekNumbers?: boolean;

Expand Down Expand Up @@ -186,6 +189,7 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
format: formatStr = 'yyyy-MM-dd',
isoWeek,
limitEndYear = 1000,
limitStartYear,
locale: overrideLocale,
menuClassName,
appearance = 'default',
Expand Down Expand Up @@ -547,6 +551,7 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
disabledMinutes={shouldDisableMinute ?? DEPRECATED_disabledMinutes}
disabledSeconds={shouldDisableSecond ?? DEPRECATED_disabledSeconds}
limitEndYear={limitEndYear}
limitStartYear={limitStartYear}
format={formatStr}
isoWeek={isoWeek}
calendarDate={calendarDate}
Expand Down Expand Up @@ -714,6 +719,7 @@ DatePicker.propTypes = {
hideSeconds: PropTypes.func,
isoWeek: PropTypes.bool,
limitEndYear: PropTypes.number,
limitStartYear: PropTypes.number,
onChange: PropTypes.func,
onChangeCalendarDate: PropTypes.func,
onNextMonth: PropTypes.func,
Expand Down
4 changes: 4 additions & 0 deletions src/DateRangePicker/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface CalendarProps extends WithAsProps, Omit<CalendarCoreProps, Omit
index: number;
isoWeek?: boolean;
limitEndYear?: number;
limitStartYear?: number;
locale?: DatePickerLocale;
onChangeCalendarMonth?: (index: number, date: Date) => void;
onChangeCalendarTime?: (index: number, date: Date) => void;
Expand All @@ -47,6 +48,7 @@ const Calendar: RsRefForwardingComponent<'div', CalendarProps> = React.forwardRe
disabledDate,
index = 0,
limitEndYear,
limitStartYear,
onChangeCalendarMonth,
onChangeCalendarTime,
onToggleMeridian,
Expand Down Expand Up @@ -120,6 +122,7 @@ const Calendar: RsRefForwardingComponent<'div', CalendarProps> = React.forwardRe
disabledDate={disabledMonth}
index={index}
limitEndYear={limitEndYear}
limitStartYear={limitStartYear}
onChangeMonth={handleChangeMonth}
onChangeTime={handleChangeTime}
onMoveBackward={handleMoveBackward}
Expand All @@ -142,6 +145,7 @@ Calendar.propTypes = {
format: PropTypes.string,
isoWeek: PropTypes.bool,
limitEndYear: PropTypes.number,
limitStartYear: PropTypes.number,
classPrefix: PropTypes.string,
disabledDate: PropTypes.func,
onSelect: PropTypes.func,
Expand Down
8 changes: 7 additions & 1 deletion src/DateRangePicker/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ export interface DateRangePickerProps
/** ISO 8601 standard, each calendar week begins on Monday and Sunday on the seventh day */
isoWeek?: boolean;

/** Set the lower limit of the available year relative to the current selection date */
/** Set the upper limit of the available year relative to the current selection date */
limitEndYear?: number;

/** Set the lower limit of the available year relative to the current selection date */
limitStartYear?: number;

/** Whether to show week numbers */
showWeekNumbers?: boolean;

Expand Down Expand Up @@ -170,6 +173,7 @@ const DateRangePicker: DateRangePicker = React.forwardRef((props: DateRangePicke
hoverRange,
isoWeek = false,
limitEndYear = 1000,
limitStartYear,
locale: overrideLocale,
menuClassName,
menuStyle,
Expand Down Expand Up @@ -790,6 +794,7 @@ const DateRangePicker: DateRangePicker = React.forwardRef((props: DateRangePicke
hoverRangeValue: hoverDateRange ?? undefined,
isoWeek,
limitEndYear,
limitStartYear,
locale,
showWeekNumbers,
value: selectedDates,
Expand Down Expand Up @@ -941,6 +946,7 @@ DateRangePicker.propTypes = {
isoWeek: PropTypes.bool,
oneTap: PropTypes.bool,
limitEndYear: PropTypes.number,
limitStartYear: PropTypes.number,
onChange: PropTypes.func,
onOk: PropTypes.func,
disabledDate: deprecatePropTypeNew(PropTypes.func, 'Use "shouldDisableDate" property instead.'),
Expand Down