海鲜小程序

index.js 3.1KB

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