date-time-picker.js 15 KB

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