master
chen 2022-04-01 08:55:06 +08:00
parent f4f2042106
commit bbece21ba4
1 changed files with 270 additions and 258 deletions

View File

@ -1,283 +1,295 @@
<template> <template>
<view class="datetime-picker"> <view class="datetime-picker">
<view <view class="mask" :class="{ show: open }" @touchmove.stop.prevent catchtouchmove="true"></view>
class="mask" <view class="wrap" :class="{ show: open }">
:class="{ show: open }" <view class="picker-header" @touchmove.stop.prevent catchtouchmove="true">
@touchmove.stop.prevent <view class="btn-picker cancel" @click="open = false">取消</view>
catchtouchmove="true" <view class="btn-picker submit" @click="_onSubmit"></view>
></view> </view>
<view class="wrap" :class="{ show: open }"> <view class="picker-body">
<view class="picker-header" @touchmove.stop.prevent catchtouchmove="true"> <picker-view :value="value" @change="_onChange">
<view class="btn-picker cancel" @click="open = false">取消</view> <picker-view-column>
<view class="btn-picker submit" @click="_onSubmit"></view> <view class="column-item" v-for="item in years" :key="item">
</view> {{ item + "年" }}
<view class="picker-body"> </view>
<picker-view :value="value" @change="_onChange"> </picker-view-column>
<picker-view-column> <picker-view-column>
<view class="column-item" v-for="item in years" :key="item"> <view class="column-item" v-for="item in months" :key="item">
{{ item + "年" }} {{ formatNum(item) + "月" }}
</view> </view>
</picker-view-column> </picker-view-column>
<picker-view-column> <picker-view-column>
<view class="column-item" v-for="item in months" :key="item"> <view class="column-item" v-for="item in days" :key="item">
{{ formatNum(item) + "月" }} {{ formatNum(item) + "日" }}
</view> </view>
</picker-view-column> </picker-view-column>
<picker-view-column> <picker-view-column>
<view class="column-item" v-for="item in days" :key="item"> <view class="column-item" v-for="item in hours" :key="item">
{{ formatNum(item) + "日" }} {{ formatNum(item) + "时" }}
</view> </view>
</picker-view-column> </picker-view-column>
<picker-view-column> <picker-view-column>
<view class="column-item" v-for="item in hours" :key="item"> <view class="column-item" v-for="item in minutes" :key="item">
{{ formatNum(item) + "时" }} {{ formatNum(item) + "分" }}
</view> </view>
</picker-view-column> </picker-view-column>
<picker-view-column> </picker-view>
<view class="column-item" v-for="item in minutes" :key="item"> </view>
{{ formatNum(item) + "分" }} </view>
</view> </view>
</picker-view-column>
</picker-view>
</view>
</view>
</view>
</template> </template>
<script> <script>
export default { export default {
name: "buuug7-simple-datetime-picker", name: "buuug7-simple-datetime-picker",
props: { props: {
startYear: { startYear: {
type: Number, type: Number,
default: 2000, default: 2000,
}, },
endYear: { endYear: {
type: Number, type: Number,
default: 2030, default: 2030,
}, },
}, },
data() { data() {
return { return {
open: false, open: false,
years: [], years: [],
months: [], months: [],
days: [], days: [],
hours: [], hours: [],
minutes: [], minutes: [],
currentDate: new Date(), currentDate: new Date(),
year: "", year: "",
month: "", month: "",
day: "", day: "",
hour: "", hour: "",
minute: "", minute: "",
value: [0, 0, 0, 0, 0], value: [0, 0, 0, 0, 0],
}; };
}, },
mounted() { mounted() {
this.init(); this.init();
}, },
watch: { watch: {
month() { month() {
this.initDays(); this.initDays();
}, },
}, },
methods: { methods: {
init() { init() {
this.initYears(); this.initYears();
this.initMonths(); this.initMonths();
this.initDays(); this.initDays();
this.initHours(); this.initHours();
this.initMinutes(); this.initMinutes();
this.setSelectValue(); this.setSelectValue();
}, },
initYears() { initYears() {
const years = []; const years = [];
for (let year = this.startYear; year <= this.endYear; year++) { for (let year = this.startYear; year <= this.endYear; year++) {
years.push(year); years.push(year);
if (this.currentDate.getFullYear() === year) { if (this.currentDate.getFullYear() === year) {
this.$set(this.value, 0, year - this.startYear); this.$set(this.value, 0, year - this.startYear);
} }
} }
this.years = years; this.years = years;
}, },
initMonths() { initMonths() {
const months = []; const months = [];
for (let month = 1; month <= 12; month++) { for (let month = 1; month <= 12; month++) {
months.push(month); months.push(month);
if (this.currentDate.getMonth() + 1 === month) { if (this.currentDate.getMonth() + 1 === month) {
this.$set(this.value, 1, month - 1); this.$set(this.value, 1, month - 1);
} }
} }
this.months = months; this.months = months;
}, },
initDays() { initDays() {
const value = this.value; const value = this.value;
const selectedYear = this.years[value[0]]; const selectedYear = this.years[value[0]];
const selectedMonth = this.months[value[1]]; const selectedMonth = this.months[value[1]];
const days = []; const days = [];
const totalDays = new Date(selectedYear, selectedMonth, 0).getDate(); const totalDays = new Date(selectedYear, selectedMonth, 0).getDate();
for (let day = 1; day <= totalDays; day++) { for (let day = 1; day <= totalDays; day++) {
days.push(day); days.push(day);
if (this.currentDate.getDate() === day) { if (this.currentDate.getDate() === day) {
this.$set(value, 2, day - 1); this.$set(value, 2, day - 1);
} }
} }
this.days = days; this.days = days;
}, },
initHours() { initHours() {
const hours = []; const hours = [];
for (let hour = 0; hour <= 23; hour++) { for (let hour = 0; hour <= 23; hour++) {
hours.push(hour); hours.push(hour);
if (this.currentDate.getHours() === hour) { if (this.currentDate.getHours() === hour) {
this.$set(this.value, 3, hour); this.$set(this.value, 3, hour);
} }
} }
this.hours = hours; this.hours = hours;
}, },
initMinutes() { initMinutes() {
const minutes = []; const minutes = [];
for (let minute = 0; minute < 60; minute++) { for (let minute = 0; minute < 60; minute++) {
minutes.push(minute); minutes.push(minute);
if (this.currentDate.getMinutes() === minute) { if (this.currentDate.getMinutes() === minute) {
this.$set(this.value, 4, minute); this.$set(this.value, 4, minute);
} }
} }
this.minutes = minutes; this.minutes = minutes;
}, },
show() { show() {
this.open = true; this.open = true;
}, },
hide() { hide() {
this.open = false; this.open = false;
}, },
_onChange(e) { _onChange(e) {
this.value = e.detail.value; this.value = e.detail.value;
this.setSelectValue(); this.setSelectValue();
}, },
setSelectValue() { setSelectValue() {
const v = this.value; const v = this.value;
this.year = this.years[v[0]]; this.year = this.years[v[0]];
this.month = this.months[v[1]]; this.month = this.months[v[1]];
this.day = this.days[v[2]]; this.day = this.days[v[2]];
this.hour = this.hours[v[3]]; this.hour = this.hours[v[3]];
this.minute = this.minutes[v[4]]; this.minute = this.minutes[v[4]];
}, },
_onSubmit() { _onSubmit() {
const { year, month, day, hour, minute, formatNum } = this; const {
const result = { year,
year: formatNum(year), month,
month: formatNum(month), day,
day: formatNum(day), hour,
hour: formatNum(hour), minute,
minute: formatNum(minute), formatNum
}; } = this;
this.$emit("submit", result); const result = {
this.hide(); year: formatNum(year),
}, month: formatNum(month),
day: formatNum(day),
hour: formatNum(hour),
minute: formatNum(minute),
};
this.$emit("submit", result);
this.hide();
},
formatNum(num) { formatNum(num) {
return num < 10 ? "0" + num : num + ""; return num < 10 ? "0" + num : num + "";
}, },
}, },
}; };
</script> </script>
<style lang="scss"> <style lang="scss">
$transition: all 0.3s ease; $transition: all 0.3s ease;
$primary: #488ee9; $primary: #488ee9;
.datetime-picker { .datetime-picker {
position: relative; position: relative;
z-index: 999; z-index: 999;
picker-view {
height: 100%; picker-view {
} height: 100%;
.mask { }
position: fixed;
z-index: 1000; .mask {
top: 0; position: fixed;
right: 0; z-index: 1000;
bottom: 0; top: 0;
left: 0; right: 0;
background-color: rgba(0, 0, 0, 0.6); bottom: 0;
visibility: hidden; left: 0;
opacity: 0; background-color: rgba(0, 0, 0, 0.6);
transition: $transition; visibility: hidden;
&.show { opacity: 0;
visibility: visible; transition: $transition;
opacity: 1;
} &.show {
} visibility: visible;
.wrap { opacity: 1;
z-index: 1001; }
position: fixed; }
bottom: 0;
left: 0; .wrap {
width: 100%; z-index: 1001;
transition: $transition; position: fixed;
transform: translateY(100%); bottom: 0;
&.show { left: 0;
transform: translateY(0); width: 100%;
} transition: $transition;
} transform: translateY(100%);
.picker-header {
display: flex; &.show {
flex-direction: row; transform: translateY(0);
justify-content: space-between; }
align-items: center; }
padding: 8px 8px;
background-color: darken(#fff, 2%); .picker-header {
background-color: #fff; display: flex;
} flex-direction: row;
.picker-body { justify-content: space-between;
width: 100%; align-items: center;
height: 420rpx; padding: 8px 8px;
overflow: hidden; background-color: darken(#fff, 2%);
background-color: #fff; background-color: #fff;
} }
.column-item {
text-overflow: ellipsis; .picker-body {
white-space: nowrap; width: 100%;
display: flex; height: 420rpx;
justify-content: center; overflow: hidden;
align-items: center; background-color: #fff;
} }
.btn-picker {
position: relative; .column-item {
display: inline-block; text-overflow: ellipsis;
padding-left: 10px; white-space: nowrap;
padding-right: 10px; display: flex;
box-sizing: border-box; justify-content: center;
text-align: center; align-items: center;
text-decoration: none; }
line-height: 2;
-webkit-tap-highlight-color: transparent; .btn-picker {
overflow: hidden; position: relative;
background-color: #eee; display: inline-block;
font-size: 14px; padding-left: 10px;
// border-radius: 2px; padding-right: 10px;
color: #000; box-sizing: border-box;
cursor: pointer; text-align: center;
} text-decoration: none;
.btn-picker.submit { line-height: 2;
background-color: $primary; -webkit-tap-highlight-color: transparent;
color: #fff; overflow: hidden;
} background-color: #eee;
} font-size: 14px;
// border-radius: 2px;
color: #000;
cursor: pointer;
}
.btn-picker.submit {
background-color: $primary;
color: #fff;
}
}
</style> </style>