提交 59c88b61 编写于 作者: X xiaoyucoding

fix: 修复 picker 未设置 value 值时报错的问题

上级 cf84fd78
......@@ -48,7 +48,7 @@ export default {
},
value: {
type: [Number, String, Array],
default: ''
default: 0
},
mode: {
type: String,
......
<template>
<uni-picker @touchmove.prevent>
<transition
name="uni-fade">
<transition name="uni-fade">
<div
v-show="visible"
class="uni-mask"
......@@ -40,6 +39,10 @@
</uni-picker>
</template>
<script>
import {
formatDateTime
} from 'uni-shared'
const mode = {
SELECTOR: 'selector',
MULTISELECTOR: 'multiSelector',
......@@ -151,7 +154,8 @@ export default {
return val
case mode.TIME:
return this.timeArray
case mode.DATE: {
case mode.DATE:
{
let dateArray = this.dateArray
switch (this.fields) {
case fields.YEAR:
......@@ -166,11 +170,13 @@ export default {
},
startArray () {
var splitStr = this.mode === mode.DATE ? '-' : ':'
return this.start.split(splitStr).map((val, i) => (this.mode === mode.DATE ? this.dateArray : this.timeArray)[i].indexOf(val))
return this.start.split(splitStr).map((val, i) => (this.mode === mode.DATE ? this.dateArray : this.timeArray)[i].indexOf(
val))
},
endArray () {
var splitStr = this.mode === mode.DATE ? '-' : ':'
return this.end.split(splitStr).map((val, i) => (this.mode === mode.DATE ? this.dateArray : this.timeArray)[i].indexOf(val))
return this.end.split(splitStr).map((val, i) => (this.mode === mode.DATE ? this.dateArray : this.timeArray)[i].indexOf(
val))
},
units () {
switch (this.mode) {
......@@ -211,7 +217,7 @@ export default {
created () {
this._createTime()
this._createDate()
this._setValue(this.value)
this._setValue()
this.$watch('value', this._setValue)
this.$watch('mode', this._setValue)
},
......@@ -270,9 +276,21 @@ export default {
valueArray = [...val]
break
case mode.TIME:
// 处理默认值为当前时间
if (this.value === 0) {
val = formatDateTime({
mode: mode.TIME
})
}
valueArray = val.split(':').map((val, i) => this.timeArray[i].indexOf(val))
break
case mode.DATE:
// 处理默认值为当前日期
if (this.value === 0) {
val = formatDateTime({
mode: mode.DATE
})
}
valueArray = val.split('-').map((val, i) => this.dateArray[i].indexOf(val))
break
}
......@@ -316,12 +334,12 @@ export default {
}
</script>
<style>
uni-picker {
uni-picker {
display: block;
box-sizing: border-box;
}
}
uni-picker .uni-picker {
uni-picker .uni-picker {
position: fixed;
left: 0;
bottom: 0;
......@@ -331,17 +349,17 @@ uni-picker .uni-picker {
width: 100%;
background-color: #efeff4;
transition: transform 0.3s;
}
}
uni-picker .uni-picker.uni-picker_toggle {
uni-picker .uni-picker.uni-picker_toggle {
transform: translate(0, 0);
}
}
uni-picker .uni-picker * {
uni-picker .uni-picker * {
box-sizing: border-box;
}
}
uni-picker .uni-picker {
uni-picker .uni-picker {
position: fixed;
left: 0;
bottom: 0;
......@@ -352,16 +370,17 @@ uni-picker .uni-picker {
background-color: #efeff4;
transition: transform 0.3s;
transition: transform 0.3s;
}
}
uni-picker .uni-picker-content {
uni-picker .uni-picker-content {
position: relative;
display: block;
width: 100%;
height: 238px;
background-color: white;
}
uni-picker .uni-picker-item {
}
uni-picker .uni-picker-item {
padding: 0;
height: 34px;
line-height: 34px;
......@@ -370,16 +389,18 @@ uni-picker .uni-picker-item {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
uni-picker .uni-picker-header {
}
uni-picker .uni-picker-header {
display: block;
position: relative;
text-align: center;
width: 100%;
height: 45px;
background-color: #fff;
}
uni-picker .uni-picker-header:after {
}
uni-picker .uni-picker-header:after {
content: "";
position: absolute;
left: 0;
......@@ -391,8 +412,9 @@ uni-picker .uni-picker-header:after {
color: #e5e5e5;
transform-origin: 0 100%;
transform: scaleY(0.5);
}
uni-picker .uni-picker-action {
}
uni-picker .uni-picker-action {
display: block;
max-width: 50%;
top: 0;
......@@ -402,16 +424,19 @@ uni-picker .uni-picker-action {
font-size: 17px;
line-height: 45px;
overflow: hidden;
}
uni-picker .uni-picker-action.uni-picker-action-cancel {
}
uni-picker .uni-picker-action.uni-picker-action-cancel {
float: left;
color: #888;
}
uni-picker .uni-picker-action.uni-picker-action-confirm {
}
uni-picker .uni-picker-action.uni-picker-action-confirm {
float: right;
color: #007aff;
}
/* .uni-picker {
}
/* .uni-picker {
position: relative;
}
.uni-picker-units {
......
const _toString = Object.prototype.toString
const hasOwnProperty = Object.prototype.hasOwnProperty
const _completeValue = value => {
return value > 9 ? value : ('0' + value)
}
export function isFn (fn) {
return typeof fn === 'function'
}
......@@ -35,3 +39,14 @@ export function getLen (str = '') {
/* eslint-disable no-control-regex */
return ('' + str).replace(/[^\x00-\xff]/g, '**').length
}
export function formatDateTime ({
date = new Date(),
mode = 'date'
}) {
if (mode === 'time') {
return _completeValue(date.getHours()) + ':' + _completeValue(date.getMinutes())
} else {
return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate())
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册