Initial commit of akmon project
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// @ts-nocheck
|
||||
import { TimeModeValues } from './type';
|
||||
// 使用位运算定义模式
|
||||
// const MODES = {
|
||||
// YEAR: 1 << 0, // 1
|
||||
// MONTH: 1 << 1, // 2
|
||||
// DATE: 1 << 2, // 4
|
||||
// HOUR: 1 << 3, // 8
|
||||
// MINUTE: 1 << 4 // 16
|
||||
// SECOND : 1 << 4 // 32
|
||||
// };
|
||||
|
||||
export const MODE_YEAR = 1; // 0001
|
||||
export const MODE_MONTH = 2; // 0010
|
||||
export const MODE_DATE = 4; // 0100
|
||||
export const MODE_HOUR = 8; // 1000
|
||||
export const MODE_MINUTE = 16; // 10000
|
||||
export const MODE_SECOND = 32; // 100000
|
||||
|
||||
export const MODE_MAP = new Map<string, number>([
|
||||
['年', MODE_YEAR],
|
||||
['月', MODE_MONTH],
|
||||
['日', MODE_DATE],
|
||||
['时', MODE_HOUR],
|
||||
['分', MODE_MINUTE],
|
||||
['秒', MODE_SECOND],
|
||||
['year', MODE_YEAR],
|
||||
['month', MODE_MONTH],
|
||||
['date', MODE_DATE],
|
||||
['hour', MODE_HOUR],
|
||||
['minute', MODE_MINUTE],
|
||||
['second', MODE_SECOND],
|
||||
])
|
||||
|
||||
export const FORMAT_MAP = new Map<string, string>([
|
||||
['year', 'YYYY'],
|
||||
['month', 'MM'],
|
||||
['date', 'DD'],
|
||||
['hour', 'HH'],
|
||||
['minute', 'mm'],
|
||||
['second', 'ss'],
|
||||
])
|
||||
|
||||
export const UNIT_MAP = new Map<string, string>([
|
||||
['year', '年'],
|
||||
['month', '月'],
|
||||
['date', '日'],
|
||||
['hour', '时'],
|
||||
['minute', '分'],
|
||||
['second', '秒'],
|
||||
])
|
||||
|
||||
|
||||
// 定义时间列名称数组
|
||||
export const MODE_NAMES : TimeModeValues[] = ['year', 'month', 'date', 'hour', 'minute', 'second'];
|
||||
export const DEFAULT_FORMAT = 'YYYY-MM-DD HH:mm:ss'
|
||||
@@ -0,0 +1,231 @@
|
||||
<template>
|
||||
<l-picker
|
||||
:title="title"
|
||||
:titleStyle="titleStyle"
|
||||
:confirm-btn="confirmBtn"
|
||||
:confirm-style="confirmStyle"
|
||||
:cancel-btn="cancelBtn"
|
||||
:cancel-style="cancelStyle"
|
||||
:itemHeight="itemHeight"
|
||||
:itemColor="itemColor"
|
||||
:itemFontSize="itemFontSize"
|
||||
:itemActiveColor="itemActiveColor"
|
||||
:indicatorStyle="indicatorStyle"
|
||||
:bgColor="bgColor"
|
||||
:groupHeight="groupHeight"
|
||||
:radius="radius"
|
||||
:value="valueOfPicker"
|
||||
:columns="columns"
|
||||
@confirm="onConfirm"
|
||||
@cancel="onCancel"
|
||||
@change="onChange"
|
||||
@pick="onPick">
|
||||
</l-picker>
|
||||
</template>
|
||||
<script lang="uts" setup>
|
||||
import { DateTimePickerProps, DateValue, DateTimePickerColumn, TimeModeValues, DateTimePickerColumnItem } from './type';
|
||||
import { PickerColumn, PickerColumnItem, PickerConfirmEvent, PickerPickEvent, PickerValue } from '@/uni_modules/lime-picker';
|
||||
import { getMeaningColumn } from './utils';
|
||||
import { DEFAULT_FORMAT, MODE_NAMES, FORMAT_MAP, UNIT_MAP } from './constant';
|
||||
import { dayuts, Dayuts } from '@/uni_modules/lime-dayuts/common'
|
||||
import { DayutsUnit } from '@/uni_modules/lime-dayuts'
|
||||
import { clamp } from '@/uni_modules/lime-shared/clamp'
|
||||
|
||||
const emit = defineEmits(['change', 'cancel', 'confirm', 'pick', 'update:modelValue', 'update:value'])
|
||||
const props = withDefaults(defineProps<DateTimePickerProps>(), {
|
||||
mode: 1 | 2 | 4,
|
||||
format: DEFAULT_FORMAT,
|
||||
showUnit: true,
|
||||
resetIndex: false,
|
||||
minHour: 0,
|
||||
maxHour: 23,
|
||||
minMinute: 0,
|
||||
maxMinute: 59
|
||||
})
|
||||
// 默认值
|
||||
let defaultValue : DateValue = props.value ?? props.defaultValue ?? props.defaultValue ?? Date.now()
|
||||
const innerValue = computed({
|
||||
set(value : DateValue) {
|
||||
if(defaultValue == value) return
|
||||
defaultValue = value;
|
||||
emit('change', value)
|
||||
emit('update:modelValue', value)
|
||||
emit('update:value', value)
|
||||
},
|
||||
get() : DateValue {
|
||||
const value = props.value ?? props.modelValue ?? defaultValue
|
||||
return typeof value == 'string' && (value as string).length == 0 ? Date.now() : value
|
||||
}
|
||||
} as WritableComputedOptions<DateValue>)
|
||||
|
||||
|
||||
const meaningColumn = getMeaningColumn(props.mode);
|
||||
const isTimeMode = ['hour', 'minute', 'second'].includes(meaningColumn[0]);
|
||||
const normalize = (val : DateValue | null, defaultDay : Dayuts) : Dayuts => val != null && dayuts(val).isValid() ? dayuts(val) : defaultDay;
|
||||
const start = computed(() : Dayuts => normalize(props.start as DateValue | null, dayuts().subtract(10, 'year')));
|
||||
const end = computed(() : Dayuts => normalize(props.end as DateValue | null, dayuts().add(10, 'year')));
|
||||
const rationalize = (val : Dayuts) : Dayuts => {
|
||||
if (isTimeMode) return val;
|
||||
if (val.isBefore(start.value)) return start.value;
|
||||
if (val.isAfter(end.value)) return end.value;
|
||||
return val;
|
||||
};
|
||||
|
||||
const calcDate = (currentValue : DateValue | null) : Dayuts => {
|
||||
if (isTimeMode) {
|
||||
const dateStr = dayuts(start.value).format('YYYY-MM-DD');
|
||||
currentValue = `${dateStr} ${currentValue}`;
|
||||
}
|
||||
return currentValue != null && dayuts(currentValue).isValid() ? rationalize(dayuts(currentValue)) : start.value;
|
||||
};
|
||||
|
||||
const curDate = ref(calcDate(innerValue.value));
|
||||
const valueOfPicker = computed(() : string[] => meaningColumn.map((item) : string => curDate.value.get(item).toString()));
|
||||
const columnCache = new Map<string, DateTimePickerColumnItem[]>();
|
||||
const columns = computed(() : DateTimePickerColumn[] => {
|
||||
const ret : DateTimePickerColumn[] = [];
|
||||
|
||||
const getDate = (date : Dayuts) : number[] => [
|
||||
date.year(),
|
||||
date.month() + 1,
|
||||
date.date(),
|
||||
date.hour(),
|
||||
date.minute(),
|
||||
date.second(),
|
||||
];
|
||||
//@ts-ignore
|
||||
const [curYear, curMonth, curDay, curHour, curMinute] = getDate(curDate.value);
|
||||
const [minYear, minMonth, minDay, minHour, minMinute, minSecond] = getDate(start.value);
|
||||
const [maxYear, maxMonth, maxDay, maxHour, maxMinute, maxSecond] = getDate(end.value);
|
||||
|
||||
const isInMinYear = curYear == minYear;
|
||||
const isInMaxYear = curYear == maxYear;
|
||||
const isInMinMonth = isInMinYear && curMonth == minMonth;
|
||||
const isInMaxMonth = isInMaxYear && curMonth === maxMonth;
|
||||
const isInMinDay = isInMinMonth && curDay == minDay;
|
||||
const isInMaxDay = isInMaxMonth && curDay == maxDay;
|
||||
const isInMinHour = isInMinDay && curHour == minHour;
|
||||
const isInMaxHour = isInMaxDay && curHour == maxHour;
|
||||
const isInMinMinute = isInMinHour && curMinute == minMinute;
|
||||
const isInMaxMinute = isInMaxHour && curMinute == maxMinute;
|
||||
|
||||
const generateColumn = (type : string, lowerBound : number, upperBound : number) => {
|
||||
const cacheKey = `${type}-${lowerBound}-${upperBound}`;
|
||||
if (columnCache.has(cacheKey)) {
|
||||
ret.push(columnCache.get(cacheKey)!)
|
||||
return
|
||||
}
|
||||
|
||||
const arr : DateTimePickerColumnItem[] = [];
|
||||
for (let i = lowerBound; i <= upperBound; i++) {
|
||||
const value = i;
|
||||
arr.push({
|
||||
label: props.renderLabel != null ? props.renderLabel!(type, i.toString()) : `${value}${props.showUnit ? UNIT_MAP.get(type) : ''}`,
|
||||
value: type == 'month' ? `${value - 1}` : value.toString(),
|
||||
} as DateTimePickerColumnItem);
|
||||
}
|
||||
|
||||
if (props.customFilter != null) {
|
||||
const _arr = props.customFilter!(type, arr)
|
||||
ret.push(_arr)
|
||||
columnCache.set(cacheKey, _arr);
|
||||
} else {
|
||||
ret.push(arr)
|
||||
columnCache.set(cacheKey, arr);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if (meaningColumn.includes('year')) {
|
||||
generateColumn('year', minYear, maxYear);
|
||||
}
|
||||
if (meaningColumn.includes('month')) {
|
||||
const lower = isInMinYear ? minMonth : 1;
|
||||
const upper = isInMaxYear ? maxMonth : 12;
|
||||
generateColumn('month', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('date')) {
|
||||
const lower = isInMinMonth ? minDay : 1;
|
||||
const upper = isInMaxMonth ? maxDay : dayuts(`${curYear}-${curMonth}`).daysInMonth();
|
||||
generateColumn('date', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('hour')) {
|
||||
const lower = isInMinDay && !isTimeMode ? minHour : clamp(props.minHour, 0, 23);// 0;
|
||||
const upper = isInMaxDay && !isTimeMode ? maxHour : clamp(props.maxHour, lower, 23);//23;
|
||||
generateColumn('hour', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('minute')) {
|
||||
const lower = isInMinHour && !isTimeMode ? minMinute : clamp(props.minMinute, 0, 59);//0;
|
||||
const upper = isInMaxHour && !isTimeMode ? maxMinute : clamp(props.maxMinute, lower, 59);//59;
|
||||
generateColumn('minute', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('second')) {
|
||||
const lower = isInMinMinute && !isTimeMode ? minSecond : 0;
|
||||
const upper = isInMaxMinute && !isTimeMode ? maxSecond : 59;
|
||||
generateColumn('second', lower, upper);
|
||||
}
|
||||
return ret;
|
||||
})
|
||||
|
||||
const innterFormat = computed(() : string => {
|
||||
const first = meaningColumn.length > 0 ? meaningColumn[0] : 'year';
|
||||
const last = meaningColumn.length > 0 ? meaningColumn[meaningColumn.length - 1] : 'date';
|
||||
|
||||
const format = DEFAULT_FORMAT.substring(
|
||||
DEFAULT_FORMAT.indexOf(FORMAT_MAP.get(first)!),
|
||||
DEFAULT_FORMAT.lastIndexOf(FORMAT_MAP.get(last)!) + FORMAT_MAP.get(last)!.length
|
||||
)
|
||||
return format
|
||||
})
|
||||
const onConfirm = ({ values } : PickerConfirmEvent) => {
|
||||
|
||||
// const first = meaningColumn.length > 0 ? meaningColumn[0]: 'year';
|
||||
// const last = meaningColumn.length > 0 ? meaningColumn[meaningColumn.length - 1]: 'date';
|
||||
|
||||
// const format = DEFAULT_FORMAT.substring(
|
||||
// DEFAULT_FORMAT.indexOf(FORMAT_MAP.get(first)!),
|
||||
// DEFAULT_FORMAT.lastIndexOf(FORMAT_MAP.get(last)!) + FORMAT_MAP.get(last)!.length
|
||||
// )
|
||||
|
||||
let cur = curDate.value
|
||||
// MODE_NAMES
|
||||
values.forEach((item, index) => {
|
||||
const type = meaningColumn[index]
|
||||
cur = cur.set(type, parseInt(`${item}`, 10))
|
||||
})
|
||||
const curValue = cur.format(props.format)
|
||||
|
||||
innerValue.value = cur.format(innterFormat.value);
|
||||
emit('confirm', curValue);
|
||||
}
|
||||
const onCancel = () => {
|
||||
emit('cancel')
|
||||
}
|
||||
const onPick = ({ values, column, index } : PickerPickEvent) => {
|
||||
const type = meaningColumn[column];
|
||||
const val = curDate.value.set(type as DayutsUnit, parseInt(columns.value[column][index].value, 10));
|
||||
|
||||
curDate.value = rationalize(val);
|
||||
emit('pick', rationalize(val).format(props.format))
|
||||
}
|
||||
|
||||
const onChange = (values : PickerValue[]) => {
|
||||
let cur = curDate.value
|
||||
values.forEach((item, index) => {
|
||||
const type = meaningColumn[index]
|
||||
cur = cur.set(type, parseInt(`${item}`, 10))
|
||||
})
|
||||
curDate.value = rationalize(cur as Dayuts)
|
||||
const curValue = curDate.value.format(innterFormat.value)
|
||||
innerValue.value = curValue
|
||||
}
|
||||
const stop = watch(innerValue, (val : DateValue) => {
|
||||
curDate.value = calcDate(val);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
stop()
|
||||
})
|
||||
</script>
|
||||
<style>
|
||||
</style>
|
||||
@@ -0,0 +1,240 @@
|
||||
<template>
|
||||
<l-picker
|
||||
:title="title"
|
||||
:titleStyle="titleStyle"
|
||||
:confirm-btn="confirmBtn"
|
||||
:confirm-style="confirmStyle"
|
||||
:cancel-btn="cancelBtn"
|
||||
:cancel-style="cancelStyle"
|
||||
:itemHeight="itemHeight"
|
||||
:itemColor="itemColor"
|
||||
:itemFontSize="itemFontSize"
|
||||
:itemActiveColor="itemActiveColor"
|
||||
:indicatorStyle="indicatorStyle"
|
||||
:bgColor="bgColor"
|
||||
:groupHeight="groupHeight"
|
||||
:radius="radius"
|
||||
:value="valueOfPicker"
|
||||
:columns="columns"
|
||||
@confirm="onConfirm"
|
||||
@cancel="onCancel"
|
||||
@change="onChange"
|
||||
@pick="onPick">
|
||||
</l-picker>
|
||||
</template>
|
||||
<script lang="ts">
|
||||
// @ts-nocheck
|
||||
import { defineComponent, computed, ref, watch, onBeforeUnmount} from '@/uni_modules/lime-shared/vue';
|
||||
import { DateTimePickerProps, DateValue, DateTimePickerColumn, TimeModeValues, DateTimePickerColumnItem } from './type';
|
||||
import { PickerColumn, PickerColumnItem, PickerConfirmEvent, PickerPickEvent } from '@/uni_modules/lime-picker';
|
||||
import { getMeaningColumn } from './utils';
|
||||
import { DEFAULT_FORMAT, MODE_NAMES, FORMAT_MAP, UNIT_MAP } from './constant';
|
||||
import { dayuts, Dayuts, DayutsUnit} from '@/uni_modules/lime-dayuts'
|
||||
import { clamp } from '@/uni_modules/lime-shared/clamp'
|
||||
import dataTimePickerProps from './props';
|
||||
export default defineComponent({
|
||||
name: 'l-date-time-picker',
|
||||
props: dataTimePickerProps,
|
||||
emits: ['change', 'cancel', 'confirm', 'pick', 'update:modelValue', 'update:value','input'],
|
||||
setup(props, {emit}) {
|
||||
// 默认值
|
||||
let defaultValue : DateValue = props.value ?? props.defaultValue ?? props.defaultValue ?? Date.now()
|
||||
const innerValue = computed({
|
||||
set(value : DateValue) {
|
||||
if(defaultValue == value) return
|
||||
defaultValue = value;
|
||||
emit('change', value)
|
||||
emit('update:modelValue', value)
|
||||
emit('update:value', value)
|
||||
// #ifdef VUE2
|
||||
emit('input', value)
|
||||
// #endif
|
||||
},
|
||||
get() : DateValue {
|
||||
const value = props.value ?? props.modelValue ?? defaultValue
|
||||
return typeof value == 'string' && value.length == 0 ? Date.now() : value
|
||||
// return props.value ?? props.modelValue ?? defaultValue
|
||||
}
|
||||
} as WritableComputedOptions<DateValue>)
|
||||
|
||||
const meaningColumn = getMeaningColumn(props.mode);
|
||||
const isTimeMode = ['hour', 'minute', 'second'].includes(meaningColumn[0]);
|
||||
const normalize = (val : DateValue | null, defaultDay : Dayuts) : Dayuts => val != null && dayuts(val).isValid() ? dayuts(val) : defaultDay;
|
||||
const start = computed(() : Dayuts => normalize(props.start as DateValue | null, dayuts().subtract(10, 'year')));
|
||||
const end = computed(() : Dayuts => normalize(props.end as DateValue | null, dayuts().add(10, 'year')));
|
||||
const rationalize = (val : Dayuts) : Dayuts => {
|
||||
if (isTimeMode) return val;
|
||||
if (val.isBefore(start.value)) return start.value;
|
||||
if (val.isAfter(end.value)) return end.value;
|
||||
return val;
|
||||
};
|
||||
const calcDate = (currentValue : DateValue | null) : Dayuts => {
|
||||
if (isTimeMode) {
|
||||
const dateStr = dayuts(start.value).format('YYYY-MM-DD');
|
||||
currentValue = `${dateStr} ${currentValue}`;
|
||||
}
|
||||
return currentValue != null && dayuts(currentValue).isValid() ? rationalize(dayuts(currentValue)) : start.value;
|
||||
};
|
||||
|
||||
const curDate = ref(calcDate(innerValue.value));
|
||||
const valueOfPicker = computed(() : string[] => meaningColumn.map((item) : string => curDate.value.get(item).toString()));
|
||||
const columnCache = new Map<string, DateTimePickerColumnItem[]>();
|
||||
const columns = computed(() : DateTimePickerColumn[] => {
|
||||
const ret : DateTimePickerColumn[] = [];
|
||||
|
||||
const getDate = (date : Dayuts) : number[] => [
|
||||
date.year(),
|
||||
date.month() + 1,
|
||||
date.date(),
|
||||
date.hour(),
|
||||
date.minute(),
|
||||
date.second(),
|
||||
];
|
||||
const [curYear, curMonth, curDay, curHour, curMinute] = getDate(curDate.value);
|
||||
const [minYear, minMonth, minDay, minHour, minMinute, minSecond] = getDate(start.value);
|
||||
const [maxYear, maxMonth, maxDay, maxHour, maxMinute, maxSecond] = getDate(end.value);
|
||||
|
||||
const isInMinYear = curYear == minYear;
|
||||
const isInMaxYear = curYear == maxYear;
|
||||
const isInMinMonth = isInMinYear && curMonth == minMonth;
|
||||
const isInMaxMonth = isInMaxYear && curMonth === maxMonth;
|
||||
const isInMinDay = isInMinMonth && curDay == minDay;
|
||||
const isInMaxDay = isInMaxMonth && curDay == maxDay;
|
||||
const isInMinHour = isInMinDay && curHour == minHour;
|
||||
const isInMaxHour = isInMaxDay && curHour == maxHour;
|
||||
const isInMinMinute = isInMinHour && curMinute == minMinute;
|
||||
const isInMaxMinute = isInMaxHour && curMinute == maxMinute;
|
||||
|
||||
const generateColumn = (type: string, lowerBound: number, upperBound: number) => {
|
||||
const cacheKey = `${type}-${lowerBound}-${upperBound}`;
|
||||
if (columnCache.has(cacheKey)) {
|
||||
ret.push(columnCache.get(cacheKey)!)
|
||||
return
|
||||
}
|
||||
|
||||
const arr: DateTimePickerColumnItem[] = [];
|
||||
for (let i = lowerBound; i <= upperBound; i++) {
|
||||
const value = i;
|
||||
arr.push({
|
||||
label: props.renderLabel !=null ? props.renderLabel!(type, i) : `${value}${props.showUnit ? UNIT_MAP.get(type) : ''}`,
|
||||
value: type == 'month' ? `${value - 1}` : value.toString(),
|
||||
} as DateTimePickerColumnItem);
|
||||
}
|
||||
|
||||
if(props.customFilter != null) {
|
||||
const _arr = props.customFilter!(type, arr)
|
||||
ret.push(_arr)
|
||||
columnCache.set(cacheKey, _arr);
|
||||
} else {
|
||||
ret.push(arr)
|
||||
columnCache.set(cacheKey, arr);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if (meaningColumn.includes('year')) {
|
||||
generateColumn('year', minYear, maxYear);
|
||||
}
|
||||
if (meaningColumn.includes('month')) {
|
||||
const lower = isInMinYear ? minMonth : 1;
|
||||
const upper = isInMaxYear ? maxMonth : 12;
|
||||
generateColumn('month', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('date')) {
|
||||
const lower = isInMinMonth ? minDay : 1;
|
||||
const upper = isInMaxMonth ? maxDay : dayuts(`${curYear}-${curMonth}`).daysInMonth();
|
||||
generateColumn('date', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('hour')) {
|
||||
const lower = isInMinDay && !isTimeMode ? minHour : clamp(props.minHour, 0, 23);// 0;
|
||||
const upper = isInMaxDay && !isTimeMode ? maxHour : clamp(props.maxHour, lower, 23);//23;
|
||||
generateColumn('hour', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('minute')) {
|
||||
const lower = isInMinHour && !isTimeMode ? minMinute : clamp(props.minMinute, 0, 59);//0;
|
||||
const upper = isInMaxHour && !isTimeMode ? maxMinute : clamp(props.maxMinute, lower, 59);//59;
|
||||
generateColumn('minute', lower, upper);
|
||||
}
|
||||
if (meaningColumn.includes('second')) {
|
||||
const lower = isInMinMinute && !isTimeMode ? minSecond : 0;
|
||||
const upper = isInMaxMinute && !isTimeMode ? maxSecond : 59;
|
||||
generateColumn('second', lower, upper);
|
||||
}
|
||||
return ret;
|
||||
})
|
||||
|
||||
const innterFormat = computed(() : string => {
|
||||
const first = meaningColumn.length > 0 ? meaningColumn[0] : 'year';
|
||||
const last = meaningColumn.length > 0 ? meaningColumn[meaningColumn.length - 1] : 'date';
|
||||
|
||||
const format = DEFAULT_FORMAT.substring(
|
||||
DEFAULT_FORMAT.indexOf(FORMAT_MAP.get(first)!),
|
||||
DEFAULT_FORMAT.lastIndexOf(FORMAT_MAP.get(last)!) + FORMAT_MAP.get(last)!.length
|
||||
)
|
||||
return format
|
||||
})
|
||||
const onConfirm = ({ values } : PickerConfirmEvent) => {
|
||||
|
||||
// const first = meaningColumn.length > 0 ? meaningColumn[0]: 'year';
|
||||
// const last = meaningColumn.length > 0 ? meaningColumn[meaningColumn.length - 1]: 'date';
|
||||
|
||||
// const format = DEFAULT_FORMAT.substring(
|
||||
// DEFAULT_FORMAT.indexOf(FORMAT_MAP.get(first)!),
|
||||
// DEFAULT_FORMAT.lastIndexOf(FORMAT_MAP.get(last)!) + FORMAT_MAP.get(last)!.length
|
||||
// )
|
||||
|
||||
let cur = curDate.value
|
||||
// MODE_NAMES
|
||||
values.forEach((item, index) => {
|
||||
const type = meaningColumn[index]
|
||||
cur = cur.set(type, parseInt(`${item}`, 10))
|
||||
})
|
||||
const curValue = cur.format(props.format)
|
||||
|
||||
innerValue.value = cur.format(innterFormat.value);
|
||||
emit('confirm', curValue);
|
||||
}
|
||||
const onCancel = () => {
|
||||
emit('cancel')
|
||||
}
|
||||
const onPick = ({ values, column, index } : PickerPickEvent) => {
|
||||
const type = meaningColumn[column];
|
||||
const val = curDate.value.set(type as DayutsUnit, parseInt(columns.value[column][index].value, 10));
|
||||
|
||||
curDate.value = rationalize(val);
|
||||
emit('pick', rationalize(val).format(props.format))
|
||||
}
|
||||
const onChange = (values: PickerValue[]) => {
|
||||
let cur = curDate.value
|
||||
values.forEach((item, index) => {
|
||||
const type = meaningColumn[index]
|
||||
cur = cur.set(type, parseInt(`${item}`, 10))
|
||||
})
|
||||
curDate.value = rationalize(cur as Dayuts)
|
||||
const curValue = curDate.value.format(innterFormat.value)
|
||||
innerValue.value = curValue
|
||||
|
||||
}
|
||||
const stop = watch(innerValue, (val : DateValue) => {
|
||||
curDate.value = calcDate(val);
|
||||
});
|
||||
|
||||
onBeforeUnmount(()=>{
|
||||
stop()
|
||||
})
|
||||
|
||||
|
||||
return {
|
||||
valueOfPicker,
|
||||
columns,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
onChange,
|
||||
onPick
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
</script>
|
||||
<style>
|
||||
</style>
|
||||
@@ -0,0 +1,133 @@
|
||||
// @ts-nocheck
|
||||
export default {
|
||||
/** 取消按钮文字 */
|
||||
cancelBtn: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
cancelStyle: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
/** 确定按钮文字 */
|
||||
confirmBtn: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
confirmStyle: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
/** 组件国际化语言,目前支持: 简体中文(zh)、(tc)、英文(en)、日语(ja)、韩语(ko)、俄语(ru)等六种语言 */
|
||||
customLocale: {
|
||||
type: String,
|
||||
default: 'zh',
|
||||
},
|
||||
/** 选择器的最大可选时间,默认为当前时间+10年 */
|
||||
end: {
|
||||
type: [String, Number],
|
||||
default: null,
|
||||
},
|
||||
/** 列选项过滤函数,支持自定义列内容。(type 值可为: year, month, date, hour, minute, second) */
|
||||
customFilter: {
|
||||
type: Function,
|
||||
},
|
||||
/** 用于格式化 pick、change、confirm 事件返回的值,[详细文档](https://day.js.org/docs/en/display/format) */
|
||||
format: {
|
||||
type: String,
|
||||
default: 'YYYY-MM-DD HH:mm:ss',
|
||||
},
|
||||
/**1 = year = 年;2 = month = 月;4 = date = 日;8 = hour = 时; 16 = minute = 分; 32 = second = 秒 */
|
||||
mode: {
|
||||
type: [String, Number],
|
||||
default: 1|2|4,
|
||||
},
|
||||
/** 选择器的最小可选时间,默认为当前时间-10年 */
|
||||
start: {
|
||||
type: [String, Number],
|
||||
default: null,
|
||||
},
|
||||
/** 时间间隔步数,示例:`{ minute: 5 }` */
|
||||
steps: {
|
||||
type: Object,
|
||||
},
|
||||
/** 标题 */
|
||||
title: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
titleStyle: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
/** 选中值 */
|
||||
value: {
|
||||
type: [String, Number],
|
||||
default: null,
|
||||
},
|
||||
/** 选中值,非受控属性 */
|
||||
defaultValue: {
|
||||
type: [String, Number],
|
||||
default: null,
|
||||
},
|
||||
modelValue: {
|
||||
type: [String, Number],
|
||||
default: null
|
||||
},
|
||||
itemHeight: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
itemColor: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
itemFontSize: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
itemActiveColor: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
|
||||
indicatorStyle: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
bgColor:{
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
groupHeight:{
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
radius:{
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
showUnit: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
resetIndex: {
|
||||
type: Boolean
|
||||
},
|
||||
minHour: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
maxHour: {
|
||||
type: Number,
|
||||
default: 23
|
||||
},
|
||||
minMinute: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
maxMinute: {
|
||||
type: Number,
|
||||
default: 59
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// @ts-nocheck
|
||||
import { PickerColumn, PickerColumnItem, PickerConfirmEvent, PickerPickEvent } from '@/uni_modules/lime-picker';
|
||||
export type TimeModeValues = 'year' | 'month' | 'date' | 'hour' | 'minute' | 'second';
|
||||
export type DateValue = any;//string | number;
|
||||
export type DateTimePickerColumn = PickerColumn//DateTimePickerColumnItem[];
|
||||
|
||||
export type DateTimePickerColumnItem = PickerColumnItem;
|
||||
|
||||
export interface DateTimePickerProps {
|
||||
/** 取消按钮文字 */
|
||||
cancelBtn?: string;
|
||||
cancelStyle ?: string;
|
||||
/** 确定按钮文字 */
|
||||
confirmBtn?: string;
|
||||
confirmStyle ?: string;
|
||||
/** 组件国际化语言,目前支持: 简体中文(zh)、(tc)、英文(en)、日语(ja)、朝鲜语(ko)、俄语(ru)等六种语言 */
|
||||
customLocale?: 'zh' | 'tc' | 'en' | 'ja' | 'ko' | 'ru';
|
||||
/** 选择器的最大可选时间,默认为当前时间+10年 */
|
||||
end?: DateValue;//string;
|
||||
/** 选择器的最小可选时间,默认为当前时间-10年 */
|
||||
start?: DateValue; //string;
|
||||
/** 时间间隔步数,示例:`{ minute: 5 }` */
|
||||
steps?: UTSJSONObject
|
||||
/** 标题 */
|
||||
title?: string;
|
||||
titleStyle ?: string;
|
||||
/** 选中值 */
|
||||
value?: DateValue;
|
||||
defaultValue?: DateValue;
|
||||
modelValue?: DateValue;
|
||||
/* 用于格式化 pick、change、confirm 事件返回的值,[详细文档](https://day.js.org/docs/en/display/format)
|
||||
*/
|
||||
format: string;
|
||||
/**
|
||||
* 时间选择器的显示模式,可以是单个模式或多个模式的组合。
|
||||
* 使用位运算符来组合多个模式。
|
||||
*
|
||||
* 例如:
|
||||
* - 单个模式:1
|
||||
* - 多个模式组合:4 | 8
|
||||
* - 1 -> 年 | 2 -> 月 | 4 -> 日 | 8 -> 时 | 16 -> 分 | 32 -> 秒
|
||||
*/
|
||||
mode: any//number
|
||||
customFilter?: ((type: TimeModeValues, columns: DateTimePickerColumn) => DateTimePickerColumn);
|
||||
/**
|
||||
* 自定义label
|
||||
*/
|
||||
renderLabel?: ((type: string, value: string) => string);
|
||||
showUnit: boolean;
|
||||
|
||||
itemHeight?: string;
|
||||
itemColor?: string;
|
||||
itemFontSize?: string;
|
||||
itemActiveColor?: string;
|
||||
|
||||
indicatorStyle?: string;
|
||||
bgColor?:string;
|
||||
groupHeight?:string;
|
||||
radius?:string;
|
||||
resetIndex: boolean
|
||||
|
||||
minHour:number;
|
||||
maxHour:number;
|
||||
minMinute:number;
|
||||
maxMinute:number;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// @ts-nocheck
|
||||
import { TimeModeValues } from './type';
|
||||
import { MODE_NAMES, MODE_MAP, MODE_YEAR, MODE_MONTH, MODE_DATE, MODE_HOUR, MODE_MINUTE, MODE_SECOND } from './constant'
|
||||
|
||||
|
||||
/**
|
||||
* 根据给定的模式返回具有意义的时间列数组。
|
||||
* @param {number} mode - 表示时间模式的位掩码。
|
||||
* @returns {TimeModeValues[]} - 返回具有意义的时间列名称数组。
|
||||
*/
|
||||
export function getMeaningColumn(mode : any) : TimeModeValues[] {
|
||||
// 初始化结果数组
|
||||
const res : TimeModeValues[] = [];
|
||||
|
||||
let _mode:number = 0;
|
||||
if(typeof mode == 'string') {
|
||||
MODE_MAP.forEach((value, key) => {
|
||||
if((mode as string).includes(key)) {
|
||||
_mode = _mode | value;
|
||||
}
|
||||
})
|
||||
} else if(typeof mode == 'number') {
|
||||
_mode = mode as number
|
||||
}
|
||||
|
||||
if(_mode <= 0) {
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
// 定义对应的位掩码数组
|
||||
const modeBitmasks = [MODE_YEAR, MODE_MONTH, MODE_DATE, MODE_HOUR, MODE_MINUTE, MODE_SECOND];
|
||||
|
||||
// 查找被设置的位掩码
|
||||
const activeBitmasks = modeBitmasks.filter(bitmask => (_mode & bitmask) != 0);
|
||||
|
||||
// 如果没有位掩码被设置,返回空数组
|
||||
if (activeBitmasks.length == 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 初始化最长连续子序列和当前连续子序列
|
||||
let longestSequence : number[] = [];
|
||||
let currentSequence : number[] = [];
|
||||
|
||||
// 遍历所有被设置的位掩码
|
||||
activeBitmasks.forEach(bitmask => {
|
||||
// 如果当前序列为空或当前位掩码是前一个位掩码的两倍,则将其加入当前序列
|
||||
if (currentSequence.length == 0 || bitmask == currentSequence[currentSequence.length - 1] * 2) {
|
||||
currentSequence.push(bitmask);
|
||||
} else {
|
||||
// 如果当前序列长度大于最长序列长度,则更新最长序列
|
||||
if (currentSequence.length > longestSequence.length) {
|
||||
longestSequence = currentSequence;
|
||||
}
|
||||
// 开始新的序列
|
||||
currentSequence = [bitmask];
|
||||
}
|
||||
});
|
||||
|
||||
// 检查最后一个序列是否是最长的
|
||||
if (currentSequence.length > longestSequence.length) {
|
||||
longestSequence = currentSequence;
|
||||
}
|
||||
|
||||
// 将最长连续子序列的位掩码转换为对应的模式名并返回
|
||||
return longestSequence.map(bitmask => MODE_NAMES[modeBitmasks.indexOf(bitmask)]);
|
||||
}
|
||||
Reference in New Issue
Block a user