index.vue 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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="wrapperStyle">
  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. computed: {
  48. count() {
  49. return this.data.options.length;
  50. },
  51. baseOffset() {
  52. const { data } = this;
  53. return (data.itemHeight * (data.visibleItemCount - 1)) / 2;
  54. },
  55. wrapperStyle() {
  56. const { data } = this;
  57. return [
  58. `transition: ${data.duration}ms`,
  59. `transform: translate3d(0, ${data.offset + data.baseOffset}px, 0)`,
  60. `line-height: ${data.itemHeight}px`
  61. ].join('; ');
  62. }
  63. },
  64. watch: {
  65. defaultIndex(value) {
  66. this.setIndex(value);
  67. }
  68. },
  69. methods: {
  70. onTouchStart(event) {
  71. this.set({
  72. startY: event.touches[0].clientY,
  73. startOffset: this.data.offset,
  74. duration: 0
  75. });
  76. },
  77. onTouchMove(event) {
  78. const { data } = this;
  79. const deltaY = event.touches[0].clientY - data.startY;
  80. this.set({
  81. offset: range(data.startOffset + deltaY, -(data.count * data.itemHeight), data.itemHeight)
  82. });
  83. },
  84. onTouchEnd() {
  85. const { data } = this;
  86. if (data.offset !== data.startOffset) {
  87. this.set({
  88. duration: DEFAULT_DURATION
  89. });
  90. const index = range(Math.round(-data.offset / data.itemHeight), 0, data.count - 1);
  91. this.setIndex(index, true);
  92. }
  93. },
  94. onClickItem(event) {
  95. const { index } = event.currentTarget.dataset;
  96. this.setIndex(index, true);
  97. },
  98. adjustIndex(index) {
  99. const { data } = this;
  100. index = range(index, 0, data.count);
  101. for (let i = index; i < data.count; i++) {
  102. if (!this.isDisabled(data.options[i]))
  103. return i;
  104. }
  105. for (let i = index - 1; i >= 0; i--) {
  106. if (!this.isDisabled(data.options[i]))
  107. return i;
  108. }
  109. },
  110. isDisabled(option) {
  111. return isObj(option) && option.disabled;
  112. },
  113. getOptionText(option) {
  114. const { data } = this;
  115. return isObj(option) && data.valueKey in option
  116. ? option[data.valueKey]
  117. : option;
  118. },
  119. setIndex(index, userAction) {
  120. const { data } = this;
  121. index = this.adjustIndex(index) || 0;
  122. const offset = -index * data.itemHeight;
  123. if (index !== data.currentIndex) {
  124. return this.set({ offset, currentIndex: index }).then(() => {
  125. userAction && this.$emit('change', index);
  126. });
  127. }
  128. return this.set({ offset });
  129. },
  130. setValue(value) {
  131. const { options } = this.data;
  132. for (let i = 0; i < options.length; i++) {
  133. if (this.getOptionText(options[i]) === value) {
  134. return this.setIndex(i);
  135. }
  136. }
  137. return Promise.resolve();
  138. },
  139. getValue() {
  140. const { data } = this;
  141. return data.options[data.currentIndex];
  142. }
  143. }
  144. });
  145. export default global['__wxComponents']['vant/picker-column/index']
  146. </script>
  147. <style platform="mp-weixin">
  148. @import '../common/index.css';.van-picker-column{overflow:hidden;font-size:16px;color:#999;text-align:center}.van-picker-column__item{padding:0 5px}.van-picker-column__item--selected{font-weight:500;color:#333}.van-picker-column__item--disabled{opacity:.3}
  149. </style>