Skip to content

Fix so that onfocus events are fired on single and multiline TextInpu… #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ - (NSAttributedString*)attributedText

- (BOOL)becomeFirstResponder
{
return [self.window makeFirstResponder:self];
BOOL success = [[self window] makeFirstResponder:self];

if (success) {
id<RCTBackedTextInputDelegate> textInputDelegate = [self textInputDelegate];
if ([textInputDelegate respondsToSelector:@selector(textInputDidBeginEditing)]) {
[textInputDelegate textInputDidBeginEditing];
}
}

return success;
}

#endif // ]TODO(macOS ISS#2323203)
Expand Down
18 changes: 9 additions & 9 deletions Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,6 @@ - (CGRect)editingRectForBounds:(CGRect)bounds

#pragma mark - NSTextViewDelegate methods

- (void)textDidBeginEditing:(NSNotification *)notification
{
[super textDidBeginEditing:notification];
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldBeginEditing:)]) {
[delegate textFieldBeginEditing:self];
}
}

- (void)textDidChange:(NSNotification *)notification
{
[super textDidChange:notification];
Expand Down Expand Up @@ -358,6 +349,15 @@ - (BOOL)becomeFirstResponder
{
BOOL isFirstResponder = [super becomeFirstResponder];
if (isFirstResponder) {
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldBeginEditing:)]) {
// The AppKit -[NSTextField textDidBeginEditing:] notification is only called when the user
// makes the first change to the text in the text field.
// The react-native -[RCTUITextFieldDelegate textFieldBeginEditing:] is intended to be
// called when the text field is focused so call it here in becomeFirstResponder.
[delegate textFieldBeginEditing:self];
}

NSScrollView *scrollView = [self enclosingScrollView];
if (scrollView != nil) {
NSRect visibleRect = [[scrollView documentView] convertRect:self.frame fromView:self];
Expand Down
176 changes: 152 additions & 24 deletions RNTester/js/FocusEventsExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Platform = require('Platform');
var {StyleSheet, Text, View, TextInput} = ReactNative;

type State = {
eventStream: string;
eventStream: string,
};

class FocusEventExample extends React.Component<{}, State> {
Expand All @@ -28,57 +28,185 @@ class FocusEventExample extends React.Component<{}, State> {
return (
<View>
<Text>
Focus events are called when a component receives or loses focus.
This can be acquired by manually focusing components {Platform.OS === 'macos' ? ' or using tab-based nav' : '' }
Focus events are called when a component receives or loses focus. This
can be acquired by manually focusing components
{Platform.OS === 'macos' ? ' or using tab-based nav' : ''}
</Text>
<View>
<TextInput
onFocus={() => {
this.setState((prevState) => ({ eventStream: prevState.eventStream + '\nTextInput Focus' }));
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nTextInput Focus',
}));
}}
onBlur={() => {
this.setState((prevState) => ({ eventStream: prevState.eventStream + '\nTextInput Blur' }));
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nTextInput Blur',
}));
}}
style={styles.default}
placeholder={'TextInput'}
placeholderTextColor={
Platform.OS === 'macos' ? {semantic: 'textColor'} : 'black'
}
style={styles.textInput}
/>

{// Only test View on MacOS, since canBecomeFirstResponder is false on all iOS, therefore we can't focus
Platform.OS === 'macos' ?
<View style={styles.default}
acceptsKeyboardFocus={true}onFocus={() => {
this.setState((prevState) => ({ eventStream: prevState.eventStream + '\nView Focus' }));
Platform.OS === 'macos' ? (
<View
acceptsKeyboardFocus={true}
enableFocusRing={true}
onFocus={() => {
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nView Focus',
}));
}}
onBlur={() => {
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nView Blur',
}));
}}>
<Text>Focusable View</Text>
</View>
) : null}

<View
onFocus={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Singleline TextInput Parent Focus',
}));
}}
onBlur={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Singleline TextInput Parent Blur',
}));
}}>
<TextInput
onFocus={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Singleline TextInput Focus',
}));
}}
onBlur={() => {
this.setState((prevState) => ({ eventStream: prevState.eventStream + '\nView Blur' }));
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Singleline TextInput Blur',
}));
}}
style={styles.textInput}
placeholder={'Nested Singleline TextInput'}
placeholderTextColor={
Platform.OS === 'macos' ? {semantic: 'textColor'} : 'black'
}
/>
</View>

{// Only test View on MacOS, since canBecomeFirstResponder is false on all iOS, therefore we can't focus
Platform.OS === 'macos' ? (
<View
onFocus={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream + '\nNested View Parent Focus',
}));
}}
>
<Text>
Focusable View
</Text>
onBlur={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream + '\nNested View Parent Blur',
}));
}}>
<View
acceptsKeyboardFocus={true}
enableFocusRing={true}
onFocus={() => {
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nNested View Focus',
}));
}}
onBlur={() => {
this.setState(prevState => ({
eventStream: prevState.eventStream + '\nNested View Blur',
}));
}}>
<Text>Nested Focusable View</Text>
</View>
</View>
: null}
<Text>
{'Events: ' + this.state.eventStream + '\n\n'}
</Text>
) : null}

<View
onFocus={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Multiline TextInput Parent Focus',
}));
}}
onBlur={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Multiline TextInput Parent Blur',
}));
}}>
<TextInput
onFocus={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream +
'\nNested Multiline TextInput Focus',
}));
}}
onBlur={() => {
this.setState(prevState => ({
eventStream:
prevState.eventStream + '\nNested Multiline TextInput Blur',
}));
}}
style={styles.textInput}
multiline={true}
placeholder={'Nested Multiline TextInput'}
placeholderTextColor={
Platform.OS === 'macos' ? {semantic: 'textColor'} : 'black'
}
/>
</View>

<Text>{'Events: ' + this.state.eventStream + '\n\n'}</Text>
</View>
</View>
);
}
}

var styles = StyleSheet.create({
default: {
textInput: {
...Platform.select({
macos: {
color: {semantic: 'textColor'},
backgroundColor: {semantic: 'textBackgroundColor'},
borderColor: {semantic: 'gridColor'},
},
default: {
borderColor: '#0f0f0f',
},
}),
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#0f0f0f',
flex: 1,
fontSize: 13,
padding: 4,
},
});

exports.title = 'Focus Events';
exports.description =
'Examples that show how Focus events can be used.';
exports.description = 'Examples that show how Focus events can be used.';
exports.examples = [
{
title: 'FocusEventExample',
Expand Down