Skip to content

Commit f8298ba

Browse files
committed
1.0.3
1 parent b044b84 commit f8298ba

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

example/src/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Test extends React.Component {
1111
input2: '', // 第2个input的值
1212
vcode2: '-1', // 第2个vcode的值
1313
code: '',
14+
width: 200,
1415
};
1516
}
1617

@@ -22,22 +23,28 @@ class Test extends React.Component {
2223

2324
onVcode2Change(v) {
2425
console.log('触发回调onChange', v);
25-
this.setState({
26-
vcode2: v,
27-
});
26+
if(v){
27+
this.setState({
28+
vcode2: v,
29+
});
30+
}
2831
}
2932

3033
onChangeImg() {
34+
const imgindex = this.state.img === 1 ? 2 : 1;
3135
this.setState({
32-
img: this.state.img === 1 ? 2 : 1,
36+
img: imgindex,
37+
code: imgindex === 1 ? ImgTest1 : ImgTest2,
38+
vcode2: '1234'
3339
});
3440
}
3541
onChangeStr() {
3642
const a = ['a', 'b', 'c', 'd'];
3743
const d = [];
3844
for (let i = 0; i < 5; i++) {
39-
d.push(Math.round(Math.random() * 3));
45+
d.push(a[Math.round(Math.random() * 3)]);
4046
}
47+
console.log('code:', d);
4148
this.setState({
4249
code: d.join(''),
4350
});
@@ -46,17 +53,25 @@ class Test extends React.Component {
4653
onVcodeClick() {
4754
this.onChangeStr();
4855
}
56+
onChangeWidth(){
57+
const l = Math.round(Math.random() * 800 + 400);
58+
console.log('改变width:', l);
59+
this.setState({
60+
width: l
61+
});
62+
}
4963
render() {
5064
return (
5165
<div>
5266
<div>
5367
<input type="text" placeholder="请输入正确的验证码" value={this.state.input2} onChange={e => this.onInput2Change(e)} maxLength={20} />
54-
<Vcode />
68+
<Vcode onChange={(v)=>this.onVcode2Change(v)} value={this.state.code} width={this.state.width}/>
5569
<span>{this.state.input2 === this.state.vcode2 ? '输入正确' : '输入错误'}</span>
5670
</div>
5771
<hr />
5872
<button onClick={() => this.onChangeImg()}>更换图片</button>
5973
<button onClick={() => this.onChangeStr()}>外部生成随机字符串</button>
74+
<button onClick={() => this.onChangeWidth()}>改变width</button>
6075
</div>
6176
);
6277
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-vcode",
3-
"version": "1.0.1",
3+
"version": "1.0.3",
44
"description": "a react verification code component",
55
"main": "dist/index.js",
66
"scripts": {

src/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
2-
// equire('es6-object-assign').polyfill();
3-
class Vcode extends React.Component {
2+
require('es6-object-assign').polyfill();
3+
class Vcode extends React.PureComponent {
44
constructor(props) {
55
super(props);
66
this.state = {
@@ -114,25 +114,27 @@ class Vcode extends React.Component {
114114
}
115115

116116
/** 组件参数改变 **/
117-
UNSAFE_componentWillReceiveProps(nextP, nextS) {
118-
if (this.props.value !== nextP.value) {
119-
this.onDraw(nextP.value);
117+
componentDidUpdate(prevP){
118+
if (this.props.value !== prevP.value) {
119+
this.onDraw(this.props.value);
120120
}
121-
if (this.props.width !== nextP.width || this.props.height !== nextP.height) {
121+
if (this.props.width !== prevP.width || this.props.height !== prevP.height || this.props.style !== prevP.style) {
122122
this.setState({
123-
width: nextP.width,
124-
height: nextP.height,
123+
width: this.props.width || 150,
124+
height: this.props.height || 40,
125+
style: Object.assign({},this.state.style, {
126+
width: this.props.width ? `${this.props.width}px` : '150px',
127+
height: this.props.height ? `${this.props.height}px` : '40px',
128+
})
125129
});
126130
}
127131
}
128132

129-
/** 用户点击的验证码图片 **/
133+
/** 用户点击了验证码图片 **/
130134
onClick() {
131-
const div = document.getElementById(this.state.id);
132-
// 如果this.props.value有值,表明值是外部受控,这个地方不需要重新渲染
133-
let code = null;
135+
// 如果用户没有设置值,就直接重新生成
134136
if (!this.props.value) {
135-
code = this.onDraw(this.props.value);
137+
this.onDraw(this.props.value);
136138
}
137139
this.props.onClick && this.props.onClick(); // 触发外部的onClick,什么都不返回
138140
}

0 commit comments

Comments
 (0)