fix 手动输入年代号“2017”回车之后还显示的是2023

This commit is contained in:
赵霄
2023-12-26 16:20:15 +08:00
parent 02b041f981
commit 9c82e02b44
+52 -111
View File
@@ -1,84 +1,45 @@
<template> <template>
<span> <a-date-picker
<!--YYYY-MM-DD HH:mm:ss--> :disabledDate="disabledDate"
<a-date-picker :disabled="disabled || readOnly"
v-if="showType === 'time'" :placeholder="placeholder"
dropdownClassName="j-date-picker" @change="handleDateChange"
:disabled="disabled || readOnly" :value="momVal"
:placeholder="placeholder" :showTime="showTime"
@change="handleDateChange" :format="dateFormat"
:value="momVal" :mode="mode"
:showTime="true" :getCalendarContainer="getCalendarContainer"
:format="dateFormat" style="width: 100%" />
style="width: 100%"
v-bind="$attrs"
v-on="childListeners"
:getCalendarContainer="getCalendarContainer">
</a-date-picker>
<!--YYYY-MM-DD-->
<a-date-picker
v-if="showType === 'day'"
dropdownClassName="j-date-picker"
:disabled="disabled || readOnly"
:placeholder="placeholder"
@change="handleDateChange"
:value="momVal"
:showTime="false"
:format="dateFormat"
style="width: 100%"
v-bind="$attrs"
v-on="childListeners"
:getCalendarContainer="getCalendarContainer">
</a-date-picker>
<!--YYYY-MM-->
<a-month-picker
v-if="showType === 'month'"
:placeholder="placeholder"
:value="momVal"
:disabled="disabled || readOnly"
:format="dateFormat"
style="width: 100%"
v-bind="$attrs"
v-on="childListeners"
@change="handleMonthChange">
</a-month-picker>
<!--YYYY-->
<a-date-picker
v-if="showType === 'year'"
:placeholder="placeholder"
mode="year"
:format="dateFormat"
:value="year"
:disabled="disabled || readOnly"
:open="yearShowOne"
v-bind="$attrs"
v-on="childListeners"
:getCalendarContainer="getCalendarContainer"
style="width: 100%"
@openChange="openChangeOne"
@panelChange="panelChangeOne">
</a-date-picker>
</span>
</template> </template>
<script> <script>
import moment from 'moment' import moment from 'moment'
export default { export default {
name: 'JDate', name: 'JDate',
props: { props: {
placeholder: { placeholder: {
type: String, type: String,
default: '请选择', default: '',
required: false required: false
}, },
value: { value: {
type: String, type: String,
required: false required: false
}, },
disabledDate: {
type: Function
},
dateFormat: { dateFormat: {
type: String, type: String,
default: 'YYYY-MM-DD', default: 'YYYY-MM-DD',
required: false required: false
}, },
// 此属性可以被废弃了
triggerChange: {
type: Boolean,
required: false,
default: false
},
readOnly: { readOnly: {
type: Boolean, type: Boolean,
required: false, required: false,
@@ -89,90 +50,70 @@ export default {
required: false, required: false,
default: false default: false
}, },
// 控制选择时分秒 showTime: {
showType: { type: Boolean,
type: String,
required: false, required: false,
default: 'time' default: false
}, },
getCalendarContainer: { getCalendarContainer: {
type: Function, type: Function,
default: (node) => node.parentNode default: (node) => node.parentNode
},
showType: {
type: String,
required: false,
default: 'time'
} }
}, },
data () { data () {
const dateStr = this.value const dateStr = this.value
return { return {
yearShowOne: false, // 控制年份模态框的显示与否 decorator: '',
year: '', // mode==“year” 专用
momVal: !dateStr ? null : moment(dateStr, this.dateFormat) momVal: !dateStr ? null : moment(dateStr, this.dateFormat)
} }
}, },
computed: { computed: {
// 透传给下级组件的事件,需要排除本组件使用的change事件 mode () {
childListeners () { if (this.showType === 'time') {
const result = Object.assign({}, return 'time'
this.$listeners }
) if (this.showType === 'day') {
delete result.change return 'date'
return result }
if (this.showType === 'month') {
return 'month'
}
if (this.showType === 'year') {
return 'year'
}
return 'date'
} }
}, },
watch: { watch: {
value (val) { value (val) {
console.log(val)
const fm = this.dateFormat
moment.prototype.toJSON = function () {
return moment(this).format(fm)
}
if (!val) { if (!val) {
if (this.showType === 'year') { this.momVal = null
this.year = null
} else {
this.momVal = null
}
} else { } else {
if (this.showType === 'year') { this.momVal = moment(val, this.dateFormat)
this.year = val
} else {
this.momVal = moment(val, this.dateFormat)
}
} }
} }
}, },
methods: { methods: {
moment, moment,
handleDateChange (mom, dateStr) { handleDateChange (mom, dateStr) {
console.log('handleDateChange----', dateStr)
this.$emit('change', dateStr) this.$emit('change', dateStr)
},
handleMonthChange (mom, dateStr) {
this.$emit('change', dateStr)
},
/**
* 弹出日历和关闭日历的回调
* status:打开或关闭的状态
* */
openChangeOne (status) {
this.yearShowOne = status
},
// 得到年份选择器的值
panelChangeOne (value) {
this.yearShowOne = false
const year = moment(value, this.dateFormat).year().toString() // 处理成字符串
this.year = value // 组件显示的
this.$emit('change', year)
} }
}, },
// 2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 // 2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 这个牛逼
model: { model: {
prop: 'value', prop: 'value',
event: 'change' event: 'change'
} }
} }
</script> </script>
<style lang="less" scoped>
<style scoped> /deep/ .ant-input {
.ant-calendar-picker { padding: 4px 6px;
/*min-width: 195px;*/
} }
</style> </style>