Skip to content

Commit 69a4cf2

Browse files
authored
Feat/guid (#29)
1 parent 255ff2e commit 69a4cf2

8 files changed

Lines changed: 36 additions & 13 deletions

File tree

example/dist/example.js

Lines changed: 14 additions & 1 deletion
Large diffs are not rendered by default.

example/dist/example.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ReactCodeInput.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ReactCodeInput.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-code-input",
3-
"version": "3.0.0",
3+
"version": "3.2.0",
44
"description": "React component for entering and validating numbers, text or password.",
55
"main": "lib/ReactCodeInput.js",
66
"scripts": {

src/ReactCodeInput.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
4+
import { uuidv4 } from './utils';
45

56
const BACKSPACE_KEY = 8;
67
const LEFT_ARROW_KEY = 37;
@@ -44,6 +45,8 @@ class ReactCodeInput extends Component {
4445
}
4546

4647
this.textInput = [];
48+
49+
this.uuid = uuidv4();
4750
}
4851

4952
componentWillReceiveProps(nextProps) {
@@ -84,13 +87,13 @@ class ReactCodeInput extends Component {
8487

8588
if (value.length > 1) {
8689
value.split('').map((chart, i) => {
87-
if (Number(e.target.id) + i < this.props.fields) {
88-
input[Number(e.target.id) + i] = chart;
90+
if (Number(e.target.dataset.id) + i < this.props.fields) {
91+
input[Number(e.target.dataset.id) + i] = chart;
8992
}
9093
return false;
9194
});
9295
} else {
93-
input[Number(e.target.id)] = value;
96+
input[Number(e.target.dataset.id)] = value;
9497
}
9598

9699
input.map((s, i) => {
@@ -100,7 +103,7 @@ class ReactCodeInput extends Component {
100103
return false;
101104
});
102105

103-
const newTarget = this.textInput[e.target.id < input.length ? Number(e.target.id) + 1 : e.target.id];
106+
const newTarget = this.textInput[e.target.dataset.id < input.length ? Number(e.target.dataset.id) + 1 : e.target.dataset.id];
104107

105108
if (newTarget) {
106109
newTarget.focus();
@@ -120,7 +123,7 @@ class ReactCodeInput extends Component {
120123
}
121124

122125
handleKeyDown(e) {
123-
const target = Number(e.target.id),
126+
const target = Number(e.target.dataset.id),
124127
nextTarget = this.textInput[target + 1],
125128
prevTarget = this.textInput[target - 1];
126129

@@ -225,7 +228,8 @@ class ReactCodeInput extends Component {
225228
ref={(ref) => {
226229
this.textInput[i] = ref;
227230
}}
228-
id={i}
231+
id={`${this.uuid}-${i}`}
232+
data-id={i}
229233
autoFocus={autoFocus && (i === 0) ? 'autoFocus' : ''}
230234
defaultValue={value}
231235
key={`input_${i}`}

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const uuidv4 = () => {
2+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
3+
let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
4+
return v.toString(16);
5+
});
6+
};

0 commit comments

Comments
 (0)