Skip to content

Commit 673596b

Browse files
committed
update 1.0.8 包版本升级
1 parent 6079367 commit 673596b

File tree

4 files changed

+33
-132
lines changed

4 files changed

+33
-132
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ mytest/*
6666
# build
6767
dist/*
6868
!dist/.gitkeep
69+
*.lock

example/src/index.js

+3-16
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class Test extends React.Component {
5555
}
5656
onChangeWidth() {
5757
const l = Math.round(Math.random() * 800 + 400);
58-
console.log("改变width:", l);
5958
this.setState({
6059
width: l,
6160
});
@@ -64,21 +63,9 @@ class Test extends React.Component {
6463
return (
6564
<div>
6665
<div>
67-
<input
68-
type="text"
69-
placeholder="请输入正确的验证码"
70-
value={this.state.input2}
71-
onChange={(e) => this.onInput2Change(e)}
72-
maxLength={20}
73-
/>
74-
<Vcode
75-
onChange={(v) => this.onVcode2Change(v)}
76-
value={this.state.code}
77-
width={this.state.width}
78-
/>
79-
<span>
80-
{this.state.input2 === this.state.vcode2 ? "输入正确" : "输入错误"}
81-
</span>
66+
<input type="text" placeholder="请输入正确的验证码" value={this.state.input2} onChange={e => this.onInput2Change(e)} maxLength={20} />
67+
<Vcode onChange={v => this.onVcode2Change(v)} value={this.state.code} width={this.state.width} />
68+
<span>{this.state.input2 === this.state.vcode2 ? "输入正确" : "输入错误"}</span>
8269
</div>
8370
<hr />
8471
<button onClick={() => this.onChangeImg()}>更换图片</button>

package.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-vcode",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "a react verification code component",
55
"main": "dist/index.js",
66
"files": [
@@ -33,29 +33,29 @@
3333
"react-dom": "^16.13.1"
3434
},
3535
"devDependencies": {
36-
"@babel/cli": "^7.10.4",
37-
"@babel/core": "^7.10.4",
38-
"@babel/plugin-proposal-decorators": "^7.10.4",
39-
"@babel/plugin-transform-runtime": "^7.10.4",
40-
"@babel/preset-env": "^7.10.4",
36+
"@babel/cli": "^7.10.5",
37+
"@babel/core": "^7.11.4",
38+
"@babel/plugin-proposal-decorators": "^7.10.5",
39+
"@babel/plugin-transform-runtime": "^7.11.0",
40+
"@babel/preset-env": "^7.11.0",
4141
"@babel/preset-react": "^7.10.4",
42-
"@babel/runtime": "^7.10.4",
43-
"@types/react": "^16.9.41",
42+
"@babel/runtime": "^7.11.2",
43+
"@types/react": "^16.9.48",
4444
"@types/react-dom": "^16.9.8",
45-
"autoprefixer": "^9.8.4",
45+
"autoprefixer": "^9.8.6",
4646
"awesome-typescript-loader": "^5.2.1",
4747
"babel-loader": "^8.1.0",
48-
"css-loader": "^3.6.0",
49-
"eslint": "^7.4.0",
48+
"css-loader": "^4.2.2",
49+
"eslint": "^7.7.0",
5050
"eslint-config-prettier": "^6.11.0",
5151
"eslint-loader": "^4.0.2",
5252
"eslint-plugin-prettier": "^3.1.4",
5353
"file-loader": "^6.0.0",
54-
"source-map-loader": "^1.0.1",
54+
"source-map-loader": "^1.0.2",
5555
"style-loader": "^1.2.1",
56-
"typescript": "^3.9.6",
56+
"typescript": "^4.0.2",
5757
"url-loader": "^4.1.0",
58-
"webpack": "^4.43.0",
58+
"webpack": "^4.44.1",
5959
"webpack-bundle-analyzer": "^3.8.0",
6060
"webpack-cli": "^3.3.12",
6161
"webpack-dev-server": "^3.11.0"

src/index.tsx

+15-102
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
4242
constructor(props: Props) {
4343
super(props);
4444
this.state = {
45-
id:
46-
this.props.id || `${new Date().getTime()}_${Math.random().toFixed(4)}`, // 需要一个唯一的ID,因为vcode要直接操作dom
45+
id: this.props.id || `${Date.now()}_${Math.random().toFixed(4)}`, // 需要一个唯一的ID,因为vcode要直接操作dom
4746
width: this.props.width || 150, // vcode宽度
4847
height: this.props.height || 40, // vcode高度
4948
len: this.props.length || 4, // 生成几位code
@@ -67,44 +66,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
6766
options: (() => {
6867
// 初始化参数
6968
const a: Options = {
70-
codes: [
71-
"a",
72-
"b",
73-
"c",
74-
"d",
75-
"e",
76-
"f",
77-
"g",
78-
"h",
79-
"i",
80-
"j",
81-
"k",
82-
"l",
83-
"m",
84-
"o",
85-
"p",
86-
"q",
87-
"r",
88-
"s",
89-
"t",
90-
"x",
91-
"u",
92-
"v",
93-
"y",
94-
"z",
95-
"w",
96-
"n",
97-
"0",
98-
"1",
99-
"2",
100-
"3",
101-
"4",
102-
"5",
103-
"6",
104-
"7",
105-
"8",
106-
"9",
107-
],
69+
codes: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "o", "p", "q", "r", "s", "t", "x", "u", "v", "y", "z", "w", "n", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
10870
fontSizeMin: 22, // 字体尺寸最小值
10971
fontSizeMax: 26, // 字体尺寸最大值
11072
colors: [
@@ -156,11 +118,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
156118
if (this.props.value !== prevP.value) {
157119
this.onDraw(this.props.value);
158120
}
159-
if (
160-
this.props.width !== prevP.width ||
161-
this.props.height !== prevP.height ||
162-
this.props.style !== prevP.style
163-
) {
121+
if (this.props.width !== prevP.width || this.props.height !== prevP.height || this.props.style !== prevP.style) {
164122
this.setState({
165123
width: this.props.width || 150,
166124
height: this.props.height || 40,
@@ -189,15 +147,8 @@ export default class Vcode extends React.PureComponent<Props, State> {
189147
*/
190148
codeCss(uW: number, i: number): string {
191149
return [
192-
`font-size:${this.randint(
193-
this.state.options.fontSizeMin,
194-
this.state.options.fontSizeMax
195-
)}px`,
196-
`color:${
197-
this.state.options.colors[
198-
this.randint(0, this.state.options.colors.length - 1)
199-
]
200-
}`,
150+
`font-size:${this.randint(this.state.options.fontSizeMin, this.state.options.fontSizeMax)}px`,
151+
`color:${this.state.options.colors[this.randint(0, this.state.options.colors.length - 1)]}`,
201152
"position: absolute",
202153
`left:${this.randint(uW * i, uW * i + uW - uW / 2)}px`,
203154
"top:50%",
@@ -206,11 +157,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
206157
`-ms-transform:rotate(${this.randint(-15, 15)}deg) translateY(-50%)`,
207158
`-moz-transform:rotate(${this.randint(-15, 15)}deg) translateY(-50%)`,
208159
`-webkit-transform:rotate(${this.randint(-15, 15)}deg) translateY(-50%)`,
209-
`font-family:${
210-
this.state.options.fonts[
211-
this.randint(0, this.state.options.fonts.length - 1)
212-
]
213-
}`,
160+
`font-family:${this.state.options.fonts[this.randint(0, this.state.options.fonts.length - 1)]}`,
214161
"font-weight:bold",
215162
"z-index:2",
216163
].join(";");
@@ -224,34 +171,17 @@ export default class Vcode extends React.PureComponent<Props, State> {
224171
return [
225172
"position: absolute",
226173
`opacity:${this.randint(3, 8) / 10}`,
227-
`width:${this.randint(
228-
this.state.options.lineWidthMin,
229-
this.state.options.lineWidthMax
230-
)}px`,
231-
`height:${this.randint(
232-
this.state.options.lineHeightMin,
233-
this.state.options.lineHeightMax
234-
)}px`,
235-
`background:${
236-
this.state.options.lineColors[
237-
this.randint(0, this.state.options.lineColors.length - 1)
238-
]
239-
}`,
240-
`left:${this.randint(
241-
-this.state.options.lineWidthMin / 2,
242-
this.state.width
243-
)}px`,
174+
`width:${this.randint(this.state.options.lineWidthMin, this.state.options.lineWidthMax)}px`,
175+
`height:${this.randint(this.state.options.lineHeightMin, this.state.options.lineHeightMax)}px`,
176+
`background:${this.state.options.lineColors[this.randint(0, this.state.options.lineColors.length - 1)]}`,
177+
`left:${this.randint(-this.state.options.lineWidthMin / 2, this.state.width)}px`,
244178
`top:${this.randint(0, this.state.height)}px`,
245179
`transform:rotate(${this.randint(-30, 30)}deg)`,
246180
`-o-transform:rotate(${this.randint(-30, 30)}deg)`,
247181
`-ms-transform:rotate(${this.randint(-30, 30)}deg)`,
248182
`-moz-transform:rotate(${this.randint(-30, 30)}deg)`,
249183
`-webkit-transform:rotate(${this.randint(-30, 30)}deg)`,
250-
`font-family:${
251-
this.state.options.fonts[
252-
this.randint(0, this.state.options.fonts.length - 1)
253-
]
254-
}`,
184+
`font-family:${this.state.options.fonts[this.randint(0, this.state.options.fonts.length - 1)]}`,
255185
`font-weight:${this.randint(400, 900)}`,
256186
].join(";");
257187
}
@@ -264,21 +194,15 @@ export default class Vcode extends React.PureComponent<Props, State> {
264194
let c = ""; // 存储生成的code
265195
const div = document.getElementById(this.state.id);
266196

267-
const isImg: boolean = /^http[s]*:\/\/|\.jpg$|\.png$|\.jpeg$|\.gif$|\.bmp$|\.webp$|^data:image/.test(
268-
value || ""
269-
); // 是否是图片
197+
const isImg: boolean = /^http[s]*:\/\/|\.jpg$|\.png$|\.jpeg$|\.gif$|\.bmp$|\.webp$|^data:image/.test(value || ""); // 是否是图片
270198
if (div) {
271199
div.innerHTML = "";
272200
}
273201

274202
if (isImg) {
275203
// 用户传递了一张图片
276204
const dom = document.createElement("img");
277-
dom.style.cssText = [
278-
"display: block",
279-
"max-width:100%",
280-
"max-height:100%",
281-
].join(";");
205+
dom.style.cssText = ["display: block", "max-width:100%", "max-height:100%"].join(";");
282206
dom.src = value as string;
283207
div && div.appendChild(dom);
284208
this.props.onChange && this.props.onChange(null);
@@ -292,11 +216,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
292216
for (let i = 0; i < length; i++) {
293217
const dom = document.createElement("span");
294218
dom.style.cssText = this.codeCss(uW, i);
295-
const temp = value
296-
? value[i]
297-
: this.state.options.codes[
298-
Math.round(Math.random() * (this.state.options.codes.length - 1))
299-
];
219+
const temp = value ? value[i] : this.state.options.codes[Math.round(Math.random() * (this.state.options.codes.length - 1))];
300220
dom.innerHTML = String(temp);
301221
c = `${c}${temp}`;
302222
div && div.appendChild(dom);
@@ -320,13 +240,6 @@ export default class Vcode extends React.PureComponent<Props, State> {
320240
}
321241

322242
render() {
323-
return (
324-
<div
325-
id={this.state.id}
326-
style={this.state.style}
327-
className={this.props.className}
328-
onClick={() => this.onClick()}
329-
/>
330-
);
243+
return <div id={this.state.id} style={this.state.style} className={this.props.className} onClick={() => this.onClick()} />;
331244
}
332245
}

0 commit comments

Comments
 (0)