合伙人运营小程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

picker-item.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-picker-item`;
  12. const DefaultDuration = 240;
  13. const range = function (num, min, max) {
  14. return Math.min(Math.max(num, min), max);
  15. };
  16. let PickerItem = class PickerItem extends SuperComponent {
  17. constructor() {
  18. super(...arguments);
  19. this.relations = {
  20. '../picker/picker': {
  21. type: 'parent',
  22. linked(parent) {
  23. if ('keys' in parent.data) {
  24. const { keys } = parent.data;
  25. this.setData({
  26. labelAlias: (keys === null || keys === void 0 ? void 0 : keys.label) || 'label',
  27. valueAlias: (keys === null || keys === void 0 ? void 0 : keys.value) || 'value',
  28. });
  29. }
  30. },
  31. },
  32. };
  33. this.externalClasses = [`${prefix}-class`];
  34. this.properties = props;
  35. this.observers = {
  36. options() {
  37. this.update();
  38. },
  39. };
  40. this.data = {
  41. prefix,
  42. classPrefix: name,
  43. offset: 0,
  44. duration: 0,
  45. value: '',
  46. curIndex: 0,
  47. columnIndex: 0,
  48. labelAlias: 'label',
  49. valueAlias: 'value',
  50. };
  51. this.lifetimes = {
  52. created() {
  53. this.StartY = 0;
  54. this.StartOffset = 0;
  55. },
  56. };
  57. this.methods = {
  58. onTouchStart(event) {
  59. this.StartY = event.touches[0].clientY;
  60. this.StartOffset = this.data.offset;
  61. this.setData({ duration: 0 });
  62. },
  63. onTouchMove(event) {
  64. const { pickItemHeight } = this.data;
  65. const { StartY, StartOffset } = this;
  66. const touchDeltaY = event.touches[0].clientY - StartY;
  67. const deltaY = this.calculateViewDeltaY(touchDeltaY, pickItemHeight);
  68. this.setData({
  69. offset: range(StartOffset + deltaY, -(this.getCount() * pickItemHeight), 0),
  70. duration: DefaultDuration,
  71. });
  72. },
  73. onTouchEnd() {
  74. const { offset, labelAlias, valueAlias, columnIndex, pickItemHeight } = this.data;
  75. const { options } = this.properties;
  76. if (offset === this.StartOffset) {
  77. return;
  78. }
  79. const index = range(Math.round(-offset / pickItemHeight), 0, this.getCount() - 1);
  80. this.setData({
  81. curIndex: index,
  82. offset: -index * pickItemHeight,
  83. });
  84. if (index === this._selectedIndex) {
  85. return;
  86. }
  87. wx.nextTick(() => {
  88. var _a, _b, _c;
  89. this._selectedIndex = index;
  90. this._selectedValue = (_a = options[index]) === null || _a === void 0 ? void 0 : _a[valueAlias];
  91. this._selectedLabel = (_b = options[index]) === null || _b === void 0 ? void 0 : _b[labelAlias];
  92. (_c = this.$parent) === null || _c === void 0 ? void 0 : _c.triggerColumnChange({
  93. index,
  94. column: columnIndex,
  95. });
  96. });
  97. },
  98. update() {
  99. var _a, _b;
  100. const { options, value, labelAlias, valueAlias, pickItemHeight } = this.data;
  101. const index = options.findIndex((item) => item[valueAlias] === value);
  102. const selectedIndex = index > 0 ? index : 0;
  103. this.setData({
  104. offset: -selectedIndex * pickItemHeight,
  105. curIndex: selectedIndex,
  106. });
  107. this._selectedIndex = selectedIndex;
  108. this._selectedValue = (_a = options[selectedIndex]) === null || _a === void 0 ? void 0 : _a[valueAlias];
  109. this._selectedLabel = (_b = options[selectedIndex]) === null || _b === void 0 ? void 0 : _b[labelAlias];
  110. },
  111. resetOrigin() {
  112. this.update();
  113. },
  114. getCount() {
  115. var _a, _b;
  116. return (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.length;
  117. },
  118. };
  119. }
  120. calculateViewDeltaY(touchDeltaY, itemHeight) {
  121. return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
  122. }
  123. };
  124. PickerItem = __decorate([
  125. wxComponent()
  126. ], PickerItem);
  127. export default PickerItem;