Keyboard.Dismiss() function does not work

Asked

Viewed 118 times

-1

I’m trying to make the virtual keyboard disappear when I press any part of the screen in React Native, but it doesn’t work.

I have tried to put this function in the views, Texts and even create a constant on the outside with TouchableWithoutFeedback, but nothing, what can I do?

export default function StatusFile() {
const searchBar = React.createRef();
const [search, setSearch] = useState('');

return(
    <View style={styles.container} onPress={**() => Keyboard.dismiss()**}>
        <ScrollView style={styles.scroll}>
            <SearchBar 
                text={search}
                ref={searchBar}
                placeholder='Procurar...'
                barStyle='black'    
                textFieldBackgroundColor='#FF8C00'
                onChangeText={setSearch}
                onSearchButtonPress={() => searchBar.current.blur()}
            />

            {search !== '' ?  
            statusFile.filter(a => a.toLowerCase().indexOf(search.toLowerCase()) !== -1).map(a => (
                <Text style={styles.texto} key={a}>
                    {a}
                </Text>
            ))
            : 
            <Text style={styles.texto}>Faça uma busca pelo status, EX: 10</Text>
            }  
        </ScrollView>
    </View>
);

1 answer

0


The element <View> does not own a property onPress. You can remove the <View> and use only the <ScrollView>, occupying the entire screen.

The estate keyboardShouldPersistTaps of <ScrollView> determines when the keyboard should remain visible after a touch. Possible values are:

  • 'never': It is the default behavior. Tapping outside a text input causes the keyboard to be closed and the touched component does not receive the touch. For example, if the keyboard is open and a button is played, the keyboard is closed and nothing else happens.

  • 'always': The keyboard will not be closed automatically and the <ScrollView> It won’t capture touches, but the children will be able to capture them. For example, if the keyboard is open and a button is played, the keyboard remains open and the button will run the onPress.

  • 'handled': The keyboard will not close automatically when the ringtone is captured by children of <ScrollView> (or if the touch is captured by an ancestor). If the touch is not captured by any element, the keyboard is closed.

In your case, 'never' or 'handled' can serve, depending on your desired behavior. For example:

<ScrollView style={styles.scroll} keyboardShouldPersistTaps='handled'>
  // ...
</ScrollView>
  • 1

    Rafael, thank you very much, solved my problem and also showed that I need to deepen more in the properties of the elements I use, a hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.