File tree Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -46,12 +46,13 @@ export default function App() {
4646
4747### Props
4848
49- | Key | Type | Default | Required | Description |
50- | ----------- | ---------- | ------- | -------- | --------------------------------------------------------- |
51- | length | ` number ` | ` 4 ` | false | How many items will be rendered |
52- | onChange | ` function ` | | true | Function called when the value changes |
53- | placeholder | ` string ` | ` · ` | false | String rendered in each item when no value has been typed |
54- | value | ` string ` | | false | Control internal input value |
49+ | Key | Type | Default | Required | Description |
50+ | ----------- | ---------- | ---------- | -------- | --------------------------------------------------------- |
51+ | autoFocus | ` boolean ` | false | false | Focus on render |
52+ | length | ` number ` | ` 4 ` | false | How many items will be rendered |
53+ | onChange | ` function ` | ` () => {} ` | true | Function called when the value changes |
54+ | placeholder | ` string ` | ` · ` | false | String rendered in each item when no value has been typed |
55+ | value | ` string ` | ` () => {} ` | false | Control internal input value |
5556
5657### CSS Properties
5758
Original file line number Diff line number Diff line change @@ -5,6 +5,6 @@ import ReactInputVerificationCode from 'react-input-verification-code';
55import './index.css' ;
66
77ReactDOM . render (
8- < ReactInputVerificationCode onChange = { console . log } /> ,
8+ < ReactInputVerificationCode autoFocus onChange = { console . log } /> ,
99 document . getElementById ( 'root' )
1010) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const KEY_CODE = {
1010} ;
1111
1212type Props = {
13+ autoFocus ?: boolean ;
1314 length ?: number ;
1415 onChange ?: ( data : string ) => any ;
1516 onCompleted ?: ( data : string ) => any ;
@@ -18,6 +19,7 @@ type Props = {
1819} ;
1920
2021const ReactInputVerificationCode = ( {
22+ autoFocus = false ,
2123 length = 4 ,
2224 onChange = ( ) => { } ,
2325 onCompleted = ( ) => { } ,
@@ -119,6 +121,13 @@ const ReactInputVerificationCode = ({
119121 setActiveIndex ( - 1 ) ;
120122 } ;
121123
124+ // autoFocus
125+ React . useEffect ( ( ) => {
126+ if ( autoFocus && itemsRef [ 0 ] . current ) {
127+ itemsRef [ 0 ] . current . focus ( ) ;
128+ }
129+ } , [ ] ) ;
130+
122131 // handle pasting
123132 React . useEffect ( ( ) => {
124133 const codeInput = codeInputRef . current ;
You can’t perform that action at this time.
0 commit comments