index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. observer(value) {
  29. this.setIndex(value);
  30. },
  31. },
  32. },
  33. data: {
  34. startY: 0,
  35. offset: 0,
  36. duration: 0,
  37. startOffset: 0,
  38. options: [],
  39. currentIndex: 0,
  40. },
  41. created() {
  42. const { defaultIndex, initialOptions } = this.data;
  43. this.set({
  44. currentIndex: defaultIndex,
  45. options: initialOptions,
  46. }).then(() => {
  47. this.setIndex(defaultIndex);
  48. });
  49. },
  50. methods: {
  51. getCount() {
  52. return this.data.options.length;
  53. },
  54. onTouchStart(event) {
  55. this.setData({
  56. startY: event.touches[0].clientY,
  57. startOffset: this.data.offset,
  58. duration: 0,
  59. });
  60. },
  61. onTouchMove(event) {
  62. const { data } = this;
  63. const deltaY = event.touches[0].clientY - data.startY;
  64. this.setData({
  65. offset: range(
  66. data.startOffset + deltaY,
  67. -(this.getCount() * data.itemHeight),
  68. data.itemHeight
  69. ),
  70. });
  71. },
  72. onTouchEnd() {
  73. const { data } = this;
  74. if (data.offset !== data.startOffset) {
  75. this.setData({ duration: DEFAULT_DURATION });
  76. const index = range(
  77. Math.round(-data.offset / data.itemHeight),
  78. 0,
  79. this.getCount() - 1
  80. );
  81. this.setIndex(index, true);
  82. }
  83. },
  84. onClickItem(event) {
  85. const { index } = event.currentTarget.dataset;
  86. this.setIndex(index, true);
  87. },
  88. adjustIndex(index) {
  89. const { data } = this;
  90. const count = this.getCount();
  91. index = range(index, 0, count);
  92. for (let i = index; i < count; i++) {
  93. if (!this.isDisabled(data.options[i])) return i;
  94. }
  95. for (let i = index - 1; i >= 0; i--) {
  96. if (!this.isDisabled(data.options[i])) return i;
  97. }
  98. },
  99. isDisabled(option) {
  100. return isObj(option) && option.disabled;
  101. },
  102. getOptionText(option) {
  103. const { data } = this;
  104. return isObj(option) && data.valueKey in option
  105. ? option[data.valueKey]
  106. : option;
  107. },
  108. setIndex(index, userAction) {
  109. const { data } = this;
  110. index = this.adjustIndex(index) || 0;
  111. const offset = -index * data.itemHeight;
  112. if (index !== data.currentIndex) {
  113. return this.set({ offset, currentIndex: index }).then(() => {
  114. userAction && this.$emit('change', index);
  115. });
  116. }
  117. return this.set({ offset });
  118. },
  119. setValue(value) {
  120. const { options } = this.data;
  121. for (let i = 0; i < options.length; i++) {
  122. if (this.getOptionText(options[i]) === value) {
  123. return this.setIndex(i);
  124. }
  125. }
  126. return Promise.resolve();
  127. },
  128. getValue() {
  129. const { data } = this;
  130. return data.options[data.currentIndex];
  131. },
  132. },
  133. });
  134. export default global['__wxComponents']['vant/picker-column/index']
  135. </script>
  136. <style platform="mp-weixin">
  137. @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)}
  138. </style>