Skip to content

Commit d1f17fe

Browse files
committed
support autoFocus prop
1 parent d48512d commit d1f17fe

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff 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

playground/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import ReactInputVerificationCode from 'react-input-verification-code';
55
import './index.css';
66

77
ReactDOM.render(
8-
<ReactInputVerificationCode onChange={console.log} />,
8+
<ReactInputVerificationCode autoFocus onChange={console.log} />,
99
document.getElementById('root')
1010
);

src/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const KEY_CODE = {
1010
};
1111

1212
type 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

2021
const 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;

0 commit comments

Comments
 (0)