date-time-picker.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 { SuperComponent, wxComponent } from '../common/src/index';
  9. import defaultLocale from './locale/zh';
  10. import props from './props';
  11. /**
  12. *
  13. mode 数组 [日期选择器的最后一个时间点,时间选择器的最后一个时间点]
  14. [year] ------ 年
  15. [month] ------ 年/月
  16. [date] ------ 年/月/日
  17. [hour] ------ 年/月/日/小时
  18. [minute] ------ 年/月/日/小时/分钟
  19. [year, hour] ------ 年/小时
  20. [year, minute] ------ 年/小时/分钟
  21. [month, hour] ------ 年/月/小时
  22. [month, minute] ------ 年/月/小时/分钟
  23. [date, hour] ------ 年/月/日/小时
  24. [date, minute] ------ 年/月/日/小时/分钟
  25. */
  26. var ModeType;
  27. (function (ModeType) {
  28. ModeType["YEAR"] = "year";
  29. ModeType["MONTH"] = "month";
  30. ModeType["DATE"] = "date";
  31. ModeType["HOUR"] = "hour";
  32. ModeType["MINUTE"] = "minute";
  33. ModeType["YEAR_ADD_HOUR"] = "year+hour";
  34. ModeType["YEAR_ADD_MINUTE"] = "year+minute";
  35. ModeType["MONTH_ADD_HOUR"] = "month+hour";
  36. ModeType["MONTH_ADD_MINUTE"] = "month+minute";
  37. ModeType["MONTH_ADD_DATE"] = "month+date";
  38. ModeType["DATE_ADD_HOUR"] = "date+hour";
  39. ModeType["DATE_ADD_MINUTE"] = "date+minute";
  40. ModeType["NULL_HOUR"] = "null+hour";
  41. ModeType["NULL_MINUTE"] = "null+minute";
  42. })(ModeType || (ModeType = {}));
  43. const DEFAULT_MIN_DATE = dayjs('2000-01-01 00:00:00');
  44. const DEFAULT_MAX_DATE = dayjs('2030-12-31 23:59:59');
  45. let DateTimePicker = class DateTimePicker extends SuperComponent {
  46. constructor() {
  47. super(...arguments);
  48. this.properties = props;
  49. this.externalClasses = ['t-class', 't-class-confirm', 't-class-cancel', 't-class-title'];
  50. this.options = {
  51. multipleSlots: true,
  52. };
  53. this.observers = {
  54. // value 变化需要同步 内部 date 实现受控属性
  55. value() {
  56. this.updateColumns();
  57. },
  58. mode(m) {
  59. const modeName = this.getModeNameByPropsMode(m);
  60. this.setData({
  61. modeName,
  62. });
  63. this.updateColumns();
  64. },
  65. };
  66. this.data = {
  67. date: null,
  68. columns: [],
  69. columnsValue: [],
  70. modeName: 'date',
  71. locale: defaultLocale,
  72. };
  73. this.methods = {
  74. updateColumns() {
  75. const { value } = this.properties;
  76. const parseDate = dayjs(value || DEFAULT_MIN_DATE);
  77. this.setData({
  78. date: parseDate,
  79. });
  80. const { columns, columnsValue } = this.getValueCols();
  81. this.setData({
  82. columns,
  83. columnsValue,
  84. });
  85. },
  86. getModeNameByPropsMode(mode) {
  87. return mode.join('+');
  88. },
  89. getMinDate() {
  90. const { disableDate } = this.properties;
  91. const startDate = disableDate === null || disableDate === void 0 ? void 0 : disableDate.before;
  92. return startDate ? dayjs(startDate) : DEFAULT_MIN_DATE;
  93. },
  94. getMaxDate() {
  95. const { disableDate } = this.properties;
  96. const endDate = disableDate === null || disableDate === void 0 ? void 0 : disableDate.after;
  97. return endDate ? dayjs(endDate) : DEFAULT_MAX_DATE;
  98. },
  99. getMinYear() {
  100. return this.getMinDate().year();
  101. },
  102. getMaxYear() {
  103. return this.getMaxDate().year();
  104. },
  105. getMinMonth() {
  106. return this.getMinDate().month();
  107. },
  108. getMaxMonth() {
  109. return this.getMaxDate().month();
  110. },
  111. getMinDay() {
  112. return this.getMinDate().date();
  113. },
  114. getMaxDay() {
  115. return this.getMaxDate().date();
  116. },
  117. getMinHour() {
  118. return this.getMinDate().hour();
  119. },
  120. getMaxHour() {
  121. return this.getMaxDate().hour();
  122. },
  123. getMinMinute() {
  124. return this.getMinDate().minute();
  125. },
  126. getMaxMinute() {
  127. return this.getMaxDate().minute();
  128. },
  129. getDate() {
  130. var _a;
  131. return this.clipDate(((_a = this.data) === null || _a === void 0 ? void 0 : _a.date) || DEFAULT_MIN_DATE);
  132. },
  133. // 数据裁减 确保数据不越界
  134. clipDate(date) {
  135. const minDate = this.getMinDate();
  136. const maxDate = this.getMaxDate();
  137. return dayjs(Math.min(Math.max(minDate.valueOf(), date.valueOf()), maxDate.valueOf()));
  138. },
  139. // 年变化时 需要修正 日数据 例如 2 月的 28 | 29
  140. setYear(date, year) {
  141. const beforeMonthDays = date.date();
  142. const afterMonthDays = date.year(year).daysInMonth();
  143. const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf()));
  144. return tempDate.year(year);
  145. },
  146. // 月变化时 需要修正 日数据边界
  147. setMonth(date, month) {
  148. const beforeMonthDays = date.date();
  149. const afterMonthDays = date.month(month).daysInMonth();
  150. const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf()));
  151. return tempDate.month(month);
  152. },
  153. getDateData() {
  154. const { modeName, locale } = this.data;
  155. const date = this.getDate();
  156. const selYear = date.year();
  157. const selMonth = date.month();
  158. const selDate = date.date();
  159. const selHour = date.hour();
  160. const minDateYear = this.getMinYear();
  161. const maxDateYear = this.getMaxYear();
  162. const minDateMonth = this.getMinMonth();
  163. const maxDateMonth = this.getMaxMonth();
  164. const minDateDay = this.getMinDay();
  165. const maxDateDay = this.getMaxDay();
  166. const minDateHour = this.getMinHour();
  167. const maxDateHour = this.getMaxHour();
  168. const minDateMinute = this.getMinMinute();
  169. const maxDateMinute = this.getMaxMinute();
  170. // 年处理
  171. const years = [];
  172. for (let i = minDateYear; i <= maxDateYear; i += 1) {
  173. years.push({
  174. type: ModeType.YEAR,
  175. value: `${i}`,
  176. label: `${i + locale.year}`,
  177. });
  178. }
  179. if (modeName === ModeType.YEAR) {
  180. return [years];
  181. }
  182. // 月处理
  183. const months = [];
  184. let minMonth = 0;
  185. let maxMonth = 11;
  186. if (minDateYear === selYear) {
  187. minMonth = minDateMonth;
  188. }
  189. if (maxDateYear === selYear) {
  190. maxMonth = maxDateMonth;
  191. }
  192. for (let i = minMonth; i <= maxMonth; i += 1) {
  193. months.push({
  194. type: ModeType.MONTH,
  195. value: `${i}`,
  196. label: `${i + 1 + locale.month}`,
  197. });
  198. }
  199. if (modeName === ModeType.MONTH) {
  200. return [years, months];
  201. }
  202. // 日处理
  203. const days = [];
  204. let minDay = 1;
  205. let maxDay = date.daysInMonth();
  206. if (minDateYear === selYear && minDateMonth === selMonth) {
  207. minDay = minDateDay;
  208. }
  209. if (maxDateYear === selYear && maxDateMonth === selMonth) {
  210. maxDay = maxDateDay;
  211. }
  212. for (let i = minDay; i <= maxDay; i += 1) {
  213. days.push({
  214. type: ModeType.DATE,
  215. value: `${i}`,
  216. label: `${i + locale.day}`,
  217. });
  218. }
  219. if (modeName === ModeType.DATE) {
  220. return [years, months, days];
  221. }
  222. // 小时
  223. const hours = [];
  224. let minHour = 0;
  225. let maxHour = 23;
  226. if (minDateYear === selYear && minDateMonth === selMonth && minDateDay === selDate) {
  227. minHour = minDateHour;
  228. }
  229. if (maxDateYear === selYear && maxDateMonth === selMonth && maxDateDay === selDate) {
  230. maxHour = maxDateHour;
  231. }
  232. for (let i = minHour; i <= maxHour; i += 1) {
  233. hours.push({
  234. type: ModeType.HOUR,
  235. value: `${i}`,
  236. label: `${i + locale.hour}`,
  237. });
  238. }
  239. // 分钟
  240. const minutes = [];
  241. let minMinute = 0;
  242. let maxMinute = 59;
  243. if (minDateYear === selYear && minDateMonth === selMonth && minDateDay === selDate && minHour === selHour) {
  244. minMinute = minDateMinute;
  245. }
  246. if (maxDateYear === selYear && maxDateMonth === selMonth && maxDateDay === selDate && maxHour === selHour) {
  247. maxMinute = maxDateMinute;
  248. }
  249. for (let i = minMinute; i <= maxMinute; i += 1) {
  250. minutes.push({
  251. type: ModeType.MINUTE,
  252. value: `${i}`,
  253. label: `${i + locale.minute}`,
  254. });
  255. }
  256. if (modeName === ModeType.MINUTE || modeName === ModeType.DATE_ADD_MINUTE) {
  257. return [years, months, days, hours, minutes];
  258. }
  259. if (modeName === ModeType.YEAR_ADD_HOUR) {
  260. return [years, hours];
  261. }
  262. if (modeName === ModeType.YEAR_ADD_MINUTE) {
  263. return [years, hours, minutes];
  264. }
  265. if (modeName === ModeType.MONTH_ADD_HOUR) {
  266. return [years, months, hours];
  267. }
  268. if (modeName === ModeType.MONTH_ADD_MINUTE) {
  269. return [years, months, hours, minutes];
  270. }
  271. if (modeName === ModeType.DATE_ADD_HOUR) {
  272. return [years, months, days, hours];
  273. }
  274. if (modeName === ModeType.NULL_HOUR) {
  275. return [hours];
  276. }
  277. if (modeName === ModeType.NULL_MINUTE) {
  278. return [hours, minutes];
  279. }
  280. if (modeName === ModeType.MONTH_ADD_DATE) {
  281. return [months, days];
  282. }
  283. },
  284. getValueCols() {
  285. return {
  286. columns: this.getDateData(),
  287. columnsValue: this.getColumnsValue(),
  288. };
  289. },
  290. getColumnsValue() {
  291. const { modeName } = this.data;
  292. const date = this.getDate();
  293. const mode2Value = {
  294. [`${ModeType.YEAR}`]: [`${date.year()}`],
  295. [`${ModeType.MONTH}`]: [`${date.year()}`, `${date.month()}`],
  296. [`${ModeType.DATE}`]: [`${date.year()}`, `${date.month()}`, `${date.date()}`],
  297. [`${ModeType.HOUR}`]: [`${date.year()}`, `${date.month()}`, `${date.date()}`, `${date.hour()}`],
  298. [`${ModeType.MINUTE}`]: [
  299. `${date.year()}`,
  300. `${date.month()}`,
  301. `${date.date()}`,
  302. `${date.hour()}`,
  303. `${date.minute()}`,
  304. ],
  305. [`${ModeType.YEAR_ADD_HOUR}`]: [`${date.year()}`, `${date.hour()}`],
  306. [`${ModeType.YEAR_ADD_MINUTE}`]: [`${date.year()}`, `${date.hour()}`, `${date.minute()}`],
  307. [`${ModeType.MONTH_ADD_HOUR}`]: [`${date.year()}`, `${date.month()}`, `${date.hour()}`],
  308. [`${ModeType.MONTH_ADD_MINUTE}`]: [`${date.year()}`, `${date.month()}`, `${date.hour()}`, `${date.minute()}`],
  309. [`${ModeType.DATE_ADD_HOUR}`]: [`${date.year()}`, `${date.month()}`, `${date.date()}`, `${date.hour()}`],
  310. [`${ModeType.DATE_ADD_MINUTE}`]: [
  311. `${date.year()}`,
  312. `${date.month()}`,
  313. `${date.date()}`,
  314. `${date.hour()}`,
  315. `${date.minute()}`,
  316. ],
  317. [`${ModeType.NULL_HOUR}`]: [`${date.hour()}`],
  318. [`${ModeType.NULL_MINUTE}`]: [`${date.hour()}`, `${date.minute()}`],
  319. [`${ModeType.MONTH_ADD_DATE}`]: [`${date.month()}`, `${date.date()}`],
  320. };
  321. if (mode2Value[modeName] === undefined) {
  322. throw new Error('mode 输入项有误');
  323. }
  324. return mode2Value[modeName];
  325. },
  326. getNewDate(value, type) {
  327. let newValue = this.getDate();
  328. switch (type) {
  329. case ModeType.YEAR:
  330. newValue = this.setYear(newValue, value);
  331. break;
  332. case ModeType.MONTH:
  333. newValue = this.setMonth(newValue, value);
  334. break;
  335. case ModeType.DATE:
  336. newValue = newValue.date(value);
  337. break;
  338. case ModeType.HOUR:
  339. newValue = newValue.hour(value);
  340. break;
  341. case ModeType.MINUTE:
  342. newValue = newValue.minute(value);
  343. break;
  344. default:
  345. break;
  346. }
  347. return this.clipDate(newValue);
  348. },
  349. onColumnChange(e) {
  350. const { value, column } = e === null || e === void 0 ? void 0 : e.detail;
  351. const { format } = this.properties;
  352. const date = this.getDate();
  353. const newValue = this.getNewDate(parseInt(value === null || value === void 0 ? void 0 : value.value, 10), value === null || value === void 0 ? void 0 : value.type);
  354. this.setData({
  355. date: newValue,
  356. });
  357. const { columns, columnsValue } = this.getValueCols();
  358. this.setData({
  359. columns,
  360. columnsValue,
  361. });
  362. this.triggerEvent('column-change', { index: column, value });
  363. this.triggerEvent('change', { value: date, formatValue: date.format(format) });
  364. },
  365. onConfirm() {
  366. const { format } = this.properties;
  367. const date = this.getDate();
  368. this.triggerEvent('confirm', { value: date, formatValue: date.format(format) });
  369. },
  370. onCancel() {
  371. const { value } = this.properties;
  372. const parseDate = dayjs(value || DEFAULT_MIN_DATE);
  373. this.setData({
  374. date: parseDate,
  375. });
  376. const { columns, columnsValue } = this.getValueCols();
  377. this.setData({
  378. columns,
  379. columnsValue,
  380. });
  381. this.triggerEvent('cancel');
  382. },
  383. };
  384. }
  385. };
  386. DateTimePicker = __decorate([
  387. wxComponent()
  388. ], DateTimePicker);
  389. export default DateTimePicker;
  390. //# sourceMappingURL=date-time-picker.js.map