riku
9 小时以前 8e3f3890e93d097df4be744648b9ac404d20a558
miniprogram_npm/tdesign-miniprogram/picker-item/picker-item.js
@@ -1,127 +1 @@
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    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;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { SuperComponent, wxComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
const { prefix } = config;
const name = `${prefix}-picker-item`;
const DefaultDuration = 240;
const range = function (num, min, max) {
    return Math.min(Math.max(num, min), max);
};
let PickerItem = class PickerItem extends SuperComponent {
    constructor() {
        super(...arguments);
        this.relations = {
            '../picker/picker': {
                type: 'parent',
                linked(parent) {
                    if ('keys' in parent.data) {
                        const { keys } = parent.data;
                        this.setData({
                            labelAlias: (keys === null || keys === void 0 ? void 0 : keys.label) || 'label',
                            valueAlias: (keys === null || keys === void 0 ? void 0 : keys.value) || 'value',
                        });
                    }
                },
            },
        };
        this.externalClasses = [`${prefix}-class`];
        this.properties = props;
        this.observers = {
            options() {
                this.update();
            },
        };
        this.data = {
            prefix,
            classPrefix: name,
            offset: 0,
            duration: 0,
            value: '',
            curIndex: 0,
            columnIndex: 0,
            labelAlias: 'label',
            valueAlias: 'value',
        };
        this.lifetimes = {
            created() {
                this.StartY = 0;
                this.StartOffset = 0;
            },
        };
        this.methods = {
            onTouchStart(event) {
                this.StartY = event.touches[0].clientY;
                this.StartOffset = this.data.offset;
                this.setData({ duration: 0 });
            },
            onTouchMove(event) {
                const { pickItemHeight } = this.data;
                const { StartY, StartOffset } = this;
                const touchDeltaY = event.touches[0].clientY - StartY;
                const deltaY = this.calculateViewDeltaY(touchDeltaY, pickItemHeight);
                this.setData({
                    offset: range(StartOffset + deltaY, -(this.getCount() * pickItemHeight), 0),
                    duration: DefaultDuration,
                });
            },
            onTouchEnd() {
                const { offset, labelAlias, valueAlias, columnIndex, pickItemHeight } = this.data;
                const { options } = this.properties;
                if (offset === this.StartOffset) {
                    return;
                }
                const index = range(Math.round(-offset / pickItemHeight), 0, this.getCount() - 1);
                this.setData({
                    curIndex: index,
                    offset: -index * pickItemHeight,
                });
                if (index === this._selectedIndex) {
                    return;
                }
                wx.nextTick(() => {
                    var _a, _b, _c;
                    this._selectedIndex = index;
                    this._selectedValue = (_a = options[index]) === null || _a === void 0 ? void 0 : _a[valueAlias];
                    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: columnIndex,
                    });
                });
            },
            update() {
                var _a, _b;
                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 * pickItemHeight,
                    curIndex: selectedIndex,
                });
                this._selectedIndex = selectedIndex;
                this._selectedValue = (_a = options[selectedIndex]) === null || _a === void 0 ? void 0 : _a[valueAlias];
                this._selectedLabel = (_b = options[selectedIndex]) === null || _b === void 0 ? void 0 : _b[labelAlias];
            },
            resetOrigin() {
                this.update();
            },
            getCount() {
                var _a, _b;
                return (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.length;
            },
        };
    }
    calculateViewDeltaY(touchDeltaY, itemHeight) {
        return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;
    }
};
PickerItem = __decorate([
    wxComponent()
], PickerItem);
export default PickerItem;
import{__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";const{prefix:prefix}=config,name=`${prefix}-picker-item`,ANIMATION_DURATION_BASE=300,ANIMATION_DURATION_MAX=600,INERTIA_TIME=300,INERTIA_DISTANCE=15,VIRTUAL_SCROLL_CONFIG={ENABLE_THRESHOLD:100,BUFFER_COUNT:8,THROTTLE_TIME:16,FAST_SCROLL_BUFFER:12,FAST_SCROLL_THRESHOLD:50},range=function(t,e,i){return Math.min(Math.max(t,e),i)},momentum=(t,e)=>{let i=t;return i=Math.abs(i/e)/.005*(i<0?-1:1),i};let PickerItem=class extends SuperComponent{constructor(){super(...arguments),this.relations={"../picker/picker":{type:"parent"}},this.options={multipleSlots:!0},this.externalClasses=[`${prefix}-class`],this.properties=Object.assign(Object.assign({},props),{useSlots:{type:Boolean,value:!0}}),this.observers={"options, keys"(){this.update()}},this.data={prefix:prefix,classPrefix:name,offset:0,duration:0,value:"",curIndex:0,columnIndex:0,keys:{},formatOptions:props.options.value,enableVirtualScroll:!1,visibleOptions:[],virtualStartIndex:0,virtualOffsetY:0,totalHeight:0,itemHeight:40,visibleItemCount:5,wrapperPaddingY:72},this.lifetimes={created(){this.StartY=0,this.StartOffset=0,this.startTime=0,this._animationTimer=null},detached(){this._animationTimer&&(clearInterval(this._animationTimer),this._animationTimer=null)}},this.methods={onClickItem(t){const{index:e}=t.currentTarget.dataset,{itemHeight:i}=this.data,a=range(e,0,this.getCount()-1);a!==this._selectedIndex&&this.setData({offset:-a*i,curIndex:a,duration:200}),this.updateSelected(a,!0)},onTouchStart(t){this.StartY=t.touches[0].clientY,this.StartOffset=this.data.offset,this.startTime=Date.now(),this.setData({duration:0})},onTouchMove(t){const{StartY:e,StartOffset:i}=this,{itemHeight:a}=this.data,n=t.touches[0].clientY-e,s=range(i+n,-(this.getCount()-1)*a,0);this.setData({offset:s})},onTouchEnd(t){const{offset:e,itemHeight:i,enableVirtualScroll:a,formatOptions:n}=this.data,{startTime:s}=this;if(e===this.StartOffset)return;let o=0;const r=t.changedTouches[0].clientY-this.StartY,l=Date.now()-s;l<300&&Math.abs(r)>15&&(o=momentum(r,l));const d=range(e+o,-this.getCount()*i,0),h=range(Math.round(-d/i),0,this.getCount()-1),u=-h*i,m=Math.abs(u-e)/i,c=Math.min(600,300+30*m),p=Math.abs(o)>3*i?VIRTUAL_SCROLL_CONFIG.FAST_SCROLL_BUFFER:VIRTUAL_SCROLL_CONFIG.BUFFER_COUNT;this._animationTimer&&(clearInterval(this._animationTimer),this._animationTimer=null);const f={offset:u,duration:c,curIndex:h};if(a){const t=Math.floor(Math.abs(e)/i),a=h,s=Math.min(t,a),o=Math.max(t,a),r=Math.max(0,s-p),l=Math.min(n.length,o+this.data.visibleItemCount+p);f.visibleOptions=n.slice(r,l),f.virtualStartIndex=r,f.virtualOffsetY=r*i}this.setData(f,()=>{if(a){const t=this.computeVirtualRange(u,n.length,i,!1);this.setData({visibleOptions:n.slice(t.startIndex,t.endIndex),virtualStartIndex:t.startIndex,virtualOffsetY:t.startIndex*i})}}),h!==this._selectedIndex&&this.updateSelected(h,!0)},formatOption:(t,e,i)=>"function"!=typeof i?t:t.map(t=>i(t,e)),updateSelected(t,e){var i,a,n;const{columnIndex:s,keys:o,formatOptions:r}=this.data;this._selectedIndex=t,this._selectedValue=null===(i=r[t])||void 0===i?void 0:i[null==o?void 0:o.value],this._selectedLabel=null===(a=r[t])||void 0===a?void 0:a[null==o?void 0:o.label],e&&(null===(n=this.$parent)||void 0===n||n.triggerColumnChange({index:t,column:s}))},update(){var t;const{options:e,value:i,keys:a,format:n,columnIndex:s,itemHeight:o,visibleItemCount:r}=this.data,l=this.formatOption(e,s,n),d=l.length,h=d>=VIRTUAL_SCROLL_CONFIG.ENABLE_THRESHOLD;let u=-1;if(d>500){u=null!==(t=new Map(l.map((t,e)=>[t[null==a?void 0:a.value],e])).get(i))&&void 0!==t?t:-1}else u=l.findIndex(t=>t[null==a?void 0:a.value]===i);const m=u>0?u:0,c={formatOptions:l,offset:-m*o,curIndex:m,enableVirtualScroll:h,totalHeight:d*o,wrapperPaddingY:(r-1)/2*o};if(h){const t=this.computeVirtualRange(-m*o,d,o);c.visibleOptions=l.slice(t.startIndex,t.endIndex),c.virtualStartIndex=t.startIndex,c.virtualOffsetY=t.startIndex*o}else c.visibleOptions=l,c.virtualStartIndex=0,c.virtualOffsetY=0;this.setData(c,()=>{this.updateSelected(m,!1)})},computeVirtualRange(t,e,i,a=!1){const n=Math.abs(t),{BUFFER_COUNT:s,FAST_SCROLL_BUFFER:o}=VIRTUAL_SCROLL_CONFIG,{visibleItemCount:r}=this.data,l=a?o:s,d=Math.floor(n/i);return{startIndex:Math.max(0,d-l),endIndex:Math.min(e,d+r+l)}},updateVisibleOptions(t,e=!1){const{formatOptions:i,itemHeight:a,enableVirtualScroll:n}=this.data;if(!n)return;const s=void 0!==t?t:this.data.offset,o=this.computeVirtualRange(s,i.length,a,e);o.startIndex===this.data.virtualStartIndex&&o.endIndex===this.data.virtualStartIndex+this.data.visibleOptions.length||this.setData({visibleOptions:i.slice(o.startIndex,o.endIndex),virtualStartIndex:o.startIndex,virtualOffsetY:o.startIndex*a})},getCount(){var t,e;return null===(e=null===(t=this.data)||void 0===t?void 0:t.options)||void 0===e?void 0:e.length},getCurrentSelected(){var t,e;const{offset:i,itemHeight:a,formatOptions:n,keys:s}=this.data,o=Math.max(0,Math.min(Math.round(-i/a),this.getCount()-1));return{index:o,value:null===(t=n[o])||void 0===t?void 0:t[null==s?void 0:s.value],label:null===(e=n[o])||void 0===e?void 0:e[null==s?void 0:s.label]}}}}};PickerItem=__decorate([wxComponent()],PickerItem);export default PickerItem;