@tarojs/components
Version:
Taro 组件库。
96 lines (90 loc) • 3 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-dd4b7ba3.js');
const index$1 = require('./index-47a1959e.js');
const columnCss = ".taro-picker-view-column-container{display:-ms-flexbox;display:flex;overflow:scroll;overflow-x:hidden;position:relative;-ms-flex-direction:column;flex-direction:column;-ms-flex:1;flex:1;text-align:center}.taro-picker-view-column-container::-webkit-scrollbar{display:none}";
let PickerViewColumn = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.onChange = index.createEvent(this, "onselect", 7);
this.onSelectStart = index.createEvent(this, "onselectstart", 7);
this.onSelectEnd = index.createEvent(this, "onselectend", 7);
// 初始化的选中位置
this.initialPosition = '0';
// 滑动距离上下留白区域-通过父视图和 indicator 计算而来
this.paddingVertical = 0;
this.isInit = false;
this.isMove = false;
// 滚动结束自动回到合适的位置
this.handleSelected = index$1.debounce(() => {
const childList = this.el.childNodes;
let sum = 0;
let selectedIndex = '0';
for (const index in childList) {
const item = childList[index];
const itemHeight = item.offsetHeight;
if (sum + itemHeight / 2.0 > this.el.scrollTop) {
selectedIndex = index;
break;
}
sum += itemHeight;
}
this.el.scrollTo({
top: sum,
behavior: 'smooth'
});
this.onChange.emit({
curIndex: this.col,
selectedIndex: selectedIndex
});
this.onSelectEnd.emit();
}, 500);
}
onScroll(_event) {
if (!this.isMove) {
this.isMove = true;
this.onSelectStart.emit();
}
this.handleSelected();
}
onMouseEnd() {
if (!this.isMove)
return;
this.isMove = false;
this.handleSelected();
}
onTouchEnd() {
this.isMove = false;
this.handleSelected();
}
componentDidUpdate() {
if (!this.isInit) {
this.isInit = true;
const childList = this.el.childNodes;
let idx = 0;
let sum = 0;
for (const index in childList) {
const item = childList[index];
if (this.initialPosition === index || !item || typeof item.offsetHeight !== 'number') {
break;
}
sum += item.offsetHeight;
idx++;
}
this.el.scrollTo({ top: sum });
if (idx >= childList.length) {
this.onChange.emit({
curIndex: this.col,
selectedIndex: idx - 1
});
}
}
}
render() {
const { paddingVertical = 0 } = this;
return (index.h(index.Host, { class: "taro-picker-view-column-container", style: { 'padding-top': `${paddingVertical}px`, 'padding-bottom': `${paddingVertical}px` } }));
}
get el() { return index.getElement(this); }
};
PickerViewColumn.style = columnCss;
exports.taro_picker_view_column_core = PickerViewColumn;