date-time-picker.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import dayjs from 'dayjs';
  8. import config from '../common/config';
  9. import { SuperComponent, wxComponent } from '../common/src/index';
  10. import defaultLocale from './locale/zh';
  11. import props from './props';
  12. const { prefix } = config;
  13. const name = `${prefix}-date-time-picker`;
  14. var ModeItem;
  15. (function (ModeItem) {
  16. ModeItem["YEAR"] = "year";
  17. ModeItem["MONTH"] = "month";
  18. ModeItem["DATE"] = "date";
  19. ModeItem["HOUR"] = "hour";
  20. ModeItem["MINUTE"] = "minute";
  21. })(ModeItem || (ModeItem = {}));
  22. const DATE_MODES = ['year', 'month', 'date'];
  23. const TIME_MODES = ['hour', 'minute'];
  24. const FULL_MODES = [...DATE_MODES, ...TIME_MODES];
  25. const DEFAULT_MIN_DATE = dayjs('2000-01-01 00:00:00');
  26. const DEFAULT_MAX_DATE = dayjs('2030-12-31 23:59:59');
  27. let DateTimePicker = class DateTimePicker extends SuperComponent {
  28. constructor() {
  29. super(...arguments);
  30. this.properties = props;
  31. this.externalClasses = [`${prefix}-class`, `${prefix}-class-confirm`, `${prefix}-class-cancel`, `${prefix}-class-title`];
  32. this.options = {
  33. multipleSlots: true,
  34. };
  35. this.observers = {
  36. 'start, end, value': function () {
  37. this.updateColumns();
  38. },
  39. mode(m) {
  40. const fullModes = this.getFullModeArray(m);
  41. this.setData({
  42. fullModes,
  43. });
  44. this.updateColumns();
  45. },
  46. };
  47. this.date = null;
  48. this.data = {
  49. prefix,
  50. classPrefix: name,
  51. columns: [],
  52. columnsValue: [],
  53. fullModes: [],
  54. locale: defaultLocale,
  55. };
  56. this.controlledProps = [
  57. {
  58. key: 'value',
  59. event: 'change',
  60. },
  61. ];
  62. this.methods = {
  63. updateColumns() {
  64. this.date = this.getParseDate();
  65. const { columns, columnsValue } = this.getValueCols();
  66. this.setData({
  67. columns,
  68. columnsValue,
  69. });
  70. },
  71. getParseDate() {
  72. const { value, defaultValue } = this.properties;
  73. const minDate = this.getMinDate();
  74. const isTimeMode = this.isTimeMode();
  75. let currentValue = value || defaultValue;
  76. if (isTimeMode) {
  77. const dateStr = dayjs(minDate).format('YYYY-MM-DD');
  78. currentValue = dayjs(`${dateStr} ${currentValue}`);
  79. }
  80. const parseDate = dayjs(currentValue || minDate);
  81. const isDateValid = parseDate.isValid();
  82. return isDateValid ? parseDate : minDate;
  83. },
  84. getMinDate() {
  85. const { start } = this.properties;
  86. return start ? dayjs(start) : DEFAULT_MIN_DATE;
  87. },
  88. getMaxDate() {
  89. const { end } = this.properties;
  90. return end ? dayjs(end) : DEFAULT_MAX_DATE;
  91. },
  92. getMinYear() {
  93. return this.getMinDate().year();
  94. },
  95. getMaxYear() {
  96. return this.getMaxDate().year();
  97. },
  98. getMinMonth() {
  99. return this.getMinDate().month();
  100. },
  101. getMaxMonth() {
  102. return this.getMaxDate().month();
  103. },
  104. getMinDay() {
  105. return this.getMinDate().date();
  106. },
  107. getMaxDay() {
  108. return this.getMaxDate().date();
  109. },
  110. getMinHour() {
  111. return this.getMinDate().hour();
  112. },
  113. getMaxHour() {
  114. return this.getMaxDate().hour();
  115. },
  116. getMinMinute() {
  117. return this.getMinDate().minute();
  118. },
  119. getMaxMinute() {
  120. return this.getMaxDate().minute();
  121. },
  122. getDate() {
  123. return this.clipDate((this === null || this === void 0 ? void 0 : this.date) || DEFAULT_MIN_DATE);
  124. },
  125. clipDate(date) {
  126. const minDate = this.getMinDate();
  127. const maxDate = this.getMaxDate();
  128. return dayjs(Math.min(Math.max(minDate.valueOf(), date.valueOf()), maxDate.valueOf()));
  129. },
  130. setYear(date, year) {
  131. const beforeMonthDays = date.date();
  132. const afterMonthDays = date.year(year).daysInMonth();
  133. const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf()));
  134. return tempDate.year(year);
  135. },
  136. setMonth(date, month) {
  137. const beforeMonthDays = date.date();
  138. const afterMonthDays = date.month(month).daysInMonth();
  139. const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf()));
  140. return tempDate.month(month);
  141. },
  142. getColumnOptions() {
  143. const { fullModes } = this.data;
  144. const dateParams = this.getCommonDateParams();
  145. const columnOptions = [];
  146. fullModes === null || fullModes === void 0 ? void 0 : fullModes.forEach((mode) => {
  147. const columnOption = this.getOptionByType(mode, dateParams);
  148. columnOptions.push(columnOption);
  149. });
  150. return columnOptions;
  151. },
  152. getCommonDateParams() {
  153. const date = this.getDate();
  154. const selYear = date.year();
  155. const selMonth = date.month();
  156. const selDate = date.date();
  157. const selHour = date.hour();
  158. const minDateYear = this.getMinYear();
  159. const maxDateYear = this.getMaxYear();
  160. const minDateMonth = this.getMinMonth();
  161. const maxDateMonth = this.getMaxMonth();
  162. const minDateDay = this.getMinDay();
  163. const maxDateDay = this.getMaxDay();
  164. const minDateHour = this.getMinHour();
  165. const maxDateHour = this.getMaxHour();
  166. const minDateMinute = this.getMinMinute();
  167. const maxDateMinute = this.getMaxMinute();
  168. return {
  169. date,
  170. selYear,
  171. selMonth,
  172. selDate,
  173. selHour,
  174. minDateYear,
  175. maxDateYear,
  176. minDateMonth,
  177. maxDateMonth,
  178. minDateDay,
  179. maxDateDay,
  180. minDateHour,
  181. maxDateHour,
  182. minDateMinute,
  183. maxDateMinute,
  184. };
  185. },
  186. getOptionByType(type, dateParams) {
  187. switch (type) {
  188. case ModeItem.YEAR:
  189. return this.getYearOptions(dateParams);
  190. case ModeItem.MONTH:
  191. return this.getMonthOptions(dateParams);
  192. case ModeItem.DATE:
  193. return this.getDayOptions(dateParams);
  194. case ModeItem.HOUR:
  195. return this.getHourOptions(dateParams);
  196. case ModeItem.MINUTE:
  197. return this.getMinuteOptions(dateParams);
  198. default:
  199. break;
  200. }
  201. },
  202. getYearOptions(dateParams) {
  203. const { locale } = this.data;
  204. const { minDateYear, maxDateYear } = dateParams;
  205. const years = [];
  206. for (let i = minDateYear; i <= maxDateYear; i += 1) {
  207. years.push({
  208. value: `${i}`,
  209. label: `${i + locale.year}`,
  210. });
  211. }
  212. return years;
  213. },
  214. getMonthOptions(dateParams) {
  215. const { locale } = this.data;
  216. const { minDateYear, maxDateYear, selYear, minDateMonth, maxDateMonth } = dateParams;
  217. const months = [];
  218. let minMonth = 0;
  219. let maxMonth = 11;
  220. if (minDateYear === selYear) {
  221. minMonth = minDateMonth;
  222. }
  223. if (maxDateYear === selYear) {
  224. maxMonth = maxDateMonth;
  225. }
  226. for (let i = minMonth; i <= maxMonth; i += 1) {
  227. months.push({
  228. value: `${i}`,
  229. label: `${i + 1 + locale.month}`,
  230. });
  231. }
  232. return months;
  233. },
  234. getDayOptions(dateParams) {
  235. const { locale } = this.data;
  236. const { minDateYear, maxDateYear, minDateMonth, maxDateMonth, minDateDay, maxDateDay, selYear, selMonth, date } = dateParams;
  237. const days = [];
  238. let minDay = 1;
  239. let maxDay = date.daysInMonth();
  240. if (minDateYear === selYear && minDateMonth === selMonth) {
  241. minDay = minDateDay;
  242. }
  243. if (maxDateYear === selYear && maxDateMonth === selMonth) {
  244. maxDay = maxDateDay;
  245. }
  246. for (let i = minDay; i <= maxDay; i += 1) {
  247. days.push({
  248. value: `${i}`,
  249. label: `${i + locale.day}`,
  250. });
  251. }
  252. return days;
  253. },
  254. getHourOptions(dateParams) {
  255. const { locale } = this.data;
  256. const { minDateYear, maxDateYear, minDateMonth, maxDateMonth, minDateDay, minDateHour, maxDateDay, maxDateHour, selYear, selMonth, selDate, } = dateParams;
  257. const hours = [];
  258. let minHour = 0;
  259. let maxHour = 23;
  260. if (minDateYear === selYear && minDateMonth === selMonth && minDateDay === selDate) {
  261. minHour = minDateHour;
  262. }
  263. if (maxDateYear === selYear && maxDateMonth === selMonth && maxDateDay === selDate) {
  264. maxHour = maxDateHour;
  265. }
  266. for (let i = minHour; i <= maxHour; i += 1) {
  267. hours.push({
  268. value: `${i}`,
  269. label: `${i + locale.hour}`,
  270. });
  271. }
  272. return hours;
  273. },
  274. getMinuteOptions(dateParams) {
  275. const { locale } = this.data;
  276. const { minDateYear, maxDateYear, minDateMonth, maxDateMonth, minDateDay, maxDateDay, minDateHour, maxDateHour, minDateMinute, maxDateMinute, selYear, selMonth, selDate, selHour, } = dateParams;
  277. const minutes = [];
  278. let minMinute = 0;
  279. let maxMinute = 59;
  280. if (minDateYear === selYear && minDateMonth === selMonth && minDateDay === selDate && minDateHour === selHour) {
  281. minMinute = minDateMinute;
  282. }
  283. if (maxDateYear === selYear && maxDateMonth === selMonth && maxDateDay === selDate && maxDateHour === selHour) {
  284. maxMinute = maxDateMinute;
  285. }
  286. for (let i = minMinute; i <= maxMinute; i += 1) {
  287. minutes.push({
  288. value: `${i}`,
  289. label: `${i + locale.minute}`,
  290. });
  291. }
  292. return minutes;
  293. },
  294. getValueCols() {
  295. return {
  296. columns: this.getColumnOptions(),
  297. columnsValue: this.getColumnsValue(),
  298. };
  299. },
  300. getColumnsValue() {
  301. const { fullModes } = this.data;
  302. const date = this.getDate();
  303. const columnsValue = [];
  304. fullModes === null || fullModes === void 0 ? void 0 : fullModes.forEach((mode) => {
  305. columnsValue.push(`${date[mode]()}`);
  306. });
  307. return columnsValue;
  308. },
  309. getNewDate(value, type) {
  310. let newValue = this.getDate();
  311. switch (type) {
  312. case ModeItem.YEAR:
  313. newValue = this.setYear(newValue, value);
  314. break;
  315. case ModeItem.MONTH:
  316. newValue = this.setMonth(newValue, value);
  317. break;
  318. case ModeItem.DATE:
  319. newValue = newValue.date(value);
  320. break;
  321. case ModeItem.HOUR:
  322. newValue = newValue.hour(value);
  323. break;
  324. case ModeItem.MINUTE:
  325. newValue = newValue.minute(value);
  326. break;
  327. default:
  328. break;
  329. }
  330. return this.clipDate(newValue);
  331. },
  332. onColumnChange(e) {
  333. const { value, column } = e === null || e === void 0 ? void 0 : e.detail;
  334. const { fullModes, format } = this.data;
  335. const columnValue = value === null || value === void 0 ? void 0 : value[column];
  336. const columnType = fullModes === null || fullModes === void 0 ? void 0 : fullModes[column];
  337. const newValue = this.getNewDate(parseInt(columnValue, 10), columnType);
  338. this.date = newValue;
  339. const { columns, columnsValue } = this.getValueCols();
  340. this.setData({
  341. columns,
  342. columnsValue,
  343. });
  344. const date = this.getDate();
  345. const pickValue = format ? date.format(format) : date.valueOf();
  346. this.triggerEvent('pick', { value: pickValue });
  347. },
  348. onConfirm() {
  349. const { format } = this.properties;
  350. const date = this.getDate();
  351. const value = format ? date.format(format) : date.valueOf();
  352. this._trigger('change', { value });
  353. this.resetColumns();
  354. },
  355. onCancel() {
  356. this.resetColumns();
  357. this.triggerEvent('cancel');
  358. },
  359. onVisibleChange(e) {
  360. if (!e.detail.visible) {
  361. this.resetColumns();
  362. }
  363. },
  364. resetColumns() {
  365. const parseDate = this.getParseDate();
  366. this.date = parseDate;
  367. const { columns, columnsValue } = this.getValueCols();
  368. this.setData({
  369. columns,
  370. columnsValue,
  371. });
  372. },
  373. };
  374. }
  375. getFullModeArray(mode) {
  376. if (typeof mode === 'string' || mode instanceof String) {
  377. return this.getFullModeByModeString(mode, FULL_MODES);
  378. }
  379. if (Array.isArray(mode)) {
  380. if ((mode === null || mode === void 0 ? void 0 : mode.length) === 1) {
  381. return this.getFullModeByModeString(mode[0], FULL_MODES);
  382. }
  383. if ((mode === null || mode === void 0 ? void 0 : mode.length) === 2) {
  384. const dateModes = this.getFullModeByModeString(mode[0], DATE_MODES);
  385. const timeModes = this.getFullModeByModeString(mode[1], TIME_MODES);
  386. return [...dateModes, ...timeModes];
  387. }
  388. }
  389. }
  390. getFullModeByModeString(modeString, matchModes) {
  391. if (!modeString) {
  392. return [];
  393. }
  394. const endIndex = matchModes === null || matchModes === void 0 ? void 0 : matchModes.findIndex((mode) => modeString === mode);
  395. return matchModes === null || matchModes === void 0 ? void 0 : matchModes.slice(0, endIndex + 1);
  396. }
  397. isTimeMode() {
  398. const { fullModes } = this.data;
  399. return fullModes[0] === ModeItem.HOUR;
  400. }
  401. };
  402. DateTimePicker = __decorate([
  403. wxComponent()
  404. ], DateTimePicker);
  405. export default DateTimePicker;