index.js 3.1 KB

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