@tarojs/components
Version:
Taro 组件库。
92 lines (88 loc) • 2.94 kB
JavaScript
import { r as registerInstance, c as createEvent, h, g as getElement, H as Host } from './index-5bd7cbab.js';
import { d as debounce } from './index-cad8203e.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) {
registerInstance(this, hostRef);
this.onChange = createEvent(this, "onselect", 7);
this.onSelectStart = createEvent(this, "onselectstart", 7);
this.onSelectEnd = createEvent(this, "onselectend", 7);
// 初始化的选中位置
this.initialPosition = '0';
// 滑动距离上下留白区域-通过父视图和 indicator 计算而来
this.paddingVertical = 0;
this.isInit = false;
this.isMove = false;
// 滚动结束自动回到合适的位置
this.handleSelected = 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 (h(Host, { class: "taro-picker-view-column-container", style: { 'padding-top': `${paddingVertical}px`, 'padding-bottom': `${paddingVertical}px` } }));
}
get el() { return getElement(this); }
};
PickerViewColumn.style = columnCss;
export { PickerViewColumn as taro_picker_view_column_core };