Skip to content
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

[TextInput] Implements onKeyPress #2082

Closed
wants to merge 1 commit into from

Conversation

dsibiski
Copy link
Contributor

  • When a key is pressed, it's key value is passed as an argument to the callback handler.
  • Example
 _handleKeyPress: function(e) {
      console.log(e.nativeEvent.key);
  },

  render: function() {
    return (
      <View style={styles.container}>
        <TextInput
            style={{width: 150, height: 25, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
        />
        <TextInput
            style={{width: 150, height: 100, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
            multiline={true}
        />
      </View>
    );
  }
  • Implements shouldChangeCharactersInRange, keyboardInputShouldDelete and deleteBackward for RCTTextField
    • keyboardInputShouldDelete and deleteBackward were implemented because UITextField doesn't trigger shouldChangeCharactersInRange when the field is empty, preventing us from having a true keyPress event.
    • Unfortunately, it looks like keyboardInputShouldDelete is not documented as alluded to in this StackOverflow answer. However, I'm only using it because of a bug in iOS 8.0 to 8.2 where the deleteBackward method doesn't trigger. This might cause this whole feature to be a no-op, though. :(
  • Implements shouldChangeTextInRange for RCTTextView
    • This one is nice and simple since UITextViews (used for multi-line TextInputs) trigger this method even when the textView is empty.
  • Addresses issue: [TextInput] Get keydown event to handle "Enter" #1882

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Jul 21, 2015
@sahrens sahrens assigned achao, amasad and sahrens and unassigned achao and amasad Jul 22, 2015
@sahrens
Copy link
Contributor

sahrens commented Jul 22, 2015

Nice work! You're going to have to rebase some conflicts once my diffs sync out though.

@dsibiski
Copy link
Contributor Author

@sahrens Not a problem. :)

@brentvatne
Copy link
Collaborator

😍 awesome @dsibiski

@dsibiski
Copy link
Contributor Author

I will need to implement an example for the UIExplorer, but I wanted to get some opinions on what's happening here (and the changes they are doing internally at FB) before knocking that out.

@brentvatne
Copy link
Collaborator

@dsibiski - in your example I see:

 _handleKeyPress: function(e) {
      console.log(e.nativeEvent.text);
  },

imo this should be e.nativeEvent.key. Also, is it deterministic which order onTextChange and onKeyPress fire? If so, we should document (I imagine onKeyPress comes first)

@dsibiski
Copy link
Contributor Author

@brentvatne I had piggy-backed off of the sendTextEventWithType:reactTag:text method that was already there in RCTEventDispatcher (it was easier hah!) However, I think you're right, we should have a different return value, instead of "text". I'll perhaps implement sendTextEventWithType:reactTag:key and return the key or eventually a dictionary with other values as well (like ASCII code).

As for the firing order, in my testing, I noticed that onKeyPress fires before onTextChange, but I didn't test it specifically.

@dsibiski
Copy link
Contributor Author

Ok, I rebased @sahrens's recent changes. I made keyPress still fire even after the maxLength threshold has been reached. Which I think is how a normal text input works on the web.

Now, I just need to implement @brentvatne's suggestion for the nativeEvent & a UIExplorer example.

@dsibiski
Copy link
Contributor Author

nativeEvent.key now used instead of nativeEvent.text. I also added an example to the TextEventsExample in UIExplorer.

/cc @brentvatne @sahrens

{
NSString *keyValue = kBackspaceKeyValue;

BOOL keyNotBackspace = ![text isEqualToString:@""];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you consolidate this with the enter logic into one if else chain?

@sahrens sahrens assigned nicklockwood and unassigned sahrens Jul 25, 2015
@sahrens
Copy link
Contributor

sahrens commented Jul 25, 2015

I'm going on PTO - nick, can you help out with this one?

@dsibiski
Copy link
Contributor Author

Build failed because of: #2134

@dsibiski
Copy link
Contributor Author

dsibiski commented Aug 6, 2015

@sahrens @nicklockwood What do you guys think about this? Just rebased the latest master to get rid of the conflicts...

@dsibiski
Copy link
Contributor Author

Note to self: Once this commit (vjeux@48548fe) lands, I'll need to move most (or all) of this into the RCTTextFieldManager where shouldChangeCharactersInRange was moved.

@dsibiski
Copy link
Contributor Author

@sahrens @nicklockwood I rebased the current changes and was able to remove some of the 'backspace' press detection hacks. Can you guys check this out when you get a chance?

I think that the "paste" detecting is a little wonky, let me know what you think.

@GantMan
Copy link
Contributor

GantMan commented Oct 27, 2015

Checking in - Is this happening?
I'd love to be able to detect when the return key is pressed for iOS.

@dsibiski
Copy link
Contributor Author

@nicklockwood @sahrens Hey guys, do you guys want me to add Android support to this as well? I imagine that might be the reason for the hold up...

@"eventCount": @(eventCount),
@"target": reactTag
}];

if (text) {
[body addEntriesFromDictionary:@{@"text": text}];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be:

if (text) {
  body[@"text"] = text;
}

@nicklockwood
Copy link
Contributor

Android support would be great, but it's OK to just mark the prop with @platform ios for now.

extern NSString *const RCTNewlineRawValue;

extern NSString *const RCTBackspaceKeyValue;
extern NSString *const RCTEnterKeyValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files should end with a blank line.

@facebook-github-bot
Copy link
Contributor

@dsibiski updated the pull request.

@dsibiski
Copy link
Contributor Author

Hmm, somehow the newline at the end of the file didn't come through...it seems to still be there in Xcode... @nicklockwood Do you know if that is something with git?

(Edit) Perhaps it's there and just GitHub doesn't see it...

@nicklockwood
Copy link
Contributor

@facebook-github-bot import

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/946196175402219/int_phab to review.

@dsibiski
Copy link
Contributor Author

Oops, I better squash my commits...

- When a key is pressed, it's `key value` is passed as an argument to the callback handler
@ghost ghost closed this in 6c7c845 Nov 2, 2015
MattFoley pushed a commit to skillz/react-native that referenced this pull request Nov 9, 2015
Summary: - When a key is pressed, it's `key value` is passed as an argument to the callback handler.
 - For `Enter` and `Backspace` keys, I'm using their `key value` as defined [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values). As per JonasJonny & brentvatne's [suggestion](facebook#1882 (comment)).

- Example
```javascript
 _handleKeyPress: function(e) {
      console.log(e.nativeEvent.key);
  },

  render: function() {
    return (
      <View style={styles.container}>
        <TextInput
            style={{width: 150, height: 25, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
        />
        <TextInput
            style={{width: 150, height: 100, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
            multiline={true}
        />
      </View>
    );
  }
```
- Implements [shouldChangeCharactersInRange](https://developer.apple.com/library/prerelease/ios/documentat
Closes facebook#2082

Reviewed By: javache

Differential Revision: D2280460

Pulled By: nicklockwood

fb-gh-sync-id: 1f824f80649043dc2520c089e2531d428d799405
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
Summary: - When a key is pressed, it's `key value` is passed as an argument to the callback handler.
 - For `Enter` and `Backspace` keys, I'm using their `key value` as defined [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values). As per JonasJonny & brentvatne's [suggestion](facebook#1882 (comment)).

- Example
```javascript
 _handleKeyPress: function(e) {
      console.log(e.nativeEvent.key);
  },

  render: function() {
    return (
      <View style={styles.container}>
        <TextInput
            style={{width: 150, height: 25, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
        />
        <TextInput
            style={{width: 150, height: 100, borderWidth: 0.5}}
            onKeyPress={this._handleKeyPress}
            multiline={true}
        />
      </View>
    );
  }
```
- Implements [shouldChangeCharactersInRange](https://developer.apple.com/library/prerelease/ios/documentat
Closes facebook#2082

Reviewed By: javache

Differential Revision: D2280460

Pulled By: nicklockwood

fb-gh-sync-id: 1f824f80649043dc2520c089e2531d428d799405
@matc4
Copy link

matc4 commented Jan 25, 2016

Any plan to have onKeyPress event for Android?

@atticoos
Copy link
Contributor

Probably a feature someone will have to contribute @MatiasCba. I'm currently in need, but I haven't scoped out what it looks like under the hood for Android. If it's already set up similarly, it shouldn't be too bad.

@mpretty-cyro
Copy link

+1 for onKeyPress in Android

@atticoos
Copy link
Contributor

Working on a fork at the moment, if I finish up I'll refer from here

@mpretty-cyro
Copy link

@ajwhite Awesome, thanks for the update

@jgibbons
Copy link

jgibbons commented Mar 1, 2016

@ajwhite Any updates on this or work you've done to date?

@atticoos
Copy link
Contributor

atticoos commented Mar 1, 2016

I haven't any updates at this time, I likely cannot visit this for another couple weeks. Busy bee at work!

@PardeepK
Copy link

@ajwhite , any update on Android support?
or any reason why it is not there for Android, as it seems very basic requirement for an edit control.

@atticoos
Copy link
Contributor

I don't, sorry folks! It became a bit of a rabbit hole and was deprioritized in our project in lieu of a workaround.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.