提交 b13a0306 编写于 作者: Q qiang

fix: H5 修复 picker 组件设置 fields 未影响返回值的问题

fix: _getDateValueArray
上级 44fb8b2b
...@@ -55,6 +55,42 @@ ...@@ -55,6 +55,42 @@
import { emitter } from 'uni-mixins' import { emitter } from 'uni-mixins'
import { formatDateTime } from 'uni-shared' import { formatDateTime } from 'uni-shared'
function getDefaultStartValue () {
if (this.mode === mode.TIME) {
return '00:00'
}
if (this.mode === mode.DATE) {
let year = new Date().getFullYear() - 100
switch (this.fields) {
case fields.YEAR:
return year
case fields.MONTH:
return year + '-01'
case fields.DAY:
return year + '-01-01'
}
}
return ''
}
function getDefaultEndValue () {
if (this.mode === mode.TIME) {
return '23:59'
}
if (this.mode === mode.DATE) {
let year = new Date().getFullYear() + 100
switch (this.fields) {
case fields.YEAR:
return year
case fields.MONTH:
return year + '-12'
case fields.DAY:
return year + '-12-31'
}
}
return ''
}
const mode = { const mode = {
SELECTOR: 'selector', SELECTOR: 'selector',
MULTISELECTOR: 'multiSelector', MULTISELECTOR: 'multiSelector',
...@@ -106,43 +142,11 @@ export default { ...@@ -106,43 +142,11 @@ export default {
}, },
start: { start: {
type: String, type: String,
default () { default: getDefaultStartValue
if (this.mode === mode.TIME) {
return '00:00'
}
if (this.mode === mode.DATE) {
let year = new Date().getFullYear() - 100
switch (this.fields) {
case fields.YEAR:
return year
case fields.MONTH:
return year + '-01'
case fields.DAY:
return year + '-01-01'
}
}
return ''
}
}, },
end: { end: {
type: String, type: String,
default () { default: getDefaultEndValue
if (this.mode === mode.TIME) {
return '23:59'
}
if (this.mode === mode.DATE) {
let year = new Date().getFullYear() + 100
switch (this.fields) {
case fields.YEAR:
return year
case fields.MONTH:
return year + '-12'
case fields.DAY:
return year + '-12-31'
}
}
return ''
}
}, },
disabled: { disabled: {
type: [Boolean, String], type: [Boolean, String],
...@@ -185,24 +189,10 @@ export default { ...@@ -185,24 +189,10 @@ export default {
} }
}, },
startArray () { startArray () {
var splitStr = this.mode === mode.DATE ? '-' : ':' return this._getDateValueArray(this.start, getDefaultStartValue.bind(this)())
var array = this.mode === mode.DATE ? this.dateArray : this.timeArray
var val = this.start.split(splitStr).map((val, i) => array[i].indexOf(
val))
if (val.indexOf(-1) >= 0) {
val = array.map(() => 0)
}
return this._filterDateValue(val)
}, },
endArray () { endArray () {
var splitStr = this.mode === mode.DATE ? '-' : ':' return this._getDateValueArray(this.end, getDefaultEndValue.bind(this)())
var array = this.mode === mode.DATE ? this.dateArray : this.timeArray
var val = this.end.split(splitStr).map((val, i) => array[i].indexOf(
val))
if (val.indexOf(-1) >= 0) {
val = array.map((val) => val.length - 1)
}
return this._filterDateValue(val)
}, },
units () { units () {
switch (this.mode) { switch (this.mode) {
...@@ -366,46 +356,14 @@ export default { ...@@ -366,46 +356,14 @@ export default {
valueArray = [...val] valueArray = [...val]
break break
case mode.TIME: case mode.TIME:
var timeValTestFail = false valueArray = this._getDateValueArray(val, formatDateTime({
if (typeof this.value !== 'string') { mode: mode.TIME
timeValTestFail = true }))
} else {
val.split(':').map((val, i) => {
var valIndex = this.timeArray[i].indexOf(val)
if (valIndex === -1) {
timeValTestFail = true
}
})
}
// 处理默认值为当前时间
if (timeValTestFail) {
val = formatDateTime({
mode: mode.TIME
})
}
valueArray = val
.split(':')
.map((val, i) => this.timeArray[i].indexOf(val))
break break
case mode.DATE: case mode.DATE:
var dateValTestFail = false valueArray = this._getDateValueArray(val, formatDateTime({
if (typeof this.value !== 'string') { mode: mode.DATE
dateValTestFail = true }))
} else {
val.split('-').map((val, i) => {
var valIndex = this.dateArray[i].indexOf(val)
if (valIndex === -1) {
dateValTestFail = true
}
})
}
// 处理默认值为当前日期
if (dateValTestFail) {
val = formatDateTime({
mode: mode.DATE
})
}
valueArray = this._filterDateValue(val.split('-').map((val, i) => this.dateArray[i].indexOf(val)))
break break
} }
this.oldValueArray = [...valueArray] this.oldValueArray = [...valueArray]
...@@ -428,16 +386,28 @@ export default { ...@@ -428,16 +386,28 @@ export default {
.join('-') .join('-')
} }
}, },
_filterDateValue (val) { _getDateValueArray (valueStr, defaultValue) {
const splitStr = this.mode === mode.DATE ? '-' : ':'
const array = this.mode === mode.DATE ? this.dateArray : this.timeArray
let max = 3
switch (this.fields) { switch (this.fields) {
case fields.YEAR: case fields.YEAR:
val.length = 1 max = 1
break break
case fields.MONTH: case fields.MONTH:
val.length = 2 max = 2
break break
} }
return val const inputArray = String(valueStr).split(splitStr)
let value = []
for (let i = 0; i < max; i++) {
const val = inputArray[i]
value.push(array[i].indexOf(val))
}
if (value.indexOf(-1) >= 0) {
value = defaultValue ? this._getDateValueArray(defaultValue) : value.map(() => 0)
}
return value
}, },
_change () { _change () {
this._close() this._close()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册