picker-item.js 4.7 KB

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