-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This adds a simple debug UI to verify the keyboard events "keyboardDidShow" and "keyboardWillShow" Changelog: [Internal][Added] - Add "KeyboardExample" to RNTester Reviewed By: cortinico Differential Revision: D38500869 fbshipit-source-id: 99913a05849a7dd27dfdee2d622058b9c2604a7f
- Loading branch information
1 parent
434adbd
commit 584b968
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/rn-tester/js/examples/Keyboard/KeyboardExample.js
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,113 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type { | ||
RNTesterModule, | ||
RNTesterModuleExample, | ||
} from '../../types/RNTesterTypes'; | ||
import type {KeyboardEvent} from 'react-native/Libraries/Components/Keyboard/Keyboard'; | ||
|
||
import * as React from 'react'; | ||
import {useEffect, useState} from 'react'; | ||
import {Keyboard, StyleSheet, Text, View} from 'react-native'; | ||
|
||
type KeybpardEventViewerProps = { | ||
showEvent: 'keyboardWillShow' | 'keyboardDidShow', | ||
hideEvent: 'keyboardWillHide' | 'keyboardDidHide', | ||
}; | ||
|
||
const KeyboardEventViewer = (props: KeybpardEventViewerProps): React.Node => { | ||
const {showEvent, hideEvent} = props; | ||
const [isShown, setIsShown] = useState(false); | ||
const [lastEvent, setLastEvent] = useState<?KeyboardEvent>(); | ||
|
||
useEffect(() => { | ||
const subscription = Keyboard.addListener(showEvent, ev => { | ||
setIsShown(true); | ||
setLastEvent(ev); | ||
}); | ||
return () => subscription.remove(); | ||
}, [showEvent]); | ||
|
||
useEffect(() => { | ||
const subscription = Keyboard.addListener(hideEvent, ev => { | ||
setIsShown(false); | ||
setLastEvent(ev); | ||
}); | ||
return () => subscription.remove(); | ||
}, [hideEvent]); | ||
|
||
return ( | ||
<View> | ||
<Text> | ||
<Text>Keyboard is </Text> | ||
{isShown ? ( | ||
<Text style={styles.openText}>open</Text> | ||
) : ( | ||
<Text style={styles.closeText}>closed</Text> | ||
)} | ||
</Text> | ||
<View style={styles.eventBox}> | ||
<Text> | ||
{lastEvent | ||
? JSON.stringify(lastEvent, null, 2) | ||
: 'No events observed'} | ||
</Text> | ||
</View> | ||
</View> | ||
); | ||
}; | ||
|
||
const keyboardWillShowHideExample: RNTesterModuleExample = { | ||
title: 'keyboardWillShow / keyboardWillHide', | ||
platform: 'ios', | ||
render: () => ( | ||
<KeyboardEventViewer | ||
showEvent="keyboardWillShow" | ||
hideEvent="keyboardWillHide" | ||
/> | ||
), | ||
}; | ||
|
||
const keyboardDidShowHideExample: RNTesterModuleExample = { | ||
title: 'keyboardDidShow / keyboardDidHide', | ||
render: () => ( | ||
<KeyboardEventViewer | ||
showEvent="keyboardDidShow" | ||
hideEvent="keyboardDidHide" | ||
/> | ||
), | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
closeText: { | ||
color: 'red', | ||
}, | ||
openText: { | ||
color: 'green', | ||
}, | ||
eventBox: { | ||
marginTop: 10, | ||
padding: 5, | ||
borderWidth: StyleSheet.hairlineWidth, | ||
}, | ||
}); | ||
|
||
const KeyboardExample: RNTesterModule = { | ||
title: 'Keyboard', | ||
description: 'Demonstrates usage of the "Keyboard" static API', | ||
documentationURL: 'https://reactnative.dev/docs/keyboard', | ||
category: 'Basic', | ||
examples: [keyboardWillShowHideExample, keyboardDidShowHideExample], | ||
}; | ||
|
||
export default KeyboardExample; |
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