Skip to content

Commit f6f919b

Browse files
authored
Merge pull request #1700 from wix/fix/Provider_onDateChange_return_type
Context Provider - fix onDateChange return value
2 parents 3d1487c + e565e34 commit f6f919b

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,8 @@
765765

766766
## [1.1270.0] - 2021-11-29
767767
- testIDs - reverting to js file with module.exports.
768+
769+
## [1.1271.0] - 2021-xx-xx
770+
771+
## Fixed
772+
- ContextProvider - 'onDateChanged' return type.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ LocaleConfig.defaultLocale = 'fr';
7373

7474
```javascript
7575
<Calendar
76-
// Initially visible month. Default = Date()
76+
// Initially visible month. Default = now
7777
current={'2012-03-01'}
7878
// Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
7979
minDate={'2012-05-10'}

src/calendar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Calendar extends Component<CalendarProps, CalendarState> {
8989
theme: PropTypes.object,
9090
/** Specify style for calendar container element. Default = {} */
9191
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.number]),
92-
/** Initially visible month. Default = Date() */
92+
/** Initially visible month in 'yyyy-MM-dd' format. Default = now */
9393
current: PropTypes.any,
9494
/** Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined */
9595
minDate: PropTypes.any,

src/expandableCalendar/Context/Presenter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const commons = require('../commons');
1111
const TOP_POSITION = 65;
1212

1313
class Presenter {
14-
_isPastDate(date: Date) {
14+
_isPastDate(date: string) {
1515
const today = new XDate();
1616
const d = new XDate(date);
1717

@@ -39,15 +39,15 @@ class Presenter {
3939
return require('../../img/up.png');
4040
};
4141

42-
getButtonIcon = (date: Date, showTodayButton = true) => {
42+
getButtonIcon = (date: string, showTodayButton = true) => {
4343
if (!showTodayButton) {
4444
return undefined;
4545
}
4646
const icon = this._isPastDate(date) ? this._getIconDown() : this._getIconUp();
4747
return icon;
4848
};
4949

50-
setDate = (props: CalendarContextProviderProps, date: Date, newDate: Date, updateState: (buttonIcon: any) => void, updateSource: UpdateSource) => {
50+
setDate = (props: CalendarContextProviderProps, date: string, newDate: string, updateState: (buttonIcon: number) => void, updateSource: UpdateSource) => {
5151
const isSameMonth = sameMonth(new XDate(date), new XDate(newDate));
5252
const buttonIcon = this.getButtonIcon(date, props.showTodayButton);
5353

@@ -75,7 +75,7 @@ class Presenter {
7575
return toMarkingFormat(new XDate());
7676
};
7777

78-
getPositionAnimation = (date: Date, todayBottomMargin = 0) => {
78+
getPositionAnimation = (date: string, todayBottomMargin = 0) => {
7979
const toValue = isToday(new XDate(date)) ? TOP_POSITION : -todayBottomMargin || -TOP_POSITION;
8080
return {
8181
toValue,

src/expandableCalendar/Context/Provider.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const updateSources = commons.UpdateSources;
1616
const TOP_POSITION = 65;
1717

1818
interface Props {
19-
/** Initial date in 'yyyy-MM-dd' format. Default = Date() */
20-
date: Date;
19+
/** Initial date in 'yyyy-MM-dd' format. Default = now */
20+
date: XDate;
2121
/** Callback for date change event */
22-
onDateChanged?: () => Date;
22+
onDateChanged?: () => XDate;
2323
/** Callback for month change event */
2424
onMonthChange?: () => DateData;
2525
/** Whether to show the today button */
@@ -43,7 +43,7 @@ class CalendarProvider extends Component<Props> {
4343
static displayName = 'CalendarProvider';
4444

4545
static propTypes = {
46-
/** Initial date in 'yyyy-MM-dd' format. Default = Date() */
46+
/** Initial date in 'yyyy-MM-dd' format. Default = now */
4747
date: PropTypes.any.isRequired,
4848
/** Callback for date change event */
4949
onDateChanged: PropTypes.func,
@@ -62,22 +62,27 @@ class CalendarProvider extends Component<Props> {
6262
style = styleConstructor(this.props.theme);
6363
presenter = new Presenter();
6464

65+
6566
state = {
66-
prevDate: this.props.date || toMarkingFormat(new XDate()),
67-
date: this.props.date || toMarkingFormat(new XDate()),
67+
prevDate: this.getDate(this.props.date),
68+
date: this.getDate(this.props.date),
6869
updateSource: updateSources.CALENDAR_INIT,
6970
buttonY: new Animated.Value(this.props.todayBottomMargin ? -this.props.todayBottomMargin : -TOP_POSITION),
70-
buttonIcon: this.presenter.getButtonIcon(this.props.date, this.props.showTodayButton),
71+
buttonIcon: this.presenter.getButtonIcon(this.getDate(this.props.date), this.props.showTodayButton),
7172
disabled: false,
7273
opacity: new Animated.Value(1)
7374
};
7475

7576
componentDidUpdate(prevProps: Props) {
76-
if (prevProps.date !== this.props.date) {
77-
this.setDate(this.props.date, updateSources.PROP_UPDATE);
77+
if (this.props.date && prevProps.date !== this.props.date) {
78+
this.setDate(toMarkingFormat(this.props.date), updateSources.PROP_UPDATE);
7879
}
7980
}
8081

82+
getDate(date: XDate) {
83+
return toMarkingFormat(date || new XDate());
84+
}
85+
8186
getProviderContextValue = () => {
8287
return {
8388
setDate: this.setDate,
@@ -88,17 +93,16 @@ class CalendarProvider extends Component<Props> {
8893
};
8994
};
9095

91-
setDate = (date: Date | string, updateSource: UpdateSource) => {
96+
setDate = (date: string, updateSource: UpdateSource) => {
9297
const {setDate} = this.presenter;
93-
const d = date instanceof Date ? date : new Date(date);
9498

95-
const updateState = (buttonIcon: any) => {
99+
const updateState = (buttonIcon: number) => {
96100
this.setState({date, prevDate: this.state.date, updateSource, buttonIcon}, () => {
97-
this.animateTodayButton(d);
101+
this.animateTodayButton(date);
98102
});
99103
};
100104

101-
setDate(this.props, d, this.state.date, updateState, updateSource);
105+
setDate(this.props, date, this.state.date, updateState, updateSource);
102106
};
103107

104108
setDisabled = (disabled: boolean) => {
@@ -113,7 +117,7 @@ class CalendarProvider extends Component<Props> {
113117
setDisabled(showTodayButton, disabled, this.state.disabled, updateState);
114118
};
115119

116-
animateTodayButton(date: Date) {
120+
animateTodayButton(date: string) {
117121
const {shouldAnimateTodayButton, getPositionAnimation} = this.presenter;
118122

119123
if (shouldAnimateTodayButton(this.props)) {

0 commit comments

Comments
 (0)