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

Add initialVisibleMonth fn prop to DayPicker #70

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .istanbul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ instrumentation:
check:
global:
statements: 89
branches: 70
branches: 69
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline with #70 (comment), updating coverage requirements

lines: 91
functions: 79
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ By default, we do not show days from the previous month and the next month in th
enableOutsideDays: PropTypes.bool
```

`initialVisibleMonth` indicates the month that should be displayed initially when the calendar is first opened. The prop is a function that must return a Moment.js object. This function will be called the first time the user focuses on the `DateRangePicker`/`SingleDatePicker` inputs or when the `focused` prop is passed to the `DayPicker` component.
```
initialVisibleMonth: PropTypes.func
```

**DayPicker presentation:**

The `orientation` prop indicates whether months are stacked on top of each other or displayed side-by-side. You can import the `HORIZONTAL_ORIENTATION` and `VERTICAL_ORIENTATION` constants from `react-dates/constants`.
Expand Down
5 changes: 5 additions & 0 deletions src/components/DateRangePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const defaultProps = {
showClearDates: false,
disabled: false,
reopenPickerOnClearDates: false,
initialVisibleMonth: () => moment(),

orientation: HORIZONTAL_ORIENTATION,
withPortal: false,
Expand Down Expand Up @@ -330,6 +331,8 @@ export default class DateRangePicker extends React.Component {
withPortal,
withFullScreenPortal,
enableOutsideDays,
initialVisibleMonth,
focusedInput,
} = this.props;

const modifiers = {
Expand Down Expand Up @@ -370,6 +373,8 @@ export default class DateRangePicker extends React.Component {
onNextMonthClick={onNextMonthClick}
monthFormat={monthFormat}
withPortal={withPortal || withFullScreenPortal}
hidden={!focusedInput}
Copy link
Collaborator

@majapw majapw Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this name to isVisible instead of hidden? I think that is more in line with the other nomenclature in these files.

I think once we move to react-with-styles, we hopefully won't need this anyways. since the component will be unmounted/remounted every time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:( unfortunately I had it named as visible before but @ljharb asked to change it to be hidden so we didn't have a boolean prop that defaulted to true.

Let me know what you would prefer now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man, I should really have read @ljharb's comments more carefully before. :) hidden sounds good to me

initialVisibleMonth={initialVisibleMonth}
onOutsideClick={onOutsideClick}
/>

Expand Down
17 changes: 16 additions & 1 deletion src/components/DayPicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const propTypes = {
modifiers: PropTypes.object,
orientation: OrientationShape,
withPortal: PropTypes.bool,
hidden: PropTypes.bool,
initialVisibleMonth: PropTypes.func,
onDayClick: PropTypes.func,
onDayMouseDown: PropTypes.func,
onDayMouseUp: PropTypes.func,
Expand All @@ -52,6 +54,8 @@ const defaultProps = {
modifiers: {},
orientation: HORIZONTAL_ORIENTATION,
withPortal: false,
hidden: false,
initialVisibleMonth: () => moment(),
onDayClick() {},
onDayMouseDown() {},
onDayMouseUp() {},
Expand All @@ -71,8 +75,10 @@ const defaultProps = {
export default class DayPicker extends React.Component {
constructor(props) {
super(props);

this.hasSetInitialVisibleMonth = !props.hidden;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly I don't think we need the hasSetInitialVisibleMonth logic either.

this.state = {
currentMonth: moment(),
currentMonth: props.hidden ? moment() : props.initialVisibleMonth(),
monthTransition: null,
translationValue: 0,
};
Expand All @@ -89,6 +95,15 @@ export default class DayPicker extends React.Component {
}
}

componentWillReceiveProps(nextProps) {
if (!this.hasSetInitialVisibleMonth && !nextProps.hidden) {
this.hasSetInitialVisibleMonth = true;
this.setState({
currentMonth: nextProps.initialVisibleMonth(),
});
}
}

shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
Expand Down
6 changes: 6 additions & 0 deletions src/components/SingleDatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const defaultProps = {
orientation: HORIZONTAL_ORIENTATION,
withPortal: false,
withFullScreenPortal: false,
initialVisibleMonth: () => moment(),


onPrevMonthClick() {},
onNextMonthClick() {},
Expand Down Expand Up @@ -181,6 +183,8 @@ export default class SingleDatePicker extends React.Component {
onNextMonthClick,
withPortal,
withFullScreenPortal,
focused,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propType and defaultProp for this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pre-existing prop on the SDP @ljharb

initialVisibleMonth,
} = this.props;

const modifiers = {
Expand Down Expand Up @@ -209,6 +213,8 @@ export default class SingleDatePicker extends React.Component {
onNextMonthClick={onNextMonthClick}
monthFormat={monthFormat}
withPortal={withPortal || withFullScreenPortal}
hidden={!focused}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, let's name this as isVisible

initialVisibleMonth={initialVisibleMonth}
onOutsideClick={onOutsideClick}
/>

Expand Down
1 change: 1 addition & 0 deletions src/shapes/DateRangePickerShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default {
endDateId: PropTypes.string,
endDatePlaceholderText: PropTypes.string,

initialVisibleMonth: PropTypes.func,
onDatesChange: PropTypes.func,
onFocusChange: PropTypes.func,
onPrevMonthClick: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions src/shapes/SingleDatePickerShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
enableOutsideDays: PropTypes.bool,
numberOfMonths: PropTypes.number,
orientation: OrientationShape,
initialVisibleMonth: PropTypes.func,

// portal options
withPortal: PropTypes.bool,
Expand Down
5 changes: 5 additions & 0 deletions stories/DateRangePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ storiesOf('DateRangePicker', module)
isDayBlocked={day1 => datesList.some(day2 => isSameDay(day1, day2))}
/>
))
.add('with month specified on open', () => (
<DateRangePickerWrapper
initialVisibleMonth={() => moment('01 2017', 'MM YYYY')}
/>
))
.add('blocks fridays', () => (
<DateRangePickerWrapper
isDayBlocked={day => moment.weekdays(day.weekday()) === 'Friday'}
Expand Down
5 changes: 5 additions & 0 deletions stories/SingleDatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ storiesOf('SingleDatePicker', module)
withFullScreenPortal
/>
))
.add('with month specified on open', () => (
<SingleDatePickerWrapper
initialVisibleMonth={() => moment('01 2017', 'MM YYYY')}
/>
))
.add('non-english locale', () => {
moment.locale('zh-cn');
return (
Expand Down