Skip to content

Commit d29b3e2

Browse files
committed
feat: use real input placeholder
1 parent 221903b commit d29b3e2

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/index.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ const ReactInputVerificationCode = ({
3232
type = 'number',
3333
}: ReactInputVerificationCodeProps) => {
3434
/**
35-
* generate a new array filled with placeholders
36-
* map through it and replace with the pasted value when possible
35+
* generate a new array, map through it
36+
* and replace with the value when possible
3737
*/
38-
const fillValues = (value: string) => {
39-
const emptyValues = new Array(length).fill(placeholder);
40-
const nextValues = value.slice(0, length);
41-
42-
return [...emptyValues].map((value, index) => nextValues[index] || value);
43-
};
38+
const fillValues = (value: string) =>
39+
new Array(length).fill('').map((_, index) => value[index] ?? '');
4440

4541
const [values, setValues] = useState(fillValues(defaultValue));
4642
const [focusedIndex, setFocusedIndex] = useState<number>(-1);
@@ -79,7 +75,7 @@ const ReactInputVerificationCode = ({
7975
setValues(nextValues);
8076

8177
const stringifiedValues = nextValues.join('');
82-
const isCompleted = !stringifiedValues.includes(placeholder);
78+
const isCompleted = stringifiedValues.length === length;
8379

8480
if (isCompleted) {
8581
onCompleted(stringifiedValues);
@@ -165,7 +161,7 @@ const ReactInputVerificationCode = ({
165161
*/
166162
event.preventDefault();
167163

168-
setValue(placeholder, focusedIndex);
164+
setValue('', focusedIndex);
169165
focusInput(index - 1);
170166

171167
return;
@@ -235,6 +231,7 @@ const ReactInputVerificationCode = ({
235231
onFocus={() => onInputFocus(i)}
236232
onKeyDown={(event) => onInputKeyDown(event, i)}
237233
onPaste={(event) => onInputPaste(event, i)}
234+
placeholder={placeholder}
238235
/>
239236
))}
240237
</div>

0 commit comments

Comments
 (0)