pixuireactcomponents 1.4.7 → 1.4.9
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/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/components/react/app/loadChecker/LoadChecker.d.ts +1 -0
- package/src/components/react/app/loadChecker/LoadChecker.js +8 -8
- package/src/components/react/app/scratchCard/ScratchCard.d.ts +15 -0
- package/src/components/react/app/scratchCard/ScratchCard.js +217 -0
- package/src/components/react/app/wheelPicker/WheelPicker.d.ts +15 -0
- package/src/components/react/app/wheelPicker/WheelPicker.js +147 -0
package/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export { DropDown } from "./src/components/react/app/dropDown/DropDown";
|
|
|
5
5
|
export { ImageViewer } from "./src/components/react/app/imageViewer/imageViewer";
|
|
6
6
|
export { ToggleGroup } from "./src/components/react/app/togglegroup/ToggleGroup";
|
|
7
7
|
export { VideoPlayer } from "./src/components/react/app/videoPlayer/VideoPlayer";
|
|
8
|
+
export { WheelPicker } from "./src/components/react/app/wheelPicker/WheelPicker";
|
|
9
|
+
export { ScratchCard } from "src/components/react/app/scratchCard/ScratchCard";
|
|
8
10
|
export { GradientText } from "./src/components/react/base/gradient/GradientText";
|
|
9
11
|
export { PVideo } from "./src/components/react/base/pixVideo/PixVideo";
|
|
10
12
|
export { OutlineText } from "./src/components/react/base/outlinetext/OutlineText";
|
package/index.js
CHANGED
|
@@ -7,6 +7,8 @@ export { ImageViewer } from './src/components/react/app/imageViewer/imageViewer'
|
|
|
7
7
|
export { Slider } from './src/components/react/app/slider/Slider';
|
|
8
8
|
export { ToggleGroup } from './src/components/react/app/togglegroup/ToggleGroup';
|
|
9
9
|
export { VideoPlayer } from './src/components/react/app/videoPlayer/VideoPlayer';
|
|
10
|
+
export { WheelPicker } from './src/components/react/app/wheelPicker/WheelPicker';
|
|
11
|
+
export { ScratchCard } from 'src/components/react/app/scratchCard/ScratchCard';
|
|
10
12
|
export { GradientText } from './src/components/react/base/gradient/GradientText';
|
|
11
13
|
export { PVideo } from './src/components/react/base/pixVideo/PixVideo';
|
|
12
14
|
export { OutlineText } from './src/components/react/base/outlinetext/OutlineText';
|
package/package.json
CHANGED
|
@@ -10,12 +10,12 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
10
10
|
import h from 'lib/dom';
|
|
11
11
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
12
12
|
export var LoadChecker = function (props) {
|
|
13
|
-
var children = props.children, _a = props.rootClass, rootClass = _a === void 0 ? '' : _a, _b = props.timeoutPeriod, timeoutPeriod = _b === void 0 ? 500 : _b, onLoad = props.onLoad, _c = props.enableCompLog, enableCompLog = _c === void 0 ? false : _c, _d = props.onlyHttp, onlyHttp = _d === void 0 ? false : _d;
|
|
14
|
-
var
|
|
15
|
-
var
|
|
16
|
-
var
|
|
17
|
-
var
|
|
18
|
-
var
|
|
13
|
+
var children = props.children, _a = props.rootClass, rootClass = _a === void 0 ? '' : _a, _b = props.timeoutPeriod, timeoutPeriod = _b === void 0 ? 500 : _b, onLoad = props.onLoad, _c = props.enableCompLog, enableCompLog = _c === void 0 ? false : _c, _d = props.onlyHttp, onlyHttp = _d === void 0 ? false : _d, _e = props.tag, tag = _e === void 0 ? '' : _e;
|
|
14
|
+
var _f = useState(false), loaded = _f[0], setLoaded = _f[1];
|
|
15
|
+
var _g = useState(false), collectFinish = _g[0], setCollectFinish = _g[1];
|
|
16
|
+
var _h = useState([]), loadedElements = _h[0], setLoadedElements = _h[1];
|
|
17
|
+
var _j = useState([]), collectSrcs = _j[0], setCollectSrcs = _j[1];
|
|
18
|
+
var _k = useState(new Date().getTime()), timeStamp = _k[0], setTimeStamp = _k[1];
|
|
19
19
|
var ref = useRef(null);
|
|
20
20
|
var myLog = function () {
|
|
21
21
|
var args = [];
|
|
@@ -23,7 +23,7 @@ export var LoadChecker = function (props) {
|
|
|
23
23
|
args[_i] = arguments[_i];
|
|
24
24
|
}
|
|
25
25
|
if (enableCompLog) {
|
|
26
|
-
console.log.apply(console, __spreadArray([
|
|
26
|
+
console.log.apply(console, __spreadArray(["LoadChecker [".concat(tag, "]: ")], args, false));
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
//0.5s后直接显示
|
|
@@ -84,7 +84,7 @@ export var LoadChecker = function (props) {
|
|
|
84
84
|
if (src.startsWith('url(')) {
|
|
85
85
|
src = src.substring(4, src.length - 1);
|
|
86
86
|
}
|
|
87
|
-
if (onlyHttp && (!src.startsWith('http') || src.startsWith('http://
|
|
87
|
+
if (onlyHttp && (!src.startsWith('http') || (src.startsWith('http://') && src.substring(7).includes(':')))) {
|
|
88
88
|
return '';
|
|
89
89
|
}
|
|
90
90
|
return src;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CSSProperties, h } from 'preact';
|
|
2
|
+
export interface ScratchCardProps {
|
|
3
|
+
cardId?: string;
|
|
4
|
+
ratio?: number;
|
|
5
|
+
moveRadius?: number;
|
|
6
|
+
startCallback?: Function;
|
|
7
|
+
clearCallback?: Function;
|
|
8
|
+
coverImage?: string;
|
|
9
|
+
coverColor?: string;
|
|
10
|
+
coverWidth: number;
|
|
11
|
+
coverHeight: number;
|
|
12
|
+
coverStyle?: CSSProperties;
|
|
13
|
+
resultStyle?: CSSProperties;
|
|
14
|
+
}
|
|
15
|
+
export declare const ScratchCard: (props: ScratchCardProps, ref: any) => h.JSX.Element;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import { h } from 'preact';
|
|
49
|
+
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
50
|
+
// export type ScratchCardRef = {
|
|
51
|
+
// // canvasContainer: HTMLCanvasElement;
|
|
52
|
+
// // initDone: boolean;
|
|
53
|
+
// clearCard: () => void;
|
|
54
|
+
// };
|
|
55
|
+
var defaultStyle = {
|
|
56
|
+
width: '100%',
|
|
57
|
+
height: '100%',
|
|
58
|
+
display: 'flex',
|
|
59
|
+
justifyContent: 'center',
|
|
60
|
+
alignItems: 'center'
|
|
61
|
+
};
|
|
62
|
+
export var ScratchCard = function (props, ref) {
|
|
63
|
+
var _a = props.cardId, cardId = _a === void 0 ? 'scratch-card' : _a, _b = props.ratio, ratio = _b === void 0 ? 1 : _b, startCallback = props.startCallback, clearCallback = props.clearCallback, coverImage = props.coverImage, coverColor = props.coverColor, coverWidth = props.coverWidth, coverHeight = props.coverHeight, coverStyle = props.coverStyle, resultStyle = props.resultStyle, _c = props.moveRadius, moveRadius = _c === void 0 ? 10 : _c;
|
|
64
|
+
var canvasRef = useRef(null);
|
|
65
|
+
var _d = useState(false), isResultLoad = _d[0], setIsResultLoad = _d[1];
|
|
66
|
+
// 记录鼠标位置,用作平滑效果
|
|
67
|
+
var lastPos = { x: 0, y: 0 };
|
|
68
|
+
// 记录首次刮卡
|
|
69
|
+
var isStarted = false;
|
|
70
|
+
// 记录是否正在刮卡
|
|
71
|
+
var isDrawing = false;
|
|
72
|
+
// 已经刮过的元素数量
|
|
73
|
+
var scratchedPixels = 0;
|
|
74
|
+
// canvas元素二维数组
|
|
75
|
+
var scratchMap;
|
|
76
|
+
useEffect(function () {
|
|
77
|
+
var drawCanvas = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
78
|
+
var canvas, ctx;
|
|
79
|
+
return __generator(this, function (_a) {
|
|
80
|
+
canvas = canvasRef.current;
|
|
81
|
+
ctx = canvas.getContext('2d');
|
|
82
|
+
console.log("canvas: ", canvas, ctx);
|
|
83
|
+
if (ctx) {
|
|
84
|
+
if (coverColor) {
|
|
85
|
+
ctx.fillStyle = coverColor;
|
|
86
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
87
|
+
setIsResultLoad(true);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
scratchMap = Array.from({ length: canvas.height }, function () { return Array(canvas.width).fill(false); });
|
|
91
|
+
canvas.addEventListener('mousedown', handleMouseDown);
|
|
92
|
+
canvas.addEventListener('mouseup', handleMouseUp);
|
|
93
|
+
canvas.addEventListener('mousemove', handleMouseMove);
|
|
94
|
+
return [2 /*return*/];
|
|
95
|
+
});
|
|
96
|
+
}); };
|
|
97
|
+
drawCanvas();
|
|
98
|
+
return function () { removeListener(); };
|
|
99
|
+
}, [coverColor]);
|
|
100
|
+
var drawImage = function () {
|
|
101
|
+
var canvas = canvasRef.current;
|
|
102
|
+
var ctx = canvas.getContext('2d');
|
|
103
|
+
if (ctx) {
|
|
104
|
+
var image = document.getElementById("".concat(cardId, "_img"));
|
|
105
|
+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
106
|
+
setIsResultLoad(true);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var handleMouseDown = function (e) {
|
|
110
|
+
// console.log("handleMouseDown: ", e )
|
|
111
|
+
isDrawing = true;
|
|
112
|
+
if (!isStarted && startCallback) {
|
|
113
|
+
startCallback();
|
|
114
|
+
isStarted = true;
|
|
115
|
+
}
|
|
116
|
+
var rect = canvasRef.current.getBoundingClientRect();
|
|
117
|
+
var x = e.clientX - rect.left;
|
|
118
|
+
var y = e.clientY - rect.top;
|
|
119
|
+
lastPos = { x: x, y: y };
|
|
120
|
+
handleScratch(x, y);
|
|
121
|
+
};
|
|
122
|
+
var handleMouseUp = function (e) {
|
|
123
|
+
// console.log("handleMouseUp: ", e )
|
|
124
|
+
isDrawing = false;
|
|
125
|
+
handleIfComplete();
|
|
126
|
+
};
|
|
127
|
+
var handleMouseMove = function (e) {
|
|
128
|
+
// console.log("handleMouseMove: ", isDrawing, e )
|
|
129
|
+
if (!isDrawing) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
var rect = canvasRef.current.getBoundingClientRect();
|
|
133
|
+
var x = e.clientX - rect.left;
|
|
134
|
+
var y = e.clientY - rect.top;
|
|
135
|
+
var ctx = canvasRef.current.getContext('2d');
|
|
136
|
+
if (ctx) {
|
|
137
|
+
ctx.globalCompositeOperation = "destination-out";
|
|
138
|
+
ctx.beginPath();
|
|
139
|
+
ctx.moveTo(lastPos.x, lastPos.y);
|
|
140
|
+
ctx.lineTo(x, y);
|
|
141
|
+
ctx.strokeStyle = 'rgba(0,0,0,1)';
|
|
142
|
+
ctx.lineWidth = moveRadius * 2;
|
|
143
|
+
ctx.stroke();
|
|
144
|
+
}
|
|
145
|
+
lastPos = { x: x, y: y };
|
|
146
|
+
handleScratch(x, y);
|
|
147
|
+
};
|
|
148
|
+
var handleScratch = function (x, y) {
|
|
149
|
+
var canvas = canvasRef.current;
|
|
150
|
+
var ctx = canvas.getContext('2d');
|
|
151
|
+
// console.log("handleScratch: ", e )
|
|
152
|
+
if (ctx) {
|
|
153
|
+
for (var i = -moveRadius; i <= moveRadius; i++) {
|
|
154
|
+
for (var j = -moveRadius; j <= moveRadius; j++) {
|
|
155
|
+
var dx = x + i;
|
|
156
|
+
var dy = y + j;
|
|
157
|
+
if (dx >= 0 && dx < canvas.width && dy >= 0 && dy < canvas.height && !scratchMap[dy][dx]) {
|
|
158
|
+
scratchMap[dy][dx] = true;
|
|
159
|
+
scratchedPixels++;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
ctx.globalCompositeOperation = coverImage ? "source-out" : "destination-out";
|
|
164
|
+
ctx.beginPath();
|
|
165
|
+
ctx.arc(x, y, moveRadius, 0, Math.PI * 2);
|
|
166
|
+
ctx.fill();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var handleIfComplete = function () {
|
|
170
|
+
var canvas = canvasRef.current;
|
|
171
|
+
var ctx = canvas.getContext('2d');
|
|
172
|
+
if (ctx) {
|
|
173
|
+
// getImageData方法目前暂不支持
|
|
174
|
+
// const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
175
|
+
// const pixels = imageData.data;
|
|
176
|
+
// let scratchedPixels = 0;
|
|
177
|
+
// for(let i = 0; i < pixels.length; i += 4) {
|
|
178
|
+
// if(pixels[i + 3] === 0) {
|
|
179
|
+
// scratchedPixels++;
|
|
180
|
+
// }
|
|
181
|
+
// }
|
|
182
|
+
// if((scratchedPixels / (pixels.length / 4)) > ratio) {
|
|
183
|
+
// if(clearCallback) {
|
|
184
|
+
// clearCallback();
|
|
185
|
+
// }
|
|
186
|
+
// clearCard();
|
|
187
|
+
// }
|
|
188
|
+
var scratchedRatio = scratchedPixels / (canvas.width * canvas.height);
|
|
189
|
+
if (scratchedRatio > ratio) {
|
|
190
|
+
if (clearCallback) {
|
|
191
|
+
clearCallback();
|
|
192
|
+
}
|
|
193
|
+
clearCard();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
var clearCard = function () {
|
|
198
|
+
var canvas = canvasRef.current;
|
|
199
|
+
var ctx = canvas.getContext('2d');
|
|
200
|
+
if (ctx) {
|
|
201
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
202
|
+
removeListener();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
var removeListener = function () {
|
|
206
|
+
var canvas = canvasRef.current;
|
|
207
|
+
if (canvas) {
|
|
208
|
+
canvas.removeEventListener('mousedown', handleMouseDown);
|
|
209
|
+
canvas.removeEventListener('mouseup', handleMouseUp);
|
|
210
|
+
canvas.removeEventListener('mousemove', handleMouseMove);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
return (h("div", { id: cardId, style: __assign({}, defaultStyle) },
|
|
214
|
+
isResultLoad && h("div", { style: __assign({}, resultStyle) }),
|
|
215
|
+
coverImage && h("img", { style: { position: 'absolute', width: "1px", height: "1px" }, id: "".concat(cardId, "_img"), src: coverImage, onLoad: drawImage }),
|
|
216
|
+
h("canvas", { ref: canvasRef, width: coverWidth, height: coverHeight, style: __assign(__assign({}, coverStyle), { position: 'absolute' }) })));
|
|
217
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CSSProperties, h } from 'preact';
|
|
2
|
+
export interface WheelPickerProps {
|
|
3
|
+
wrapperStyle: CSSProperties;
|
|
4
|
+
items: string[];
|
|
5
|
+
visibleAbove?: number;
|
|
6
|
+
visibleBelow?: number;
|
|
7
|
+
itemTextStyle: CSSProperties;
|
|
8
|
+
selectedTextStyle?: CSSProperties;
|
|
9
|
+
AboveMaskAreaStyle?: CSSProperties;
|
|
10
|
+
BelowMaskAreaStyle?: CSSProperties;
|
|
11
|
+
selectedAreaStyle?: CSSProperties;
|
|
12
|
+
firstSelectIndex?: number;
|
|
13
|
+
onChange?: (index: number) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const WheelPicker: (props: WheelPickerProps) => h.JSX.Element;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { h } from 'preact';
|
|
13
|
+
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
14
|
+
var defaulWrapperStyle = {
|
|
15
|
+
display: 'flex',
|
|
16
|
+
justifyContent: 'center',
|
|
17
|
+
alignItems: 'center'
|
|
18
|
+
};
|
|
19
|
+
var defaultItemTextStyle = {
|
|
20
|
+
fontFamily: 'Font_FounderZZ',
|
|
21
|
+
fontStyle: 'normal',
|
|
22
|
+
fontSize: '24px',
|
|
23
|
+
lineHeight: '32px',
|
|
24
|
+
display: 'flex',
|
|
25
|
+
alignItems: 'center',
|
|
26
|
+
textAlign: 'center',
|
|
27
|
+
color: '#000000'
|
|
28
|
+
};
|
|
29
|
+
var defaultSelectedTextStyle = {
|
|
30
|
+
fontFamily: 'Font_FounderZZ',
|
|
31
|
+
fontStyle: 'normal',
|
|
32
|
+
fontSize: '24px',
|
|
33
|
+
lineHeight: '32px',
|
|
34
|
+
display: 'flex',
|
|
35
|
+
alignItems: 'center',
|
|
36
|
+
textAlign: 'center',
|
|
37
|
+
color: '#F0E6D2',
|
|
38
|
+
};
|
|
39
|
+
var defaultScrollStyle = {
|
|
40
|
+
width: '100%',
|
|
41
|
+
height: '100%',
|
|
42
|
+
overflow: 'scroll',
|
|
43
|
+
display: 'flex',
|
|
44
|
+
flexDirection: 'column',
|
|
45
|
+
justifyContent: 'start',
|
|
46
|
+
alignItems: 'center',
|
|
47
|
+
overflowStyle: 'none',
|
|
48
|
+
};
|
|
49
|
+
var defaultMaskAreaStyle = {
|
|
50
|
+
position: 'absolute',
|
|
51
|
+
pointerEvents: 'none'
|
|
52
|
+
};
|
|
53
|
+
var defaultItemStyle = {
|
|
54
|
+
display: 'flex',
|
|
55
|
+
justifyContent: 'center',
|
|
56
|
+
alignItems: 'center',
|
|
57
|
+
width: '100%'
|
|
58
|
+
};
|
|
59
|
+
export var WheelPicker = function (props) {
|
|
60
|
+
var wrapperStyle = props.wrapperStyle, items = props.items, _a = props.visibleAbove, visibleAbove = _a === void 0 ? 1 : _a, _b = props.visibleBelow, visibleBelow = _b === void 0 ? 1 : _b, itemTextStyle = props.itemTextStyle, selectedTextStyle = props.selectedTextStyle, _c = props.firstSelectIndex, firstSelectIndex = _c === void 0 ? 0 : _c, AboveMaskAreaStyle = props.AboveMaskAreaStyle, BelowMaskAreaStyle = props.BelowMaskAreaStyle, selectedAreaStyle = props.selectedAreaStyle, onChange = props.onChange;
|
|
61
|
+
var scrollRef = useRef(null);
|
|
62
|
+
var _d = useState(0), itemHeight = _d[0], setItemHeight = _d[1];
|
|
63
|
+
var _e = useState([]), itemList = _e[0], setItemList = _e[1];
|
|
64
|
+
var _f = useState(false), isDraging = _f[0], setIsDraging = _f[1];
|
|
65
|
+
var _g = useState(0), selectIndex = _g[0], setSelectIndex = _g[1];
|
|
66
|
+
var scrollTimmer = useRef(null);
|
|
67
|
+
useEffect(function () {
|
|
68
|
+
var calcuItemHeight = parseInt(wrapperStyle.height) / (visibleAbove + visibleBelow + 1);
|
|
69
|
+
setItemHeight(calcuItemHeight);
|
|
70
|
+
setSelectIndex(firstSelectIndex + visibleAbove);
|
|
71
|
+
setItemList(fillBlankItems());
|
|
72
|
+
var timer = setTimeout(function () {
|
|
73
|
+
if (scrollRef.current) {
|
|
74
|
+
scrollRef.current.scrollTop = firstSelectIndex * calcuItemHeight;
|
|
75
|
+
}
|
|
76
|
+
}, 0);
|
|
77
|
+
return function () { return clearTimeout(timer); };
|
|
78
|
+
}, []);
|
|
79
|
+
useEffect(function () {
|
|
80
|
+
if (onChange) {
|
|
81
|
+
onChange(selectIndex - visibleAbove);
|
|
82
|
+
}
|
|
83
|
+
}, [selectIndex]);
|
|
84
|
+
var fillBlankItems = function () {
|
|
85
|
+
var startPadding = new Array(visibleAbove).fill('');
|
|
86
|
+
var endPadding = new Array(visibleBelow).fill('');
|
|
87
|
+
return startPadding.concat(items, endPadding);
|
|
88
|
+
};
|
|
89
|
+
var onScroll = function (e) {
|
|
90
|
+
// console.log('onScroll: ', scrollRef.current.scrollTop)
|
|
91
|
+
if (scrollTimmer.current) {
|
|
92
|
+
clearTimeout(scrollTimmer.current);
|
|
93
|
+
scrollTimmer.current = null;
|
|
94
|
+
}
|
|
95
|
+
scrollTimmer.current = window.setTimeout(function () {
|
|
96
|
+
var finalPos = (selectIndex - visibleAbove) * itemHeight;
|
|
97
|
+
if (!isDraging && scrollRef.current.scrollTop < finalPos - 1 || scrollRef.current.scrollTop > finalPos + 1) {
|
|
98
|
+
animateScroll(scrollRef.current.scrollTop, finalPos, performance.now(), 100);
|
|
99
|
+
}
|
|
100
|
+
if (scrollTimmer.current) {
|
|
101
|
+
clearTimeout(scrollTimmer.current);
|
|
102
|
+
scrollTimmer.current = null;
|
|
103
|
+
}
|
|
104
|
+
}, 50);
|
|
105
|
+
var nowSelectIndex = Math.round((scrollRef.current.scrollTop + visibleAbove * itemHeight) / itemHeight);
|
|
106
|
+
if (selectIndex !== nowSelectIndex) {
|
|
107
|
+
setSelectIndex(nowSelectIndex);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var animateScroll = function (currentPos, targetPos, startTime, duration) {
|
|
111
|
+
var step = function () {
|
|
112
|
+
var currentTime = performance.now();
|
|
113
|
+
var time = Math.min(1, (currentTime - startTime) / duration);
|
|
114
|
+
var timeFunction = time * (2 - time);
|
|
115
|
+
var newPos = currentPos + (targetPos - currentPos) * timeFunction;
|
|
116
|
+
scrollRef.current.scrollTop = newPos;
|
|
117
|
+
if (time < 1 && !isDraging) {
|
|
118
|
+
requestAnimationFrame(step);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
requestAnimationFrame(step);
|
|
122
|
+
};
|
|
123
|
+
var onMouseEvent = function (isDraging) {
|
|
124
|
+
if (isDraging) {
|
|
125
|
+
setIsDraging(true);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
setIsDraging(false);
|
|
129
|
+
if (!scrollTimmer.current) {
|
|
130
|
+
var finalPos = (selectIndex - visibleAbove) * itemHeight;
|
|
131
|
+
animateScroll(scrollRef.current.scrollTop, finalPos, performance.now(), 100);
|
|
132
|
+
var nowSelectIndex = Math.round((scrollRef.current.scrollTop + visibleAbove * itemHeight) / itemHeight);
|
|
133
|
+
if (selectIndex !== nowSelectIndex) {
|
|
134
|
+
setSelectIndex(nowSelectIndex);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
return (h("div", { style: __assign(__assign({}, wrapperStyle), defaulWrapperStyle) },
|
|
140
|
+
h("div", { style: __assign({}, defaultScrollStyle),
|
|
141
|
+
// movement-type={'clamped'}
|
|
142
|
+
ref: scrollRef, onScroll: function (e) { onScroll(e); }, onMouseDown: function () { onMouseEvent(true); }, onMouseUp: function () { onMouseEvent(false); } }, itemList.map(function (item, index) { return (h("div", { style: __assign(__assign({}, defaultItemStyle), { minHeight: "".concat(itemHeight, "px") }) },
|
|
143
|
+
h("text", { style: selectedTextStyle ? (index === selectIndex ? selectedTextStyle : itemTextStyle) : itemTextStyle }, item))); })),
|
|
144
|
+
selectedAreaStyle && h("div", { style: __assign(__assign(__assign({}, selectedAreaStyle), defaultMaskAreaStyle), { height: "".concat(itemHeight, "px"), top: "".concat(itemHeight * visibleAbove, "px") }) }),
|
|
145
|
+
AboveMaskAreaStyle && h("div", { style: __assign(__assign(__assign({}, AboveMaskAreaStyle), defaultMaskAreaStyle), { height: "".concat(itemHeight * visibleAbove, "px"), top: '0px' }) }),
|
|
146
|
+
BelowMaskAreaStyle && h("div", { style: __assign(__assign(__assign({}, BelowMaskAreaStyle), defaultMaskAreaStyle), { height: "".concat(itemHeight * visibleBelow, "px"), top: "".concat(itemHeight * (visibleAbove + 1), "px") }) })));
|
|
147
|
+
};
|