DatePicker.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _construct2 = _interopRequireDefault(require("@babel/runtime/helpers/construct"));
  6. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  7. var _utils = require("../utils");
  8. var _date = require("../utils/validate/date");
  9. var _string = require("../utils/format/string");
  10. var _utils2 = require("./utils");
  11. var _shared = require("./shared");
  12. var currentYear = new Date().getFullYear();
  13. var _createNamespace = (0, _utils.createNamespace)('date-picker'),
  14. createComponent = _createNamespace[0];
  15. var _default2 = createComponent({
  16. mixins: [_shared.TimePickerMixin],
  17. props: (0, _extends2.default)({}, _shared.sharedProps, {
  18. type: {
  19. type: String,
  20. default: 'datetime'
  21. },
  22. minDate: {
  23. type: Date,
  24. default: function _default() {
  25. return new Date(currentYear - 10, 0, 1);
  26. },
  27. validator: _date.isDate
  28. },
  29. maxDate: {
  30. type: Date,
  31. default: function _default() {
  32. return new Date(currentYear + 10, 11, 31);
  33. },
  34. validator: _date.isDate
  35. }
  36. }),
  37. watch: {
  38. filter: 'updateInnerValue',
  39. minDate: function minDate() {
  40. var _this = this;
  41. this.$nextTick(function () {
  42. _this.updateInnerValue();
  43. });
  44. },
  45. maxDate: function maxDate(value) {
  46. if (this.innerValue.valueOf() >= value.valueOf()) {
  47. this.innerValue = value;
  48. } else {
  49. this.updateInnerValue();
  50. }
  51. },
  52. value: function value(val) {
  53. val = this.formatValue(val);
  54. if (val && val.valueOf() !== this.innerValue.valueOf()) {
  55. this.innerValue = val;
  56. }
  57. }
  58. },
  59. computed: {
  60. ranges: function ranges() {
  61. var _this$getBoundary = this.getBoundary('max', this.innerValue ? this.innerValue : this.minDate),
  62. maxYear = _this$getBoundary.maxYear,
  63. maxDate = _this$getBoundary.maxDate,
  64. maxMonth = _this$getBoundary.maxMonth,
  65. maxHour = _this$getBoundary.maxHour,
  66. maxMinute = _this$getBoundary.maxMinute;
  67. var _this$getBoundary2 = this.getBoundary('min', this.innerValue ? this.innerValue : this.minDate),
  68. minYear = _this$getBoundary2.minYear,
  69. minDate = _this$getBoundary2.minDate,
  70. minMonth = _this$getBoundary2.minMonth,
  71. minHour = _this$getBoundary2.minHour,
  72. minMinute = _this$getBoundary2.minMinute;
  73. var result = [{
  74. type: 'year',
  75. range: [minYear, maxYear]
  76. }, {
  77. type: 'month',
  78. range: [minMonth, maxMonth]
  79. }, {
  80. type: 'day',
  81. range: [minDate, maxDate]
  82. }, {
  83. type: 'hour',
  84. range: [minHour, maxHour]
  85. }, {
  86. type: 'minute',
  87. range: [minMinute, maxMinute]
  88. }];
  89. switch (this.type) {
  90. case 'date':
  91. result = result.slice(0, 3);
  92. break;
  93. case 'year-month':
  94. result = result.slice(0, 2);
  95. break;
  96. case 'month-day':
  97. result = result.slice(1, 3);
  98. break;
  99. case 'datehour':
  100. result = result.slice(0, 4);
  101. break;
  102. }
  103. if (this.columnsOrder) {
  104. var columnsOrder = this.columnsOrder.concat(result.map(function (column) {
  105. return column.type;
  106. }));
  107. result.sort(function (a, b) {
  108. return columnsOrder.indexOf(a.type) - columnsOrder.indexOf(b.type);
  109. });
  110. }
  111. return result;
  112. }
  113. },
  114. methods: {
  115. formatValue: function formatValue(value) {
  116. var _this2 = this;
  117. if (!(0, _date.isDate)(value)) {
  118. return null;
  119. }
  120. var minDate = new Date(this.minDate);
  121. var maxDate = new Date(this.maxDate);
  122. var dateMethods = {
  123. year: 'getFullYear',
  124. month: 'getMonth',
  125. day: 'getDate',
  126. hour: 'getHours',
  127. minute: 'getMinutes'
  128. };
  129. if (this.originColumns) {
  130. var dateColumns = this.originColumns.map(function (_ref, index) {
  131. var type = _ref.type,
  132. values = _ref.values;
  133. var range = _this2.ranges[index].range;
  134. var minDateVal = minDate[dateMethods[type]]();
  135. var maxDateVal = maxDate[dateMethods[type]]();
  136. var min = type === 'month' ? +values[0] - 1 : +values[0];
  137. var max = type === 'month' ? +values[values.length - 1] - 1 : +values[values.length - 1];
  138. return {
  139. type: type,
  140. values: [minDateVal < range[0] ? Math.max(minDateVal, min) : min || minDateVal, maxDateVal > range[1] ? Math.min(maxDateVal, max) : max || maxDateVal]
  141. };
  142. });
  143. if (this.type === 'month-day') {
  144. var year = (this.innerValue || this.minDate).getFullYear();
  145. dateColumns.unshift({
  146. type: 'year',
  147. values: [year, year]
  148. });
  149. }
  150. var dates = Object.keys(dateMethods).map(function (type) {
  151. var _dateColumns$filter$;
  152. return (_dateColumns$filter$ = dateColumns.filter(function (item) {
  153. return item.type === type;
  154. })[0]) == null ? void 0 : _dateColumns$filter$.values;
  155. }).filter(function (item) {
  156. return item;
  157. });
  158. minDate = (0, _construct2.default)(Date, dates.map(function (val) {
  159. return (0, _utils2.getTrueValue)(val[0]);
  160. }));
  161. maxDate = (0, _construct2.default)(Date, dates.map(function (val) {
  162. return (0, _utils2.getTrueValue)(val[1]);
  163. }));
  164. }
  165. value = Math.max(value, minDate.getTime());
  166. value = Math.min(value, maxDate.getTime());
  167. return new Date(value);
  168. },
  169. getBoundary: function getBoundary(type, value) {
  170. var _ref2;
  171. var boundary = this[type + "Date"];
  172. var year = boundary.getFullYear();
  173. var month = 1;
  174. var date = 1;
  175. var hour = 0;
  176. var minute = 0;
  177. if (type === 'max') {
  178. month = 12;
  179. date = (0, _utils2.getMonthEndDay)(value.getFullYear(), value.getMonth() + 1);
  180. hour = 23;
  181. minute = 59;
  182. }
  183. if (value.getFullYear() === year) {
  184. month = boundary.getMonth() + 1;
  185. if (value.getMonth() + 1 === month) {
  186. date = boundary.getDate();
  187. if (value.getDate() === date) {
  188. hour = boundary.getHours();
  189. if (value.getHours() === hour) {
  190. minute = boundary.getMinutes();
  191. }
  192. }
  193. }
  194. }
  195. return _ref2 = {}, _ref2[type + "Year"] = year, _ref2[type + "Month"] = month, _ref2[type + "Date"] = date, _ref2[type + "Hour"] = hour, _ref2[type + "Minute"] = minute, _ref2;
  196. },
  197. updateInnerValue: function updateInnerValue() {
  198. var _this3 = this;
  199. var type = this.type;
  200. var indexes = this.getPicker().getIndexes();
  201. var getValue = function getValue(type) {
  202. var index = 0;
  203. _this3.originColumns.forEach(function (column, columnIndex) {
  204. if (type === column.type) {
  205. index = columnIndex;
  206. }
  207. });
  208. var values = _this3.originColumns[index].values;
  209. return (0, _utils2.getTrueValue)(values[indexes[index]]);
  210. };
  211. var year;
  212. var month;
  213. var day;
  214. if (type === 'month-day') {
  215. year = (this.innerValue || this.minDate).getFullYear();
  216. month = getValue('month');
  217. day = getValue('day');
  218. } else {
  219. year = getValue('year');
  220. month = getValue('month');
  221. day = type === 'year-month' ? 1 : getValue('day');
  222. }
  223. var maxDay = (0, _utils2.getMonthEndDay)(year, month);
  224. day = day > maxDay ? maxDay : day;
  225. var hour = 0;
  226. var minute = 0;
  227. if (type === 'datehour') {
  228. hour = getValue('hour');
  229. }
  230. if (type === 'datetime') {
  231. hour = getValue('hour');
  232. minute = getValue('minute');
  233. }
  234. var value = new Date(year, month - 1, day, hour, minute);
  235. this.innerValue = this.formatValue(value);
  236. },
  237. onChange: function onChange(picker) {
  238. var _this4 = this;
  239. this.updateInnerValue();
  240. this.$nextTick(function () {
  241. _this4.$nextTick(function () {
  242. // https://github.com/vant-ui/vant/issues/9775
  243. _this4.updateInnerValue();
  244. _this4.$emit('change', picker);
  245. });
  246. });
  247. },
  248. updateColumnValue: function updateColumnValue() {
  249. var _this5 = this;
  250. var value = this.innerValue ? this.innerValue : this.minDate;
  251. var formatter = this.formatter;
  252. var values = this.originColumns.map(function (column) {
  253. switch (column.type) {
  254. case 'year':
  255. return formatter('year', "" + value.getFullYear());
  256. case 'month':
  257. return formatter('month', (0, _string.padZero)(value.getMonth() + 1));
  258. case 'day':
  259. return formatter('day', (0, _string.padZero)(value.getDate()));
  260. case 'hour':
  261. return formatter('hour', (0, _string.padZero)(value.getHours()));
  262. case 'minute':
  263. return formatter('minute', (0, _string.padZero)(value.getMinutes()));
  264. default:
  265. // no default
  266. return null;
  267. }
  268. });
  269. this.$nextTick(function () {
  270. _this5.getPicker().setValues(values);
  271. });
  272. }
  273. }
  274. });
  275. exports.default = _default2;