index.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { VantComponent } from '../common/component';
  2. import { isObj, range } from '../common/utils';
  3. const DEFAULT_DURATION = 200;
  4. VantComponent({
  5. classes: ['active-class'],
  6. props: {
  7. valueKey: String,
  8. className: String,
  9. itemHeight: Number,
  10. visibleItemCount: Number,
  11. initialOptions: {
  12. type: Array,
  13. value: []
  14. },
  15. defaultIndex: {
  16. type: Number,
  17. value: 0
  18. }
  19. },
  20. data: {
  21. startY: 0,
  22. offset: 0,
  23. duration: 0,
  24. startOffset: 0,
  25. options: [],
  26. currentIndex: 0
  27. },
  28. created() {
  29. const { defaultIndex, initialOptions } = this.data;
  30. this.set({
  31. currentIndex: defaultIndex,
  32. options: initialOptions
  33. }).then(() => {
  34. this.setIndex(defaultIndex);
  35. });
  36. },
  37. computed: {
  38. count() {
  39. return this.data.options.length;
  40. },
  41. baseOffset() {
  42. const { data } = this;
  43. return (data.itemHeight * (data.visibleItemCount - 1)) / 2;
  44. },
  45. wrapperStyle() {
  46. const { data } = this;
  47. return [
  48. `transition: ${data.duration}ms`,
  49. `transform: translate3d(0, ${data.offset + data.baseOffset}px, 0)`,
  50. `line-height: ${data.itemHeight}px`
  51. ].join('; ');
  52. }
  53. },
  54. watch: {
  55. defaultIndex(value) {
  56. this.setIndex(value);
  57. }
  58. },
  59. methods: {
  60. onTouchStart(event) {
  61. this.set({
  62. startY: event.touches[0].clientY,
  63. startOffset: this.data.offset,
  64. duration: 0
  65. });
  66. },
  67. onTouchMove(event) {
  68. const { data } = this;
  69. const deltaY = event.touches[0].clientY - data.startY;
  70. this.set({
  71. offset: range(data.startOffset + deltaY, -(data.count * data.itemHeight), data.itemHeight)
  72. });
  73. },
  74. onTouchEnd() {
  75. const { data } = this;
  76. if (data.offset !== data.startOffset) {
  77. this.set({
  78. duration: DEFAULT_DURATION
  79. });
  80. const index = range(Math.round(-data.offset / data.itemHeight), 0, data.count - 1);
  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. index = range(index, 0, data.count);
  91. for (let i = index; i < data.count; i++) {
  92. if (!this.isDisabled(data.options[i]))
  93. return i;
  94. }
  95. for (let i = index - 1; i >= 0; i--) {
  96. if (!this.isDisabled(data.options[i]))
  97. return i;
  98. }
  99. },
  100. isDisabled(option) {
  101. return isObj(option) && option.disabled;
  102. },
  103. getOptionText(option) {
  104. const { data } = this;
  105. return isObj(option) && data.valueKey in option
  106. ? option[data.valueKey]
  107. : option;
  108. },
  109. setIndex(index, userAction) {
  110. const { data } = this;
  111. index = this.adjustIndex(index) || 0;
  112. const offset = -index * data.itemHeight;
  113. if (index !== data.currentIndex) {
  114. return this.set({ offset, currentIndex: index }).then(() => {
  115. userAction && this.$emit('change', index);
  116. });
  117. }
  118. return this.set({ offset });
  119. },
  120. setValue(value) {
  121. const { options } = this.data;
  122. for (let i = 0; i < options.length; i++) {
  123. if (this.getOptionText(options[i]) === value) {
  124. return this.setIndex(i);
  125. }
  126. }
  127. return Promise.resolve();
  128. },
  129. getValue() {
  130. const { data } = this;
  131. return data.options[data.currentIndex];
  132. }
  133. }
  134. });