picker-item.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 itemHeight = 80;
  13. const DefaultDuration = 240;
  14. const { windowWidth } = wx.getSystemInfoSync();
  15. const rpx2px = (rpx) => Math.floor((windowWidth * rpx) / 750);
  16. const range = function (num, min, max) {
  17. return Math.min(Math.max(num, min), max);
  18. };
  19. let PickerItem = class PickerItem extends SuperComponent {
  20. constructor() {
  21. super(...arguments);
  22. this.relations = {
  23. '../picker/picker': {
  24. type: 'parent',
  25. },
  26. };
  27. this.properties = props;
  28. this.observers = {
  29. options() {
  30. this.update();
  31. },
  32. };
  33. this.data = {
  34. classPrefix: name,
  35. offset: 0,
  36. duration: 0,
  37. value: '',
  38. curIndex: 0,
  39. };
  40. this.methods = {
  41. onTouchStart(event) {
  42. this.StartY = event.touches[0].clientY;
  43. this.StartOffset = this.data.offset;
  44. this.setData({ duration: 0 });
  45. },
  46. onTouchMove(event) {
  47. const { StartY, StartOffset, itemHeight } = this;
  48. const touchDeltaY = event.touches[0].clientY - StartY;
  49. const deltaY = this.calculateViewDeltaY(touchDeltaY);
  50. this.setData({
  51. offset: range(StartOffset + deltaY, -(this.getCount() * itemHeight), 0),
  52. duration: DefaultDuration,
  53. });
  54. },
  55. onTouchEnd() {
  56. const { offset } = this.data;
  57. const { options } = this.properties;
  58. if (offset === this.StartOffset) {
  59. return;
  60. }
  61. const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);
  62. this.setData({
  63. curIndex: index,
  64. offset: -index * this.itemHeight,
  65. });
  66. if (index === this._selectedIndex) {
  67. return;
  68. }
  69. wx.nextTick(() => {
  70. var _a, _b, _c;
  71. this._selectedIndex = index;
  72. this._selectedValue = (_a = options[index]) === null || _a === void 0 ? void 0 : _a.value;
  73. this._selectedLabel = (_b = options[index]) === null || _b === void 0 ? void 0 : _b.label;
  74. (_c = this.$parent) === null || _c === void 0 ? void 0 : _c.triggerColumnChange({
  75. index,
  76. column: this.columnIndex || 0,
  77. });
  78. });
  79. },
  80. update() {
  81. var _a, _b;
  82. const { options, value } = this.data;
  83. const index = options.findIndex((item) => item.value === value);
  84. const selectedIndex = index > 0 ? index : 0;
  85. this.setData({
  86. offset: -selectedIndex * this.itemHeight,
  87. curIndex: selectedIndex,
  88. });
  89. this._selectedIndex = selectedIndex;
  90. this._selectedValue = (_a = options[selectedIndex]) === null || _a === void 0 ? void 0 : _a.value;
  91. this._selectedLabel = (_b = options[selectedIndex]) === null || _b === void 0 ? void 0 : _b.label;
  92. },
  93. resetOrigin() {
  94. this.update();
  95. },
  96. getCount() {
  97. var _a, _b;
  98. return (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.length;
  99. },
  100. };
  101. }
  102. calculateViewDeltaY(touchDeltaY) {
  103. return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
  104. }
  105. created() {
  106. this.StartY = 0;
  107. this.StartOffset = 0;
  108. this.itemHeight = rpx2px(itemHeight);
  109. }
  110. };
  111. PickerItem = __decorate([
  112. wxComponent()
  113. ], PickerItem);
  114. export default PickerItem;