picker-item.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 './picker-item-props';
  10. const itemHeight = 80;
  11. const DefaultDuration = 240;
  12. const { windowWidth } = wx.getSystemInfoSync();
  13. const rpx2px = (rpx) => Math.floor((windowWidth * rpx) / 750);
  14. const range = function (num, min, max) {
  15. return Math.min(Math.max(num, min), max);
  16. };
  17. let PickerItem = class PickerItem extends SuperComponent {
  18. constructor() {
  19. super(...arguments);
  20. this.relations = {
  21. './picker': {
  22. type: 'parent',
  23. linked(parent) {
  24. this.parent = parent;
  25. },
  26. },
  27. };
  28. this.properties = props;
  29. this.observers = {
  30. options() {
  31. this.update();
  32. },
  33. };
  34. this.data = {
  35. prefix: `${config.prefix}-picker-item`,
  36. offset: 0,
  37. duration: 0,
  38. value: '',
  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. offset: -index * this.itemHeight,
  64. });
  65. if (index === this._selectedIndex) {
  66. return;
  67. }
  68. wx.nextTick(() => {
  69. var _a, _b, _c;
  70. this._selectedIndex = index;
  71. this._selectedValue = (_a = options[index]) === null || _a === void 0 ? void 0 : _a.value;
  72. this._selectedLabel = (_b = options[index]) === null || _b === void 0 ? void 0 : _b.label;
  73. (_c = this.parent) === null || _c === void 0 ? void 0 : _c.triggerColumnChange({
  74. index,
  75. column: this.columnIndex || 0,
  76. });
  77. });
  78. },
  79. update() {
  80. var _a, _b;
  81. const { options, value } = this.data;
  82. const index = options.findIndex((item) => item.value === value);
  83. const selectedIndex = index > 0 ? index : 0;
  84. this.setData({ offset: -selectedIndex * this.itemHeight });
  85. this._selectedIndex = selectedIndex;
  86. this._selectedValue = (_a = options[selectedIndex]) === null || _a === void 0 ? void 0 : _a.value;
  87. this._selectedLabel = (_b = options[selectedIndex]) === null || _b === void 0 ? void 0 : _b.label;
  88. },
  89. resetOrigin() {
  90. this.update();
  91. },
  92. getCount() {
  93. var _a, _b;
  94. return (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.length;
  95. },
  96. };
  97. }
  98. calculateViewDeltaY(touchDeltaY) {
  99. return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
  100. }
  101. created() {
  102. this.StartY = 0;
  103. this.StartOffset = 0;
  104. this.itemHeight = rpx2px(itemHeight);
  105. }
  106. };
  107. PickerItem = __decorate([
  108. wxComponent()
  109. ], PickerItem);
  110. export default PickerItem;