Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ const CodeInput = () => {
const [code, setCode] = useState('');
const [containerIsFocused, setContainerIsFocused] = useState(false);

const codeDigitsArray = new Array(CODE_LENGTH);
const codeDigitsArray = [...Array(CODE_LENGTH)];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use spread operator to create array of CODE_LENGTH length, the original code could not be mapped

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixed my issue as well. Otherwise, the fields do not show.


const ref = useRef<TextInput>(null);

const setCodeInput = (input: string) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore non-numeric input

if (input.length === 0) setCode(input);
const last = input[input.length - 1];
if (last?.trim() === '') return; // ignore space, (+' ') == 0
// make sure the input is numeric before updating
if (+last >= 0 && +last <= 9) setCode(input);
};

const handleOnPress = () => {
setContainerIsFocused(true);
ref?.current?.focus();
Expand All @@ -37,10 +45,10 @@ const CodeInput = () => {

const isFocused = isCurrentDigit || (isLastDigit && isCodeFull);

const containerStyle =
containerIsFocused && isFocused
? {...style.inputContainer, ...style.inputContainerFocused}
: style.inputContainer;
const containerStyle = [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the style prop can accept an array of styles

style.code_input_inputContainer,
isFocused && containerIsFocused && style.code_input_inputContainerFocused,
];

return (
<View key={idx} style={containerStyle}>
Expand All @@ -57,8 +65,9 @@ const CodeInput = () => {
<TextInput
ref={ref}
value={code}
onChangeText={setCode}
onChangeText={setCodeInput}
onSubmitEditing={handleOnBlur}
onBlur={handleOnBlur}
keyboardType="number-pad"
returnKeyType="done"
textContentType="oneTimeCode"
Expand Down