Skip to content

Commit 3d7973a

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Add focus/blur example to RNTester (#51634)
Summary: Pull Request resolved: #51634 Creates an example in the ViewExample setup for how onBlur/onFocus behave when using keyboard navigation Changelog: [Internal] Reviewed By: javache Differential Revision: D75238317 fbshipit-source-id: e69122ca17727fc7f71e9bb7a09098a2771b098e
1 parent af0a76c commit 3d7973a

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

packages/rn-tester/js/examples/View/ViewExample.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {RNTesterModule} from '../../types/RNTesterTypes';
1414

1515
import RNTesterText from '../../components/RNTesterText';
1616
import * as React from 'react';
17+
import {useState} from 'react';
1718
import {
1819
Platform,
1920
PlatformColor,
@@ -699,6 +700,74 @@ function BoxSizingExample(): React.Node {
699700
);
700701
}
701702

703+
function FocusableInnerRow({focusable}: {focusable: boolean}) {
704+
const styles = StyleSheet.create({
705+
focused: {
706+
borderColor: 'blue',
707+
borderWidth: 2,
708+
},
709+
innerBox: {
710+
backgroundColor: 'red',
711+
width: '100%',
712+
height: 50,
713+
borderColor: 'transparent',
714+
borderWidth: 2,
715+
},
716+
innerBoxTextColor: {
717+
color: 'white',
718+
},
719+
});
720+
const [focused, setFocused] = useState(false);
721+
return (
722+
<View
723+
accessible={focusable}
724+
focusable={focusable}
725+
onBlur={() => setFocused(false)}
726+
onFocus={() => setFocused(true)}
727+
style={[styles.innerBox, focused && styles.focused]}>
728+
<RNTesterText style={styles.innerBoxTextColor}>
729+
Focusable: {focusable ? 'true' : 'false'}
730+
</RNTesterText>
731+
<RNTesterText style={styles.innerBoxTextColor}>
732+
Focused: {focused ? 'true' : 'false'}
733+
</RNTesterText>
734+
</View>
735+
);
736+
}
737+
738+
function FocusBlurExample(): React.Node {
739+
const styles = StyleSheet.create({
740+
focused: {
741+
borderColor: 'blue',
742+
borderWidth: 2,
743+
},
744+
outerBox: {
745+
backgroundColor: 'green',
746+
borderColor: 'transparent',
747+
borderWidth: 2,
748+
padding: 10,
749+
},
750+
outerBoxTextColor: {
751+
color: 'white',
752+
},
753+
});
754+
const [outerFocused, setOuterFocused] = useState(false);
755+
return (
756+
<View
757+
onBlur={() => setOuterFocused(false)}
758+
onFocus={() => setOuterFocused(true)}
759+
style={[styles.outerBox, outerFocused && styles.focused]}>
760+
<RNTesterText style={styles.outerBoxTextColor}>
761+
Focused: {outerFocused ? 'true' : 'false'}
762+
</RNTesterText>
763+
<FocusableInnerRow focusable={true} />
764+
<FocusableInnerRow focusable={true} />
765+
<FocusableInnerRow focusable={false} />
766+
<FocusableInnerRow focusable={true} />
767+
</View>
768+
);
769+
}
770+
702771
export default ({
703772
title: 'View',
704773
documentationURL: 'https://reactnative.dev/docs/view',
@@ -1363,5 +1432,10 @@ export default ({
13631432
name: 'box-sizing',
13641433
render: BoxSizingExample,
13651434
},
1435+
{
1436+
title: 'Focus/Blur',
1437+
name: 'focus-blur',
1438+
render: FocusBlurExample,
1439+
},
13661440
],
13671441
}: RNTesterModule);

0 commit comments

Comments
 (0)