UNPKG

@tarojs/components

Version:
182 lines (173 loc) 7.12 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-a7069008.js'); const index$1 = require('./index-8a70c333.js'); const index$2 = require('./index-b873e40f.js'); function convertStyle(style) { if (style) { const regex = /([\w-]*)\s*:\s*([^;]*)/g; const properties = {}; let match; while ((match = regex.exec(style))) properties[`${match[1]}`] = match[2].trim(); return properties; } } const columnCss = ".taro-picker-view-column-container{text-align:center;-ms-flex-direction:column;flex-direction:column;-ms-flex:1;flex:1;display:-ms-flexbox;display:flex;position:relative;overflow:hidden scroll}.taro-picker-view-column-container::-webkit-scrollbar{display:none}"; const 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.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); this.col = undefined; this.initialPosition = '0'; this.paddingVertical = 0; this.isInit = false; } onTouchStart() { this.onSelectStart.emit(); } onTouchEnd() { this.handleSelected(); } componentDidLoad() { this.handleChange(); } componentDidUpdate() { this.handleChange(); } handleChange() { 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; const indexCss = ".taro-picker-view-container{display:-ms-flexbox;display:flex;position:relative}.taro-picker-view-mask-container{pointer-events:none;-ms-flex-direction:column;flex-direction:column;display:-ms-flexbox;display:flex;position:absolute;inset:0}.taro-picker-view-mask-indicator{border-top:1px solid #ddd;border-bottom:1px solid #ddd;height:50px;display:-ms-flexbox;display:flex}.taro-picker-view-mask-top{background-image:-webkit-gradient(linear,left top, left bottom,from(rgba(255,255,255,.95)),to(rgba(255,255,255,.6)));background-image:linear-gradient(rgba(255,255,255,.95),rgba(255,255,255,.6));-ms-flex:1;flex:1}.taro-picker-view-mask-bottom{background:-webkit-gradient(linear,left top, left bottom,from(rgba(255,255,255,.6)),to(rgba(255,255,255,.95)));background:linear-gradient(rgba(255,255,255,.6),rgba(255,255,255,.95));-ms-flex:1;flex:1}"; const PickerView = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.onChange = index.createEvent(this, "change", 7); this.onPickStart = index.createEvent(this, "pickstart", 7); this.onPickEnd = index.createEvent(this, "pickend", 7); this.indicatorStyle = undefined; this.indicatorClass = undefined; this.value = undefined; this.maskStyle = undefined; this.maskClass = undefined; } onPropsChange() { this.handleValueChange(); } onSelect(e) { e.stopPropagation(); if (e.target.tagName !== 'TARO-PICKER-VIEW-COLUMN-CORE') return; let _curIndex = +e.detail.curIndex; let _selectedIndex = +e.detail.selectedIndex; this.value[_curIndex] = _selectedIndex; this.onChange.emit({ value: this.value }); } onSelectStart(e) { e.stopPropagation(); if (e.target.tagName !== 'TARO-PICKER-VIEW-COLUMN-CORE') return; this.onPickStart.emit(); } onPickerColEnd(e) { e.stopPropagation(); if (e.target.tagName !== 'TARO-PICKER-VIEW-COLUMN-CORE') return; this.onPickEnd.emit(); } componentDidLoad() { this.handleValueChange(); } handleValueChange() { const childList = this.el.querySelectorAll('taro-picker-view-column-core'); childList.forEach((element, index) => { var _a; element.setAttribute('col', `${index}`); let selectIndex = 0; if (!!this.value && this.value.length > index) { selectIndex = this.value[index]; } const pickerHeight = this.el.getBoundingClientRect().height; const indicatorHeight = ((_a = this.indicator) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 0; const paddingVertical = (pickerHeight - indicatorHeight) / 2.0; element.setAttribute('initial-position', `${selectIndex}`); element.setAttribute('padding-vertical', `${paddingVertical}`); }); } // 过滤非 PickerViewColumn 组件 componentDidRender() { this.el.childNodes.forEach(item => { const childEle = item; if ('TARO-PICKER-VIEW-COLUMN-CORE' !== childEle.tagName && childEle.className !== 'taro-picker-view-mask-container') { this.el.removeChild(item); } }); } render() { const indicatorCls = index$2.classnames('taro-picker-view-mask-indicator', this.indicatorClass); const maskTopCls = index$2.classnames('taro-picker-view-mask-top', this.maskClass); const maskBtmCls = index$2.classnames('taro-picker-view-mask-bottom', this.maskClass); const indicatorStyle = convertStyle(this.indicatorStyle); const maskTopStyle = convertStyle(this.maskStyle); const maskBottomStyle = convertStyle(this.maskStyle); return (index.h(index.Host, { class: "taro-picker-view-container" }, index.h("slot", null), index.h("div", { class: "taro-picker-view-mask-container" }, index.h("div", { class: maskTopCls, style: maskTopStyle }), index.h("div", { class: indicatorCls, style: indicatorStyle, ref: indicator => (this.indicator = indicator) }), index.h("div", { class: maskBtmCls, style: maskBottomStyle })))); } get el() { return index.getElement(this); } static get watchers() { return { "value": ["onPropsChange"] }; } }; PickerView.style = indexCss; exports.taro_picker_view_column_core = PickerViewColumn; exports.taro_picker_view_core = PickerView;