index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <uni-shadow-root class="vant-picker-column-index"><view class="van-picker-column custom-class" :style="'height: '+(itemHeight * visibleItemCount)+'px'" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  3. <view :style="'transition: transform '+(duration)+'ms; line-height: '+(itemHeight)+'px; transform: translate3d(0, '+(offset + (itemHeight * (visibleItemCount - 1)) / 2)+'px, 0)'">
  4. <view v-for="(option,index) in (options)" :key="option.index" :data-index="index" :style="'height: '+(itemHeight)+'px'" :class="'van-ellipsis van-picker-column__item '+(option && option.disabled ? 'van-picker-column__item--disabled' : '')+' '+(index === currentIndex ? 'van-picker-column__item--selected active-class' : '')" @click="onClickItem">{{ getOptionText(option, valueKey) }}</view>
  5. </view>
  6. </view></uni-shadow-root>
  7. </template>
  8. <wxs src="./index.wxs" module="getOptionText"></wxs>
  9. <script>
  10. global['__wxRoute'] = 'vant/picker-column/index'
  11. import { VantComponent } from '../common/component';
  12. import { isObj, range } from '../common/utils';
  13. const DEFAULT_DURATION = 200;
  14. VantComponent({
  15. classes: ['active-class'],
  16. props: {
  17. valueKey: String,
  18. className: String,
  19. itemHeight: Number,
  20. visibleItemCount: Number,
  21. initialOptions: {
  22. type: Array,
  23. value: []
  24. },
  25. defaultIndex: {
  26. type: Number,
  27. value: 0
  28. }
  29. },
  30. data: {
  31. startY: 0,
  32. offset: 0,
  33. duration: 0,
  34. startOffset: 0,
  35. options: [],
  36. currentIndex: 0
  37. },
  38. created() {
  39. const { defaultIndex, initialOptions } = this.data;
  40. this.set({
  41. currentIndex: defaultIndex,
  42. options: initialOptions
  43. }).then(() => {
  44. this.setIndex(defaultIndex);
  45. });
  46. },
  47. watch: {
  48. defaultIndex(value) {
  49. this.setIndex(value);
  50. }
  51. },
  52. methods: {
  53. getCount() {
  54. return this.data.options.length;
  55. },
  56. onTouchStart(event) {
  57. this.setData({
  58. startY: event.touches[0].clientY,
  59. startOffset: this.data.offset,
  60. duration: 0
  61. });
  62. },
  63. onTouchMove(event) {
  64. const { data } = this;
  65. const deltaY = event.touches[0].clientY - data.startY;
  66. this.setData({
  67. offset: range(data.startOffset + deltaY, -(this.getCount() * data.itemHeight), data.itemHeight)
  68. });
  69. },
  70. onTouchEnd() {
  71. const { data } = this;
  72. if (data.offset !== data.startOffset) {
  73. this.setData({ duration: DEFAULT_DURATION });
  74. const index = range(Math.round(-data.offset / data.itemHeight), 0, this.getCount() - 1);
  75. this.setIndex(index, true);
  76. }
  77. },
  78. onClickItem(event) {
  79. const { index } = event.currentTarget.dataset;
  80. this.setIndex(index, true);
  81. },
  82. adjustIndex(index) {
  83. const { data } = this;
  84. const count = this.getCount();
  85. index = range(index, 0, count);
  86. for (let i = index; i < count; i++) {
  87. if (!this.isDisabled(data.options[i]))
  88. return i;
  89. }
  90. for (let i = index - 1; i >= 0; i--) {
  91. if (!this.isDisabled(data.options[i]))
  92. return i;
  93. }
  94. },
  95. isDisabled(option) {
  96. return isObj(option) && option.disabled;
  97. },
  98. getOptionText(option) {
  99. const { data } = this;
  100. return isObj(option) && data.valueKey in option
  101. ? option[data.valueKey]
  102. : option;
  103. },
  104. setIndex(index, userAction) {
  105. const { data } = this;
  106. index = this.adjustIndex(index) || 0;
  107. const offset = -index * data.itemHeight;
  108. if (index !== data.currentIndex) {
  109. return this.set({ offset, currentIndex: index }).then(() => {
  110. userAction && this.$emit('change', index);
  111. });
  112. }
  113. return this.set({ offset });
  114. },
  115. setValue(value) {
  116. const { options } = this.data;
  117. for (let i = 0; i < options.length; i++) {
  118. if (this.getOptionText(options[i]) === value) {
  119. return this.setIndex(i);
  120. }
  121. }
  122. return Promise.resolve();
  123. },
  124. getValue() {
  125. const { data } = this;
  126. return data.options[data.currentIndex];
  127. }
  128. }
  129. });
  130. export default global['__wxComponents']['vant/picker-column/index']
  131. </script>
  132. <style platform="mp-weixin">
  133. @import '../common/index.css';.van-picker-column{overflow:hidden;text-align:center;color:#000;color:var(--picker-option-text-color,#000);font-size:16px;font-size:var(--picker-option-font-size,16px)}.van-picker-column__item{padding:0 5px}.van-picker-column__item--selected{font-weight:500;font-weight:var(--font-weight-bold,500);color:#323233;color:var(--picker-option-selected-text-color,#323233)}.van-picker-column__item--disabled{opacity:.3;opacity:var(--picker-option-disabled-opacity,.3)}
  134. </style>