| | |
| | | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { |
| | | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; |
| | | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); |
| | | 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; |
| | | return c > 3 && r && Object.defineProperty(target, key, r), r; |
| | | }; |
| | | var _a, _b; |
| | | import dayjs from 'dayjs'; |
| | | import localeData from 'dayjs/plugin/localeData'; |
| | | import config from '../common/config'; |
| | | import { SuperComponent, wxComponent } from '../common/src/index'; |
| | | import props from './props'; |
| | | import dayjsLocaleMap from './locale/dayjs'; |
| | | dayjs.extend(localeData); |
| | | dayjs.locale('zh-cn'); |
| | | 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); |
| | | const { prefix } = config; |
| | | const name = `${prefix}-date-time-picker`; |
| | | var ModeItem; |
| | | (function (ModeItem) { |
| | | ModeItem["YEAR"] = "year"; |
| | | ModeItem["MONTH"] = "month"; |
| | | ModeItem["DATE"] = "date"; |
| | | ModeItem["HOUR"] = "hour"; |
| | | ModeItem["MINUTE"] = "minute"; |
| | | ModeItem["SECOND"] = "second"; |
| | | })(ModeItem || (ModeItem = {})); |
| | | const DATE_MODES = ['year', 'month', 'date']; |
| | | const TIME_MODES = ['hour', 'minute', 'second']; |
| | | const FULL_MODES = [...DATE_MODES, ...TIME_MODES]; |
| | | const DEFAULT_MIN_DATE = dayjs('2000-01-01 00:00:00'); |
| | | const DEFAULT_MAX_DATE = dayjs('2030-12-31 23:59:59'); |
| | | let DateTimePicker = class DateTimePicker extends SuperComponent { |
| | | constructor() { |
| | | super(...arguments); |
| | | this.properties = props; |
| | | this.externalClasses = [`${prefix}-class`, `${prefix}-class-confirm`, `${prefix}-class-cancel`, `${prefix}-class-title`]; |
| | | this.options = { |
| | | multipleSlots: true, |
| | | }; |
| | | this.observers = { |
| | | 'start, end, value': function () { |
| | | this.updateColumns(); |
| | | }, |
| | | customLocale(v) { |
| | | if (!v || !dayjsLocaleMap[v].key) |
| | | return; |
| | | this.setData({ |
| | | locale: dayjsLocaleMap[v].i18n, |
| | | dayjsLocale: dayjsLocaleMap[v].key, |
| | | }); |
| | | }, |
| | | mode(m) { |
| | | const fullModes = this.getFullModeArray(m); |
| | | this.setData({ |
| | | fullModes, |
| | | }); |
| | | this.updateColumns(); |
| | | }, |
| | | }; |
| | | this.date = null; |
| | | this.data = { |
| | | prefix, |
| | | classPrefix: name, |
| | | columns: [], |
| | | columnsValue: [], |
| | | fullModes: [], |
| | | locale: dayjsLocaleMap[defaultLocale].i18n, |
| | | dayjsLocale: dayjsLocaleMap[defaultLocale].key, |
| | | }; |
| | | this.controlledProps = [ |
| | | { |
| | | key: 'value', |
| | | event: 'change', |
| | | }, |
| | | ]; |
| | | this.methods = { |
| | | updateColumns() { |
| | | this.date = this.getParseDate(); |
| | | const { columns, columnsValue } = this.getValueCols(); |
| | | this.setData({ |
| | | columns, |
| | | columnsValue, |
| | | }); |
| | | }, |
| | | getParseDate() { |
| | | const { value, defaultValue } = this.properties; |
| | | const minDate = this.getMinDate(); |
| | | const isTimeMode = this.isTimeMode(); |
| | | let currentValue = value || defaultValue; |
| | | if (isTimeMode) { |
| | | const dateStr = dayjs(minDate).format('YYYY-MM-DD'); |
| | | currentValue = dayjs(`${dateStr} ${currentValue}`); |
| | | } |
| | | const parseDate = dayjs(currentValue || minDate); |
| | | const isDateValid = parseDate.isValid(); |
| | | return isDateValid ? parseDate : minDate; |
| | | }, |
| | | getMinDate() { |
| | | const { start } = this.properties; |
| | | return start ? dayjs(start) : DEFAULT_MIN_DATE; |
| | | }, |
| | | getMaxDate() { |
| | | const { end } = this.properties; |
| | | return end ? dayjs(end) : DEFAULT_MAX_DATE; |
| | | }, |
| | | getDateRect(type = 'default') { |
| | | const map = { |
| | | min: 'getMinDate', |
| | | max: 'getMaxDate', |
| | | default: 'getDate', |
| | | }; |
| | | const date = this[map[type]](); |
| | | const keys = ['year', 'month', 'date', 'hour', 'minute', 'second']; |
| | | return keys.map((k) => { var _a; return (_a = date[k]) === null || _a === void 0 ? void 0 : _a.call(date); }); |
| | | }, |
| | | getDate() { |
| | | return this.clipDate((this === null || this === void 0 ? void 0 : this.date) || DEFAULT_MIN_DATE); |
| | | }, |
| | | clipDate(date) { |
| | | const minDate = this.getMinDate(); |
| | | const maxDate = this.getMaxDate(); |
| | | return dayjs(Math.min(Math.max(minDate.valueOf(), date.valueOf()), maxDate.valueOf())); |
| | | }, |
| | | setYear(date, year) { |
| | | const beforeMonthDays = date.date(); |
| | | const afterMonthDays = date.year(year).daysInMonth(); |
| | | const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf())); |
| | | return tempDate.year(year); |
| | | }, |
| | | setMonth(date, month) { |
| | | const beforeMonthDays = date.date(); |
| | | const afterMonthDays = date.month(month).daysInMonth(); |
| | | const tempDate = date.date(Math.min(beforeMonthDays.valueOf(), afterMonthDays.valueOf())); |
| | | return tempDate.month(month); |
| | | }, |
| | | getColumnOptions() { |
| | | const { fullModes } = this.data; |
| | | const columnOptions = []; |
| | | fullModes === null || fullModes === void 0 ? void 0 : fullModes.forEach((mode) => { |
| | | const columnOption = this.getOptionByType(mode); |
| | | columnOptions.push(columnOption); |
| | | }); |
| | | return columnOptions; |
| | | }, |
| | | getOptionByType(type) { |
| | | var _a; |
| | | const { locale, steps } = this.data; |
| | | const options = []; |
| | | const minEdge = this.getOptionEdge('min', type); |
| | | const maxEdge = this.getOptionEdge('max', type); |
| | | const step = (_a = steps === null || steps === void 0 ? void 0 : steps[type]) !== null && _a !== void 0 ? _a : 1; |
| | | const dayjsMonthsShort = dayjs().locale(this.data.dayjsLocale).localeData().monthsShort(); |
| | | for (let i = minEdge; i <= maxEdge; i += step) { |
| | | options.push({ |
| | | value: `${i}`, |
| | | label: type === 'month' ? dayjsMonthsShort[i] : `${i + locale[type]}`, |
| | | }); |
| | | } |
| | | return options; |
| | | }, |
| | | getYearOptions(dateParams) { |
| | | const { locale } = this.data; |
| | | const { minDateYear, maxDateYear } = dateParams; |
| | | const years = []; |
| | | for (let i = minDateYear; i <= maxDateYear; i += 1) { |
| | | years.push({ |
| | | value: `${i}`, |
| | | label: `${i + locale.year}`, |
| | | }); |
| | | } |
| | | return years; |
| | | }, |
| | | getOptionEdge(minOrMax, type) { |
| | | const selDateArray = this.getDateRect(); |
| | | const compareArray = this.getDateRect(minOrMax); |
| | | const edge = { |
| | | month: [0, 11], |
| | | date: [1, this.getDate().daysInMonth()], |
| | | hour: [0, 23], |
| | | minute: [0, 59], |
| | | second: [0, 59], |
| | | }; |
| | | const types = ['year', 'month', 'date', 'hour', 'minute', 'second']; |
| | | for (let i = 0, size = selDateArray.length; i < size; i += 1) { |
| | | if (types[i] === type) |
| | | return compareArray[i]; |
| | | if (compareArray[i] !== selDateArray[i]) |
| | | return edge[type][minOrMax === 'min' ? 0 : 1]; |
| | | } |
| | | return edge[type][minOrMax === 'min' ? 0 : 1]; |
| | | }, |
| | | getMonthOptions() { |
| | | const months = []; |
| | | const minMonth = this.getOptionEdge('min', 'month'); |
| | | const maxMonth = this.getOptionEdge('max', 'month'); |
| | | const dayjsMonthsShort = dayjs.monthsShort(); |
| | | for (let i = minMonth; i <= maxMonth; i += 1) { |
| | | months.push({ |
| | | value: `${i}`, |
| | | label: dayjsMonthsShort[i], |
| | | }); |
| | | } |
| | | return months; |
| | | }, |
| | | getDayOptions() { |
| | | const { locale } = this.data; |
| | | const days = []; |
| | | const minDay = this.getOptionEdge('min', 'date'); |
| | | const maxDay = this.getOptionEdge('max', 'date'); |
| | | for (let i = minDay; i <= maxDay; i += 1) { |
| | | days.push({ |
| | | value: `${i}`, |
| | | label: `${i + locale.day}`, |
| | | }); |
| | | } |
| | | return days; |
| | | }, |
| | | getHourOptions() { |
| | | const { locale } = this.data; |
| | | const hours = []; |
| | | const minHour = this.getOptionEdge('min', 'hour'); |
| | | const maxHour = this.getOptionEdge('max', 'hour'); |
| | | for (let i = minHour; i <= maxHour; i += 1) { |
| | | hours.push({ |
| | | value: `${i}`, |
| | | label: `${i + locale.hour}`, |
| | | }); |
| | | } |
| | | return hours; |
| | | }, |
| | | getMinuteOptions() { |
| | | const { locale } = this.data; |
| | | const minutes = []; |
| | | const minMinute = this.getOptionEdge('min', 'minute'); |
| | | const maxMinute = this.getOptionEdge('max', 'minute'); |
| | | for (let i = minMinute; i <= maxMinute; i += 1) { |
| | | minutes.push({ |
| | | value: `${i}`, |
| | | label: `${i + locale.minute}`, |
| | | }); |
| | | } |
| | | return minutes; |
| | | }, |
| | | getValueCols() { |
| | | return { |
| | | columns: this.getColumnOptions(), |
| | | columnsValue: this.getColumnsValue(), |
| | | }; |
| | | }, |
| | | getColumnsValue() { |
| | | const { fullModes } = this.data; |
| | | const date = this.getDate(); |
| | | const columnsValue = []; |
| | | fullModes === null || fullModes === void 0 ? void 0 : fullModes.forEach((mode) => { |
| | | columnsValue.push(`${date[mode]()}`); |
| | | }); |
| | | return columnsValue; |
| | | }, |
| | | getNewDate(value, type) { |
| | | let newValue = this.getDate(); |
| | | switch (type) { |
| | | case ModeItem.YEAR: |
| | | newValue = this.setYear(newValue, value); |
| | | break; |
| | | case ModeItem.MONTH: |
| | | newValue = this.setMonth(newValue, value); |
| | | break; |
| | | case ModeItem.DATE: |
| | | newValue = newValue.date(value); |
| | | break; |
| | | case ModeItem.HOUR: |
| | | newValue = newValue.hour(value); |
| | | break; |
| | | case ModeItem.MINUTE: |
| | | newValue = newValue.minute(value); |
| | | break; |
| | | case ModeItem.SECOND: |
| | | newValue = newValue.second(value); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | return this.clipDate(newValue); |
| | | }, |
| | | onColumnChange(e) { |
| | | const { value, column } = e === null || e === void 0 ? void 0 : e.detail; |
| | | const { fullModes, format } = this.data; |
| | | const columnValue = value === null || value === void 0 ? void 0 : value[column]; |
| | | const columnType = fullModes === null || fullModes === void 0 ? void 0 : fullModes[column]; |
| | | const newValue = this.getNewDate(parseInt(columnValue, 10), columnType); |
| | | this.date = newValue; |
| | | const { columns, columnsValue } = this.getValueCols(); |
| | | this.setData({ |
| | | columns, |
| | | columnsValue, |
| | | }); |
| | | const date = this.getDate(); |
| | | const pickValue = format ? date.format(format) : date.valueOf(); |
| | | this.triggerEvent('pick', { value: pickValue }); |
| | | }, |
| | | onConfirm() { |
| | | const { format } = this.properties; |
| | | const date = this.getDate(); |
| | | const value = format ? date.format(format) : date.valueOf(); |
| | | this._trigger('change', { value }); |
| | | this.triggerEvent('confirm', { value }); |
| | | this.resetColumns(); |
| | | }, |
| | | onCancel() { |
| | | this.resetColumns(); |
| | | this.triggerEvent('cancel'); |
| | | }, |
| | | onVisibleChange(e) { |
| | | if (!e.detail.visible) { |
| | | this.resetColumns(); |
| | | } |
| | | }, |
| | | onClose(e) { |
| | | const { trigger } = e.detail; |
| | | this.triggerEvent('close', { trigger }); |
| | | }, |
| | | resetColumns() { |
| | | const parseDate = this.getParseDate(); |
| | | this.date = parseDate; |
| | | const { columns, columnsValue } = this.getValueCols(); |
| | | this.setData({ |
| | | columns, |
| | | columnsValue, |
| | | }); |
| | | }, |
| | | }; |
| | | } |
| | | getFullModeArray(mode) { |
| | | if (typeof mode === 'string' || mode instanceof String) { |
| | | return this.getFullModeByModeString(mode, FULL_MODES); |
| | | } |
| | | if (Array.isArray(mode)) { |
| | | if ((mode === null || mode === void 0 ? void 0 : mode.length) === 1) { |
| | | return this.getFullModeByModeString(mode[0], FULL_MODES); |
| | | } |
| | | if ((mode === null || mode === void 0 ? void 0 : mode.length) === 2) { |
| | | const dateModes = this.getFullModeByModeString(mode[0], DATE_MODES); |
| | | const timeModes = this.getFullModeByModeString(mode[1], TIME_MODES); |
| | | return [...dateModes, ...timeModes]; |
| | | } |
| | | } |
| | | } |
| | | getFullModeByModeString(modeString, matchModes) { |
| | | if (!modeString) { |
| | | return []; |
| | | } |
| | | const endIndex = matchModes === null || matchModes === void 0 ? void 0 : matchModes.findIndex((mode) => modeString === mode); |
| | | return matchModes === null || matchModes === void 0 ? void 0 : matchModes.slice(0, endIndex + 1); |
| | | } |
| | | isTimeMode() { |
| | | const { fullModes } = this.data; |
| | | return fullModes[0] === ModeItem.HOUR; |
| | | } |
| | | }; |
| | | DateTimePicker = __decorate([ |
| | | wxComponent() |
| | | ], DateTimePicker); |
| | | export default DateTimePicker; |
| | | import{__decorate}from"tslib";import config from"../common/config";import{SuperComponent,wxComponent}from"../common/src/index";import usingConfig from"../mixins/using-config";import props from"./props";const dayjs=require("dayjs"),localeData=require("dayjs/plugin/localeData");dayjs.extend(localeData),dayjs.locale("zh-cn");const{prefix:prefix}=config,componentName="date-time-picker";var ModeItem;!function(t){t.YEAR="year",t.MONTH="month",t.DATE="date",t.HOUR="hour",t.MINUTE="minute",t.SECOND="second"}(ModeItem||(ModeItem={}));const DATE_MODES=["year","month","date"],TIME_MODES=["hour","minute","second"],FULL_MODES=[...DATE_MODES,...TIME_MODES];let DateTimePicker=class extends SuperComponent{constructor(){super(...arguments),this.behaviors=[usingConfig({componentName:componentName})],this.properties=props,this.externalClasses=[`${prefix}-class`,`${prefix}-class-confirm`,`${prefix}-class-cancel`,`${prefix}-class-title`],this.options={multipleSlots:!0},this.observers={"start, end, value, globalConfig"(){this.updateColumns()},mode(t){const e=this.getFullModeArray(t);this.setData({fullModes:e}),this.updateColumns()}},this.date=null,this.data={prefix:prefix,classPrefix:`${prefix}-${componentName}`,columns:[],columnsValue:[],fullModes:[]},this.controlledProps=[{key:"value",event:"change"}],this.methods={updateColumns(){this.date=this.getParseDate();const{columns:t,columnsValue:e}=this.getValueCols();this.setData({columns:t,columnsValue:e})},getDaysOfWeekInMonth(t,e){var o;const{globalConfig:s,steps:a}=this.data,n=t.startOf("month"),i=this.getOptionEdge("min",e),l=this.getOptionEdge("max",e),r=null!==(o=null==a?void 0:a[e])&&void 0!==o?o:1,u=[],d=(null==s?void 0:s.dayjsLocale)||"zh-cn";for(let t=i;t<=l;t+=r){const e=n.date(t).locale(d).format("ddd");u.push({value:`${t}`,label:`${t}${null==s?void 0:s.dateLabel} ${e}`})}return u},getParseDate(){const{value:t,defaultValue:e}=this.properties,o=this.getMinDate();let s=t||e;if(this.isTimeMode()){const t=dayjs(o).format("YYYY-MM-DD");s=dayjs(`${t} ${s}`)}let a=dayjs(s||o);if(!a.isValid()&&"string"==typeof s){const t=s.match(/(\d{4}[-/]\d{1,2}[-/]\d{1,2}(\s+\d{1,2}:\d{1,2}(:\d{1,2})?)?)/);t&&(a=dayjs(t[1]))}return a.isValid()?a:o},normalize:(t,e)=>t&&dayjs(t).isValid()?dayjs(t):e,getMinDate(){return this.normalize(this.properties.start,dayjs().subtract(10,"year"))},getMaxDate(){return this.normalize(this.properties.end,dayjs().add(10,"year"))},getDateRect(t="default"){const e=this[{min:"getMinDate",max:"getMaxDate",default:"getDate"}[t]]();return["year","month","date","hour","minute","second"].map(t=>{var o;return null===(o=e[t])||void 0===o?void 0:o.call(e)})},getDate(){return this.clipDate((null==this?void 0:this.date)||this.getMinDate())},clipDate(t){const e=this.getMinDate(),o=this.getMaxDate();return dayjs(Math.min(Math.max(e.valueOf(),t.valueOf()),o.valueOf()))},setYear(t,e){const o=t.date(),s=t.year(e).daysInMonth();return t.date(Math.min(o.valueOf(),s.valueOf())).year(e)},setMonth(t,e){const o=t.date(),s=t.month(e).daysInMonth();return t.date(Math.min(o.valueOf(),s.valueOf())).month(e)},getColumnOptions(){const{fullModes:t,filter:e}=this.data,o=[];return null==t||t.forEach(t=>{const s=this.getOptionByType(t);"function"==typeof e?o.push(e(t,s)):o.push(s)}),o},getOptionByType(t){var e,o;const{globalConfig:s,steps:a,showWeek:n}=this.data,i=[],l=this.getOptionEdge("min",t),r=this.getOptionEdge("max",t),u=null!==(e=null==a?void 0:a[t])&&void 0!==e?e:1;if("date"===t&&n)return this.getDaysOfWeekInMonth(this.date,t);for(let e=l;e<=r;e+=u)i.push({value:`${e}`,label:"month"===t?null===(o=s.months)||void 0===o?void 0:o[e]:`${e}${s[`${t}Label`]}`});return i},getYearOptions(t){const{globalConfig:e}=this.data,{minDateYear:o,maxDateYear:s}=t,a=[];for(let t=o;t<=s;t+=1)a.push({value:`${t}`,label:`${t}${e.yearLabel}`});return a},getOptionEdge(t,e){const o=this.getDateRect(),s=this.getDateRect(t),a={month:[0,11],date:[1,this.getDate().daysInMonth()],hour:[0,23],minute:[0,59],second:[0,59]},n=["year","month","date","hour","minute","second"];for(let i=0,l=o.length;i<l;i+=1){if(n[i]===e)return s[i];if(s[i]!==o[i])return a[e]["min"===t?0:1]}return a[e]["min"===t?0:1]},getMonthOptions(){const{globalConfig:t}=this.data,e=[],o=this.getOptionEdge("min","month"),s=this.getOptionEdge("max","month");for(let a=o;a<=s;a+=1)e.push({value:`${a}`,label:t.months[a]});return e},getDayOptions(){const{globalConfig:t}=this.data,e=[],o=this.getOptionEdge("min","date"),s=this.getOptionEdge("max","date");for(let a=o;a<=s;a+=1)e.push({value:`${a}`,label:`${a}${t.dateLabel}`});return e},getHourOptions(){const{globalConfig:t}=this.data,e=[],o=this.getOptionEdge("min","hour"),s=this.getOptionEdge("max","hour");for(let a=o;a<=s;a+=1)e.push({value:`${a}`,label:`${a}${t.hourLabel}`});return e},getMinuteOptions(){const{globalConfig:t}=this.data,e=[],o=this.getOptionEdge("min","minute"),s=this.getOptionEdge("max","minute");for(let a=o;a<=s;a+=1)e.push({value:`${a}`,label:`${a}${t.minuteLabel}`});return e},getValueCols(){return{columns:this.getColumnOptions(),columnsValue:this.getColumnsValue()}},getColumnsValue(){const{fullModes:t}=this.data,e=this.getDate(),o=[];return null==t||t.forEach(t=>{o.push(`${e[t]()}`)}),o},getNewDate(t,e){let o=this.getDate();switch(e){case ModeItem.YEAR:o=this.setYear(o,t);break;case ModeItem.MONTH:o=this.setMonth(o,t);break;case ModeItem.DATE:o=o.date(t);break;case ModeItem.HOUR:o=o.hour(t);break;case ModeItem.MINUTE:o=o.minute(t);break;case ModeItem.SECOND:o=o.second(t)}return this.clipDate(o)},onColumnChange(t){const{value:e,column:o}=null==t?void 0:t.detail,{fullModes:s,format:a}=this.data,n=null==e?void 0:e[o],i=null==s?void 0:s[o],l=this.getNewDate(parseInt(n,10),i);this.date=l;const{columns:r,columnsValue:u}=this.getValueCols();this.setData({columns:r,columnsValue:u});const d=this.getDate(),h=a?d.format(a):d.valueOf();this.triggerEvent("pick",{value:h})},onConfirm(){const{format:t}=this.properties,e=this.getDate(),o=t?e.format(t):e.valueOf();this._trigger("change",{value:o}),this.triggerEvent("confirm",{value:o}),this.resetColumns()},onCancel(){this.resetColumns(),this.triggerEvent("cancel")},onVisibleChange(t){t.detail.visible||this.resetColumns()},onClose(t){const{trigger:e}=t.detail;this.triggerEvent("close",{trigger:e})},resetColumns(){const t=this.getParseDate();this.date=t;const{columns:e,columnsValue:o}=this.getValueCols();this.setData({columns:e,columnsValue:o})}}}getFullModeArray(t){if("string"==typeof t||t instanceof String)return this.getFullModeByModeString(t,FULL_MODES);if(Array.isArray(t)){if(1===(null==t?void 0:t.length))return this.getFullModeByModeString(t[0],FULL_MODES);if(2===(null==t?void 0:t.length)){return[...this.getFullModeByModeString(t[0],DATE_MODES),...this.getFullModeByModeString(t[1],TIME_MODES)]}}}getFullModeByModeString(t,e){if(!t)return[];const o=null==e?void 0:e.findIndex(e=>t===e);return null==e?void 0:e.slice(0,o+1)}isTimeMode(){const{fullModes:t}=this.data;return t[0]===ModeItem.HOUR}};DateTimePicker=__decorate([wxComponent()],DateTimePicker);export default DateTimePicker; |