picker-item.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.options = {
  34. multipleSlots: true,
  35. };
  36. this.externalClasses = [`${prefix}-class`];
  37. this.properties = props;
  38. this.observers = {
  39. options() {
  40. this.update();
  41. },
  42. };
  43. this.data = {
  44. prefix,
  45. classPrefix: name,
  46. offset: 0,
  47. duration: 0,
  48. value: '',
  49. curIndex: 0,
  50. columnIndex: 0,
  51. labelAlias: 'label',
  52. valueAlias: 'value',
  53. };
  54. this.lifetimes = {
  55. created() {
  56. this.StartY = 0;
  57. this.StartOffset = 0;
  58. },
  59. };
  60. this.methods = {
  61. onTouchStart(event) {
  62. this.StartY = event.touches[0].clientY;
  63. this.StartOffset = this.data.offset;
  64. this.setData({ duration: 0 });
  65. },
  66. onTouchMove(event) {
  67. const { pickItemHeight } = this.data;
  68. const { StartY, StartOffset } = this;
  69. const touchDeltaY = event.touches[0].clientY - StartY;
  70. const deltaY = this.calculateViewDeltaY(touchDeltaY, pickItemHeight);
  71. this.setData({
  72. offset: range(StartOffset + deltaY, -(this.getCount() * pickItemHeight), 0),
  73. duration: DefaultDuration,
  74. });
  75. },
  76. onTouchEnd() {
  77. const { offset, labelAlias, valueAlias, columnIndex, pickItemHeight } = this.data;
  78. const { options } = this.properties;
  79. if (offset === this.StartOffset) {
  80. return;
  81. }
  82. const index = range(Math.round(-offset / pickItemHeight), 0, this.getCount() - 1);
  83. this.setData({
  84. curIndex: index,
  85. offset: -index * pickItemHeight,
  86. });
  87. if (index === this._selectedIndex) {
  88. return;
  89. }
  90. wx.nextTick(() => {
  91. var _a, _b, _c;
  92. this._selectedIndex = index;
  93. this._selectedValue = (_a = options[index]) === null || _a === void 0 ? void 0 : _a[valueAlias];
  94. this._selectedLabel = (_b = options[index]) === null || _b === void 0 ? void 0 : _b[labelAlias];
  95. (_c = this.$parent) === null || _c === void 0 ? void 0 : _c.triggerColumnChange({
  96. index,
  97. column: columnIndex,
  98. });
  99. });
  100. },
  101. update() {
  102. var _a, _b;
  103. const { options, value, labelAlias, valueAlias, pickItemHeight } = this.data;
  104. const index = options.findIndex((item) => item[valueAlias] === value);
  105. const selectedIndex = index > 0 ? index : 0;
  106. this.setData({
  107. offset: -selectedIndex * pickItemHeight,
  108. curIndex: selectedIndex,
  109. });
  110. this._selectedIndex = selectedIndex;
  111. this._selectedValue = (_a = options[selectedIndex]) === null || _a === void 0 ? void 0 : _a[valueAlias];
  112. this._selectedLabel = (_b = options[selectedIndex]) === null || _b === void 0 ? void 0 : _b[labelAlias];
  113. },
  114. resetOrigin() {
  115. this.update();
  116. },
  117. getCount() {
  118. var _a, _b;
  119. return (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.length;
  120. },
  121. };
  122. }
  123. calculateViewDeltaY(touchDeltaY, itemHeight) {
  124. return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
  125. }
  126. };
  127. PickerItem = __decorate([
  128. wxComponent()
  129. ], PickerItem);
  130. export default PickerItem;