react-calendar
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -144,7 +144,16 @@ 'use strict'; | ||
} | ||
var minDate = this.props.minDate; | ||
var _props = this.props, | ||
maxDate = _props.maxDate, | ||
minDate = _props.minDate; | ||
var rawValueFrom = value instanceof Array ? value[0] : value; | ||
var valueFrom = (0, _dates.getBegin)(this.valueType, rawValueFrom); | ||
return minDate && minDate > valueFrom ? minDate : valueFrom; | ||
if (minDate && minDate > valueFrom) { | ||
return minDate; | ||
} | ||
if (maxDate && maxDate < valueFrom) { | ||
return maxDate; | ||
} | ||
return valueFrom; | ||
} | ||
@@ -157,7 +166,16 @@ }, { | ||
} | ||
var maxDate = this.props.maxDate; | ||
var _props2 = this.props, | ||
maxDate = _props2.maxDate, | ||
minDate = _props2.minDate; | ||
var rawValueFrom = value instanceof Array ? value[1] : value; | ||
var valueTo = (0, _dates.getEnd)(this.valueType, rawValueFrom); | ||
return maxDate && maxDate < valueTo ? maxDate : valueTo; | ||
if (minDate && minDate > valueTo) { | ||
return minDate; | ||
} | ||
if (maxDate && maxDate < valueTo) { | ||
return maxDate; | ||
} | ||
return valueTo; | ||
} | ||
@@ -294,8 +312,8 @@ | ||
valueType = this.valueType; | ||
var _props = this.props, | ||
calendarType = _props.calendarType, | ||
maxDate = _props.maxDate, | ||
minDate = _props.minDate, | ||
renderChildren = _props.renderChildren, | ||
value = _props.value; | ||
var _props3 = this.props, | ||
calendarType = _props3.calendarType, | ||
maxDate = _props3.maxDate, | ||
minDate = _props3.minDate, | ||
renderChildren = _props3.renderChildren, | ||
value = _props3.value; | ||
var _state = this.state, | ||
@@ -302,0 +320,0 @@ activeStartDate = _state.activeStartDate, |
@@ -41,8 +41,12 @@ 'use strict'; | ||
value: function render() { | ||
var startWeekNumber = this.startWeekNumber; | ||
var year = this.year, | ||
monthIndex = this.monthIndex, | ||
day = this.day; | ||
var calendarType = this.props.calendarType; | ||
var weekNumbers = []; | ||
for (var index = 0; index < this.numberOfWeeks; index += 1) { | ||
var currentNumber = index + startWeekNumber; | ||
weekNumbers.push(currentNumber > 52 ? currentNumber % 52 : currentNumber); | ||
var date = new Date(year, monthIndex, day + index * 7); | ||
weekNumbers.push((0, _dates.getWeekNumber)(date, calendarType)); | ||
} | ||
@@ -89,17 +93,27 @@ | ||
}, { | ||
key: 'startWeekNumber', | ||
key: 'year', | ||
get: function get() { | ||
var _props2 = this.props, | ||
activeStartDate = _props2.activeStartDate, | ||
calendarType = _props2.calendarType; | ||
var activeStartDate = this.props.activeStartDate; | ||
return (0, _dates.getWeekNumber)(activeStartDate, calendarType); | ||
return (0, _dates.getYear)(activeStartDate); | ||
} | ||
}, { | ||
key: 'monthIndex', | ||
get: function get() { | ||
var activeStartDate = this.props.activeStartDate; | ||
return (0, _dates.getMonthIndex)(activeStartDate); | ||
} | ||
}, { | ||
key: 'day', | ||
get: function get() { | ||
var activeStartDate = this.props.activeStartDate; | ||
return (0, _dates.getDay)(activeStartDate); | ||
} | ||
}, { | ||
key: 'numberOfWeeks', | ||
get: function get() { | ||
var days = this.numberOfDays - (7 - this.startWeekday); | ||
var weeks = 1 + Math.ceil(days / 7); | ||
return weeks; | ||
return 1 + Math.ceil(days / 7); | ||
} | ||
@@ -106,0 +120,0 @@ }]); |
@@ -202,2 +202,17 @@ 'use strict'; | ||
/** | ||
* Returns the beginning of a given week. | ||
* | ||
* @param {Date} date Date. | ||
* @param {String} calendarType Calendar type. Can be ISO 8601 or US. | ||
*/ | ||
var getBeginOfWeek = exports.getBeginOfWeek = function getBeginOfWeek(date) { | ||
var calendarType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'ISO 8601'; | ||
var year = getYear(date); | ||
var monthIndex = getMonthIndex(date); | ||
var day = date.getDate() - getDayOfWeek(date, calendarType); | ||
return new Date(year, monthIndex, day); | ||
}; | ||
/** | ||
* Returns an array with the beginning and the end of a given month. | ||
@@ -258,10 +273,25 @@ * | ||
/** | ||
* Gets week number according to ISO 8601 or US standard. | ||
* In ISO 8601 week 1 is the one with January 4. | ||
* In US calendar week 1 is the one with January 1. | ||
* | ||
* @param {Date} date Date. | ||
* @param {String} calendarType Calendar type. Can be ISO 8601 or US. | ||
*/ | ||
var getWeekNumber = exports.getWeekNumber = function getWeekNumber(date) { | ||
var calendarType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'ISO 8601'; | ||
var tempDate = new Date(+date); | ||
tempDate.setDate(getDay(tempDate) + (4 - getDayOfWeek(tempDate, calendarType))); | ||
var yearStart = getBeginOfYear(tempDate); | ||
var weekNumber = Math.ceil((tempDate - yearStart) / 8.64e7 / 7); | ||
return weekNumber === 0 ? 52 : weekNumber; | ||
var year = getYear(date) + 1; | ||
var dayInWeekOne = void 0; | ||
var beginOfFirstWeek = void 0; | ||
// Look for the first week one that does not come after a given date | ||
do { | ||
dayInWeekOne = new Date(year, 0, calendarType === 'ISO 8601' ? 4 : 1); | ||
beginOfFirstWeek = getBeginOfWeek(dayInWeekOne, calendarType); | ||
year -= 1; | ||
} while (date - beginOfFirstWeek < 0); | ||
return Math.floor((date - beginOfFirstWeek) / (8.64e7 * 7)) + 1; | ||
}; | ||
@@ -268,0 +298,0 @@ |
{ | ||
"name": "react-calendar", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A component for picking dates or date periods for your React application.", | ||
@@ -5,0 +5,0 @@ "main": "build/entry.js", |
@@ -52,2 +52,32 @@ import React from 'react'; | ||
it('renders maximum allowed view when attempting to externally switch to a view that is not allowed', () => { | ||
const component = mount( | ||
<Calendar | ||
maxDetail="year" | ||
view="year" | ||
/> | ||
); | ||
component.setProps({ view: 'month' }); | ||
const yearView = component.find('.react-calendar__year-view'); | ||
expect(yearView).toHaveLength(1); | ||
}); | ||
it('renders maximum allowed view when given changed maxDetail', () => { | ||
const component = mount( | ||
<Calendar | ||
maxDetail="month" | ||
view="month" | ||
/> | ||
); | ||
component.setProps({ maxDetail: 'year' }); | ||
const yearView = component.find('.react-calendar__year-view'); | ||
expect(yearView).toHaveLength(1); | ||
}); | ||
it('renders month view when given view = "month"', () => { | ||
@@ -227,2 +257,66 @@ const component = mount( | ||
}); | ||
it('calls onChange function returning beginning of selected period, but no earlier than minDate', () => { | ||
const onChange = jest.fn(); | ||
const component = mount( | ||
<Calendar | ||
minDate={new Date(2017, 0, 1, 12)} | ||
onChange={onChange} | ||
returnValue="start" | ||
view="month" | ||
/> | ||
); | ||
component.node.onChange(new Date(2017, 0, 1)); | ||
expect(onChange).toHaveBeenCalledWith(new Date(2017, 0, 1, 12)); | ||
}); | ||
it('calls onChange function returning beginning of selected period, but no later than maxDate', () => { | ||
const onChange = jest.fn(); | ||
const component = mount( | ||
<Calendar | ||
maxDate={new Date(2017, 0, 1, 12)} | ||
onChange={onChange} | ||
returnValue="start" | ||
view="month" | ||
/> | ||
); | ||
component.node.onChange(new Date(2017, 0, 2)); | ||
expect(onChange).toHaveBeenCalledWith(new Date(2017, 0, 1, 12)); | ||
}); | ||
it('calls onChange function returning the end of selected period, but no earlier than minDate', () => { | ||
const onChange = jest.fn(); | ||
const component = mount( | ||
<Calendar | ||
minDate={new Date(2017, 0, 2, 12)} | ||
onChange={onChange} | ||
returnValue="end" | ||
view="month" | ||
/> | ||
); | ||
component.node.onChange(new Date(2017, 0, 1)); | ||
expect(onChange).toHaveBeenCalledWith(new Date(2017, 0, 2, 12)); | ||
}); | ||
it('calls onChange function returning the end of selected period, but no later than maxDate', () => { | ||
const onChange = jest.fn(); | ||
const component = mount( | ||
<Calendar | ||
maxDate={new Date(2017, 0, 1, 12)} | ||
onChange={onChange} | ||
returnValue="end" | ||
view="month" | ||
/> | ||
); | ||
component.node.onChange(new Date(2017, 0, 2)); | ||
expect(onChange).toHaveBeenCalledWith(new Date(2017, 0, 1, 12)); | ||
}); | ||
}); |
@@ -52,10 +52,13 @@ import React, { Component } from 'react'; | ||
} | ||
const { minDate } = this.props; | ||
const { maxDate, minDate } = this.props; | ||
const rawValueFrom = value instanceof Array ? value[0] : value; | ||
const valueFrom = getBegin(this.valueType, rawValueFrom); | ||
return ( | ||
minDate && minDate > valueFrom ? | ||
minDate : | ||
valueFrom | ||
); | ||
if (minDate && minDate > valueFrom) { | ||
return minDate; | ||
} | ||
if (maxDate && maxDate < valueFrom) { | ||
return maxDate; | ||
} | ||
return valueFrom; | ||
} | ||
@@ -67,10 +70,13 @@ | ||
} | ||
const { maxDate } = this.props; | ||
const { maxDate, minDate } = this.props; | ||
const rawValueFrom = value instanceof Array ? value[1] : value; | ||
const valueTo = getEnd(this.valueType, rawValueFrom); | ||
return ( | ||
maxDate && maxDate < valueTo ? | ||
maxDate : | ||
valueTo | ||
); | ||
if (minDate && minDate > valueTo) { | ||
return minDate; | ||
} | ||
if (maxDate && maxDate < valueTo) { | ||
return maxDate; | ||
} | ||
return valueTo; | ||
} | ||
@@ -77,0 +83,0 @@ |
@@ -9,6 +9,6 @@ import React from 'react'; | ||
describe('WeekNumbers', () => { | ||
it('renders proper weekNumbers for a year that starts on Monday (ISO 8601)', () => { | ||
it('renders proper weekNumbers for a year that starts in week 1 (ISO 8601)', () => { | ||
const component = mount( | ||
<WeekNumbers | ||
activeStartDate={new Date(2007, 0, 1)} | ||
activeStartDate={new Date(2018, 0, 1)} | ||
calendarType="ISO 8601" | ||
@@ -24,7 +24,7 @@ /> | ||
it('renders proper weekNumbers for a year that starts on Sunday (US)', () => { | ||
it('renders proper weekNumbers for a year that starts on week 52 (ISO 8601)', () => { | ||
const component = mount( | ||
<WeekNumbers | ||
activeStartDate={new Date(2017, 0, 1)} | ||
calendarType="US" | ||
calendarType="ISO 8601" | ||
/> | ||
@@ -35,10 +35,10 @@ ); | ||
expect(children).toHaveLength(5); | ||
expect(children.first().text()).toBe('1'); | ||
expect(children).toHaveLength(6); | ||
expect(children.first().text()).toBe('52'); | ||
}); | ||
it('renders proper weekNumbers for a year that does not start on Monday (ISO 8601)', () => { | ||
it('renders proper weekNumbers for a year that starts on week 53 (ISO 8601)', () => { | ||
const component = mount( | ||
<WeekNumbers | ||
activeStartDate={new Date(2017, 0, 1)} | ||
activeStartDate={new Date(2016, 0, 1)} | ||
calendarType="ISO 8601" | ||
@@ -50,10 +50,10 @@ /> | ||
expect(children).toHaveLength(6); | ||
expect(children.first().text()).toBe('52'); | ||
expect(children).toHaveLength(5); | ||
expect(children.first().text()).toBe('53'); | ||
}); | ||
it('renders proper weekNumbers for a year that does not start on Sunday (US)', () => { | ||
it('renders proper weekNumbers for a year that starts in week 1 (US)', () => { | ||
const component = mount( | ||
<WeekNumbers | ||
activeStartDate={new Date(2016, 0, 1)} | ||
activeStartDate={new Date(2017, 0, 1)} | ||
calendarType="US" | ||
@@ -65,5 +65,5 @@ /> | ||
expect(children).toHaveLength(6); | ||
expect(children.first().text()).toBe('52'); | ||
expect(children).toHaveLength(5); | ||
expect(children.first().text()).toBe('1'); | ||
}); | ||
}); |
@@ -5,5 +5,8 @@ import React, { Component } from 'react'; | ||
import { | ||
getDay, | ||
getDayOfWeek, | ||
getDaysInMonth, | ||
getMonthIndex, | ||
getWeekNumber, | ||
getYear, | ||
} from '../shared/dates'; | ||
@@ -23,20 +26,30 @@ import { isCalendarType } from '../shared/propTypes'; | ||
get startWeekNumber() { | ||
const { activeStartDate, calendarType } = this.props; | ||
return getWeekNumber(activeStartDate, calendarType); | ||
get year() { | ||
const { activeStartDate } = this.props; | ||
return getYear(activeStartDate); | ||
} | ||
get monthIndex() { | ||
const { activeStartDate } = this.props; | ||
return getMonthIndex(activeStartDate); | ||
} | ||
get day() { | ||
const { activeStartDate } = this.props; | ||
return getDay(activeStartDate); | ||
} | ||
get numberOfWeeks() { | ||
const days = this.numberOfDays - (7 - this.startWeekday); | ||
const weeks = 1 + Math.ceil(days / 7); | ||
return weeks; | ||
return 1 + Math.ceil(days / 7); | ||
} | ||
render() { | ||
const { startWeekNumber } = this; | ||
const { year, monthIndex, day } = this; | ||
const { calendarType } = this.props; | ||
const weekNumbers = []; | ||
for (let index = 0; index < this.numberOfWeeks; index += 1) { | ||
const currentNumber = index + startWeekNumber; | ||
weekNumbers.push(currentNumber > 52 ? currentNumber % 52 : currentNumber); | ||
const date = new Date(year, monthIndex, day + (index * 7)); | ||
weekNumbers.push(getWeekNumber(date, calendarType)); | ||
} | ||
@@ -43,0 +56,0 @@ |
@@ -28,2 +28,3 @@ import { | ||
getEndOfMonth, | ||
getBeginOfWeek, | ||
getMonthRange, | ||
@@ -400,2 +401,31 @@ getBeginOfPreviousMonth, | ||
describe('getBeginOfWeek', () => { | ||
it('returns proper beginning of the week (ISO 8601)', () => { | ||
const date = new Date(2017, 0, 1); | ||
const beginOfWeekDate = new Date(2016, 11, 26); | ||
const beginOfWeek = getBeginOfWeek(date, 'ISO 8601'); | ||
expect(beginOfWeek.toISOString()).toBe(beginOfWeekDate.toISOString()); | ||
}); | ||
it('returns proper beginning of the week (US)', () => { | ||
const date = new Date(2016, 0, 1); | ||
const beginOfWeekDate = new Date(2015, 11, 27); | ||
const beginOfWeek = getBeginOfWeek(date, 'US'); | ||
expect(beginOfWeek.toISOString()).toBe(beginOfWeekDate.toISOString()); | ||
}); | ||
it('returns proper beginning of the week (default)', () => { | ||
const date = new Date(2017, 0, 1); | ||
const beginOfWeekDate = new Date(2016, 11, 26); | ||
const beginOfWeek = getBeginOfWeek(date); | ||
expect(beginOfWeek.toISOString()).toBe(beginOfWeekDate.toISOString()); | ||
}); | ||
}); | ||
describe('getMonthRange', () => { | ||
@@ -485,41 +515,99 @@ it('returns proper month date range', () => { | ||
describe('getWeekNumber', () => { | ||
it('returns proper week number for a year that starts on Monday (ISO 8601)', () => { | ||
const date = new Date(2007, 0, 1); | ||
it('returns proper week number for a sample week 1 (ISO 8601)', () => { | ||
const year = 2018; | ||
const month = 0; | ||
const startDate = 1; | ||
const dayOfWeek = getWeekNumber(date, 'ISO 8601'); | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
expect(dayOfWeek).toBe(1); | ||
const dayOfWeek = getWeekNumber(date, 'ISO 8601'); | ||
expect(dayOfWeek).toBe(1); | ||
} | ||
}); | ||
it('returns proper week number a year that starts on Sunday (US)', () => { | ||
const date = new Date(2017, 0, 1); | ||
it('returns proper week number for a sample week 52 (ISO 8601)', () => { | ||
const year = 2016; | ||
const month = 11; | ||
const startDate = 26; | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
expect(dayOfWeek).toBe(1); | ||
const dayOfWeek = getWeekNumber(date, 'ISO 8601'); | ||
expect(dayOfWeek).toBe(52); | ||
} | ||
}); | ||
it('returns proper week number for a year that does not start on Monday (ISO 8601)', () => { | ||
const date = new Date(2017, 0, 1); | ||
it('returns proper week number for a sample week 53 (ISO 8601)', () => { | ||
const year = 2015; | ||
const month = 11; | ||
const startDate = 28; | ||
const dayOfWeek = getWeekNumber(date, 'ISO 8601'); | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
expect(dayOfWeek).toBe(52); | ||
const dayOfWeek = getWeekNumber(date, 'ISO 8601'); | ||
expect(dayOfWeek).toBe(53); | ||
} | ||
}); | ||
it('returns proper week number a year that does not start on Sunday (US)', () => { | ||
const date = new Date(2016, 0, 1); | ||
it('returns proper week number for a sample week 1 (US)', () => { | ||
const year = 2015; | ||
const month = 11; | ||
const startDate = 27; | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
expect(dayOfWeek).toBe(52); | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
expect(dayOfWeek).toBe(1); | ||
} | ||
}); | ||
it('returns proper week number (default)', () => { | ||
const date = new Date(2017, 0, 1); | ||
it('returns proper week number for a sample week 1 (US)', () => { | ||
const year = 2015; | ||
const month = 11; | ||
const startDate = 27; | ||
const dayOfWeek = getWeekNumber(date); | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
expect(dayOfWeek).toBe(52); | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
expect(dayOfWeek).toBe(1); | ||
} | ||
}); | ||
it('returns proper week number for a sample week 52 (US)', () => { | ||
const year = 2017; | ||
const month = 11; | ||
const startDate = 24; | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
expect(dayOfWeek).toBe(52); | ||
} | ||
}); | ||
it('returns proper week number for a sample week 53 (US)', () => { | ||
const year = 2016; | ||
const month = 11; | ||
const startDate = 25; | ||
for (let currentDate = startDate; currentDate < startDate + 7; currentDate += 1) { | ||
const date = new Date(year, month, currentDate); | ||
const dayOfWeek = getWeekNumber(date, 'US'); | ||
expect(dayOfWeek).toBe(53); | ||
} | ||
}); | ||
}); | ||
@@ -526,0 +614,0 @@ |
@@ -179,2 +179,15 @@ /* Simple getters - getting a property of a given point in time */ | ||
/** | ||
* Returns the beginning of a given week. | ||
* | ||
* @param {Date} date Date. | ||
* @param {String} calendarType Calendar type. Can be ISO 8601 or US. | ||
*/ | ||
export const getBeginOfWeek = (date, calendarType = 'ISO 8601') => { | ||
const year = getYear(date); | ||
const monthIndex = getMonthIndex(date); | ||
const day = date.getDate() - getDayOfWeek(date, calendarType); | ||
return new Date(year, monthIndex, day); | ||
}; | ||
/** | ||
* Returns an array with the beginning and the end of a given month. | ||
@@ -231,8 +244,23 @@ * | ||
/** | ||
* Gets week number according to ISO 8601 or US standard. | ||
* In ISO 8601 week 1 is the one with January 4. | ||
* In US calendar week 1 is the one with January 1. | ||
* | ||
* @param {Date} date Date. | ||
* @param {String} calendarType Calendar type. Can be ISO 8601 or US. | ||
*/ | ||
export const getWeekNumber = (date, calendarType = 'ISO 8601') => { | ||
const tempDate = new Date(+date); | ||
tempDate.setDate(getDay(tempDate) + (4 - getDayOfWeek(tempDate, calendarType))); | ||
const yearStart = getBeginOfYear(tempDate); | ||
const weekNumber = Math.ceil((((tempDate - yearStart) / 8.64e7)) / 7); | ||
return weekNumber === 0 ? 52 : weekNumber; | ||
let year = getYear(date) + 1; | ||
let dayInWeekOne; | ||
let beginOfFirstWeek; | ||
// Look for the first week one that does not come after a given date | ||
do { | ||
dayInWeekOne = new Date(year, 0, calendarType === 'ISO 8601' ? 4 : 1); | ||
beginOfFirstWeek = getBeginOfWeek(dayInWeekOne, calendarType); | ||
year -= 1; | ||
} while (date - beginOfFirstWeek < 0); | ||
return Math.floor((date - beginOfFirstWeek) / (8.64e7 * 7)) + 1; | ||
}; | ||
@@ -239,0 +267,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240234
5878