selector-picker.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="selector-picker picker-item"
  3. :style="{ height: height }">
  4. <picker-view :value="pickerValue"
  5. :indicator-style="indicatorStyle"
  6. :style="{ height: height }"
  7. @change="handleChange">
  8. <picker-view-column>
  9. <view v-for="(item, i) in list"
  10. :class="[
  11. 'lb-picker-column',
  12. item[props.value] || item === selectValue
  13. ? 'lb-picker-column-active'
  14. : ''
  15. ]"
  16. :key="i"
  17. :style="{ height: columnHeight, lineHeight: columnHeight }">
  18. <view class="lb-picker-column-label">
  19. {{ item[props.label] || item }}
  20. </view>
  21. </view>
  22. </picker-view-column>
  23. </picker-view>
  24. </view>
  25. </template>
  26. <script>
  27. import { getIndicatorHeight, isObject } from '../utils.js'
  28. const indicatorHeight = getIndicatorHeight()
  29. export default {
  30. props: {
  31. value: [String, Number],
  32. list: Array,
  33. props: Object,
  34. visible: Boolean,
  35. height: String
  36. },
  37. data () {
  38. return {
  39. pickerValue: [],
  40. selectValue: '',
  41. columnHeight: indicatorHeight + 'px',
  42. indicatorStyle: `height: ${indicatorHeight}px`
  43. }
  44. },
  45. created () {
  46. this.init('init')
  47. },
  48. methods: {
  49. init (changeType) {
  50. if (this.list && this.list.length) {
  51. let index = this.list.findIndex(item => {
  52. return isObject(item)
  53. ? item[this.props.value] === this.value
  54. : item === this.value
  55. })
  56. index = index > -1 ? index : 0
  57. const listItem = this.list[index]
  58. this.pickerValue = [index]
  59. this.selectValue = isObject(listItem)
  60. ? listItem[this.props.value]
  61. : listItem
  62. this.$emit('change', {
  63. value: this.selectValue,
  64. item: listItem,
  65. index: index,
  66. change: changeType
  67. })
  68. }
  69. },
  70. handleChange (item) {
  71. const index = item.detail.value[0] || 0
  72. const listItem = this.list[index]
  73. this.selectValue = isObject(listItem)
  74. ? listItem[this.props.value]
  75. : listItem
  76. this.pickerValue = item.detail.value
  77. this.$emit('change', {
  78. value: this.selectValue,
  79. item: listItem,
  80. index: index,
  81. change: 'scroll'
  82. })
  83. }
  84. },
  85. watch: {
  86. list () {
  87. this.init('list')
  88. },
  89. value (newVal) {
  90. this.init('value')
  91. }
  92. }
  93. }
  94. </script>
  95. <style lang="scss" scoped>
  96. @import "../style/picker-item.scss";
  97. </style>