unfocus search input when keyboard is hidden

This commit is contained in:
Dawid Wysokiński 2021-04-10 14:55:15 +02:00
parent 6ea0969b45
commit 1fd7164b88
2 changed files with 24 additions and 3 deletions

View File

@ -1,7 +1,8 @@
/// <reference path="../../../node_modules/native-base/index.d.ts" />
import { FlatListProps } from 'react-native';
declare module 'native-base' {
import { FlatListProps } from 'react-native';
namespace NativeBase {
interface List {
ListEmptyComponent?: FlatListProps<any>['ListEmptyComponent'];

View File

@ -1,6 +1,7 @@
import React, { useState } from 'react';
import React, { useState, useRef, useEffect } from 'react';
import { useDebounce } from 'react-use';
import { Keyboard, TextInput } from 'react-native';
import { Icon, Input, Item, Header as NBHeader } from 'native-base';
export interface HeaderProps {
@ -8,6 +9,7 @@ export interface HeaderProps {
}
const Header = ({ onSearch }: HeaderProps) => {
const inputRef = useRef<Input>(null);
const [search, setSearch] = useState('');
useDebounce(
() => {
@ -18,12 +20,30 @@ const Header = ({ onSearch }: HeaderProps) => {
500,
[search],
);
useEffect(() => {
const subscription = Keyboard.addListener('keyboardDidHide', () => {
const input = (inputRef.current as any)?._root as TextInput;
if (input?.isFocused()) {
input?.blur();
}
});
return () => {
subscription.remove();
};
}, []);
return (
<NBHeader searchBar rounded hasSegment>
<Item>
<Icon name="ios-search" />
<Input placeholder="Wyszukaj kwalifikację" onChangeText={setSearch} />
<Input
placeholder="Wyszukaj kwalifikację"
onChangeText={setSearch}
value={search}
ref={inputRef}
/>
</Item>
<Icon name="ios-search" />
</NBHeader>
);
};