-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: App crashes after pressing Backspace/Delete key in Incorrect magic code page #23734
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,6 @@ function MagicCodeInput(props) { | |
}; | ||
|
||
const focusMagicCodeInput = () => { | ||
setFocusedIndex(0); | ||
inputRefs.current[0].focus(); | ||
}; | ||
|
||
|
@@ -120,9 +119,6 @@ function MagicCodeInput(props) { | |
focusMagicCodeInput(); | ||
}, | ||
clear() { | ||
setInput(''); | ||
setFocusedIndex(0); | ||
setEditIndex(0); | ||
inputRefs.current[0].focus(); | ||
props.onChangeText(''); | ||
}, | ||
|
@@ -176,23 +172,13 @@ function MagicCodeInput(props) { | |
}, []); | ||
|
||
/** | ||
* Focuses on the input when it is pressed. | ||
* | ||
* @param {Object} event | ||
* @param {Number} index | ||
*/ | ||
const onFocus = (event) => { | ||
event.preventDefault(); | ||
}; | ||
|
||
/** | ||
* Callback for the onPress event, updates the indexes | ||
* Callback for the onFocus event, updates the indexes | ||
* of the currently focused input. | ||
* | ||
* @param {Object} event | ||
* @param {Number} index | ||
*/ | ||
const onPress = (event, index) => { | ||
const onFocus = (event, index) => { | ||
event.preventDefault(); | ||
setInput(''); | ||
setFocusedIndex(index); | ||
|
@@ -224,8 +210,7 @@ function MagicCodeInput(props) { | |
let numbers = decomposeString(props.value, props.maxLength); | ||
numbers = [...numbers.slice(0, editIndex), ...numbersArr, ...numbers.slice(numbersArr.length + editIndex, props.maxLength)]; | ||
|
||
setFocusedIndex(updatedFocusedIndex); | ||
setInput(value); | ||
inputRefs.current[updatedFocusedIndex].focus(); | ||
Comment on lines
-227
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to set the input to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intentionally take out the value bcoz I think we need to reset it once the value has been given for the input box. Its impact can be seen in the last input box where the focusedIndex isn't changing if the full code isn't Fulfilled. Screen.Recording.2023-07-28.at.12.27.09.AM.mp4 |
||
|
||
const finalInput = composeToString(numbers); | ||
props.onChangeText(finalInput); | ||
|
@@ -265,13 +250,6 @@ function MagicCodeInput(props) { | |
} | ||
|
||
const newFocusedIndex = Math.max(0, focusedIndex - 1); | ||
|
||
// Saves the input string so that it can compare to the change text | ||
// event that will be triggered, this is a workaround for mobile that | ||
// triggers the change text on the event after the key press. | ||
setInput(''); | ||
setFocusedIndex(newFocusedIndex); | ||
setEditIndex(newFocusedIndex); | ||
props.onChangeText(composeToString(numbers)); | ||
|
||
if (!_.isUndefined(newFocusedIndex)) { | ||
|
@@ -280,15 +258,9 @@ function MagicCodeInput(props) { | |
} | ||
if (keyValue === 'ArrowLeft' && !_.isUndefined(focusedIndex)) { | ||
const newFocusedIndex = Math.max(0, focusedIndex - 1); | ||
setInput(''); | ||
setFocusedIndex(newFocusedIndex); | ||
setEditIndex(newFocusedIndex); | ||
inputRefs.current[newFocusedIndex].focus(); | ||
} else if (keyValue === 'ArrowRight' && !_.isUndefined(focusedIndex)) { | ||
const newFocusedIndex = Math.min(focusedIndex + 1, props.maxLength - 1); | ||
setInput(''); | ||
setFocusedIndex(newFocusedIndex); | ||
setEditIndex(newFocusedIndex); | ||
inputRefs.current[newFocusedIndex].focus(); | ||
} else if (keyValue === 'Enter') { | ||
// We should prevent users from submitting when it's offline. | ||
|
@@ -346,8 +318,7 @@ function MagicCodeInput(props) { | |
onChangeText(value); | ||
}} | ||
onKeyPress={onKeyPress} | ||
onPress={(event) => onPress(event, index)} | ||
onFocus={onFocus} | ||
onFocus={(event) => onFocus(event, index)} | ||
caretHidden={isMobileSafari} | ||
inputStyle={[isMobileSafari ? styles.magicCodeInputTransparent : undefined]} | ||
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will set the input to an empty string. I think we can remove the resetFocus method now as it's same as focus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resetFocus
is added here for this issue. Are you suggesting that we can usefocus
method there instead ofresetFocus
and removeresetFocus
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree that we don't need an extra method now, I just committed the changes.