Skip to content
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

(Bug) email/sms code verification not responsive #2796

Merged
merged 1 commit into from
Dec 10, 2020
Merged
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
25 changes: 19 additions & 6 deletions src/components/common/form/OtpInput.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import React, { useEffect, useState } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { Platform, TextInput, View } from 'react-native'
import { withStyles } from '../../../lib/styles'
import { getDesignRelativeHeight, getDesignRelativeWidth } from '../../../lib/utils/sizes'
Expand Down Expand Up @@ -74,16 +74,29 @@ const getSingleOtpInputStylesFromProps = ({ theme }) => ({
},
})

const useTextInputSelection = (value, setSelection) => {
const hasValue = useMemo(() => value && value.length, [value])
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use isEmpty from lodash


const updateSelection = useCallback(() => {
setSelection(hasValue ? { start: 0, end: 1 } : undefined)
}, [hasValue, setSelection])

return [hasValue, updateSelection]
}

const Input = ({ min, max, pattern, focus, shouldAutoFocus, onChange, value, focusNextInput, ...props }) => {
let input: ?HTMLInputElement = null
const [selection, setSelection] = useState({ start: 0, end: value && value.length ? 1 : 0 })
const [selection, setSelection] = useState(undefined)

// Don't pass selection prop if value is empty (known TextInput bug)
const [hasValue, updateSelection] = useTextInputSelection(value, setSelection)

// Focus on first render
// Only when shouldAutoFocus is true
useEffect(() => {
if (input && focus && shouldAutoFocus) {
input.focus()
setSelection({ start: 0, end: value && value.length ? 1 : 0 })
updateSelection()
}
}, [])

Expand All @@ -92,12 +105,12 @@ const Input = ({ min, max, pattern, focus, shouldAutoFocus, onChange, value, foc
useEffect(() => {
if (input && focus) {
input.focus()
setSelection({ start: 0, end: value && value.length ? 1 : 0 })
updateSelection()
}
}, [focus])

const handleSelection = ({ nativeEvent: { selection: nativeSelection } }) => {
setSelection({ start: 0, end: value && value.length ? 1 : 0 })
updateSelection()
}

const handleValidation = (inputValue: number | string): boolean =>
Expand All @@ -118,7 +131,7 @@ const Input = ({ min, max, pattern, focus, shouldAutoFocus, onChange, value, foc
onChangeText={handleOnChange}
ref={inputRef => (input = inputRef)}
onSelectionChange={handleSelection}
selection={selection}
selection={hasValue ? selection : undefined}
selectTextOnFocus={true}
/>
)
Expand Down