-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new
useFocusedInputHandler
hook
- Loading branch information
1 parent
85c3293
commit 0b1f53c
Showing
9 changed files
with
158 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React, { useState } from 'react'; | ||
import { runOnJS } from 'react-native-reanimated'; | ||
import { act, render } from '@testing-library/react-native'; | ||
|
||
import { FocusedInputHandler, FocusedInputTextChangedEvent, useFocusedInputHandler, useReanimatedFocusedInput } from 'react-native-keyboard-controller'; | ||
import { Text } from 'react-native'; | ||
|
||
function WhatUserTyped() { | ||
const [text, setText] = useState(''); | ||
|
||
useFocusedInputHandler({ | ||
onChangeText: (e) => { | ||
'worklet'; | ||
|
||
runOnJS(setText)(e.text); | ||
}, | ||
}); | ||
|
||
return <Text testID='text'>{text}</Text>; | ||
} | ||
|
||
describe('`useFocusedInputHandler` specification', () => { | ||
it('should execute all handlers and change corresponding elements', () => { | ||
let handlers: FocusedInputHandler = {}; | ||
(useFocusedInputHandler as jest.Mock).mockImplementation( | ||
(handler) => (handlers = handler) | ||
); | ||
const onChangeText = (e: FocusedInputTextChangedEvent) => handlers.onChangeText?.(e); | ||
|
||
const { getByTestId } = render(<WhatUserTyped />); | ||
|
||
expect(getByTestId('text')).toHaveTextContent(''); | ||
act(() => onChangeText({text: '1'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('1'); | ||
|
||
act(() => onChangeText({text: '12'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('12'); | ||
|
||
act(() => onChangeText({text: '123'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('123'); | ||
|
||
act(() => onChangeText({text: ''})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
keywords: [react-native, react native, react-native-keyboard-controller, useFocusedInputHandler, onTextChanged, onChangeText, input interceptor, react-native-reanimated, worklet, react hook] | ||
--- | ||
|
||
# useFocusedInputHandler | ||
|
||
`useFocusedInputHandler` is a hook that allows to intercept events from a focused `TextInput`. | ||
|
||
## Example | ||
|
||
```ts | ||
useFocusedInputHandler({ | ||
onChangeText: ({text}) => { | ||
'worklet'; | ||
} | ||
}, []); | ||
``` | ||
|
||
### Handlers | ||
|
||
#### `onChangeText` | ||
|
||
Fires an event whenever user changes text in focused `TextInput` (i. e. adds or deletes symbols). Event has following structure: | ||
|
||
```ts | ||
type FocusedInputTextChangedEvent = { | ||
text: string; | ||
}; | ||
``` | ||
|
||
This handler can be handy when you need to have an access to what user typed on a global level (i. e. when you don't have a direct access to your `TextInput`), for example: | ||
|
||
- you develop a generic component for any kind of avoidance focused inputs (i. e. `AwareScrollView`) that doesn't have an access to child `TextInputs` by design; | ||
- you track user activity on the screen and if there is no activity for certain period of time then you do a certain action (logout for example). If you want to reset timer when user interacts with a keyboard - usage of this hook can be a good choice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React, { useState } from 'react'; | ||
import { runOnJS } from 'react-native-reanimated'; | ||
import { act, render } from '@testing-library/react-native'; | ||
|
||
import { FocusedInputHandler, FocusedInputTextChangedEvent, useFocusedInputHandler, useReanimatedFocusedInput } from 'react-native-keyboard-controller'; | ||
import { Text } from 'react-native'; | ||
|
||
function WhatUserTyped() { | ||
const [text, setText] = useState(''); | ||
|
||
useFocusedInputHandler({ | ||
onChangeText: (e) => { | ||
'worklet'; | ||
|
||
runOnJS(setText)(e.text); | ||
}, | ||
}); | ||
|
||
return <Text testID='text'>{text}</Text>; | ||
} | ||
|
||
describe('`useFocusedInputHandler` specification', () => { | ||
it('should execute all handlers and change corresponding elements', () => { | ||
let handlers: FocusedInputHandler = {}; | ||
(useFocusedInputHandler as jest.Mock).mockImplementation( | ||
(handler) => (handlers = handler) | ||
); | ||
const onChangeText = (e: FocusedInputTextChangedEvent) => handlers.onChangeText?.(e); | ||
|
||
const { getByTestId } = render(<WhatUserTyped />); | ||
|
||
expect(getByTestId('text')).toHaveTextContent(''); | ||
act(() => onChangeText({text: '1'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('1'); | ||
|
||
act(() => onChangeText({text: '12'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('12'); | ||
|
||
act(() => onChangeText({text: '123'})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent('123'); | ||
|
||
act(() => onChangeText({text: ''})); | ||
|
||
expect(getByTestId('text')).toHaveTextContent(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters