Skip to content

Commit cc27ee0

Browse files
author
ugogo
committed
add onChange prop
1 parent 2b654fb commit cc27ee0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

playground/src/index.tsx

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

88
ReactDOM.render(
9-
<ReactInputVerificationCode />,
9+
<ReactInputVerificationCode onChange={console.log} />,
1010
document.getElementById('root')
1111
);

src/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ const KEY_CODE = {
88
DELETE: 46,
99
};
1010

11-
export default ({ length = 4, placeholder = '·' }) => {
11+
type Props = {
12+
length?: number;
13+
onChange: (data: string) => any;
14+
placeholder?: string;
15+
};
16+
17+
export default ({ length = 4, onChange, placeholder = '·' }: Props) => {
1218
const [activeIndex, setActiveIndex] = React.useState<number>(-1);
1319
const [value, setValue] = React.useState<string[]>(
1420
new Array(length).fill(placeholder)
@@ -93,6 +99,10 @@ export default ({ length = 4, placeholder = '·' }) => {
9399
blurItem(activeIndex);
94100
};
95101

102+
React.useEffect(() => {
103+
onChange(value.join(''));
104+
}, [value]);
105+
96106
return (
97107
<div
98108
className='ReactInputVerificationCode__container'

0 commit comments

Comments
 (0)