1 |
- {"version":3,"sources":["../src/picker/picker-column.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAe,MAAM,qBAAqB,CAAC;AAgBlE,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,cAAc;IACtD,SAAS;;;;MAIP;IAEF,UAAU,qCAAS;IAEnB,SAAS;oBACK,YAAY;sBAGV,YAAY;MAG1B;IAEF,IAAI;;;;MAIF;IAEF,OAAO;;;;;;;MA6EL;IAEF;;;OAGG;IACH,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAIhD,OAAO;CAKR","file":"picker-column.d.ts","sourcesContent":["import { SuperComponent, wxComponent } from '../common/src/index';\nimport config from '../common/config';\nimport props from './picker-item-props';\n\nconst itemHeight = 80;\nconst DefaultDuration = 600;\n\nconst { windowWidth } = wx.getSystemInfoSync();\n\nconst rpx2px = (rpx) => Math.floor((windowWidth * rpx) / 750);\n\nconst range = function (num: number, min: number, max: number) {\n return Math.min(Math.max(num, min), max);\n};\n\n@wxComponent()\nexport default class PickerColumn extends SuperComponent {\n relations = {\n './picker': {\n type: 'parent' as 'parent',\n },\n };\n\n properties = props;\n\n observers = {\n value(this: PickerColumn) {\n this.updateColumns();\n },\n options(this: PickerColumn) {\n this.updateColumns();\n },\n };\n\n data = {\n prefix: `${config.prefix}-picker-column`,\n offset: 0, // 滚动偏移量\n duration: 0, // 滚动动画延迟\n };\n\n methods = {\n onTouchStart(event) {\n this.StartY = event.touches[0].clientY;\n this.StartOffset = this.data.offset;\n this.setData({ duration: 0 });\n },\n\n onTouchMove(event) {\n const { StartY, StartOffset, itemHeight } = this;\n\n // touch偏移增量\n const touchDeltaY = event.touches[0].clientY - StartY;\n const deltaY = this.calculateViewDeltaY(touchDeltaY);\n\n this.setData({\n offset: range(StartOffset + deltaY, -(this.getCount() * itemHeight), 0),\n duration: DefaultDuration,\n });\n },\n\n onTouchEnd() {\n const { offset } = this.data;\n const { options } = this.properties;\n\n if (offset === this.StartOffset) {\n return;\n }\n // 调整偏移量\n const index = range(Math.round(-offset / this.itemHeight), 0, this.getCount() - 1);\n this.setData({\n offset: -index * this.itemHeight,\n });\n\n if (index === this._selectedIndex) {\n return;\n }\n\n wx.nextTick(() => {\n const changeObj = {\n index,\n value: options[index],\n };\n\n this._selectedIndex = index;\n this._selectedValue = options[index];\n this.triggerEvent('change', changeObj);\n\n const picker = this.getRelationNodes('./picker')?.[0];\n if (picker) {\n picker.triggerChange({\n ...changeObj,\n column: this.columnIndex || 0,\n });\n }\n });\n },\n\n // 刷新选中状态\n updateColumns() {\n const { options, value } = this.properties;\n\n const index = options.findIndex((item) => item.value === value);\n const selectedIndex = index > 0 ? index : 0;\n\n this.setData({ offset: -selectedIndex * this.itemHeight });\n\n this._selectedIndex = selectedIndex;\n this._selectedValue = options[selectedIndex];\n },\n\n resetOrigin() {\n this.updateColumns();\n },\n\n getCount() {\n return this.data?.options?.length;\n },\n };\n\n /**\n * 将屏幕滑动距离换算为视图偏移量 模拟渐进式滚动\n * @param touchDeltaY 屏幕滑动距离\n */\n calculateViewDeltaY(touchDeltaY: number): number {\n return Math.abs(touchDeltaY) > itemHeight ? 1.2 * touchDeltaY : touchDeltaY;\n }\n\n created() {\n this.StartY = 0;\n this.StartOffset = 0;\n this.itemHeight = rpx2px(itemHeight);\n }\n}\n"]}
|