index.js 3.6 KB

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