@tarojs/components-react 4.1.12-beta.6 → 4.1.12-beta.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/image/index.js +5 -3
- package/dist/components/image/index.js.map +1 -1
- package/dist/components/map/MapContext.js +628 -0
- package/dist/components/map/MapContext.js.map +1 -0
- package/dist/components/map/MapCustomCallout.js +91 -0
- package/dist/components/map/MapCustomCallout.js.map +1 -0
- package/dist/components/map/common.js +4 -0
- package/dist/components/map/common.js.map +1 -0
- package/dist/components/map/createMapContext.js +34 -0
- package/dist/components/map/createMapContext.js.map +1 -0
- package/dist/components/map/handler.js +50 -0
- package/dist/components/map/handler.js.map +1 -0
- package/dist/components/map/index.js +329 -0
- package/dist/components/map/index.js.map +1 -0
- package/dist/components/picker/index.js +129 -38
- package/dist/components/picker/index.js.map +1 -1
- package/dist/components/picker/picker-group-legacy.js +936 -0
- package/dist/components/picker/picker-group-legacy.js.map +1 -0
- package/dist/components/picker/picker-group-slider.js +1046 -0
- package/dist/components/picker/picker-group-slider.js.map +1 -0
- package/dist/components/picker/picker-group.js +38 -638
- package/dist/components/picker/picker-group.js.map +1 -1
- package/dist/components/scroll-view/index.js +80 -36
- package/dist/components/scroll-view/index.js.map +1 -1
- package/dist/components/switch/index.js +125 -0
- package/dist/components/switch/index.js.map +1 -0
- package/dist/components/switch/style/index.scss.js +4 -0
- package/dist/components/switch/style/index.scss.js.map +1 -0
- package/dist/contexts/ScrollElementContext.js +6 -0
- package/dist/contexts/ScrollElementContext.js.map +1 -0
- package/dist/index.css +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/original/components/image/index.js +5 -3
- package/dist/original/components/image/index.js.map +1 -1
- package/dist/original/components/image/style/index.scss +5 -1
- package/dist/original/components/map/MapContext.js +628 -0
- package/dist/original/components/map/MapContext.js.map +1 -0
- package/dist/original/components/map/MapCustomCallout.js +91 -0
- package/dist/original/components/map/MapCustomCallout.js.map +1 -0
- package/dist/original/components/map/common.js +4 -0
- package/dist/original/components/map/common.js.map +1 -0
- package/dist/original/components/map/createMapContext.js +34 -0
- package/dist/original/components/map/createMapContext.js.map +1 -0
- package/dist/original/components/map/handler.js +50 -0
- package/dist/original/components/map/handler.js.map +1 -0
- package/dist/original/components/map/index.js +329 -0
- package/dist/original/components/map/index.js.map +1 -0
- package/dist/original/components/picker/index.js +129 -38
- package/dist/original/components/picker/index.js.map +1 -1
- package/dist/original/components/picker/picker-group-legacy.js +936 -0
- package/dist/original/components/picker/picker-group-legacy.js.map +1 -0
- package/dist/original/components/picker/picker-group-slider.js +1046 -0
- package/dist/original/components/picker/picker-group-slider.js.map +1 -0
- package/dist/original/components/picker/picker-group.js +38 -638
- package/dist/original/components/picker/picker-group.js.map +1 -1
- package/dist/original/components/picker/style/index.scss +9 -8
- package/dist/original/components/scroll-view/index.js +80 -36
- package/dist/original/components/scroll-view/index.js.map +1 -1
- package/dist/original/components/switch/index.js +125 -0
- package/dist/original/components/switch/index.js.map +1 -0
- package/dist/original/components/switch/style/index.scss +35 -0
- package/dist/original/contexts/ScrollElementContext.js +6 -0
- package/dist/original/contexts/ScrollElementContext.js.map +1 -0
- package/dist/original/index.js +5 -1
- package/dist/original/index.js.map +1 -1
- package/dist/solid/components/image/index.js +5 -3
- package/dist/solid/components/image/index.js.map +1 -1
- package/dist/solid/components/picker/index.js +142 -45
- package/dist/solid/components/picker/index.js.map +1 -1
- package/dist/solid/components/picker/picker-group-legacy.js +952 -0
- package/dist/solid/components/picker/picker-group-legacy.js.map +1 -0
- package/dist/solid/components/picker/picker-group-slider.js +1071 -0
- package/dist/solid/components/picker/picker-group-slider.js.map +1 -0
- package/dist/solid/components/picker/picker-group.js +34 -655
- package/dist/solid/components/picker/picker-group.js.map +1 -1
- package/dist/solid/components/scroll-view/index.js +84 -40
- package/dist/solid/components/scroll-view/index.js.map +1 -1
- package/dist/solid/contexts/ScrollElementContext.js +6 -0
- package/dist/solid/contexts/ScrollElementContext.js.map +1 -0
- package/dist/solid/index.css +1 -1
- package/package.json +8 -6
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import { View, ScrollView } from '@tarojs/components';
|
|
2
|
+
import Taro from '@tarojs/taro';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
function usePickerItemScrollIntoView() {
|
|
7
|
+
const [scrollIntoView, setScrollIntoView] = React.useState('');
|
|
8
|
+
const pulseRef = React.useRef(0);
|
|
9
|
+
const scrollToItemId = React.useCallback(itemId => {
|
|
10
|
+
pulseRef.current += 1;
|
|
11
|
+
const token = pulseRef.current;
|
|
12
|
+
setScrollIntoView('');
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
if (pulseRef.current !== token) return;
|
|
15
|
+
setScrollIntoView(itemId);
|
|
16
|
+
}, 0);
|
|
17
|
+
}, []);
|
|
18
|
+
return [scrollIntoView, scrollToItemId];
|
|
19
|
+
}
|
|
20
|
+
// 定义常量
|
|
21
|
+
const PICKER_LINE_HEIGHT = 34; // px
|
|
22
|
+
const PICKER_VISIBLE_ITEMS = 7; // 可见行数
|
|
23
|
+
const PICKER_BLANK_ITEMS = 3; // 空白行数
|
|
24
|
+
const getIndicatorStyle = lineColor => {
|
|
25
|
+
return {
|
|
26
|
+
borderTopColor: lineColor,
|
|
27
|
+
borderBottomColor: lineColor
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
// 大屏方案版本要求
|
|
31
|
+
const MIN_DESIGN_APP_VERSION = 16;
|
|
32
|
+
// 判断是否启用测量值缩放适配(true=启用, false=使用系统侧缩放)
|
|
33
|
+
const resolveUseMeasuredScale = res => {
|
|
34
|
+
// H5/weapp 不参与大屏系数
|
|
35
|
+
if (process.env.TARO_ENV === 'h5' || process.env.TARO_ENV === 'weapp') {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
const designAppVersionRaw = res.designAppVersion;
|
|
39
|
+
const designAppVersionMajor = designAppVersionRaw != null ? parseInt(String(designAppVersionRaw).trim(), 10) : Number.NaN;
|
|
40
|
+
if (!Number.isFinite(designAppVersionMajor) || designAppVersionMajor < MIN_DESIGN_APP_VERSION) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const platform = String(res.platform || '').toLowerCase();
|
|
44
|
+
if (platform === 'harmony' || platform === 'android' || platform === 'ios') {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
};
|
|
49
|
+
// 辅助函数:计算 lengthScaleRatio
|
|
50
|
+
const calculateLengthScaleRatio = res => {
|
|
51
|
+
let lengthScaleRatio = res === null || res === void 0 ? void 0 : res.lengthScaleRatio;
|
|
52
|
+
if (lengthScaleRatio == null || lengthScaleRatio === 0) {
|
|
53
|
+
console.warn('Taro.getSystemInfo: lengthScaleRatio 不存在,使用计算值');
|
|
54
|
+
lengthScaleRatio = 1;
|
|
55
|
+
if (res.windowWidth < 320) {
|
|
56
|
+
lengthScaleRatio = res.windowWidth / 320;
|
|
57
|
+
} else if (res.windowWidth >= 400 && res.windowWidth < 600) {
|
|
58
|
+
lengthScaleRatio = res.windowWidth / 400;
|
|
59
|
+
}
|
|
60
|
+
const shortSide = res.windowWidth < res.windowHeight ? res.windowWidth : res.windowHeight;
|
|
61
|
+
const isBigScreen = shortSide >= 600;
|
|
62
|
+
if (isBigScreen) {
|
|
63
|
+
lengthScaleRatio = shortSide / 720;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return lengthScaleRatio;
|
|
67
|
+
};
|
|
68
|
+
// 辅助函数:获取系统信息的 lengthScaleRatio 并设置 targetScrollTop
|
|
69
|
+
const setTargetScrollTopWithScale = function (setTargetScrollTop, baseValue, randomOffset) {
|
|
70
|
+
let lengthScaleRatio = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
|
|
71
|
+
// H5 和 weapp 不参与放大计算,直接使用 baseValue
|
|
72
|
+
if (process.env.TARO_ENV === 'h5' || process.env.TARO_ENV === 'weapp') {
|
|
73
|
+
const finalValue = randomOffset !== undefined ? baseValue + randomOffset : baseValue;
|
|
74
|
+
setTargetScrollTop(finalValue);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (process.env.TARO_PLATFORM === 'harmony') {
|
|
78
|
+
const scaledValue = baseValue;
|
|
79
|
+
const finalValue = randomOffset !== undefined ? scaledValue + randomOffset : scaledValue;
|
|
80
|
+
setTargetScrollTop(finalValue);
|
|
81
|
+
} else {
|
|
82
|
+
const scaledValue = baseValue * lengthScaleRatio;
|
|
83
|
+
const finalValue = randomOffset !== undefined ? scaledValue + randomOffset : scaledValue;
|
|
84
|
+
setTargetScrollTop(finalValue);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
// 根据 scrollTop 计算选中索引
|
|
88
|
+
// 系统侧缩放模式:scrollHeight 已被系统缩放,直接相除即可
|
|
89
|
+
// 自行缩放模式:需要除以 ratio 换算(harmony 特殊处理,ratio 已内嵌在 itemHeight 中)
|
|
90
|
+
const getSelectedIndex = function (scrollTop, itemHeight) {
|
|
91
|
+
let lengthScaleRatio = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|
92
|
+
let useMeasuredScale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
93
|
+
if (!useMeasuredScale || process.env.TARO_PLATFORM === 'harmony') {
|
|
94
|
+
return Math.round(scrollTop / itemHeight);
|
|
95
|
+
}
|
|
96
|
+
return Math.round(scrollTop / lengthScaleRatio / itemHeight);
|
|
97
|
+
};
|
|
98
|
+
// 计算单项高度(返回设计稿值)
|
|
99
|
+
const calculateItemHeight = function (scrollView, lengthScaleRatio) {
|
|
100
|
+
let useMeasuredScale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
101
|
+
if (process.env.TARO_PLATFORM === 'harmony') {
|
|
102
|
+
return useMeasuredScale ? PICKER_LINE_HEIGHT * lengthScaleRatio : PICKER_LINE_HEIGHT;
|
|
103
|
+
}
|
|
104
|
+
if (scrollView && (scrollView === null || scrollView === void 0 ? void 0 : scrollView.scrollHeight)) {
|
|
105
|
+
return useMeasuredScale ? scrollView.scrollHeight / lengthScaleRatio / scrollView.childNodes.length : scrollView.scrollHeight / scrollView.childNodes.length;
|
|
106
|
+
}
|
|
107
|
+
console.warn('Height measurement anomaly');
|
|
108
|
+
return PICKER_LINE_HEIGHT;
|
|
109
|
+
};
|
|
110
|
+
function PickerGroupBasic(props) {
|
|
111
|
+
const {
|
|
112
|
+
range = [],
|
|
113
|
+
rangeKey,
|
|
114
|
+
columnId,
|
|
115
|
+
updateIndex,
|
|
116
|
+
onColumnChange,
|
|
117
|
+
selectedIndex = 0,
|
|
118
|
+
// 使用selectedIndex参数,默认为0
|
|
119
|
+
colors = {},
|
|
120
|
+
enableClickItemScroll = true
|
|
121
|
+
} = props;
|
|
122
|
+
const indicatorStyle = colors.lineColor ? getIndicatorStyle(colors.lineColor) : null;
|
|
123
|
+
const [targetScrollTop, setTargetScrollTop] = React.useState(0);
|
|
124
|
+
const [scrollIntoView, scrollToItemId] = usePickerItemScrollIntoView();
|
|
125
|
+
const scrollViewRef = React.useRef(null);
|
|
126
|
+
// 使用selectedIndex初始化当前索引
|
|
127
|
+
const [currentIndex, setCurrentIndex] = React.useState(selectedIndex);
|
|
128
|
+
// 触摸状态用于优化用户体验
|
|
129
|
+
const isTouchingRef = React.useRef(false);
|
|
130
|
+
const lengthScaleRatioRef = React.useRef(1);
|
|
131
|
+
const useMeasuredScaleRef = React.useRef(false);
|
|
132
|
+
const itemHeightRef = React.useRef(PICKER_LINE_HEIGHT);
|
|
133
|
+
// 初始化时计算 lengthScaleRatio 并判定缩放模式
|
|
134
|
+
React.useEffect(() => {
|
|
135
|
+
Taro.getSystemInfo({
|
|
136
|
+
success: res => {
|
|
137
|
+
lengthScaleRatioRef.current = calculateLengthScaleRatio(res);
|
|
138
|
+
useMeasuredScaleRef.current = resolveUseMeasuredScale(res);
|
|
139
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
140
|
+
},
|
|
141
|
+
fail: () => {
|
|
142
|
+
lengthScaleRatioRef.current = 1;
|
|
143
|
+
useMeasuredScaleRef.current = false;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}, []);
|
|
147
|
+
React.useEffect(() => {
|
|
148
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
149
|
+
}, [range.length]); // 只在range长度变化时重新计算
|
|
150
|
+
// 滚动到指定索引(供程序化对齐与无障碍步进复用)
|
|
151
|
+
const scrollToIndex = React.useCallback(function (index) {
|
|
152
|
+
let withRandomOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
153
|
+
const baseValue = index * itemHeightRef.current;
|
|
154
|
+
const randomOffset = withRandomOffset ? Math.random() * 0.001 : undefined;
|
|
155
|
+
setTargetScrollTopWithScale(setTargetScrollTop, baseValue, randomOffset, lengthScaleRatioRef.current);
|
|
156
|
+
}, []);
|
|
157
|
+
// props 的 selectedIndex 变化:滚至对应项
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
if (scrollViewRef.current && range.length > 0 && !isTouchingRef.current) {
|
|
160
|
+
scrollToIndex(selectedIndex);
|
|
161
|
+
setCurrentIndex(selectedIndex);
|
|
162
|
+
}
|
|
163
|
+
}, [selectedIndex, range, scrollToIndex]);
|
|
164
|
+
// 是否处于归中状态
|
|
165
|
+
const isCenterTimerId = React.useRef(null);
|
|
166
|
+
// 滚动静止后归中并同步选中
|
|
167
|
+
const handleScrollEnd = () => {
|
|
168
|
+
if (!scrollViewRef.current) return;
|
|
169
|
+
if (isCenterTimerId.current) {
|
|
170
|
+
clearTimeout(isCenterTimerId.current);
|
|
171
|
+
isCenterTimerId.current = null;
|
|
172
|
+
}
|
|
173
|
+
// 100ms 内无新滚动则归中并提交索引
|
|
174
|
+
isCenterTimerId.current = setTimeout(() => {
|
|
175
|
+
if (!scrollViewRef.current) return;
|
|
176
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
177
|
+
const newIndex = getSelectedIndex(scrollTop, itemHeightRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
178
|
+
isTouchingRef.current = false;
|
|
179
|
+
scrollToIndex(newIndex, true);
|
|
180
|
+
updateIndex(newIndex, columnId);
|
|
181
|
+
onColumnChange === null || onColumnChange === void 0 ? void 0 : onColumnChange({
|
|
182
|
+
columnId,
|
|
183
|
+
index: newIndex
|
|
184
|
+
});
|
|
185
|
+
isCenterTimerId.current = null;
|
|
186
|
+
}, 100);
|
|
187
|
+
};
|
|
188
|
+
// 滚动中:按 scrollTop 更新高亮索引
|
|
189
|
+
const handleScroll = () => {
|
|
190
|
+
if (!scrollViewRef.current) return;
|
|
191
|
+
if (isCenterTimerId.current) {
|
|
192
|
+
clearTimeout(isCenterTimerId.current);
|
|
193
|
+
isCenterTimerId.current = null;
|
|
194
|
+
}
|
|
195
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
196
|
+
const ih = itemHeightRef.current;
|
|
197
|
+
const newIndex = getSelectedIndex(scrollTop, ih, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
198
|
+
if (newIndex !== currentIndex) {
|
|
199
|
+
setCurrentIndex(newIndex);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
// 无障碍:整列作单 slider,转子 increment/decrement 切相邻项
|
|
203
|
+
const clampIndex = index => Math.max(0, Math.min(index, range.length - 1));
|
|
204
|
+
// 切换后主动播报当前项:焦点停在 slider 容器上,aria 属性更新读屏不会自动重读,需主动触发
|
|
205
|
+
const announceIndex = index => {
|
|
206
|
+
const item = range[index];
|
|
207
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
208
|
+
const text = content != null ? String(content) : '';
|
|
209
|
+
if (!text) return;
|
|
210
|
+
const fn = Taro.accessibilityAnnounce;
|
|
211
|
+
if (typeof fn !== 'function') return;
|
|
212
|
+
fn({
|
|
213
|
+
announce: text,
|
|
214
|
+
delay: 300
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
const commitIndexByA11y = nextIndex => {
|
|
218
|
+
const clamped = clampIndex(nextIndex);
|
|
219
|
+
if (clamped === currentIndex) return;
|
|
220
|
+
isTouchingRef.current = false;
|
|
221
|
+
setCurrentIndex(clamped);
|
|
222
|
+
scrollToIndex(clamped, true);
|
|
223
|
+
updateIndex(clamped, columnId);
|
|
224
|
+
onColumnChange === null || onColumnChange === void 0 ? void 0 : onColumnChange({
|
|
225
|
+
columnId,
|
|
226
|
+
index: clamped
|
|
227
|
+
});
|
|
228
|
+
announceIndex(clamped);
|
|
229
|
+
};
|
|
230
|
+
const handleAriaAction = e => {
|
|
231
|
+
var _a;
|
|
232
|
+
const actionName = (_a = e === null || e === void 0 ? void 0 : e.detail) === null || _a === void 0 ? void 0 : _a.actionName;
|
|
233
|
+
if (actionName === 'increment') {
|
|
234
|
+
commitIndexByA11y(currentIndex + 1);
|
|
235
|
+
} else if (actionName === 'decrement') {
|
|
236
|
+
commitIndexByA11y(currentIndex - 1);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
// 无障碍 label:当前项值(用 rangeKey 取显示值),仅 string 或 number(转 string)挂载,对象等其它类型不挂载
|
|
240
|
+
const currentItem = range[currentIndex];
|
|
241
|
+
const currentValue = rangeKey && currentItem && typeof currentItem === 'object' ? currentItem[rangeKey] : currentItem;
|
|
242
|
+
const ariaLabel = typeof currentValue === 'string' ? currentValue : typeof currentValue === 'number' ? String(currentValue) : undefined;
|
|
243
|
+
// 渲染选项
|
|
244
|
+
const pickerItem = range.map((item, index) => {
|
|
245
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
246
|
+
return /*#__PURE__*/jsx(View, {
|
|
247
|
+
id: `picker-item-${columnId}-${index}`,
|
|
248
|
+
ariaHidden: true,
|
|
249
|
+
className: `taro-picker__item${index === currentIndex ? ' taro-picker__item--selected' : ''}`,
|
|
250
|
+
...(enableClickItemScroll ? {
|
|
251
|
+
onClick: () => scrollToItemId(`picker-item-${columnId}-${index}`)
|
|
252
|
+
} : {}),
|
|
253
|
+
style: {
|
|
254
|
+
height: PICKER_LINE_HEIGHT,
|
|
255
|
+
color: index === currentIndex ? colors.itemSelectedColor || undefined : colors.itemDefaultColor || undefined
|
|
256
|
+
},
|
|
257
|
+
children: content
|
|
258
|
+
}, index);
|
|
259
|
+
});
|
|
260
|
+
const realPickerItems = [...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
261
|
+
ariaHidden: true,
|
|
262
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
263
|
+
style: {
|
|
264
|
+
height: PICKER_LINE_HEIGHT
|
|
265
|
+
}
|
|
266
|
+
}, `blank-top-${idx}`)), ...pickerItem, ...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
267
|
+
ariaHidden: true,
|
|
268
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
269
|
+
style: {
|
|
270
|
+
height: PICKER_LINE_HEIGHT
|
|
271
|
+
}
|
|
272
|
+
}, `blank-bottom-${idx}`))];
|
|
273
|
+
const clickScrollViewProps = enableClickItemScroll ? {
|
|
274
|
+
scrollIntoView,
|
|
275
|
+
scrollIntoViewAlignment: 'center'
|
|
276
|
+
} : {};
|
|
277
|
+
return /*#__PURE__*/jsxs(View, {
|
|
278
|
+
className: "taro-picker__group",
|
|
279
|
+
style: {
|
|
280
|
+
position: 'relative'
|
|
281
|
+
},
|
|
282
|
+
children: [/*#__PURE__*/jsx(View, {
|
|
283
|
+
className: "taro-picker__mask",
|
|
284
|
+
ariaHidden: true
|
|
285
|
+
}), /*#__PURE__*/jsx(View, {
|
|
286
|
+
className: "taro-picker__indicator",
|
|
287
|
+
ariaHidden: true,
|
|
288
|
+
...(indicatorStyle ? {
|
|
289
|
+
style: indicatorStyle
|
|
290
|
+
} : {})
|
|
291
|
+
}), /*#__PURE__*/jsx(ScrollView, {
|
|
292
|
+
ref: scrollViewRef,
|
|
293
|
+
scrollY: true,
|
|
294
|
+
showScrollbar: false,
|
|
295
|
+
className: "taro-picker__content",
|
|
296
|
+
ariaHidden: true,
|
|
297
|
+
accessibilityElementsHidden: true,
|
|
298
|
+
importantForAccessibility: 'no-hide-descendants',
|
|
299
|
+
style: {
|
|
300
|
+
height: PICKER_LINE_HEIGHT * PICKER_VISIBLE_ITEMS
|
|
301
|
+
},
|
|
302
|
+
scrollTop: targetScrollTop,
|
|
303
|
+
...clickScrollViewProps,
|
|
304
|
+
onScroll: handleScroll,
|
|
305
|
+
onTouchStart: () => {
|
|
306
|
+
isTouchingRef.current = true;
|
|
307
|
+
},
|
|
308
|
+
onScrollEnd: handleScrollEnd,
|
|
309
|
+
scrollWithAnimation: true,
|
|
310
|
+
children: realPickerItems
|
|
311
|
+
}), /*#__PURE__*/jsx(View, {
|
|
312
|
+
className: "taro-picker__a11y-overlay",
|
|
313
|
+
style: {
|
|
314
|
+
position: 'absolute',
|
|
315
|
+
top: 0,
|
|
316
|
+
left: 0,
|
|
317
|
+
right: 0,
|
|
318
|
+
bottom: 0,
|
|
319
|
+
pointerEvents: 'none'
|
|
320
|
+
},
|
|
321
|
+
ariaRole: "slider",
|
|
322
|
+
...(ariaLabel !== undefined ? {
|
|
323
|
+
ariaLabel
|
|
324
|
+
} : {}),
|
|
325
|
+
ariaAction: [{
|
|
326
|
+
name: 'increment',
|
|
327
|
+
label: 'increment'
|
|
328
|
+
}, {
|
|
329
|
+
name: 'decrement',
|
|
330
|
+
label: 'decrement'
|
|
331
|
+
}],
|
|
332
|
+
onAriaAction: handleAriaAction
|
|
333
|
+
})]
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
// 时间选择器实现
|
|
337
|
+
function PickerGroupTime(props) {
|
|
338
|
+
const {
|
|
339
|
+
range = [],
|
|
340
|
+
rangeKey,
|
|
341
|
+
columnId,
|
|
342
|
+
updateIndex,
|
|
343
|
+
selectedIndex = 0,
|
|
344
|
+
colors = {},
|
|
345
|
+
timeA11yLimitFocus,
|
|
346
|
+
timeA11yLimitEventNonce,
|
|
347
|
+
enableClickItemScroll = true
|
|
348
|
+
} = props;
|
|
349
|
+
const indicatorStyle = colors.lineColor ? getIndicatorStyle(colors.lineColor) : null;
|
|
350
|
+
const [targetScrollTop, setTargetScrollTop] = React.useState(0);
|
|
351
|
+
const [scrollIntoView, scrollToItemId] = usePickerItemScrollIntoView();
|
|
352
|
+
const scrollViewRef = React.useRef(null);
|
|
353
|
+
const [currentIndex, setCurrentIndex] = React.useState(selectedIndex);
|
|
354
|
+
const isTouchingRef = React.useRef(false);
|
|
355
|
+
const lengthScaleRatioRef = React.useRef(1);
|
|
356
|
+
const useMeasuredScaleRef = React.useRef(false);
|
|
357
|
+
const itemHeightRef = React.useRef(PICKER_LINE_HEIGHT);
|
|
358
|
+
// 初始化时计算 lengthScaleRatio 并判定缩放模式
|
|
359
|
+
React.useEffect(() => {
|
|
360
|
+
Taro.getSystemInfo({
|
|
361
|
+
success: res => {
|
|
362
|
+
lengthScaleRatioRef.current = calculateLengthScaleRatio(res);
|
|
363
|
+
useMeasuredScaleRef.current = resolveUseMeasuredScale(res);
|
|
364
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
365
|
+
},
|
|
366
|
+
fail: () => {
|
|
367
|
+
lengthScaleRatioRef.current = 1;
|
|
368
|
+
useMeasuredScaleRef.current = false;
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}, []);
|
|
372
|
+
React.useEffect(() => {
|
|
373
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
374
|
+
}, [range.length]); // 只在range长度变化时重新计算
|
|
375
|
+
// 滚动到指定索引(供程序化对齐与无障碍步进复用)
|
|
376
|
+
const scrollToIndex = React.useCallback(function (index) {
|
|
377
|
+
let withRandomOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
378
|
+
const baseValue = index * itemHeightRef.current;
|
|
379
|
+
const randomOffset = withRandomOffset ? Math.random() * 0.001 : undefined;
|
|
380
|
+
setTargetScrollTopWithScale(setTargetScrollTop, baseValue, randomOffset, lengthScaleRatioRef.current);
|
|
381
|
+
}, []);
|
|
382
|
+
// props 的 selectedIndex 变化:滚至对应项
|
|
383
|
+
React.useEffect(() => {
|
|
384
|
+
if (scrollViewRef.current && range.length > 0 && !isTouchingRef.current) {
|
|
385
|
+
scrollToIndex(selectedIndex);
|
|
386
|
+
setCurrentIndex(selectedIndex);
|
|
387
|
+
}
|
|
388
|
+
}, [selectedIndex, range, scrollToIndex]);
|
|
389
|
+
// 限位回弹强制对齐:限位后即使 selectedIndex 值不变(回弹到同一边界),nonce 变化也强制重新对齐 scrollTop
|
|
390
|
+
React.useEffect(() => {
|
|
391
|
+
if (!timeA11yLimitEventNonce) return;
|
|
392
|
+
if (!scrollViewRef.current || range.length === 0 || isTouchingRef.current) return;
|
|
393
|
+
scrollToIndex(selectedIndex, true);
|
|
394
|
+
setCurrentIndex(selectedIndex);
|
|
395
|
+
// 仅被限位触发的列在回弹稳定后补播报一次修正值(nonce 两列共享,需按 columnId 过滤)
|
|
396
|
+
if (timeA11yLimitFocus && timeA11yLimitFocus.columnId === columnId) {
|
|
397
|
+
const item = range[selectedIndex];
|
|
398
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
399
|
+
const text = content != null ? String(content) : '';
|
|
400
|
+
const fn = Taro.accessibilityAnnounce;
|
|
401
|
+
if (text && typeof fn === 'function') {
|
|
402
|
+
fn({
|
|
403
|
+
announce: text,
|
|
404
|
+
delay: 300
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}, [timeA11yLimitEventNonce]); // 仅依赖 nonce,其余用 ref
|
|
409
|
+
// 是否处于归中状态
|
|
410
|
+
const isCenterTimerId = React.useRef(null);
|
|
411
|
+
// 滚动静止后归中并同步选中(保留 time 限位业务:超 start/end 由父级修正后经 selectedIndex 回弹)
|
|
412
|
+
const handleScrollEnd = () => {
|
|
413
|
+
if (!scrollViewRef.current) return;
|
|
414
|
+
if (isCenterTimerId.current) {
|
|
415
|
+
clearTimeout(isCenterTimerId.current);
|
|
416
|
+
isCenterTimerId.current = null;
|
|
417
|
+
}
|
|
418
|
+
// 100ms 内无新滚动则归中并提交索引
|
|
419
|
+
isCenterTimerId.current = setTimeout(() => {
|
|
420
|
+
if (!scrollViewRef.current) return;
|
|
421
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
422
|
+
const newIndex = getSelectedIndex(scrollTop, itemHeightRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
423
|
+
isTouchingRef.current = false;
|
|
424
|
+
const isLimited = Boolean(updateIndex(newIndex, columnId, true));
|
|
425
|
+
// 限位时不归中:父级会通过 selectedIndex / nonce 下发修正值触发重新对齐
|
|
426
|
+
if (!isLimited) {
|
|
427
|
+
scrollToIndex(newIndex, true);
|
|
428
|
+
}
|
|
429
|
+
isCenterTimerId.current = null;
|
|
430
|
+
}, 100);
|
|
431
|
+
};
|
|
432
|
+
// 滚动中:按 scrollTop 更新高亮索引
|
|
433
|
+
const handleScroll = () => {
|
|
434
|
+
if (!scrollViewRef.current) return;
|
|
435
|
+
if (isCenterTimerId.current) {
|
|
436
|
+
clearTimeout(isCenterTimerId.current);
|
|
437
|
+
isCenterTimerId.current = null;
|
|
438
|
+
}
|
|
439
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
440
|
+
const ih = itemHeightRef.current;
|
|
441
|
+
const newIndex = getSelectedIndex(scrollTop, ih, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
442
|
+
if (newIndex !== currentIndex) {
|
|
443
|
+
setCurrentIndex(newIndex);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
// 无障碍:整列作单 slider,转子 increment/decrement 切相邻项(保留 time 限位)
|
|
447
|
+
const clampIndex = index => Math.max(0, Math.min(index, range.length - 1));
|
|
448
|
+
// 切换后主动播报当前项:焦点停在 slider 覆盖层上,aria 属性更新读屏不会自动重读,需主动触发
|
|
449
|
+
const announceIndex = index => {
|
|
450
|
+
const item = range[index];
|
|
451
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
452
|
+
const text = content != null ? String(content) : '';
|
|
453
|
+
if (!text) return;
|
|
454
|
+
const fn = Taro.accessibilityAnnounce;
|
|
455
|
+
if (typeof fn !== 'function') return;
|
|
456
|
+
fn({
|
|
457
|
+
announce: text,
|
|
458
|
+
delay: 300
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
const commitIndexByA11y = nextIndex => {
|
|
462
|
+
const clamped = clampIndex(nextIndex);
|
|
463
|
+
if (clamped === currentIndex) return;
|
|
464
|
+
isTouchingRef.current = false;
|
|
465
|
+
setCurrentIndex(clamped);
|
|
466
|
+
scrollToIndex(clamped, true);
|
|
467
|
+
// 保留 time 限位:超 start/end 时父级修正并经 selectedIndex / nonce 回弹对齐
|
|
468
|
+
updateIndex(clamped, columnId, true);
|
|
469
|
+
announceIndex(clamped);
|
|
470
|
+
};
|
|
471
|
+
const handleAriaAction = e => {
|
|
472
|
+
var _a;
|
|
473
|
+
const actionName = (_a = e === null || e === void 0 ? void 0 : e.detail) === null || _a === void 0 ? void 0 : _a.actionName;
|
|
474
|
+
if (actionName === 'increment') {
|
|
475
|
+
commitIndexByA11y(currentIndex + 1);
|
|
476
|
+
} else if (actionName === 'decrement') {
|
|
477
|
+
commitIndexByA11y(currentIndex - 1);
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
// 无障碍 label:当前项值,仅 string 或 number(转 string)挂载,对象等其它类型不挂载
|
|
481
|
+
const currentItem = range[currentIndex];
|
|
482
|
+
const currentValue = rangeKey && currentItem && typeof currentItem === 'object' ? currentItem[rangeKey] : currentItem;
|
|
483
|
+
const ariaLabel = typeof currentValue === 'string' ? currentValue : typeof currentValue === 'number' ? String(currentValue) : undefined;
|
|
484
|
+
// 渲染选项
|
|
485
|
+
const pickerItem = range.map((item, index) => {
|
|
486
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
487
|
+
return /*#__PURE__*/jsx(View, {
|
|
488
|
+
id: `picker-item-${columnId}-${index}`,
|
|
489
|
+
ariaHidden: true,
|
|
490
|
+
className: `taro-picker__item${index === currentIndex ? ' taro-picker__item--selected' : ''}`,
|
|
491
|
+
...(enableClickItemScroll ? {
|
|
492
|
+
onClick: () => scrollToItemId(`picker-item-${columnId}-${index}`)
|
|
493
|
+
} : {}),
|
|
494
|
+
style: {
|
|
495
|
+
height: PICKER_LINE_HEIGHT,
|
|
496
|
+
color: index === currentIndex ? colors.itemSelectedColor || undefined : colors.itemDefaultColor || undefined
|
|
497
|
+
},
|
|
498
|
+
children: content
|
|
499
|
+
}, index);
|
|
500
|
+
});
|
|
501
|
+
const realPickerItems = [...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
502
|
+
ariaHidden: true,
|
|
503
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
504
|
+
style: {
|
|
505
|
+
height: PICKER_LINE_HEIGHT
|
|
506
|
+
}
|
|
507
|
+
}, `blank-top-${idx}`)), ...pickerItem, ...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
508
|
+
ariaHidden: true,
|
|
509
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
510
|
+
style: {
|
|
511
|
+
height: PICKER_LINE_HEIGHT
|
|
512
|
+
}
|
|
513
|
+
}, `blank-bottom-${idx}`))];
|
|
514
|
+
const clickScrollViewProps = enableClickItemScroll ? {
|
|
515
|
+
scrollIntoView,
|
|
516
|
+
scrollIntoViewAlignment: 'center'
|
|
517
|
+
} : {};
|
|
518
|
+
return /*#__PURE__*/jsxs(View, {
|
|
519
|
+
className: "taro-picker__group",
|
|
520
|
+
style: {
|
|
521
|
+
position: 'relative'
|
|
522
|
+
},
|
|
523
|
+
children: [/*#__PURE__*/jsx(View, {
|
|
524
|
+
className: "taro-picker__mask",
|
|
525
|
+
ariaHidden: true
|
|
526
|
+
}), /*#__PURE__*/jsx(View, {
|
|
527
|
+
className: "taro-picker__indicator",
|
|
528
|
+
ariaHidden: true,
|
|
529
|
+
...(indicatorStyle ? {
|
|
530
|
+
style: indicatorStyle
|
|
531
|
+
} : {})
|
|
532
|
+
}), /*#__PURE__*/jsx(ScrollView, {
|
|
533
|
+
ref: scrollViewRef,
|
|
534
|
+
scrollY: true,
|
|
535
|
+
showScrollbar: false,
|
|
536
|
+
className: "taro-picker__content",
|
|
537
|
+
ariaHidden: true,
|
|
538
|
+
accessibilityElementsHidden: true,
|
|
539
|
+
importantForAccessibility: 'no-hide-descendants',
|
|
540
|
+
style: {
|
|
541
|
+
height: PICKER_LINE_HEIGHT * PICKER_VISIBLE_ITEMS
|
|
542
|
+
},
|
|
543
|
+
scrollTop: targetScrollTop,
|
|
544
|
+
...clickScrollViewProps,
|
|
545
|
+
onScroll: handleScroll,
|
|
546
|
+
onTouchStart: () => {
|
|
547
|
+
isTouchingRef.current = true;
|
|
548
|
+
},
|
|
549
|
+
onScrollEnd: handleScrollEnd,
|
|
550
|
+
scrollWithAnimation: true,
|
|
551
|
+
children: realPickerItems
|
|
552
|
+
}), /*#__PURE__*/jsx(View, {
|
|
553
|
+
className: "taro-picker__a11y-overlay",
|
|
554
|
+
style: {
|
|
555
|
+
position: 'absolute',
|
|
556
|
+
top: 0,
|
|
557
|
+
left: 0,
|
|
558
|
+
right: 0,
|
|
559
|
+
bottom: 0,
|
|
560
|
+
pointerEvents: 'none'
|
|
561
|
+
},
|
|
562
|
+
ariaRole: "slider",
|
|
563
|
+
...(ariaLabel !== undefined ? {
|
|
564
|
+
ariaLabel
|
|
565
|
+
} : {}),
|
|
566
|
+
ariaAction: [{
|
|
567
|
+
name: 'increment',
|
|
568
|
+
label: 'increment'
|
|
569
|
+
}, {
|
|
570
|
+
name: 'decrement',
|
|
571
|
+
label: 'decrement'
|
|
572
|
+
}],
|
|
573
|
+
onAriaAction: handleAriaAction
|
|
574
|
+
})]
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
// 日期选择器实现
|
|
578
|
+
function PickerGroupDate(props) {
|
|
579
|
+
const {
|
|
580
|
+
range = [],
|
|
581
|
+
columnId,
|
|
582
|
+
updateDay,
|
|
583
|
+
selectedIndex = 0,
|
|
584
|
+
colors = {},
|
|
585
|
+
enableClickItemScroll = true
|
|
586
|
+
} = props;
|
|
587
|
+
const indicatorStyle = colors.lineColor ? getIndicatorStyle(colors.lineColor) : null;
|
|
588
|
+
const [targetScrollTop, setTargetScrollTop] = React.useState(0);
|
|
589
|
+
const [scrollIntoView, scrollToItemId] = usePickerItemScrollIntoView();
|
|
590
|
+
const scrollViewRef = React.useRef(null);
|
|
591
|
+
const [currentIndex, setCurrentIndex] = React.useState(selectedIndex);
|
|
592
|
+
const isTouchingRef = React.useRef(false);
|
|
593
|
+
const lengthScaleRatioRef = React.useRef(1);
|
|
594
|
+
const useMeasuredScaleRef = React.useRef(false);
|
|
595
|
+
const itemHeightRef = React.useRef(PICKER_LINE_HEIGHT);
|
|
596
|
+
// 初始化时计算 lengthScaleRatio 并判定缩放模式
|
|
597
|
+
React.useEffect(() => {
|
|
598
|
+
Taro.getSystemInfo({
|
|
599
|
+
success: res => {
|
|
600
|
+
lengthScaleRatioRef.current = calculateLengthScaleRatio(res);
|
|
601
|
+
useMeasuredScaleRef.current = resolveUseMeasuredScale(res);
|
|
602
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
603
|
+
},
|
|
604
|
+
fail: () => {
|
|
605
|
+
lengthScaleRatioRef.current = 1;
|
|
606
|
+
useMeasuredScaleRef.current = false;
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}, []);
|
|
610
|
+
React.useEffect(() => {
|
|
611
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
612
|
+
}, [range.length]); // 只在range长度变化时重新计算
|
|
613
|
+
// 滚动到指定索引(供程序化对齐与无障碍步进复用)
|
|
614
|
+
const scrollToIndex = React.useCallback(function (index) {
|
|
615
|
+
let withRandomOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
616
|
+
const baseValue = index * itemHeightRef.current;
|
|
617
|
+
const randomOffset = withRandomOffset ? Math.random() * 0.001 : undefined;
|
|
618
|
+
setTargetScrollTopWithScale(setTargetScrollTop, baseValue, randomOffset, lengthScaleRatioRef.current);
|
|
619
|
+
}, []);
|
|
620
|
+
// props 的 selectedIndex 变化:滚至对应项
|
|
621
|
+
React.useEffect(() => {
|
|
622
|
+
if (scrollViewRef.current && range.length > 0 && !isTouchingRef.current) {
|
|
623
|
+
scrollToIndex(selectedIndex);
|
|
624
|
+
setCurrentIndex(selectedIndex);
|
|
625
|
+
}
|
|
626
|
+
}, [selectedIndex, range, scrollToIndex]);
|
|
627
|
+
// 提交日期值:解析文本中的数字(移除年、月、日等后缀)后回调 updateDay
|
|
628
|
+
const commitDay = index => {
|
|
629
|
+
if (!updateDay) return;
|
|
630
|
+
const valueText = range[index] || '';
|
|
631
|
+
const numericValue = parseInt(valueText.replace(/[^0-9]/g, ''));
|
|
632
|
+
updateDay(isNaN(numericValue) ? 0 : numericValue, parseInt(columnId));
|
|
633
|
+
};
|
|
634
|
+
// 是否处于归中状态
|
|
635
|
+
const isCenterTimerId = React.useRef(null);
|
|
636
|
+
// 滚动静止后归中并同步选中
|
|
637
|
+
const handleScrollEnd = () => {
|
|
638
|
+
if (!scrollViewRef.current) return;
|
|
639
|
+
if (isCenterTimerId.current) {
|
|
640
|
+
clearTimeout(isCenterTimerId.current);
|
|
641
|
+
isCenterTimerId.current = null;
|
|
642
|
+
}
|
|
643
|
+
// 100ms 内无新滚动则归中并提交索引
|
|
644
|
+
isCenterTimerId.current = setTimeout(() => {
|
|
645
|
+
if (!scrollViewRef.current) return;
|
|
646
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
647
|
+
const newIndex = getSelectedIndex(scrollTop, itemHeightRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
648
|
+
isTouchingRef.current = false;
|
|
649
|
+
scrollToIndex(newIndex, true);
|
|
650
|
+
commitDay(newIndex);
|
|
651
|
+
isCenterTimerId.current = null;
|
|
652
|
+
}, 100);
|
|
653
|
+
};
|
|
654
|
+
// 滚动中:按 scrollTop 更新高亮索引
|
|
655
|
+
const handleScroll = () => {
|
|
656
|
+
if (!scrollViewRef.current) return;
|
|
657
|
+
if (isCenterTimerId.current) {
|
|
658
|
+
clearTimeout(isCenterTimerId.current);
|
|
659
|
+
isCenterTimerId.current = null;
|
|
660
|
+
}
|
|
661
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
662
|
+
const ih = itemHeightRef.current;
|
|
663
|
+
const newIndex = getSelectedIndex(scrollTop, ih, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
664
|
+
if (newIndex !== currentIndex) {
|
|
665
|
+
setCurrentIndex(newIndex);
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
// 无障碍:整列作单 slider,转子 increment/decrement 切相邻项
|
|
669
|
+
const clampIndex = index => Math.max(0, Math.min(index, range.length - 1));
|
|
670
|
+
// 切换后主动播报当前项:焦点停在 slider 覆盖层上,aria 属性更新读屏不会自动重读,需主动触发
|
|
671
|
+
const announceIndex = index => {
|
|
672
|
+
const text = range[index] != null ? String(range[index]) : '';
|
|
673
|
+
if (!text) return;
|
|
674
|
+
const fn = Taro.accessibilityAnnounce;
|
|
675
|
+
if (typeof fn !== 'function') return;
|
|
676
|
+
fn({
|
|
677
|
+
announce: text,
|
|
678
|
+
delay: 300
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
const commitIndexByA11y = nextIndex => {
|
|
682
|
+
const clamped = clampIndex(nextIndex);
|
|
683
|
+
if (clamped === currentIndex) return;
|
|
684
|
+
isTouchingRef.current = false;
|
|
685
|
+
setCurrentIndex(clamped);
|
|
686
|
+
scrollToIndex(clamped, true);
|
|
687
|
+
commitDay(clamped);
|
|
688
|
+
announceIndex(clamped);
|
|
689
|
+
};
|
|
690
|
+
const handleAriaAction = e => {
|
|
691
|
+
var _a;
|
|
692
|
+
const actionName = (_a = e === null || e === void 0 ? void 0 : e.detail) === null || _a === void 0 ? void 0 : _a.actionName;
|
|
693
|
+
if (actionName === 'increment') {
|
|
694
|
+
commitIndexByA11y(currentIndex + 1);
|
|
695
|
+
} else if (actionName === 'decrement') {
|
|
696
|
+
commitIndexByA11y(currentIndex - 1);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
// 无障碍 label:当前项值,仅 string 或 number(转 string)挂载,对象等其它类型不挂载
|
|
700
|
+
const currentValue = range[currentIndex];
|
|
701
|
+
const ariaLabel = typeof currentValue === 'string' ? currentValue : typeof currentValue === 'number' ? String(currentValue) : undefined;
|
|
702
|
+
// 渲染选项
|
|
703
|
+
const pickerItem = range.map((item, index) => {
|
|
704
|
+
return /*#__PURE__*/jsx(View, {
|
|
705
|
+
id: `picker-item-${columnId}-${index}`,
|
|
706
|
+
ariaHidden: true,
|
|
707
|
+
className: `taro-picker__item${index === currentIndex ? ' taro-picker__item--selected' : ''}`,
|
|
708
|
+
...(enableClickItemScroll ? {
|
|
709
|
+
onClick: () => scrollToItemId(`picker-item-${columnId}-${index}`)
|
|
710
|
+
} : {}),
|
|
711
|
+
style: {
|
|
712
|
+
height: PICKER_LINE_HEIGHT,
|
|
713
|
+
color: index === currentIndex ? colors.itemSelectedColor || undefined : colors.itemDefaultColor || undefined
|
|
714
|
+
},
|
|
715
|
+
children: item
|
|
716
|
+
}, index);
|
|
717
|
+
});
|
|
718
|
+
const realPickerItems = [...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
719
|
+
ariaHidden: true,
|
|
720
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
721
|
+
style: {
|
|
722
|
+
height: PICKER_LINE_HEIGHT
|
|
723
|
+
}
|
|
724
|
+
}, `blank-top-${idx}`)), ...pickerItem, ...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
725
|
+
ariaHidden: true,
|
|
726
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
727
|
+
style: {
|
|
728
|
+
height: PICKER_LINE_HEIGHT
|
|
729
|
+
}
|
|
730
|
+
}, `blank-bottom-${idx}`))];
|
|
731
|
+
const clickScrollViewProps = enableClickItemScroll ? {
|
|
732
|
+
scrollIntoView,
|
|
733
|
+
scrollIntoViewAlignment: 'center'
|
|
734
|
+
} : {};
|
|
735
|
+
return /*#__PURE__*/jsxs(View, {
|
|
736
|
+
className: "taro-picker__group",
|
|
737
|
+
style: {
|
|
738
|
+
position: 'relative'
|
|
739
|
+
},
|
|
740
|
+
children: [/*#__PURE__*/jsx(View, {
|
|
741
|
+
className: "taro-picker__mask",
|
|
742
|
+
ariaHidden: true
|
|
743
|
+
}), /*#__PURE__*/jsx(View, {
|
|
744
|
+
className: "taro-picker__indicator",
|
|
745
|
+
ariaHidden: true,
|
|
746
|
+
...(indicatorStyle ? {
|
|
747
|
+
style: indicatorStyle
|
|
748
|
+
} : {})
|
|
749
|
+
}), /*#__PURE__*/jsx(ScrollView, {
|
|
750
|
+
ref: scrollViewRef,
|
|
751
|
+
scrollY: true,
|
|
752
|
+
showScrollbar: false,
|
|
753
|
+
className: "taro-picker__content",
|
|
754
|
+
ariaHidden: true,
|
|
755
|
+
accessibilityElementsHidden: true,
|
|
756
|
+
importantForAccessibility: 'no-hide-descendants',
|
|
757
|
+
style: {
|
|
758
|
+
height: PICKER_LINE_HEIGHT * PICKER_VISIBLE_ITEMS
|
|
759
|
+
},
|
|
760
|
+
scrollTop: targetScrollTop,
|
|
761
|
+
...clickScrollViewProps,
|
|
762
|
+
onScroll: handleScroll,
|
|
763
|
+
onTouchStart: () => {
|
|
764
|
+
isTouchingRef.current = true;
|
|
765
|
+
},
|
|
766
|
+
onScrollEnd: handleScrollEnd,
|
|
767
|
+
scrollWithAnimation: true,
|
|
768
|
+
children: realPickerItems
|
|
769
|
+
}), /*#__PURE__*/jsx(View, {
|
|
770
|
+
className: "taro-picker__a11y-overlay",
|
|
771
|
+
style: {
|
|
772
|
+
position: 'absolute',
|
|
773
|
+
top: 0,
|
|
774
|
+
left: 0,
|
|
775
|
+
right: 0,
|
|
776
|
+
bottom: 0,
|
|
777
|
+
pointerEvents: 'none'
|
|
778
|
+
},
|
|
779
|
+
ariaRole: "slider",
|
|
780
|
+
...(ariaLabel !== undefined ? {
|
|
781
|
+
ariaLabel
|
|
782
|
+
} : {}),
|
|
783
|
+
ariaAction: [{
|
|
784
|
+
name: 'increment',
|
|
785
|
+
label: 'increment'
|
|
786
|
+
}, {
|
|
787
|
+
name: 'decrement',
|
|
788
|
+
label: 'decrement'
|
|
789
|
+
}],
|
|
790
|
+
onAriaAction: handleAriaAction
|
|
791
|
+
})]
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
// 地区选择器实现
|
|
795
|
+
function PickerGroupRegion(props) {
|
|
796
|
+
const {
|
|
797
|
+
range = [],
|
|
798
|
+
rangeKey,
|
|
799
|
+
columnId,
|
|
800
|
+
updateIndex,
|
|
801
|
+
selectedIndex = 0,
|
|
802
|
+
// 使用selectedIndex参数,默认为0
|
|
803
|
+
colors = {},
|
|
804
|
+
enableClickItemScroll = true
|
|
805
|
+
} = props;
|
|
806
|
+
const indicatorStyle = colors.lineColor ? getIndicatorStyle(colors.lineColor) : null;
|
|
807
|
+
const scrollViewRef = React.useRef(null);
|
|
808
|
+
const [targetScrollTop, setTargetScrollTop] = React.useState(0);
|
|
809
|
+
const [scrollIntoView, scrollToItemId] = usePickerItemScrollIntoView();
|
|
810
|
+
const [currentIndex, setCurrentIndex] = React.useState(selectedIndex);
|
|
811
|
+
const isTouchingRef = React.useRef(false);
|
|
812
|
+
// 程序化滚动标记:开屏/受控同步预设值时置 true,避免被 region 级联门误判为用户滚动而重置后续列
|
|
813
|
+
const syncScrollFromPropsRef = React.useRef(false);
|
|
814
|
+
const lengthScaleRatioRef = React.useRef(1);
|
|
815
|
+
const useMeasuredScaleRef = React.useRef(false);
|
|
816
|
+
const itemHeightRef = React.useRef(PICKER_LINE_HEIGHT);
|
|
817
|
+
// 初始化时计算 lengthScaleRatio 并判定缩放模式
|
|
818
|
+
React.useEffect(() => {
|
|
819
|
+
Taro.getSystemInfo({
|
|
820
|
+
success: res => {
|
|
821
|
+
lengthScaleRatioRef.current = calculateLengthScaleRatio(res);
|
|
822
|
+
useMeasuredScaleRef.current = resolveUseMeasuredScale(res);
|
|
823
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
824
|
+
},
|
|
825
|
+
fail: () => {
|
|
826
|
+
lengthScaleRatioRef.current = 1;
|
|
827
|
+
useMeasuredScaleRef.current = false;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}, []);
|
|
831
|
+
React.useEffect(() => {
|
|
832
|
+
itemHeightRef.current = calculateItemHeight(scrollViewRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
833
|
+
}, [range.length]); // 只在range长度变化时重新计算
|
|
834
|
+
// 滚动到指定索引(供程序化对齐与无障碍步进复用)
|
|
835
|
+
const scrollToIndex = React.useCallback(function (index) {
|
|
836
|
+
let withRandomOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
837
|
+
const baseValue = index * itemHeightRef.current;
|
|
838
|
+
const randomOffset = withRandomOffset ? Math.random() * 0.001 : undefined;
|
|
839
|
+
setTargetScrollTopWithScale(setTargetScrollTop, baseValue, randomOffset, lengthScaleRatioRef.current);
|
|
840
|
+
}, []);
|
|
841
|
+
// props 的 selectedIndex 变化:滚至对应项,并短时标记程序化滚动(避免级联门误判)
|
|
842
|
+
React.useEffect(() => {
|
|
843
|
+
if (scrollViewRef.current && range.length > 0 && !isTouchingRef.current) {
|
|
844
|
+
syncScrollFromPropsRef.current = true;
|
|
845
|
+
scrollToIndex(selectedIndex);
|
|
846
|
+
setCurrentIndex(selectedIndex);
|
|
847
|
+
const tid = setTimeout(() => {
|
|
848
|
+
syncScrollFromPropsRef.current = false;
|
|
849
|
+
}, 400);
|
|
850
|
+
return () => clearTimeout(tid);
|
|
851
|
+
}
|
|
852
|
+
}, [selectedIndex, range, scrollToIndex]);
|
|
853
|
+
// 滚动静止后归中(debounce)
|
|
854
|
+
const isCenterTimerId = React.useRef(null);
|
|
855
|
+
const handleScrollEnd = () => {
|
|
856
|
+
if (!scrollViewRef.current) return;
|
|
857
|
+
if (isCenterTimerId.current) {
|
|
858
|
+
clearTimeout(isCenterTimerId.current);
|
|
859
|
+
isCenterTimerId.current = null;
|
|
860
|
+
}
|
|
861
|
+
// 100ms 内无新滚动则归中并提交索引
|
|
862
|
+
isCenterTimerId.current = setTimeout(() => {
|
|
863
|
+
if (!scrollViewRef.current) return;
|
|
864
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
865
|
+
const newIndex = getSelectedIndex(scrollTop, itemHeightRef.current, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
866
|
+
// 程序化滚动(受控同步预设值)不触发级联初始化,仅用户滚动才翻转初始化标志并重置后续列
|
|
867
|
+
const isUserScroll = !syncScrollFromPropsRef.current;
|
|
868
|
+
isTouchingRef.current = false;
|
|
869
|
+
scrollToIndex(newIndex, true);
|
|
870
|
+
updateIndex(newIndex, columnId, false, isUserScroll);
|
|
871
|
+
syncScrollFromPropsRef.current = false;
|
|
872
|
+
isCenterTimerId.current = null;
|
|
873
|
+
}, 100);
|
|
874
|
+
};
|
|
875
|
+
// 滚动中:按 scrollTop 更新高亮索引
|
|
876
|
+
const handleScroll = () => {
|
|
877
|
+
if (!scrollViewRef.current) return;
|
|
878
|
+
if (isCenterTimerId.current) {
|
|
879
|
+
clearTimeout(isCenterTimerId.current);
|
|
880
|
+
isCenterTimerId.current = null;
|
|
881
|
+
}
|
|
882
|
+
// 用户触摸滚动立即清程序化标志,使其 scrollEnd 被正确判为用户滚动(触发 region 级联)
|
|
883
|
+
if (isTouchingRef.current) {
|
|
884
|
+
syncScrollFromPropsRef.current = false;
|
|
885
|
+
}
|
|
886
|
+
const scrollTop = scrollViewRef.current.scrollTop;
|
|
887
|
+
const ih = itemHeightRef.current;
|
|
888
|
+
const newIndex = getSelectedIndex(scrollTop, ih, lengthScaleRatioRef.current, useMeasuredScaleRef.current);
|
|
889
|
+
if (newIndex !== currentIndex) {
|
|
890
|
+
setCurrentIndex(newIndex);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
// 无障碍:整列作单 slider,转子 increment/decrement 切相邻项
|
|
894
|
+
const clampIndex = index => Math.max(0, Math.min(index, range.length - 1));
|
|
895
|
+
// 切换后主动播报当前项:焦点停在 slider 覆盖层上,aria 属性更新读屏不会自动重读,需主动触发
|
|
896
|
+
const announceIndex = index => {
|
|
897
|
+
const item = range[index];
|
|
898
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
899
|
+
const text = content != null ? String(content) : '';
|
|
900
|
+
if (!text) return;
|
|
901
|
+
const fn = Taro.accessibilityAnnounce;
|
|
902
|
+
if (typeof fn !== 'function') return;
|
|
903
|
+
fn({
|
|
904
|
+
announce: text,
|
|
905
|
+
delay: 300
|
|
906
|
+
});
|
|
907
|
+
};
|
|
908
|
+
const commitIndexByA11y = nextIndex => {
|
|
909
|
+
const clamped = clampIndex(nextIndex);
|
|
910
|
+
if (clamped === currentIndex) return;
|
|
911
|
+
isTouchingRef.current = false;
|
|
912
|
+
setCurrentIndex(clamped);
|
|
913
|
+
scrollToIndex(clamped, true);
|
|
914
|
+
updateIndex(clamped, columnId, false, true);
|
|
915
|
+
announceIndex(clamped);
|
|
916
|
+
};
|
|
917
|
+
const handleAriaAction = e => {
|
|
918
|
+
var _a;
|
|
919
|
+
const actionName = (_a = e === null || e === void 0 ? void 0 : e.detail) === null || _a === void 0 ? void 0 : _a.actionName;
|
|
920
|
+
if (actionName === 'increment') {
|
|
921
|
+
commitIndexByA11y(currentIndex + 1);
|
|
922
|
+
} else if (actionName === 'decrement') {
|
|
923
|
+
commitIndexByA11y(currentIndex - 1);
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
// 无障碍 label:当前项值(region 用 rangeKey 取显示值),仅 string 或 number 挂载
|
|
927
|
+
const currentItem = range[currentIndex];
|
|
928
|
+
const currentValue = rangeKey && currentItem && typeof currentItem === 'object' ? currentItem[rangeKey] : currentItem;
|
|
929
|
+
const ariaLabel = typeof currentValue === 'string' ? currentValue : typeof currentValue === 'number' ? String(currentValue) : undefined;
|
|
930
|
+
// 渲染选项
|
|
931
|
+
const pickerItem = range.map((item, index) => {
|
|
932
|
+
const content = rangeKey && item && typeof item === 'object' ? item[rangeKey] : item;
|
|
933
|
+
return /*#__PURE__*/jsx(View, {
|
|
934
|
+
id: `picker-item-${columnId}-${index}`,
|
|
935
|
+
ariaHidden: true,
|
|
936
|
+
className: `taro-picker__item${index === currentIndex ? ' taro-picker__item--selected' : ''}`,
|
|
937
|
+
...(enableClickItemScroll ? {
|
|
938
|
+
onClick: () => scrollToItemId(`picker-item-${columnId}-${index}`)
|
|
939
|
+
} : {}),
|
|
940
|
+
style: {
|
|
941
|
+
height: PICKER_LINE_HEIGHT,
|
|
942
|
+
color: index === currentIndex ? colors.itemSelectedColor || undefined : colors.itemDefaultColor || undefined
|
|
943
|
+
},
|
|
944
|
+
children: content
|
|
945
|
+
}, index);
|
|
946
|
+
});
|
|
947
|
+
const realPickerItems = [...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
948
|
+
ariaHidden: true,
|
|
949
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
950
|
+
style: {
|
|
951
|
+
height: PICKER_LINE_HEIGHT
|
|
952
|
+
}
|
|
953
|
+
}, `blank-top-${idx}`)), ...pickerItem, ...new Array(PICKER_BLANK_ITEMS).fill(null).map((_, idx) => /*#__PURE__*/jsx(View, {
|
|
954
|
+
ariaHidden: true,
|
|
955
|
+
className: "taro-picker__item taro-picker__item--blank",
|
|
956
|
+
style: {
|
|
957
|
+
height: PICKER_LINE_HEIGHT
|
|
958
|
+
}
|
|
959
|
+
}, `blank-bottom-${idx}`))];
|
|
960
|
+
const clickScrollViewProps = enableClickItemScroll ? {
|
|
961
|
+
scrollIntoView,
|
|
962
|
+
scrollIntoViewAlignment: 'center'
|
|
963
|
+
} : {};
|
|
964
|
+
return /*#__PURE__*/jsxs(View, {
|
|
965
|
+
className: "taro-picker__group",
|
|
966
|
+
style: {
|
|
967
|
+
position: 'relative'
|
|
968
|
+
},
|
|
969
|
+
children: [/*#__PURE__*/jsx(View, {
|
|
970
|
+
className: "taro-picker__mask",
|
|
971
|
+
ariaHidden: true
|
|
972
|
+
}), /*#__PURE__*/jsx(View, {
|
|
973
|
+
className: "taro-picker__indicator",
|
|
974
|
+
ariaHidden: true,
|
|
975
|
+
...(indicatorStyle ? {
|
|
976
|
+
style: indicatorStyle
|
|
977
|
+
} : {})
|
|
978
|
+
}), /*#__PURE__*/jsx(ScrollView, {
|
|
979
|
+
ref: scrollViewRef,
|
|
980
|
+
scrollY: true,
|
|
981
|
+
showScrollbar: false,
|
|
982
|
+
className: "taro-picker__content",
|
|
983
|
+
ariaHidden: true,
|
|
984
|
+
accessibilityElementsHidden: true,
|
|
985
|
+
importantForAccessibility: 'no-hide-descendants',
|
|
986
|
+
style: {
|
|
987
|
+
height: PICKER_LINE_HEIGHT * PICKER_VISIBLE_ITEMS
|
|
988
|
+
},
|
|
989
|
+
scrollTop: targetScrollTop,
|
|
990
|
+
...clickScrollViewProps,
|
|
991
|
+
onScroll: handleScroll,
|
|
992
|
+
onTouchStart: () => {
|
|
993
|
+
isTouchingRef.current = true;
|
|
994
|
+
},
|
|
995
|
+
onScrollEnd: handleScrollEnd,
|
|
996
|
+
scrollWithAnimation: true,
|
|
997
|
+
children: realPickerItems
|
|
998
|
+
}), /*#__PURE__*/jsx(View, {
|
|
999
|
+
className: "taro-picker__a11y-overlay",
|
|
1000
|
+
style: {
|
|
1001
|
+
position: 'absolute',
|
|
1002
|
+
top: 0,
|
|
1003
|
+
left: 0,
|
|
1004
|
+
right: 0,
|
|
1005
|
+
bottom: 0,
|
|
1006
|
+
pointerEvents: 'none'
|
|
1007
|
+
},
|
|
1008
|
+
ariaRole: "slider",
|
|
1009
|
+
...(ariaLabel !== undefined ? {
|
|
1010
|
+
ariaLabel
|
|
1011
|
+
} : {}),
|
|
1012
|
+
ariaAction: [{
|
|
1013
|
+
name: 'increment',
|
|
1014
|
+
label: 'increment'
|
|
1015
|
+
}, {
|
|
1016
|
+
name: 'decrement',
|
|
1017
|
+
label: 'decrement'
|
|
1018
|
+
}],
|
|
1019
|
+
onAriaAction: handleAriaAction
|
|
1020
|
+
})]
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
// 新版无障碍(slider 遮挡方案),根据 mode 自动分发
|
|
1024
|
+
function PickerGroupSlider(props) {
|
|
1025
|
+
switch (props.mode) {
|
|
1026
|
+
case 'time':
|
|
1027
|
+
return /*#__PURE__*/jsx(PickerGroupTime, {
|
|
1028
|
+
...props
|
|
1029
|
+
});
|
|
1030
|
+
case 'date':
|
|
1031
|
+
return /*#__PURE__*/jsx(PickerGroupDate, {
|
|
1032
|
+
...props
|
|
1033
|
+
});
|
|
1034
|
+
case 'region':
|
|
1035
|
+
return /*#__PURE__*/jsx(PickerGroupRegion, {
|
|
1036
|
+
...props
|
|
1037
|
+
});
|
|
1038
|
+
default:
|
|
1039
|
+
return /*#__PURE__*/jsx(PickerGroupBasic, {
|
|
1040
|
+
...props
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
export { PickerGroupBasic, PickerGroupDate, PickerGroupRegion, PickerGroupSlider, PickerGroupTime };
|
|
1046
|
+
//# sourceMappingURL=picker-group-slider.js.map
|