@@ -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 }
0 commit comments