picker.vue 11.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<template>
  <uni-picker @touchmove.prevent>
3
    <transition name="uni-fade">
4 5 6
      <div
        v-show="visible"
        class="uni-mask"
7
        @click="_cancel"/>
fxy060608's avatar
fxy060608 已提交
8
    </transition>
9 10
    <div
      :class="{'uni-picker-toggle':visible}"
fxy060608's avatar
fxy060608 已提交
11
      class="uni-picker">
12 13
      <div
        class="uni-picker-header"
fxy060608's avatar
fxy060608 已提交
14
        @click.stop>
15 16
        <div
          class="uni-picker-action uni-picker-action-cancel"
fxy060608's avatar
fxy060608 已提交
17
          @click="_cancel">取消</div>
18 19
        <div
          class="uni-picker-action uni-picker-action-confirm"
fxy060608's avatar
fxy060608 已提交
20 21
          @click="_change">确定</div>
      </div>
22 23 24
      <v-uni-picker-view
        v-if="visible"
        :value.sync="valueArray"
fxy060608's avatar
fxy060608 已提交
25
        class="uni-picker-content">
26 27
        <v-uni-picker-view-column
          v-for="(range,index0) in rangeArray"
fxy060608's avatar
fxy060608 已提交
28
          :key="index0">
29 30 31
          <div
            v-for="(item,index) in range"
            :key="index"
32 33
            class="uni-picker-item"
          >{{ typeof item==='object'?item[rangeKey]||'':item }}{{ units[index0]||'' }}</div>
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38
        </v-uni-picker-view-column>
      </v-uni-picker-view>
      <!-- 第二种时间单位展示方式-暂时不用这种 -->
      <!-- <div v-if="units.length" class="uni-picker-units">
        <div v-for="(item,index) in units" :key="index">{{item}}</div>
39
      </div>-->
fxy060608's avatar
fxy060608 已提交
40 41 42 43
    </div>
  </uni-picker>
</template>
<script>
44 45 46 47
import {
  formatDateTime
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
const mode = {
  SELECTOR: 'selector',
  MULTISELECTOR: 'multiSelector',
  TIME: 'time',
  DATE: 'date',
  REGION: 'region'
}
const fields = {
  YEAR: 'year',
  MONTH: 'month',
  DAY: 'day'
}
export default {
  name: 'Picker',
  props: {
    pageId: {
      type: Number,
      default: 0
    },
    range: {
      type: Array,
      default () {
        return []
      }
    },
    rangeKey: {
      type: String,
      default: ''
    },
    value: {
      type: [Number, String, Array],
      default: 0
    },
    mode: {
      type: String,
      default: mode.SELECTOR
    },
    fields: {
      type: String,
      default: fields.DAY
    },
    start: {
      type: String,
      default () {
        if (this.mode === mode.TIME) {
          return '00:00'
        }
        if (this.mode === mode.DATE) {
          let year = (new Date()).getFullYear() - 150
          switch (this.fields) {
            case fields.YEAR:
              return year
            case fields.MONTH:
              return year + '-01'
            case fields.DAY:
              return year + '-01-01'
          }
        }
        return ''
      }
    },
    end: {
      type: String,
      default () {
        if (this.mode === mode.TIME) {
          return '23:59'
        }
        if (this.mode === mode.DATE) {
          let year = (new Date()).getFullYear() + 150
          switch (this.fields) {
            case fields.YEAR:
              return year
            case fields.MONTH:
              return year + '-12'
            case fields.DAY:
              return year + '-12-31'
          }
        }
        return ''
      }
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    customItem: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      timeArray: [],
      dateArray: [],
146 147
      valueArray: [],
      oldValueArray: []
fxy060608's avatar
fxy060608 已提交
148 149 150 151 152 153 154 155 156 157 158 159
    }
  },
  computed: {
    rangeArray () {
      var val = this.range
      switch (this.mode) {
        case mode.SELECTOR:
          return [val]
        case mode.MULTISELECTOR:
          return val
        case mode.TIME:
          return this.timeArray
160 161
        case mode.DATE:
        {
fxy060608's avatar
fxy060608 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175
          let dateArray = this.dateArray
          switch (this.fields) {
            case fields.YEAR:
              return [dateArray[0]]
            case fields.MONTH:
              return [dateArray[0], dateArray[1]]
            case fields.DAY:
              return [dateArray[0], dateArray[1], dateArray[2]]
          }
        }
      }
    },
    startArray () {
      var splitStr = this.mode === mode.DATE ? '-' : ':'
176 177
      var array = this.mode === mode.DATE ? this.dateArray : this.timeArray
      var val = this.start.split(splitStr).map((val, i) => array[i].indexOf(
178
        val))
179 180 181 182
      if (val.indexOf(-1) >= 0) {
        val = array.map(() => 0)
      }
      return val
fxy060608's avatar
fxy060608 已提交
183 184 185
    },
    endArray () {
      var splitStr = this.mode === mode.DATE ? '-' : ':'
186 187
      var array = this.mode === mode.DATE ? this.dateArray : this.timeArray
      var val = this.end.split(splitStr).map((val, i) => array[i].indexOf(
188
        val))
189 190 191 192
      if (val.indexOf(-1) >= 0) {
        val = array.map((val) => val.length - 1)
      }
      return val
fxy060608's avatar
fxy060608 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    },
    units () {
      switch (this.mode) {
        case mode.DATE:
          return ['', '', '']
        case mode.TIME:
          return ['', '']
        default:
          return []
      }
    }
  },
  watch: {
    valueArray (val) {
      if (this.mode === mode.TIME || this.mode === mode.DATE) {
        let getValue = this.mode === mode.TIME ? this._getTimeValue : this._getDateValue
        let valueArray = this.valueArray
        let startArray = this.startArray
        let endArray = this.endArray
郭胜强 已提交
212 213 214 215 216 217 218 219 220 221
        if (this.mode === mode.DATE) {
          const dateArray = this.dateArray
          let max = dateArray[2].length
          let day = dateArray[2][valueArray[2]]
          let realDay = new Date(`${dateArray[0][valueArray[0]]}/${dateArray[1][valueArray[1]]}/${day}`).getDate()
          day = Number(day)
          if (realDay < day) {
            valueArray[2] -= realDay + max - day
          }
        }
fxy060608's avatar
fxy060608 已提交
222 223 224 225 226 227
        if (getValue(valueArray) < getValue(startArray)) {
          this._cloneArray(valueArray, startArray)
        } else if (getValue(valueArray) > getValue(endArray)) {
          this._cloneArray(valueArray, endArray)
        }
      }
228 229 230 231 232 233 234 235 236 237 238 239
      val.forEach((value, column) => {
        if (value !== this.oldValueArray[column]) {
          this.oldValueArray[column] = value
          if (this.mode === mode.MULTISELECTOR) {
            // 触发 View 层 columnchange 事件
            UniServiceJSBridge.publishHandler(this.pageId + '-picker-columnchange', {
              column,
              value
            }, this.pageId)
          }
        }
      })
fxy060608's avatar
fxy060608 已提交
240 241 242 243 244
    }
  },
  created () {
    this._createTime()
    this._createDate()
245
    this._setValue()
fxy060608's avatar
fxy060608 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    this.$watch('value', this._setValue)
    this.$watch('mode', this._setValue)
  },
  methods: {
    _createTime () {
      var hours = []
      var minutes = []
      hours.splice(0, hours.length)
      for (let i = 0; i < 24; i++) {
        hours.push((i < 10 ? '0' : '') + i)
      }
      minutes.splice(0, minutes.length)
      for (let i = 0; i < 60; i++) {
        minutes.push((i < 10 ? '0' : '') + i)
      }
      this.timeArray.push(hours, minutes)
    },
    _createDate () {
      var years = []
      var year = (new Date()).getFullYear()
      for (let i = year - 150, end = year + 150; i <= end; i++) {
        years.push(String(i))
      }
      var months = []
      for (let i = 1; i <= 12; i++) {
        months.push((i < 10 ? '0' : '') + i)
      }
      var days = []
      for (let i = 1; i <= 31; i++) {
        days.push((i < 10 ? '0' : '') + i)
      }
      this.dateArray.push(years, months, days)
    },
    _getTimeValue (val) {
      return val[0] * 60 + val[1]
    },
    _getDateValue (val) {
      return val[0] * 366 + (val[1] || 0 * 31) + (val[2] || 0)
    },
    /**
286 287
			 * 将右侧数组值同步到左侧(交集部分)
			 */
fxy060608's avatar
fxy060608 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    _cloneArray (val1, val2) {
      for (let i = 0; i < val1.length && i < val2.length; i++) {
        val1[i] = val2[i]
      }
    },
    _setValue () {
      var val = this.value
      var valueArray
      switch (this.mode) {
        case mode.SELECTOR:
          valueArray = [val]
          break
        case mode.MULTISELECTOR:
          valueArray = [...val]
          break
        case mode.TIME:
304 305 306 307 308 309
          // 处理默认值为当前时间
          if (this.value === 0) {
            val = formatDateTime({
              mode: mode.TIME
            })
          }
fxy060608's avatar
fxy060608 已提交
310 311 312
          valueArray = val.split(':').map((val, i) => this.timeArray[i].indexOf(val))
          break
        case mode.DATE:
313 314 315 316 317 318
          // 处理默认值为当前日期
          if (this.value === 0) {
            val = formatDateTime({
              mode: mode.DATE
            })
          }
fxy060608's avatar
fxy060608 已提交
319 320 321
          valueArray = val.split('-').map((val, i) => this.dateArray[i].indexOf(val))
          break
      }
322 323
      this.oldValueArray = [...valueArray]
      this.valueArray = [...valueArray]
fxy060608's avatar
fxy060608 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    },
    _getValue () {
      var val = this.valueArray
      switch (this.mode) {
        case mode.SELECTOR:
          return val[0]
        case mode.MULTISELECTOR:
          return val.map(val => val)
        case mode.TIME:
          return this.valueArray.map((val, i) => this.timeArray[i][val]).join(':')
        case mode.DATE:
          return this.valueArray.map((val, i) => this.dateArray[i][val]).join('-')
      }
    },
    _change () {
      this.$emit('close')
      // 触发 View 层 change 事件
      UniServiceJSBridge.publishHandler(this.pageId + '-picker-change', {
        value: this._getValue()
      }, this.pageId)
    },
    _cancel () {
      // 通知父组件修改 visible
      this.$emit('close')
      // 触发 View 层 cancel 事件
      UniServiceJSBridge.publishHandler(this.pageId + '-picker-cancel', {}, this.pageId)
    }
  }
}
</script>
<style>
355 356 357 358
uni-picker {
  display: block;
  box-sizing: border-box;
}
fxy060608's avatar
fxy060608 已提交
359

360 361 362
uni-picker .uni-picker * {
  box-sizing: border-box;
}
fxy060608's avatar
fxy060608 已提交
363

364 365 366 367 368 369 370 371 372
uni-picker .uni-picker {
  position: fixed;
  left: 0;
  bottom: 0;
  transform: translate(0, 100%);
  backface-visibility: hidden;
  z-index: 999;
  width: 100%;
  background-color: #efeff4;
373 374 375 376 377 378 379 380
  visibility: hidden;
  transition-property: transform,visibility;
  transition-duration: 0.3s, 0.3s;
}

uni-picker .uni-picker.uni-picker-toggle {
  visibility: visible;
  transform: translate(0, 0);
381
}
fxy060608's avatar
fxy060608 已提交
382

383 384 385 386 387 388 389
uni-picker .uni-picker-content {
  position: relative;
  display: block;
  width: 100%;
  height: 238px;
  background-color: white;
}
390

391 392 393 394 395 396 397 398 399 400
uni-picker .uni-picker-item {
  padding: 0;
  height: 34px;
  line-height: 34px;
  text-align: center;
  color: #000;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
401

402 403 404 405 406 407 408 409
uni-picker .uni-picker-header {
  display: block;
  position: relative;
  text-align: center;
  width: 100%;
  height: 45px;
  background-color: #fff;
}
410

411 412 413 414 415 416 417 418 419 420 421 422 423
uni-picker .uni-picker-header:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  height: 1px;
  clear: both;
  border-bottom: 1px solid #e5e5e5;
  color: #e5e5e5;
  transform-origin: 0 100%;
  transform: scaleY(0.5);
}
424

425 426 427 428 429 430 431 432 433 434 435
uni-picker .uni-picker-action {
  display: block;
  max-width: 50%;
  top: 0;
  height: 100%;
  box-sizing: border-box;
  padding: 0 14px;
  font-size: 17px;
  line-height: 45px;
  overflow: hidden;
}
436

437 438 439 440
uni-picker .uni-picker-action.uni-picker-action-cancel {
  float: left;
  color: #888;
}
441

442 443 444 445
uni-picker .uni-picker-action.uni-picker-action-confirm {
  float: right;
  color: #007aff;
}
446

447
/* .uni-picker {
fxy060608's avatar
fxy060608 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  position: relative;
}
.uni-picker-units {
  position: absolute;
  display: flex;
  width: 100%;
  line-height: 16px;
  font-size: 14px;
  top: 50%;
  margin-top: 22.5px;
  transform: translateY(-50%);
  overflow: hidden;
  color: #666666;
  pointer-events: none;
}
.uni-picker-units > div {
  flex: 1;
  text-align: center;
  transform: translateX(2em);
} */
468
</style>