index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view
  3. class="van-picker-column custom-class"
  4. :style="'height: ' + itemHeight * visibleItemCount + 'px'"
  5. @touchstart="onTouchStart"
  6. @touchmove.stop.prevent="onTouchMove"
  7. @touchend="onTouchEnd"
  8. @touchcancel="onTouchEnd"
  9. >
  10. <view
  11. :style="
  12. 'transition: transform ' +
  13. duration +
  14. 'ms; line-height: ' +
  15. itemHeight +
  16. 'px; transform: translate3d(0, ' +
  17. (offset + (itemHeight * (visibleItemCount - 1)) / 2) +
  18. 'px, 0)'
  19. "
  20. >
  21. <view
  22. :data-index="index"
  23. :style="'height: ' + itemHeight + 'px'"
  24. :class="
  25. 'van-ellipsis van-picker-column__item ' +
  26. (option && option.disabled ? 'van-picker-column__item--disabled' : '') +
  27. ' ' +
  28. (index === currentIndex ? 'van-picker-column__item--selected active-class' : '')
  29. "
  30. @tap="onClickItem"
  31. v-for="(option, index) in options"
  32. :key="index"
  33. >
  34. {{ getOptionText(option, valueKey) }}
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script module="getOptionText" lang="wxs" src="@/miniprogram_npm/@vant/weapp/picker-column/index.wxs"></script>
  40. <script>
  41. 'use strict';
  42. Object.defineProperty(exports, '__esModule', {
  43. value: true
  44. });
  45. var component_1 = require('../common/component');
  46. var utils_1 = require('../common/utils');
  47. var DEFAULT_DURATION = 200;
  48. component_1.VantComponent({
  49. classes: ['active-class'],
  50. props: {
  51. valueKey: String,
  52. className: String,
  53. itemHeight: Number,
  54. visibleItemCount: Number,
  55. initialOptions: {
  56. type: Array,
  57. value: []
  58. },
  59. defaultIndex: {
  60. type: Number,
  61. value: 0,
  62. observer: function (value) {
  63. this.setIndex(value);
  64. }
  65. }
  66. },
  67. data: {
  68. startY: 0,
  69. offset: 0,
  70. duration: 0,
  71. startOffset: 0,
  72. options: [],
  73. currentIndex: 0
  74. },
  75. created: function () {
  76. var that = this;
  77. var _a = this;
  78. var defaultIndex = _a.defaultIndex;
  79. var initialOptions = _a.initialOptions;
  80. this.set({
  81. currentIndex: defaultIndex,
  82. options: initialOptions
  83. }).then(function () {
  84. that.setIndex(defaultIndex);
  85. });
  86. },
  87. methods: {
  88. getCount: function () {
  89. return this.options.length;
  90. },
  91. onTouchStart: function (event) {
  92. this.setData({
  93. startY: event.touches[0].clientY,
  94. startOffset: this.offset,
  95. duration: 0
  96. });
  97. },
  98. onTouchMove: function (event) {
  99. var data = this;
  100. var deltaY = event.touches[0].clientY - data.startY;
  101. this.setData({
  102. offset: utils_1.range(data.startOffset + deltaY, -(this.getCount() * data.itemHeight), data.itemHeight)
  103. });
  104. },
  105. onTouchEnd: function () {
  106. var data = this;
  107. if (data.offset !== data.startOffset) {
  108. this.setData({
  109. duration: DEFAULT_DURATION
  110. });
  111. var index = utils_1.range(Math.round(-data.offset / data.itemHeight), 0, this.getCount() - 1);
  112. this.setIndex(index, true);
  113. }
  114. },
  115. onClickItem: function (event) {
  116. var index = event.currentTarget.dataset.index;
  117. this.setIndex(index, true);
  118. },
  119. adjustIndex: function (index) {
  120. var data = this;
  121. var count = this.getCount();
  122. index = utils_1.range(index, 0, count);
  123. for (var i = index; i < count; i++) {
  124. if (!this.isDisabled(data.options[i])) {
  125. return i;
  126. }
  127. }
  128. for (var i = index - 1; i >= 0; i--) {
  129. if (!this.isDisabled(data.options[i])) {
  130. return i;
  131. }
  132. }
  133. },
  134. isDisabled: function (option) {
  135. return utils_1.isObj(option) && option.disabled;
  136. },
  137. getOptionText: function (option) {
  138. var data = this;
  139. return utils_1.isObj(option) && data.valueKey in option ? option[data.valueKey] : option;
  140. },
  141. setIndex: function (index, userAction) {
  142. var that = this;
  143. var data = this;
  144. index = this.adjustIndex(index) || 0;
  145. var offset = -index * data.itemHeight;
  146. if (index !== data.currentIndex) {
  147. return this.set({
  148. offset: offset,
  149. currentIndex: index
  150. }).then(function () {
  151. if (userAction) {
  152. that.$emit('change', index);
  153. }
  154. });
  155. }
  156. return this.set({
  157. offset: offset
  158. });
  159. },
  160. setValue: function (value) {
  161. var options = this.options;
  162. for (var i = 0; i < options.length; i++) {
  163. if (this.getOptionText(options[i]) === value) {
  164. return this.setIndex(i);
  165. }
  166. }
  167. return Promise.resolve();
  168. },
  169. getValue: function () {
  170. var data = this;
  171. return data.options[data.currentIndex];
  172. }
  173. }
  174. });
  175. </script>
  176. <style>
  177. @import './index.css';
  178. </style>