Commit 05608d10 by chenwq

格式化

1 parent 7341edba
...@@ -4,38 +4,38 @@ ...@@ -4,38 +4,38 @@
<el-date-picker v-if="editableStart" <el-date-picker v-if="editableStart"
:key="startPickerKey" :key="startPickerKey"
v-model="startDateValue" v-model="startDateValue"
type="date" class="period-picker start-picker"
value-format="yyyy-MM-dd"
format="yyyy/MM/dd" format="yyyy/MM/dd"
placeholder="开始日期" placeholder="开始日期"
type="date"
value-format="yyyy-MM-dd"
:default-value="startPickerDefaultValue" :default-value="startPickerDefaultValue"
:disabled="disabled || !targetMonth" :disabled="disabled || !targetMonth"
:picker-options="startPickerOptions" :picker-options="startPickerOptions"
class="period-picker start-picker"
@change="handleStartChange" /> @change="handleStartChange" />
<el-date-picker v-else <el-date-picker v-else
:value="startDate" class="period-picker start-picker"
type="date"
value-format="yyyy-MM-dd"
format="yyyy/MM/dd" format="yyyy/MM/dd"
placeholder="开始日期" placeholder="开始日期"
type="date"
value-format="yyyy-MM-dd"
disabled disabled
class="period-picker start-picker" /> :value="startDate" />
<span class="range-separator">-</span> <span class="range-separator">-</span>
<el-date-picker :key="endPickerKey" <el-date-picker :key="endPickerKey"
v-model="endDate" v-model="endDate"
type="date" class="period-picker end-picker"
value-format="yyyy-MM-dd"
format="yyyy/MM/dd" format="yyyy/MM/dd"
placeholder="结束日期" placeholder="结束日期"
type="date"
value-format="yyyy-MM-dd"
:default-value="endPickerDefaultValue" :default-value="endPickerDefaultValue"
:disabled="disabled || !targetMonth || !startDate" :disabled="disabled || !targetMonth || !startDate"
:picker-options="endPickerOptions" :picker-options="endPickerOptions"
class="period-picker end-picker"
@change="handleEndChange" /> @change="handleEndChange" />
<el-checkbox v-model="isFullMonth" <el-checkbox v-model="isFullMonth"
:disabled="disabled || !targetMonth || !startDate"
class="full-month-check" class="full-month-check"
:disabled="disabled || !targetMonth || !startDate"
@change="handleFullMonthChange"> @change="handleFullMonthChange">
全月 全月
</el-checkbox> </el-checkbox>
...@@ -48,164 +48,170 @@ import { ...@@ -48,164 +48,170 @@ import {
getEditablePeriodStartPickerDefaultDate, getEditablePeriodStartPickerDefaultDate,
getEditableSettlementPeriodRange, getEditableSettlementPeriodRange,
getSettlementPeriodPickerDefaultDate, getSettlementPeriodPickerDefaultDate,
getSettlementPeriodRange getSettlementPeriodRange,
} from '@/common/format' } from "@/common/format";
export default { export default {
name: 'PeriodDateRange', name: "PeriodDateRange",
props: { props: {
value: { value: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
targetMonth: { targetMonth: {
type: String, type: String,
default: '' default: "",
}, },
settlementDate: { settlementDate: {
type: [Number, String], type: [Number, String],
default: 0 default: 0,
}, },
editableStart: { editableStart: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false default: false,
} },
}, },
data() { data() {
return { return {
startDateValue: '', startDateValue: "",
endDate: '', endDate: "",
isFullMonth: false, isFullMonth: false,
startPickerOptions: { startPickerOptions: {
disabledDate: (time) => this.isStartDateDisabled(time) disabledDate: time => this.isStartDateDisabled(time),
}, },
endPickerOptions: { endPickerOptions: {
disabledDate: (time) => this.isEndDateDisabled(time) disabledDate: time => this.isEndDateDisabled(time),
} },
} };
}, },
computed: { computed: {
periodRangeLimit() { periodRangeLimit() {
if (this.editableStart) { if (this.editableStart) {
return getEditableSettlementPeriodRange(this.targetMonth, this.startDateValue) || {} return getEditableSettlementPeriodRange(this.targetMonth, this.startDateValue) || {};
} }
return getSettlementPeriodRange(this.targetMonth, this.settlementDate) || {} return getSettlementPeriodRange(this.targetMonth, this.settlementDate) || {};
}, },
startDate() { startDate() {
return this.editableStart ? this.startDateValue : (this.periodRangeLimit.startDate || '') return this.editableStart ? this.startDateValue : (this.periodRangeLimit.startDate || "");
}, },
startPickerDefaultValue() { startPickerDefaultValue() {
return getEditablePeriodStartPickerDefaultDate(this.targetMonth) return getEditablePeriodStartPickerDefaultDate(this.targetMonth);
}, },
startPickerKey() { startPickerKey() {
return `start-${this.targetMonth || ''}-${this.editableStart ? 'editable' : 'fixed'}` return `start-${this.targetMonth || ""}-${this.editableStart ? "editable" : "fixed"}`;
}, },
endPickerKey() { endPickerKey() {
return `end-${this.targetMonth || ''}-${this.startDate || ''}-${this.endPickerDefaultValue ? this.endPickerDefaultValue.getTime() : ''}` return `end-${this.targetMonth || ""}-${this.startDate || ""}-${this.endPickerDefaultValue ? this.endPickerDefaultValue.getTime() : ""}`;
}, },
endPickerDefaultValue() { endPickerDefaultValue() {
if (this.editableStart) { if (this.editableStart) {
const { maxEndDate } = this.periodRangeLimit const { maxEndDate } = this.periodRangeLimit;
if (!maxEndDate) return null if (!maxEndDate) {
const [year, month, day] = maxEndDate.split('-').map(item => parseInt(item, 10)) return null;
return new Date(year, month - 1, day)
} }
return getSettlementPeriodPickerDefaultDate(this.targetMonth, this.settlementDate) const [year, month, day] = maxEndDate.split("-").map(item => Number.parseInt(item, 10));
return new Date(year, month - 1, day);
} }
return getSettlementPeriodPickerDefaultDate(this.targetMonth, this.settlementDate);
},
}, },
watch: { watch: {
value: { value: {
immediate: true, immediate: true,
handler(val) { handler(val) {
if (this.editableStart) { if (this.editableStart) {
this.startDateValue = val && val.length === 2 && val[0] ? val[0] : '' this.startDateValue = val && val.length === 2 && val[0] ? val[0] : "";
}
this.endDate = val && val.length === 2 && val[1] ? val[1] : ''
this.syncFullMonthState()
} }
this.endDate = val && val.length === 2 && val[1] ? val[1] : "";
this.syncFullMonthState();
},
}, },
targetMonth(val, oldVal) { targetMonth(val, oldVal) {
if (val !== oldVal && oldVal) { if (val !== oldVal && oldVal) {
this.resetRangeState() this.resetRangeState();
} }
}, },
settlementDate(val, oldVal) { settlementDate(val, oldVal) {
if (val !== oldVal) { if (val !== oldVal) {
this.resetRangeState() this.resetRangeState();
}
} }
}, },
},
methods: { methods: {
isStartDateDisabled(time) { isStartDateDisabled(time) {
const startRange = getEditablePeriodStartDateRange(this.targetMonth) const startRange = getEditablePeriodStartDateRange(this.targetMonth);
if (!startRange) return true if (!startRange) {
const timeStr = this.formatDateStr(time) return true;
return timeStr < startRange.minStartDate || timeStr > startRange.maxStartDate }
const timeStr = this.formatDateStr(time);
return timeStr < startRange.minStartDate || timeStr > startRange.maxStartDate;
}, },
isEndDateDisabled(time) { isEndDateDisabled(time) {
const { startDate, maxEndDate } = this.periodRangeLimit const { startDate, maxEndDate } = this.periodRangeLimit;
if (!startDate || !maxEndDate) return true if (!startDate || !maxEndDate) {
const timeStr = this.formatDateStr(time) return true;
return timeStr < startDate || timeStr > maxEndDate }
const timeStr = this.formatDateStr(time);
return timeStr < startDate || timeStr > maxEndDate;
}, },
resetRangeState() { resetRangeState() {
this.startDateValue = '' this.startDateValue = "";
this.endDate = '' this.endDate = "";
this.isFullMonth = false this.isFullMonth = false;
this.emitChange([]) this.emitChange([]);
}, },
formatDateStr(time) { formatDateStr(time) {
const y = time.getFullYear() const y = time.getFullYear();
const m = String(time.getMonth() + 1).padStart(2, '0') const m = String(time.getMonth() + 1).padStart(2, "0");
const d = String(time.getDate()).padStart(2, '0') const d = String(time.getDate()).padStart(2, "0");
return `${y}-${m}-${d}` return `${y}-${m}-${d}`;
}, },
syncFullMonthState() { syncFullMonthState() {
if (!this.targetMonth || !this.endDate || !this.startDate) { if (!this.targetMonth || !this.endDate || !this.startDate) {
this.isFullMonth = false this.isFullMonth = false;
return return;
} }
const { fullEndDate } = this.periodRangeLimit const { fullEndDate } = this.periodRangeLimit;
this.isFullMonth = this.endDate === fullEndDate this.isFullMonth = this.endDate === fullEndDate;
}, },
emitChange(range) { emitChange(range) {
this.$emit('input', range) this.$emit("input", range);
this.$emit('change', range) this.$emit("change", range);
}, },
handleStartChange(val) { handleStartChange(val) {
this.endDate = '' this.endDate = "";
this.isFullMonth = false this.isFullMonth = false;
this.emitChange(val ? [val, ''] : []) this.emitChange(val ? [val, ""] : []);
}, },
handleEndChange(val) { handleEndChange(val) {
if (!val || !this.startDate) { if (!val || !this.startDate) {
this.isFullMonth = false this.isFullMonth = false;
this.emitChange([]) this.emitChange([]);
return return;
} }
this.syncFullMonthState() this.syncFullMonthState();
this.emitChange([this.startDate, val]) this.emitChange([this.startDate, val]);
}, },
handleFullMonthChange(checked) { handleFullMonthChange(checked) {
if (checked) { if (checked) {
const { startDate, fullEndDate } = this.periodRangeLimit const { startDate, fullEndDate } = this.periodRangeLimit;
if (startDate && fullEndDate) { if (startDate && fullEndDate) {
this.endDate = fullEndDate this.endDate = fullEndDate;
this.emitChange([startDate, fullEndDate]) this.emitChange([startDate, fullEndDate]);
} else { } else {
this.isFullMonth = false this.isFullMonth = false;
} }
} else { } else {
this.endDate = '' this.endDate = "";
this.emitChange([]) this.emitChange([]);
}
} }
} },
} },
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!