Skip to content

Commit

Permalink
Allow utcOffset to be specified as a string (Hacker0x01#1500)
Browse files Browse the repository at this point in the history
Moment, which ultimately consumes the offsets, accepts either numbers or
strings. Updated prop types definitions and added a few tests.
  • Loading branch information
wacii authored and martijnrusschen committed Oct 17, 2018
1 parent d12832a commit 73291de
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 14 deletions.
25 changes: 20 additions & 5 deletions docs-site/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -35901,7 +35901,10 @@
todayButton: _propTypes2.default.string,
useWeekdaysShort: _propTypes2.default.bool,
formatWeekDay: _propTypes2.default.func,
utcOffset: _propTypes2.default.number,
utcOffset: _propTypes2.default.oneOfType([
_propTypes2.default.number,
_propTypes2.default.string
]),
value: _propTypes2.default.string,
weekLabel: _propTypes2.default.string,
withPortal: _propTypes2.default.bool,
Expand Down Expand Up @@ -36859,7 +36862,10 @@
useWeekdaysShort: _propTypes2.default.bool,
formatWeekDay: _propTypes2.default.func,
withPortal: _propTypes2.default.bool,
utcOffset: _propTypes2.default.number,
utcOffset: _propTypes2.default.oneOfType([
_propTypes2.default.number,
_propTypes2.default.string
]),
weekLabel: _propTypes2.default.string,
yearDropdownItemNumber: _propTypes2.default.number,
setOpen: _propTypes2.default.func,
Expand Down Expand Up @@ -57849,7 +57855,10 @@
selectsStart: _propTypes2.default.bool,
showWeekNumbers: _propTypes2.default.bool,
startDate: _propTypes2.default.object,
utcOffset: _propTypes2.default.number
utcOffset: _propTypes2.default.oneOfType([
_propTypes2.default.number,
_propTypes2.default.string
])
};
exports.default = Month;

Expand Down Expand Up @@ -58071,7 +58080,10 @@
selectsStart: _propTypes2.default.bool,
showWeekNumber: _propTypes2.default.bool,
startDate: _propTypes2.default.object,
utcOffset: _propTypes2.default.number
utcOffset: _propTypes2.default.oneOfType([
_propTypes2.default.number,
_propTypes2.default.string
])
};
exports.default = Week;

Expand Down Expand Up @@ -58389,7 +58401,10 @@
selectsEnd: _propTypes2.default.bool,
selectsStart: _propTypes2.default.bool,
startDate: _propTypes2.default.object,
utcOffset: _propTypes2.default.number
utcOffset: _propTypes2.default.oneOfType([
_propTypes2.default.number,
_propTypes2.default.string
])
};
exports.default = Day;

Expand Down
2 changes: 1 addition & 1 deletion docs/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ General datepicker component.
| `title` | `string` | | |
| `todayButton` | `string` | | |
| `useWeekdaysShort` | `bool` | | |
| `utcOffset` | `number` | | |
| `utcOffset` | `union(number\|string)` | | |
| `value` | `string` | | |
| `weekLabel` | `string` | | |
| `withPortal` | `bool` | `false` | |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ General datepicker component.
| `todayButton` | `string` | | |
| `useShortMonthInDropdown` | `bool` | | |
| `useWeekdaysShort` | `bool` | | |
| `utcOffset` | `number` | | |
| `utcOffset` | `union(number\|string)` | | |
| `value` | `string` | | |
| `weekLabel` | `string` | | |
| `withPortal` | `bool` | `false` | |
Expand Down
2 changes: 1 addition & 1 deletion src/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class Calendar extends React.Component {
useWeekdaysShort: PropTypes.bool,
formatWeekDay: PropTypes.func,
withPortal: PropTypes.bool,
utcOffset: PropTypes.number,
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
weekLabel: PropTypes.string,
yearDropdownItemNumber: PropTypes.number,
setOpen: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion src/day.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class Day extends React.Component {
selectsEnd: PropTypes.bool,
selectsStart: PropTypes.bool,
startDate: PropTypes.object,
utcOffset: PropTypes.number
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
};

handleClick = event => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default class DatePicker extends React.Component {
todayButton: PropTypes.string,
useWeekdaysShort: PropTypes.bool,
formatWeekDay: PropTypes.func,
utcOffset: PropTypes.number,
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
value: PropTypes.string,
weekLabel: PropTypes.string,
withPortal: PropTypes.bool,
Expand Down
2 changes: 1 addition & 1 deletion src/month.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Month extends React.Component {
selectsStart: PropTypes.bool,
showWeekNumbers: PropTypes.bool,
startDate: PropTypes.object,
utcOffset: PropTypes.number
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
};

handleDayClick = (day, event) => {
Expand Down
2 changes: 1 addition & 1 deletion src/week.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class Week extends React.Component {
selectsStart: PropTypes.bool,
showWeekNumber: PropTypes.bool,
startDate: PropTypes.object,
utcOffset: PropTypes.number
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
};

handleDayClick = (day, event) => {
Expand Down
11 changes: 9 additions & 2 deletions test/calendar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ describe("Calendar", function() {
});

it("should start with the today date with specified time zone", function() {
const utcOffset = 12;
const calendar = getCalendar({ utcOffset });
let utcOffset = 12;
let calendar = getCalendar({ utcOffset });
assert(
utils.isSameDay(calendar.state().date, utils.newDateWithOffset(utcOffset))
);

// using string offsets
utcOffset = "+12:00";
calendar = getCalendar({ utcOffset });
assert(
utils.isSameDay(calendar.state().date, utils.newDateWithOffset(utcOffset))
);
Expand Down
18 changes: 18 additions & 0 deletions test/date_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe("date_utils", function() {
setUTCOffset(newDate("2016-02-10"), -210)
)
).to.be.false;
expect(
isSameUtcOffset(
setUTCOffset(newDate("2016-02-10"), "-05:45"),
setUTCOffset(newDate("2016-02-10"), "+01:00")
)
).to.be.false;
});

it("should return true for equal utc offsets, regardless of dates", function() {
Expand All @@ -126,6 +132,18 @@ describe("date_utils", function() {
setUTCOffset(newDate("2016-02-15"), 6)
)
).to.be.true;
expect(
isSameUtcOffset(
setUTCOffset(newDate("2016-12-10"), "+04:00"),
setUTCOffset(newDate("2016-02-15"), "+04:00")
)
).to.be.true;
expect(
isSameUtcOffset(
setUTCOffset(newDate("2016-12-10"), "-02:00"),
setUTCOffset(newDate("2016-02-15"), "-0200")
)
).to.be.true;
});
});

Expand Down
16 changes: 16 additions & 0 deletions test/datepicker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,22 @@ describe("DatePicker", () => {
expect(tmzDatePicker.find("input").prop("value")).to.equal(
"2016-11-22 06:00"
);

// using string offsets
tmzDatePicker.setState({ startDate: date, utcOffset: "-06:00" });

expect(tmzDatePicker.find("input").prop("value")).to.equal(
"2016-11-21 18:00"
);

tmzDatePicker.setState({
utcOffset: "+06:00",
startDate: utils.setUTCOffset(utils.cloneDate(date), 6)
});

expect(tmzDatePicker.find("input").prop("value")).to.equal(
"2016-11-22 06:00"
);
});
it("should correctly update the input when the value prop changes", () => {
const datePicker = mount(<DatePicker />);
Expand Down

0 comments on commit 73291de

Please sign in to comment.