/**
* 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 strict-local
* @format
*/
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import RNTesterText from '../../components/RNTesterText';
import {useState} from 'react';
import {Alert, Pressable, StyleSheet, TextInput, View} from 'react-native';
type SectionProps = {
children: React.Node,
};
const FocusEventSection = ({children}: SectionProps): React.Node => {
const [outerFocused, setOuterFocused] = useState(false);
return (
setOuterFocused(true)}
onBlur={() => setOuterFocused(false)}>
Section focused: {outerFocused ? 'true' : 'false'}
{children}
);
};
const ViewExampleInnerRow = ({focusable}: {focusable: boolean}) => {
const [focused, setFocused] = useState(false);
return (
setFocused(true)}
onBlur={() => setFocused(false)}
style={[styles.viewInnerRow]}>
Focusable: {focusable ? 'true' : 'false'}
Focused: {focused ? 'true' : 'false'}
);
};
const ViewExample = () => {
return (
);
};
const PressableExample = () => {
const [pressableFocused, setPressableFocused] = useState(false);
const [disabledPressableFocused, setDisabledPressableFocused] =
useState(false);
return (
Alert.alert('Pressable pressed!')}
onFocus={() => setPressableFocused(true)}
onBlur={() => setPressableFocused(false)}>
Pressable focused: {pressableFocused ? 'true' : 'false'}
Alert.alert('Disabled Pressable pressed!')}
onFocus={() => setDisabledPressableFocused(true)}
onBlur={() => setDisabledPressableFocused(false)}>
Disabled Pressable focused:{' '}
{disabledPressableFocused ? 'true' : 'false'}
);
};
const TextInputExample = () => {
const [input1Focused, setInput1Focused] = useState(false);
const [input2Focused, setInput2Focused] = useState(false);
const [input3Focused, setInput3Focused] = useState(false);
return (
setInput1Focused(true)}
onBlur={() => setInput1Focused(false)}
style={[styles.textInput, input1Focused && styles.textInputFocused]}
placeholder={`Focused: ${input1Focused ? 'true' : 'false'}`}
/>
setInput2Focused(true)}
onBlur={() => setInput2Focused(false)}
style={[styles.textInput, input2Focused && styles.textInputFocused]}
placeholder={`Focused: ${input2Focused ? 'true' : 'false'}`}
/>
setInput3Focused(true)}
onBlur={() => setInput3Focused(false)}
style={[styles.textInput, input3Focused && styles.textInputFocused]}
placeholder={`Not editable focused: ${input3Focused ? 'true' : 'false'}`}
editable={false}
/>
);
};
const styles = StyleSheet.create({
container: {
padding: 8,
rowGap: 16,
},
sectionSubtitle: {
paddingBottom: 8,
},
sectionTitle: {
paddingBottom: 2,
fontSize: 20,
},
sectionWrapperFocused: {
borderColor: 'blue',
},
textFocused: {
backgroundColor: 'lightblue',
},
textLink: {color: 'blue'},
textInput: {
borderColor: 'rgba(40, 40, 40, 0.3)',
borderRadius: 4,
borderWidth: 1,
marginTop: 4,
},
textInputFocused: {
borderColor: 'blue',
},
viewInnerRow: {
backgroundColor: '#424B54',
borderRadius: 8,
borderColor: 'transparent',
borderWidth: 2,
height: 50,
marginTop: 4,
padding: 4,
width: '100%',
},
viewInnerRowTextColor: {
color: 'white',
},
});
export default {
title: 'Focus Events',
description: 'Examples that show how Focus events can be used.',
examples: [
{
title: 'View Example',
render: function (): React.Node {
return ;
},
},
{
title: 'TextInput Example',
render: function (): React.Node {
return ;
},
},
{
title: 'Pressable Example',
render: function (): React.Node {
return ;
},
},
] as Array,
};