riku
2025-04-27 f46786f11c5c08ead7501a82e5a71430ad69b782
miniprogram_npm/tdesign-miniprogram/picker-item/picker-item.js
@@ -9,10 +9,7 @@
import props from './props';
const { prefix } = config;
const name = `${prefix}-picker-item`;
const itemHeight = 80;
const DefaultDuration = 240;
const { windowWidth } = wx.getSystemInfoSync();
const rpx2px = (rpx) => Math.floor((windowWidth * rpx) / 750);
const range = function (num, min, max) {
    return Math.min(Math.max(num, min), max);
};
@@ -47,8 +44,15 @@
            duration: 0,
            value: '',
            curIndex: 0,
            columnIndex: 0,
            labelAlias: 'label',
            valueAlias: 'value',
        };
        this.lifetimes = {
            created() {
                this.StartY = 0;
                this.StartOffset = 0;
            },
        };
        this.methods = {
            onTouchStart(event) {
@@ -57,24 +61,25 @@
                this.setData({ duration: 0 });
            },
            onTouchMove(event) {
                const { StartY, StartOffset, itemHeight } = this;
                const { pickItemHeight } = this.data;
                const { StartY, StartOffset } = this;
                const touchDeltaY = event.touches[0].clientY - StartY;
                const deltaY = this.calculateViewDeltaY(touchDeltaY);
                const deltaY = this.calculateViewDeltaY(touchDeltaY, pickItemHeight);
                this.setData({
                    offset: range(StartOffset + deltaY, -(this.getCount() * itemHeight), 0),
                    offset: range(StartOffset + deltaY, -(this.getCount() * pickItemHeight), 0),
                    duration: DefaultDuration,
                });
            },
            onTouchEnd() {
                const { offset, labelAlias, valueAlias } = this.data;
                const { offset, labelAlias, valueAlias, columnIndex, pickItemHeight } = this.data;
                const { options } = this.properties;
                if (offset === this.StartOffset) {
                    return;
                }
                const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);
                const index = range(Math.round(-offset / pickItemHeight), 0, this.getCount() - 1);
                this.setData({
                    curIndex: index,
                    offset: -index * this.itemHeight,
                    offset: -index * pickItemHeight,
                });
                if (index === this._selectedIndex) {
                    return;
@@ -86,17 +91,17 @@
                    this._selectedLabel = (_b = options[index]) === null || _b === void 0 ? void 0 : _b[labelAlias];
                    (_c = this.$parent) === null || _c === void 0 ? void 0 : _c.triggerColumnChange({
                        index,
                        column: this.columnIndex || 0,
                        column: columnIndex,
                    });
                });
            },
            update() {
                var _a, _b;
                const { options, value, labelAlias, valueAlias } = this.data;
                const { options, value, labelAlias, valueAlias, pickItemHeight } = this.data;
                const index = options.findIndex((item) => item[valueAlias] === value);
                const selectedIndex = index > 0 ? index : 0;
                this.setData({
                    offset: -selectedIndex * this.itemHeight,
                    offset: -selectedIndex * pickItemHeight,
                    curIndex: selectedIndex,
                });
                this._selectedIndex = selectedIndex;
@@ -112,13 +117,8 @@
            },
        };
    }
    calculateViewDeltaY(touchDeltaY) {
    calculateViewDeltaY(touchDeltaY, itemHeight) {
        return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
    }
    created() {
        this.StartY = 0;
        this.StartOffset = 0;
        this.itemHeight = rpx2px(itemHeight);
    }
};
PickerItem = __decorate([