diff --git a/common/changes/master_2017-04-27-00-31.json b/common/changes/master_2017-04-27-00-31.json new file mode 100644 index 0000000000000..69b92e3f2acb5 --- /dev/null +++ b/common/changes/master_2017-04-27-00-31.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "office-ui-fabric-react", + "comment": "Calendar: Support auto-navigation to next/previous month when selected date changes via props", + "type": "patch" + } + ], + "email": "johannao@microsoft.com" +} diff --git a/packages/office-ui-fabric-react/src/components/Calendar/Calendar.tsx b/packages/office-ui-fabric-react/src/components/Calendar/Calendar.tsx index a6e2ded862f10..e5cb13f01b69b 100644 --- a/packages/office-ui-fabric-react/src/components/Calendar/Calendar.tsx +++ b/packages/office-ui-fabric-react/src/components/Calendar/Calendar.tsx @@ -3,6 +3,7 @@ import { ICalendar, ICalendarProps } from './Calendar.Props'; import { DayOfWeek, DateRangeType } from '../../utilities/dateValues/DateValues'; import { CalendarDay } from './CalendarDay'; import { CalendarMonth } from './CalendarMonth'; +import { compareDates } from '../../utilities/dateMath/DateMath'; import { autobind, css, @@ -54,7 +55,16 @@ export class Calendar extends BaseComponent impl } public componentWillReceiveProps(nextProps: ICalendarProps) { - let { value } = nextProps; + let { autoNavigateOnSelection, value } = nextProps; + + // Make sure auto-navigation is supported for programmatic changes to selected date, i.e., + // if selected date is updated via props, we may need to modify the navigated date + let overrideNavigatedDate = (autoNavigateOnSelection && !compareDates(value, this.props.value)); + if (overrideNavigatedDate) { + this.setState({ + navigatedDate: value + }); + } this.setState({ selectedDate: value || new Date() diff --git a/packages/office-ui-fabric-react/src/components/Calendar/CalendarPage.tsx b/packages/office-ui-fabric-react/src/components/Calendar/CalendarPage.tsx index 1f589a3cf1a0a..5ba0bc5a6b920 100644 --- a/packages/office-ui-fabric-react/src/components/Calendar/CalendarPage.tsx +++ b/packages/office-ui-fabric-react/src/components/Calendar/CalendarPage.tsx @@ -30,11 +30,11 @@ export class CalendarPage extends React.Component + showGoToToday={ true } showNavigateButtons={ true } /> + showGoToToday={ true } showNavigateButtons={ true } /> diff --git a/packages/office-ui-fabric-react/src/components/Calendar/examples/Calendar.Inline.Example.tsx b/packages/office-ui-fabric-react/src/components/Calendar/examples/Calendar.Inline.Example.tsx index 4d29012171c22..f683d6d105ff6 100644 --- a/packages/office-ui-fabric-react/src/components/Calendar/examples/Calendar.Inline.Example.tsx +++ b/packages/office-ui-fabric-react/src/components/Calendar/examples/Calendar.Inline.Example.tsx @@ -1,4 +1,6 @@ import * as React from 'react'; +import { Button } from 'office-ui-fabric-react/lib/Button'; +import { addDays, getDateRangeArray } from 'office-ui-fabric-react/lib/utilities/dateMath/DateMath'; import { Calendar, DayOfWeek @@ -69,6 +71,7 @@ export interface ICalendarInlineExampleProps { dateRangeType: DateRangeType; autoNavigateOnSelection: boolean; showGoToToday: boolean; + showNavigateButtons?: boolean; } export class CalendarInlineExample extends React.Component { @@ -82,14 +85,20 @@ export class CalendarInlineExample extends React.Component + { this.props.showNavigateButtons && +
+
+ } ); } @@ -128,6 +143,33 @@ export class CalendarInlineExample extends React.Component { + let selectedDate = prevState.selectedDate || new Date(); + let dateRangeArray = getDateRangeArray(selectedDate, this.props.dateRangeType, DayOfWeek.Sunday); + + let subtractFrom = dateRangeArray[0]; + let daysToSubtract = dateRangeArray.length; + + if (this.props.dateRangeType === DateRangeType.Month) { + subtractFrom = new Date(subtractFrom.getFullYear(), subtractFrom.getMonth(), 1); + daysToSubtract = 1; + } + + let newSelectedDate = addDays(subtractFrom, -daysToSubtract); + return prevState.selectedDate = newSelectedDate; + }); + } + + private _goNext() { + this.setState((prevState: ICalendarInlineExampleState) => { + let selectedDate = prevState.selectedDate || new Date(); + let dateRangeArray = getDateRangeArray(selectedDate, this.props.dateRangeType, DayOfWeek.Sunday); + let newSelectedDate = addDays(dateRangeArray.pop(), 1); + return prevState.selectedDate = newSelectedDate; + }); + } + private _onSelectDate(date: Date, dateRangeArray: Date[]) { this.setState((prevState: ICalendarInlineExampleState) => { prevState.selectedDate = date;