index.js 3.6 KB

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