-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Input Accessory View (iOS Only) [1/N]
Reviewed By: mmmulani Differential Revision: D6886573 fbshipit-source-id: 71e1f812b1cc1698e4380211a6cedd59011b5495
- Loading branch information
1 parent
c87d03a
commit 38197c8
Showing
13 changed files
with
427 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @providesModule InputAccessoryView | ||
* @flow | ||
* @format | ||
*/ | ||
'use strict'; | ||
|
||
const ColorPropType = require('ColorPropType'); | ||
const React = require('React'); | ||
const StyleSheet = require('StyleSheet'); | ||
const ViewPropTypes = require('ViewPropTypes'); | ||
|
||
const requireNativeComponent = require('requireNativeComponent'); | ||
|
||
const RCTInputAccessoryView = requireNativeComponent('RCTInputAccessoryView'); | ||
|
||
/** | ||
* Note: iOS only | ||
* | ||
* A component which enables customization of the keyboard input accessory view. | ||
* The input accessory view is displayed above the keyboard whenever a TextInput | ||
* has focus. This component can be used to create custom toolbars. | ||
* | ||
* To use this component wrap your custom toolbar with the | ||
* InputAccessoryView component, and set a nativeID. Then, pass that nativeID | ||
* as the inputAccessoryViewID of whatever TextInput you desire. A simple | ||
* example: | ||
* | ||
* ```ReactNativeWebPlayer | ||
* import React, { Component } from 'react'; | ||
* import { AppRegistry, TextInput, InputAccessoryView, Button } from 'react-native'; | ||
* | ||
* export default class UselessTextInput extends Component { | ||
* constructor(props) { | ||
* super(props); | ||
* this.state = {text: 'Placeholder Text'}; | ||
* } | ||
* | ||
* render() { | ||
* const inputAccessoryViewID = "uniqueID"; | ||
* return ( | ||
* <View> | ||
* <ScrollView keyboardDismissMode="interactive"> | ||
* <TextInput | ||
* style={{ | ||
* padding: 10, | ||
* paddingTop: 50, | ||
* }} | ||
* inputAccessoryViewID=inputAccessoryViewID | ||
* onChangeText={text => this.setState({text})} | ||
* value={this.state.text} | ||
* /> | ||
* </ScrollView> | ||
* <InputAccessoryView nativeID=inputAccessoryViewID> | ||
* <Button | ||
* onPress={() => this.setState({text: 'Placeholder Text'})} | ||
* title="Reset Text" | ||
* /> | ||
* </InputAccessoryView> | ||
* </View> | ||
* ); | ||
* } | ||
* } | ||
* | ||
* // skip this line if using Create React Native App | ||
* AppRegistry.registerComponent('AwesomeProject', () => UselessTextInput); | ||
* ``` | ||
* | ||
* This component can also be used to create sticky text inputs (text inputs | ||
* which are anchored to the top of the keyboard). To do this, wrap a | ||
* TextInput with the InputAccessoryView component, and don't set a nativeID. | ||
* For an example, look at InputAccessoryViewExample.js in RNTester. | ||
*/ | ||
|
||
type Props = { | ||
+children: React.Node, | ||
/** | ||
* An ID which is used to associate this `InputAccessoryView` to | ||
* specified TextInput(s). | ||
*/ | ||
nativeID?: string, | ||
style?: ViewPropTypes.style, | ||
backgroundColor?: ColorPropType, | ||
}; | ||
|
||
class InputAccessoryView extends React.Component<Props> { | ||
render(): React.Node { | ||
if (React.Children.count(this.props.children) === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RCTInputAccessoryView | ||
style={[this.props.style, styles.container]} | ||
nativeID={this.props.nativeID} | ||
backgroundColor={this.props.backgroundColor}> | ||
{this.props.children} | ||
</RCTInputAccessoryView> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
position: 'absolute', | ||
}, | ||
}); | ||
|
||
module.exports = InputAccessoryView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@class RCTBridge; | ||
@class RCTInputAccessoryViewContent; | ||
|
||
@interface RCTInputAccessoryView : UIView | ||
|
||
- (instancetype)initWithBridge:(RCTBridge *)bridge; | ||
|
||
@property (nonatomic, readonly, strong) RCTInputAccessoryViewContent *content; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTInputAccessoryView.h" | ||
|
||
#import <React/RCTBridge.h> | ||
#import <React/RCTTouchHandler.h> | ||
#import <React/UIView+React.h> | ||
|
||
#import "RCTInputAccessoryViewContent.h" | ||
|
||
@implementation RCTInputAccessoryView | ||
{ | ||
BOOL _contentShouldBeFirstResponder; | ||
} | ||
|
||
- (instancetype)initWithBridge:(RCTBridge *)bridge | ||
{ | ||
if (self = [super init]) { | ||
_content = [RCTInputAccessoryViewContent new]; | ||
RCTTouchHandler *const touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge]; | ||
[touchHandler attachToView:_content.inputAccessoryView]; | ||
[self addSubview:_content]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)reactSetFrame:(CGRect)frame | ||
{ | ||
[_content.inputAccessoryView setFrame:frame]; | ||
[_content.contentView setFrame:frame]; | ||
|
||
if (_contentShouldBeFirstResponder) { | ||
_contentShouldBeFirstResponder = NO; | ||
[_content becomeFirstResponder]; | ||
} | ||
} | ||
|
||
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index | ||
{ | ||
[super insertReactSubview:subview atIndex:index]; | ||
[_content insertReactSubview:subview atIndex:index]; | ||
} | ||
|
||
- (void)removeReactSubview:(UIView *)subview | ||
{ | ||
[super removeReactSubview:subview]; | ||
[_content removeReactSubview:subview]; | ||
} | ||
|
||
- (void)didUpdateReactSubviews | ||
{ | ||
// Do nothing, as subviews are managed by `insertReactSubview:atIndex:` | ||
} | ||
|
||
- (void)didSetProps:(NSArray<NSString *> *)changedProps | ||
{ | ||
// If the accessory view is not linked to a text input via nativeID, assume it is | ||
// a standalone component that should get focus whenever it is rendered | ||
if (![changedProps containsObject:@"nativeID"] && !self.nativeID) { | ||
_contentShouldBeFirstResponder = YES; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface RCTInputAccessoryViewContent : UIView | ||
|
||
@property (nonatomic, readwrite, retain) UIView *contentView; | ||
|
||
@end |
Oops, something went wrong.