@vaadin/vaadin-date-picker 4.4.4 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -97,13 +97,14 @@ To use the Material theme, import the correspondent file from the `theme/materia
97
97
 
98
98
  1. Fork the `vaadin-date-picker` repository and clone it locally.
99
99
 
100
- 1. Make sure you have [npm](https://www.npmjs.com/) and [Bower](https://bower.io) installed.
100
+ 2. Make sure you have [npm](https://www.npmjs.com/) and [Bower](https://bower.io)
101
+ and [Polymer](https://polymer-library.polymer-project.org/) installed.
101
102
 
102
- 1. When in the `vaadin-date-picker` directory, run `npm install` and then `bower install` to install dependencies.
103
+ 3. When in the `vaadin-date-picker` directory, run `npm install` and then `bower install` to install dependencies.
103
104
 
104
- 1. Run `npm start`, browser will automatically open the component API documentation.
105
+ 4. Run `npm start`, browser will automatically open the component API documentation.
105
106
 
106
- 1. You can also open demo or in-browser tests by adding **demo** or **test** to the URL, for example:
107
+ 5. You can also open demo or in-browser tests by adding **demo** or **test** to the URL, for example:
107
108
 
108
109
  - http://127.0.0.1:3000/components/vaadin-date-picker/demo
109
110
  - http://127.0.0.1:3000/components/vaadin-date-picker/test
@@ -111,8 +112,8 @@ To use the Material theme, import the correspondent file from the `theme/materia
111
112
 
112
113
  ## Running tests from the command line
113
114
 
114
- 1. When in the `vaadin-date-picker` directory, run `polymer test`
115
-
115
+ 1. When in the `vaadin-date-picker` directory, run `npm test` (this will execute:"test": "wct")
116
+ (tests will be fetched from the `test/basics.html` file)
116
117
 
117
118
  ## Following the coding style
118
119
 
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "repository": "vaadin/vaadin-date-picker",
11
11
  "homepage": "https://vaadin.com/components",
12
12
  "name": "@vaadin/vaadin-date-picker",
13
- "version": "4.4.4",
13
+ "version": "4.5.0",
14
14
  "main": "vaadin-date-picker.js",
15
15
  "author": "Vaadin Ltd",
16
16
  "license": "Apache-2.0",
@@ -37,6 +37,53 @@ export const DatePickerHelper = class VaadinDatePickerHelper {
37
37
  return Math.floor((daysSinceFirstOfJanuary) / 7 + 1);
38
38
  }
39
39
 
40
+ /**
41
+ * Calculate the year of the date based on the provided reference date.
42
+ * Gets a two-digit year and returns a full year.
43
+ * @param {!Date} referenceDate The date to act as basis in the calculation
44
+ * @param {!number} year Should be in the range of [0, 99]
45
+ * @param {number} month
46
+ * @param {number} day
47
+ * @return {!number} Adjusted year value
48
+ */
49
+ static _getAdjustedYear(referenceDate, year, month = 0, day = 1) {
50
+ if (year > 99) {
51
+ throw new Error('The provided year cannot have more than 2 digits.');
52
+ }
53
+ if (year < 0) {
54
+ throw new Error('The provided year cannot be negative.');
55
+ }
56
+ // Year values up to 2 digits are parsed based on the reference date.
57
+ let adjustedYear = year + Math.floor(referenceDate.getFullYear() / 100) * 100;
58
+ if (referenceDate < new Date(adjustedYear - 50, month, day)) {
59
+ adjustedYear -= 100;
60
+ } else if (referenceDate > new Date(adjustedYear + 50, month, day)) {
61
+ adjustedYear += 100;
62
+ }
63
+ return adjustedYear;
64
+ }
65
+
66
+ /**
67
+ * Parse date string of one of the following date formats:
68
+ * - ISO 8601 `"YYYY-MM-DD"`
69
+ * - 6-digit extended ISO 8601 `"+YYYYYY-MM-DD"`, `"-YYYYYY-MM-DD"`
70
+ * @param {!string} str Date string to parse
71
+ * @return {Date} Parsed date
72
+ */
73
+ static _parseDate(str) {
74
+ // Parsing with RegExp to ensure correct format
75
+ var parts = /^([-+]\d{1}|\d{2,4}|[-+]\d{6})-(\d{1,2})-(\d{1,2})$/.exec(str);
76
+ if (!parts) {
77
+ return;
78
+ }
79
+
80
+ var date = new Date(0, 0); // Wrong date (1900-01-01), but with midnight in local time
81
+ date.setFullYear(parseInt(parts[1], 10));
82
+ date.setMonth(parseInt(parts[2], 10) - 1);
83
+ date.setDate(parseInt(parts[3], 10));
84
+ return date;
85
+ }
86
+
40
87
  /**
41
88
  * Check if two dates are equal.
42
89
  *
@@ -141,12 +141,22 @@ interface DatePickerMixin {
141
141
  * // Translation of the Cancel button text.
142
142
  * cancel: 'Cancel',
143
143
  *
144
+ * // Used for adjusting the year value when parsing dates with short years.
145
+ * // The year values between 0 and 99 are evaluated and adjusted.
146
+ * // Example: for a referenceDate of 1970-10-30;
147
+ * // dateToBeParsed: 40-10-30, result: 1940-10-30
148
+ * // dateToBeParsed: 80-10-30, result: 1980-10-30
149
+ * // dateToBeParsed: 10-10-30, result: 2010-10-30
150
+ * // Supported date format: ISO 8601 `"YYYY-MM-DD"` (default)
151
+ * // The default value is the current date.
152
+ * referenceDate: '',
153
+ *
144
154
  * // A function to format given `Object` as
145
155
  * // date string. Object is in the format `{ day: ..., month: ..., year: ... }`
146
156
  * // Note: The argument month is 0-based. This means that January = 0 and December = 11.
147
- * formatDate: d => {
148
- * // returns a string representation of the given
149
- * // object in 'MM/DD/YYYY' -format
157
+ * formatDate(d) {
158
+ * const yearStr = String(d.year).replace(/\d+/, (y) => '0000'.substr(y.length) + y);
159
+ * return [d.month + 1, d.day, yearStr].join('/');
150
160
  * },
151
161
  *
152
162
  * // A function to parse the given text to an `Object` in the format `{ day: ..., month: ..., year: ... }`.
@@ -178,12 +178,22 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
178
178
  // Translation of the Cancel button text.
179
179
  cancel: 'Cancel',
180
180
 
181
+ // Used for adjusting the year value when parsing dates with short years.
182
+ // The year values between 0 and 99 are evaluated and adjusted.
183
+ // Example: for a referenceDate of 1970-10-30;
184
+ // dateToBeParsed: 40-10-30, result: 1940-10-30
185
+ // dateToBeParsed: 80-10-30, result: 1980-10-30
186
+ // dateToBeParsed: 10-10-30, result: 2010-10-30
187
+ // Supported date format: ISO 8601 `"YYYY-MM-DD"` (default)
188
+ // The default value is the current date.
189
+ referenceDate: '',
190
+
181
191
  // A function to format given `Object` as
182
192
  // date string. Object is in the format `{ day: ..., month: ..., year: ... }`
183
193
  // Note: The argument month is 0-based. This means that January = 0 and December = 11.
184
- formatDate: d => {
185
- // returns a string representation of the given
186
- // object in 'MM/DD/YYYY' -format
194
+ formatDate(d) {
195
+ const yearStr = String(d.year).replace(/\d+/, (y) => '0000'.substr(y.length) + y);
196
+ return [d.month + 1, d.day, yearStr].join('/');
187
197
  },
188
198
 
189
199
  // A function to parse the given text to an `Object` in the format `{ day: ..., month: ..., year: ... }`.
@@ -210,8 +220,18 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
210
220
  value: () => {
211
221
  return {
212
222
  monthNames: [
213
- 'January', 'February', 'March', 'April', 'May',
214
- 'June', 'July', 'August', 'September', 'October', 'November', 'December'
223
+ 'January',
224
+ 'February',
225
+ 'March',
226
+ 'April',
227
+ 'May',
228
+ 'June',
229
+ 'July',
230
+ 'August',
231
+ 'September',
232
+ 'October',
233
+ 'November',
234
+ 'December',
215
235
  ],
216
236
  weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
217
237
  weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
@@ -221,22 +241,24 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
221
241
  clear: 'Clear',
222
242
  today: 'Today',
223
243
  cancel: 'Cancel',
244
+ referenceDate: '',
224
245
  formatDate: d => {
225
246
  const yearStr = String(d.year).replace(/\d+/, y => '0000'.substr(y.length) + y);
226
247
  return [d.month + 1, d.day, yearStr].join('/');
227
248
  },
228
- parseDate: text => {
249
+ parseDate(text) {
229
250
  const parts = text.split('/');
230
251
  const today = new Date();
231
252
  let date, month = today.getMonth(), year = today.getFullYear();
232
253
 
233
254
  if (parts.length === 3) {
255
+ month = parseInt(parts[0]) - 1;
256
+ date = parseInt(parts[1]);
234
257
  year = parseInt(parts[2]);
235
258
  if (parts[2].length < 3 && year >= 0) {
236
- year += year < 50 ? 2000 : 1900;
259
+ const usedReferenceDate = this.referenceDate ? DatePickerHelper._parseDate(this.referenceDate) : new Date();
260
+ year = DatePickerHelper._getAdjustedYear(usedReferenceDate, year, month, date);
237
261
  }
238
- month = parseInt(parts[0]) - 1;
239
- date = parseInt(parts[1]);
240
262
  } else if (parts.length === 2) {
241
263
  month = parseInt(parts[0]) - 1;
242
264
  date = parseInt(parts[1]);
@@ -249,10 +271,10 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
249
271
  }
250
272
  },
251
273
  formatTitle: (monthName, fullYear) => {
252
- return monthName + ' ' + fullYear;
253
- }
274
+ return `${monthName} ${fullYear}`;
275
+ },
254
276
  };
255
- }
277
+ },
256
278
  },
257
279
 
258
280
  /**
@@ -1004,7 +1026,7 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
1004
1026
 
1005
1027
  /** @private */
1006
1028
  _userInputValueChanged(value) {
1007
- if (this.opened && this._inputValue) {
1029
+ if (this._inputValue) {
1008
1030
  const parsedDate = this._getParsedDate();
1009
1031
 
1010
1032
  if (this._isValidDate(parsedDate)) {
@@ -1028,7 +1050,6 @@ export const DatePickerMixin = subclass => class VaadinDatePickerMixin extends m
1028
1050
  get _overlayContent() {
1029
1051
  return this.$.overlay.content.querySelector('#overlay-content');
1030
1052
  }
1031
-
1032
1053
  /**
1033
1054
  * Fired when the user commits a value change.
1034
1055
  *
@@ -147,7 +147,7 @@ class DatePickerElement extends
147
147
  }
148
148
 
149
149
  static get version() {
150
- return '4.4.4';
150
+ return '4.5.0';
151
151
  }
152
152
 
153
153
  static get properties() {