/**
* 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.
*
* @flow
* @format
*/
'use strict';
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import * as React from 'react';
import {useState} from 'react';
import {
Alert,
Button,
KeyboardAvoidingView,
Modal,
Pressable,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native';
const onButtonPress = () => {
Alert.alert('Successfully Registered!');
};
const TextInputForm = () => {
return (
);
};
const CloseButton = (props: {setModalOpen: (v: boolean) => void}) => {
return (
props.setModalOpen(false)}
style={styles.closeButton}>
Close
);
};
type KeyboardAvoidingBehavior = 'padding' | 'position' | 'height';
const BEHAVIORS: Array = [
'padding',
'position',
'height',
];
const BEHAVIOR_DESCRIPTIONS: {[key: KeyboardAvoidingBehavior]: string} = {
padding: 'Sets bottom padding equal to the keyboard overlap.',
position: 'Offsets the content container up by the keyboard overlap.',
height: 'Shrinks the view height by the keyboard overlap.',
};
const BehaviorPicker = (props: {
behavior: KeyboardAvoidingBehavior,
setBehavior: (v: KeyboardAvoidingBehavior) => void,
}) => {
return (
{BEHAVIORS.map(behavior => {
const selected = props.behavior === behavior;
return (
props.setBehavior(behavior)}
style={[
styles.pillStyle,
{backgroundColor: selected ? 'blue' : 'white'},
]}>
{behavior}
);
})}
{BEHAVIOR_DESCRIPTIONS[props.behavior]}
);
};
const KeyboardAvoidingViewBehaviour = () => {
const [modalOpen, setModalOpen] = useState(false);
const [behavior, setBehavior] = useState('padding');
return (
setModalOpen(true)}>
Open Example
);
};
const KeyboardAvoidingDisabled = () => {
const [modalOpen, setModalOpen] = useState(false);
return (
setModalOpen(true)}>
Open Example
);
};
const KeyboardAvoidingVerticalOffset = () => {
const [modalOpen, setModalOpen] = useState(false);
return (
setModalOpen(true)}>
Open Example
);
};
const KeyboardAvoidingContentContainerStyle = () => {
const [modalOpen, setModalOpen] = useState(false);
return (
setModalOpen(true)}>
Open Example
);
};
const KeyboardAvoidingScrollView = () => {
const [modalOpen, setModalOpen] = useState(false);
const [behavior, setBehavior] = useState('padding');
return (
{Array.from({length: 10}, (_, index) => (
Item {index + 1}
))}
setModalOpen(true)}>
Open Example
);
};
const styles = StyleSheet.create({
outerContainer: {
flex: 1,
},
container: {
flex: 1,
justifyContent: 'center',
alignSelf: 'center',
paddingHorizontal: 20,
paddingTop: 20,
width: '100%',
maxWidth: 340,
},
contentContainer: {
paddingTop: 20,
backgroundColor: '#abdebf',
},
scrollViewContainer: {
flex: 1,
paddingTop: 100,
},
scrollViewContent: {
paddingBottom: 60,
paddingHorizontal: 20,
},
fillerItem: {
backgroundColor: '#eeeeee',
borderRadius: 8,
paddingVertical: 16,
paddingHorizontal: 12,
marginBottom: 20,
alignItems: 'center',
},
textInput: {
borderRadius: 5,
borderWidth: 1,
minHeight: 44,
marginBottom: 20,
paddingHorizontal: 10,
},
multilineTextInput: {
minHeight: 88,
paddingVertical: 10,
textAlignVertical: 'top',
},
pillStyle: {
padding: 10,
marginHorizontal: 5,
marginVertical: 10,
borderRadius: 20,
borderWidth: 1,
borderColor: 'blue',
},
closeButton: {
alignSelf: 'flex-end',
marginVertical: 10,
padding: 10,
},
touchableText: {
fontWeight: '500',
color: 'blue',
},
behaviorDescription: {
textAlign: 'center',
alignSelf: 'center',
width: 200,
},
});
exports.title = 'KeyboardAvoidingView';
exports.description =
'Base component for views that automatically adjust their height or position to move out of the way of the keyboard.';
exports.examples = [
{
title: 'Keyboard Avoiding View with different behaviors',
description:
('Specify how to react to the presence of the keyboard. Android and iOS both interact ' +
'with this prop differently. On both iOS and Android, setting behavior is recommended.') as string,
render(): React.Node {
return ;
},
},
{
title: 'Keyboard Avoiding View with keyboardVerticalOffset={distance}',
description:
('This is the distance between the top of the user screen and the React Native ' +
'view, may be non-zero in some use cases. Defaults to 0.') as string,
render(): React.Node {
return ;
},
},
{
title: 'Keyboard Avoiding View with enabled={false}',
description: 'Disable the KeyboardAvoidingView.' as string,
render(): React.Node {
return ;
},
},
{
title: 'Keyboard Avoiding View with contentContainerStyle',
description:
'Specify the style of the content container View when behavior is set to position.' as string,
render(): React.Node {
return ;
},
},
{
title: 'Keyboard Avoiding View with ScrollView',
description:
('A ScrollView with filler content and TextInputs at the bottom inside the scrollable content. ' +
'TextInputs should still be visible when focused.') as string,
render(): React.Node {
return ;
},
},
] as Array;