perf(date-picker): 代码优化

This commit is contained in:
就眠儀式 2022-03-27 06:03:30 +08:00
parent 05aa22eacc
commit c7c4e69930
2 changed files with 78 additions and 95 deletions

View File

@ -0,0 +1,36 @@
/**
*
*/
const getYears = () => {
let years = [];
for (let i = 1970; i < getYear() + 100; i++) {
years.push(i);
}
return years;
}
/**
*
*/
const getDate = () => {
return new Date();
}
/**
*
*/
const getYear = () => {
return getDate().getFullYear();
}
/**
*
*
* @param year
* @param month
*/
const getDayLength = (year: number, month: number): number => {
return new Date(year, month + 1, 0).getDate();
};
export { getDayLength, getYears }

View File

@ -17,8 +17,8 @@
></i ></i
> >
<div class="laydate-set-ym"> <div class="laydate-set-ym">
<span @click="showYearPanel">{{ curYear }} </span <span @click="showYearPanel">{{ currentYear }} </span
><span @click="showPanel = 'month'">{{ curMonth + 1 }} </span> ><span @click="showPanel = 'month'">{{ currentMonth + 1 }} </span>
</div> </div>
<i <i
class="layui-icon laydate-icon laydate-next-m" class="layui-icon laydate-icon laydate-next-m"
@ -100,9 +100,9 @@
<li <li
v-for="item of yearList" v-for="item of yearList"
:key="item" :key="item"
:class="[{ 'layui-this': curYear === item }]" :class="[{ 'layui-this': currentYear === item }]"
@click=" @click="
curYear = item; currentYear = item;
showPanel = 'date'; showPanel = 'date';
" "
> >
@ -136,8 +136,8 @@
></i ></i
> >
<div class="laydate-set-ym"> <div class="laydate-set-ym">
<span @click="showYearPanel">{{ curYear }} </span <span @click="showYearPanel">{{ currentYear }} </span
><span @click="showPanel = 'month'">{{ curMonth + 1 }} </span> ><span @click="showPanel = 'month'">{{ currentMonth + 1 }} </span>
</div> </div>
<i <i
class="layui-icon laydate-icon laydate-next-y" class="layui-icon laydate-icon laydate-next-y"
@ -151,10 +151,10 @@
v-for="item of MONTH_NAME" v-for="item of MONTH_NAME"
:key="item" :key="item"
:class="[ :class="[
{ 'layui-this': MONTH_NAME.indexOf(item) === curMonth }, { 'layui-this': MONTH_NAME.indexOf(item) === currentMonth },
]" ]"
@click=" @click="
curMonth = MONTH_NAME.indexOf(item); currentMonth = MONTH_NAME.indexOf(item);
showPanel = 'date'; showPanel = 'date';
" "
> >
@ -178,7 +178,7 @@
</div> </div>
</div> </div>
<!-- 事件选择器 --> <!-- 时间选择器 -->
<div class="layui-laydate" v-if="showPanel == 'time'"> <div class="layui-laydate" v-if="showPanel == 'time'">
<div class="layui-laydate-main laydate-main-list-0 laydate-time-show"> <div class="layui-laydate-main laydate-main-list-0 laydate-time-show">
<div class="layui-laydate-header"> <div class="layui-laydate-header">
@ -190,10 +190,10 @@
<ul class="layui-laydate-list laydate-time-list"> <ul class="layui-laydate-list laydate-time-list">
<li <li
class="num-list" class="num-list"
v-for="item in hms.insertEls" v-for="item in els"
:key="item.type" :key="item.type"
> >
<ol class="scroll" @click="chooseTime"> <ol class="scroll" @click="choseTime">
<li <li
v-for="(it, index) in item.count" v-for="(it, index) in item.count"
:id="item.type + index.toString()" :id="item.type + index.toString()"
@ -231,9 +231,21 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, nextTick, ref, watch, defineProps, defineEmits } from "vue"; import { computed, nextTick, ref, watch, defineProps, defineEmits, onMounted } from "vue";
import LayInput from "../input/index.vue"; import LayInput from "../input/index.vue";
import LayDropdown from "../dropdown/index.vue"; import LayDropdown from "../dropdown/index.vue";
import { getDayLength, getYears } from "./day";
const $emits = defineEmits(["update:modelValue"]);
const WEEK_NAME = ["日", "一", "二", "三", "四", "五", "六"];
const MONTH_NAME = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
const hms = ref({hh: "00",mm: "00",ss: "00"});
const els = [
{count: 24, type: "hh"},
{count: 60, type: "mm"},
{count: 60, type: "ss"},
];
export interface LayDatePickerProps { export interface LayDatePickerProps {
modelValue?: string; modelValue?: string;
@ -245,51 +257,16 @@ const props = withDefaults(defineProps<LayDatePickerProps>(), {
type: "date", type: "date",
}); });
const $emits = defineEmits(["update:modelValue"]);
const currentDate = new Date(); const currentDate = new Date();
const yearList = ref<number[]>([]); const currentYear = ref(currentDate.getFullYear());
const currentMonth = ref(currentDate.getMonth());
const yearList = ref<number[]>(getYears());
const dateList = ref<any[]>([]); const dateList = ref<any[]>([]);
const curYear = ref(currentDate.getFullYear());
const curMonth = ref(currentDate.getMonth());
const showPanel = ref("date"); const showPanel = ref("date");
const selectedDay = ref<number>(); const selectedDay = ref<number>();
const WEEK_NAME = ["日", "一", "二", "三", "四", "五", "六"]; //
const MONTH_NAME = [
"1月",
"2月",
"3月",
"4月",
"5月",
"6月",
"7月",
"8月",
"9月",
"10月",
"11月",
"12月",
];
const hms = ref({
hh: "00",
mm: "00",
ss: "00",
insertEls: [
{
count: 24,
type: "hh",
},
{
count: 60,
type: "mm",
},
{
count: 60,
type: "ss",
},
],
});
const dateValue = computed<string>(() => { const dateValue = computed<string>(() => {
if (!selectedDay.value) return ""; if (!selectedDay.value) return "";
const d = new Date(selectedDay.value); const d = new Date(selectedDay.value);
@ -301,18 +278,6 @@ const dateValue = computed<string>(() => {
return currentValue; return currentValue;
}); });
// 100
(() => {
for (let i = 1970; i < curYear.value + 100; i++) {
yearList.value.push(i);
}
})();
//
const getDayLength = (year: number, month: number): number => {
return new Date(year, month + 1, 0).getDate();
};
// //
const setDateList = (year: number, month: number) => { const setDateList = (year: number, month: number) => {
const curDays = getDayLength(year, month); // const curDays = getDayLength(year, month); //
@ -345,7 +310,7 @@ const setDateList = (year: number, month: number) => {
for (let i = 1; i <= nextDays; i++) { for (let i = 1; i <= nextDays; i++) {
list.push({ list.push({
day: i, day: i,
value: +new Date(year, month + 1, i), value: + new Date(year, month + 1, i),
isRange: false, isRange: false,
isSelected: false, isSelected: false,
type: "next", type: "next",
@ -355,10 +320,11 @@ const setDateList = (year: number, month: number) => {
dateList.value = list; dateList.value = list;
}; };
// ,
watch( watch(
[curYear, curMonth], [currentYear, currentMonth],
() => { () => {
setDateList(curYear.value, curMonth.value); setDateList(currentYear.value, currentMonth.value);
}, },
{ immediate: true } { immediate: true }
); );
@ -367,8 +333,7 @@ watch(
const handleDayClick = (item: any) => { const handleDayClick = (item: any) => {
selectedDay.value = item.value; selectedDay.value = item.value;
if (item.type !== "current") { if (item.type !== "current") {
curMonth.value = currentMonth.value = item.type === "prev" ? currentMonth.value - 1 : currentMonth.value + 1;
item.type === "prev" ? curMonth.value - 1 : curMonth.value + 1;
} }
}; };
@ -378,18 +343,17 @@ const ok = () => {};
// //
const changeYearOrMonth = (type: "year" | "month", num: number) => { const changeYearOrMonth = (type: "year" | "month", num: number) => {
if (type === "year") { if (type === "year") {
curYear.value += num; currentYear.value += num;
} else { } else {
let month = curMonth.value + num; let month = currentMonth.value + num;
if (month > 11) { if (month > 11) {
month = 0; month = 0;
curYear.value++; currentYear.value++;
} else if (month < 0) { } else if (month < 0) {
month = 11; month = 11;
curYear.value--; currentYear.value--;
} }
curMonth.value = month; currentMonth.value = month;
} }
}; };
@ -397,29 +361,12 @@ const changeYearOrMonth = (type: "year" | "month", num: number) => {
const showYearPanel = () => { const showYearPanel = () => {
showPanel.value = "year"; showPanel.value = "year";
nextTick(() => { nextTick(() => {
( (document.querySelector(".year-panel-item.active") as HTMLElement).scrollIntoView({ block: "center" });
document.querySelector(".year-panel-item.active") as HTMLElement
).scrollIntoView({ block: "center" });
}); });
}; };
const showHmPanel = ref(false); // - hms
const choseTime = (e: any) => {
const openHmPanel = () => {
let activeEl = document.querySelectorAll(".hms-change .isactive") as any;
let count = 0;
const initScrollIntoView = () => {
if (count > activeEl.length) return;
activeEl[count].scrollIntoView({ behavior: "smooth" });
count++;
setTimeout(initScrollIntoView, Number(activeEl[count].dataset.value) * 16);
};
initScrollIntoView();
showHmPanel.value = !showHmPanel.value;
};
const chooseTime = (e: any) => {
if (e.target.nodeName == "LI") { if (e.target.nodeName == "LI") {
let { value, type } = e.target.dataset; let { value, type } = e.target.dataset;
hms.value[type as keyof typeof hms.value] = value; hms.value[type as keyof typeof hms.value] = value;