From 71f65a0be8e72a67f334c57e79ae3ff5fb640630 Mon Sep 17 00:00:00 2001 From: Rohan Malhotra Date: Sat, 3 Feb 2018 16:24:04 +0530 Subject: [PATCH] Added validRange props in calendar to control date range --- components/calendar/Header.tsx | 51 +++++++++++++++++---- components/calendar/__tests__/index.test.js | 48 +++++++++++++++++++ components/calendar/index.en-US.md | 1 + components/calendar/index.tsx | 24 ++++++++++ 4 files changed, 115 insertions(+), 9 deletions(-) diff --git a/components/calendar/Header.tsx b/components/calendar/Header.tsx index 39571b5c4b..3ac668e78a 100644 --- a/components/calendar/Header.tsx +++ b/components/calendar/Header.tsx @@ -15,6 +15,7 @@ export interface HeaderProps { onValueChange?: (value: moment.Moment) => void; onTypeChange?: (type: string) => void; value: any; + validRange ?: [moment.Moment, moment.Moment]; } export default class Header extends React.Component { @@ -27,11 +28,21 @@ export default class Header extends React.Component { private calenderHeaderNode: HTMLDivElement; getYearSelectElement(year: number) { - const { yearSelectOffset, yearSelectTotal, locale, prefixCls, fullscreen } = this.props; - const start = year - (yearSelectOffset as number); - const end = start + (yearSelectTotal as number); + const { + yearSelectOffset, + yearSelectTotal, + locale, + prefixCls, + fullscreen, + validRange, + } = this.props; + let start = year - (yearSelectOffset as number); + let end = start + (yearSelectTotal as number); + if (validRange) { + start = validRange[0].get('year'); + end = validRange[1].get('year') + 1; + } const suffix = locale.year === '年' ? '年' : ''; - const options: React.ReactElement[] = []; for (let index = start; index < end; index++) { options.push(); @@ -63,13 +74,22 @@ export default class Header extends React.Component { getMonthSelectElement(month: number, months: number[]) { const props = this.props; - const { prefixCls, fullscreen } = props; + const { prefixCls, fullscreen, validRange, value } = props; const options: React.ReactElement[] = []; - - for (let index = 0; index < 12; index++) { + let start = 0; + let end = 12; + if (validRange) { + const [rangeStart, rangeEnd] = validRange; + const currentYear = value.get('year'); + if (rangeEnd.get('year') === currentYear) { + end = rangeEnd.get('month') + 1; + } else { + start = rangeStart.get('month'); + } + } + for (let index = start; index < end; index++) { options.push(); } - return (