Skip to content

Keyboard accessory view safe area api support #827

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 6 commits into from
Jul 5, 2020
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {PureComponent} from 'react';
import {ScrollView, StyleSheet, TextInput} from 'react-native';
import {Keyboard, Text, View, Colors, Spacings, Constants, Typography, Button} from 'react-native-ui-lib';
import {Keyboard, Text, View, Colors, Spacings, Constants, Typography, Button, Switch} from 'react-native-ui-lib';
const KeyboardAccessoryView = Keyboard.KeyboardAccessoryView;
const KeyboardUtils = Keyboard.KeyboardUtils;
import {_} from 'lodash';

import './demoKeyboards';
const KeyboardRegistry = Keyboard.KeyboardRegistry;
Expand All @@ -15,7 +16,9 @@ export default class KeyboardInputViewScreen extends PureComponent {
component: undefined,
initialProps: undefined
},
receivedKeyboardData: undefined
receivedKeyboardData: undefined,
useSafeArea: true,
keyboardOpenState: false
};

onKeyboardItemSelected = (keyboardId, params) => {
Expand Down Expand Up @@ -49,12 +52,44 @@ export default class KeyboardInputViewScreen extends PureComponent {
return buttons;
}

isCustomKeyboardOpen = () => {
const {keyboardOpenState, customKeyboard} = this.state;
return keyboardOpenState && !_.isEmpty(customKeyboard);
}

resetKeyboardView = () => {
this.setState({customKeyboard: {}});
};

dismissKeyboard = () => {
KeyboardUtils.dismiss();
}

toggleUseSafeArea = () => {
const {useSafeArea} = this.state;
this.setState({useSafeArea: !useSafeArea});

if (this.isCustomKeyboardOpen()) {
this.dismissKeyboard();
this.showLastKeyboard();
}
};

showLastKeyboard() {
const {customKeyboard} = this.state;
this.setState({customKeyboard: {}});

setTimeout(() => {
this.setState({
keyboardOpenState: true,
customKeyboard
});
}, 500);
}

showKeyboardView(component, title) {
this.setState({
keyboardOpenState: true,
customKeyboard: {
component,
initialProps: {title}
Expand Down Expand Up @@ -84,7 +119,6 @@ export default class KeyboardInputViewScreen extends PureComponent {
/>
<Button label="Close" link onPress={KeyboardUtils.dismiss} style={styles.button}/>
</View>

<View row>
{this.getToolbarButtons().map((button, index) => (
<Button label={button.text} link onPress={button.onPress} key={index} style={styles.button}/>
Expand All @@ -107,10 +141,26 @@ export default class KeyboardInputViewScreen extends PureComponent {
});
};

safeAreaSwitchToggle = () => {
if (!Constants.isIOS) {
return;
}
const {useSafeArea} = this.state;
return (
<View column center>
<View style={styles.separatorLine}/>
<View centerV row margin-10>
<Text text80 dark40>Safe Area Enabled:</Text>
<Switch value={useSafeArea} onValueChange={this.toggleUseSafeArea} marginL-14/>
</View>
<View style={styles.separatorLine}/>
</View>
);
}

render() {
const {message} = this.props;
const {receivedKeyboardData, customKeyboard} = this.state;

const {receivedKeyboardData, customKeyboard, useSafeArea} = this.state;
return (
<View flex bg-dark80>
<ScrollView
Expand All @@ -122,6 +172,7 @@ export default class KeyboardInputViewScreen extends PureComponent {
</Text>
<Text testID={'demo-message'}>{receivedKeyboardData}</Text>
<Button label={'Open keyboard #1'} link onPress={this.requestShowKeyboard} style={styles.button}/>
{ this.safeAreaSwitchToggle() }
</ScrollView>

<KeyboardAccessoryView
Expand All @@ -135,6 +186,7 @@ export default class KeyboardInputViewScreen extends PureComponent {
onKeyboardResigned={this.onKeyboardResigned}
revealKeyboardInteractive
onRequestShowKeyboard={this.onRequestShowKeyboard}
useSafeArea={useSafeArea}
/>
</View>
);
Expand All @@ -149,7 +201,7 @@ const styles = StyleSheet.create({
},
textInput: {
flex: 1,
padding: Spacings.s2,
padding: Spacings.s1,
...Typography.text70
},
button: {
Expand All @@ -159,5 +211,10 @@ const styles = StyleSheet.create({
backgroundColor: Colors.white,
borderWidth: 1,
borderColor: Colors.dark60
},
separatorLine: {
flex: 1,
height: 1,
backgroundColor: Colors.dark80
}
});
12 changes: 9 additions & 3 deletions lib/components/Keyboard/KeyboardInput/CustomKeyboardView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default class CustomKeyboardView extends CustomKeyboardViewBase {
inputRef: PropTypes.object,
initialProps: PropTypes.object,
component: PropTypes.string,
onItemSelected: PropTypes.func
onItemSelected: PropTypes.func,
useSafeArea: PropTypes.bool
};

static defaultProps = {
useSafeArea: true
};

constructor(props) {
Expand Down Expand Up @@ -42,14 +47,15 @@ export default class CustomKeyboardView extends CustomKeyboardViewBase {
}

async UNSAFE_componentWillReceiveProps(nextProps) {
const {inputRef: nextInputRef, component: nextComponent, initialProps: nextInitialProps} = nextProps;
const {inputRef: nextInputRef, component: nextComponent, initialProps: nextInitialProps, useSafeArea} = nextProps;
const {component} = this.props;

if (nextInputRef && nextComponent !== component) {
if (nextComponent) {
TextInputKeyboardManager.setInputComponent(nextInputRef, {
component: nextComponent,
initialProps: nextInitialProps
initialProps: nextInitialProps,
useSafeArea
});
} else {
TextInputKeyboardManager.removeInputComponent(nextInputRef);
Expand Down
13 changes: 11 additions & 2 deletions lib/components/Keyboard/KeyboardInput/KeyboardAccessoryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ class KeyboardAccessoryView extends Component {
*
* default: false
*/
allowHitsOutsideBounds: PropTypes.bool
allowHitsOutsideBounds: PropTypes.bool,

/**
* iOS only.
* Whether or not to handle SafeArea
* default: true
*/
useSafeArea: PropTypes.bool
};

static iosScrollBehaviors = IOS_SCROLL_BEHAVIORS;
Expand Down Expand Up @@ -236,7 +243,8 @@ class KeyboardAccessoryView extends Component {
kbInputRef,
kbComponent,
onItemSelected,
onRequestShowKeyboard
onRequestShowKeyboard,
useSafeArea
} = this.props;

return (
Expand All @@ -258,6 +266,7 @@ class KeyboardAccessoryView extends Component {
initialProps={this.processInitialProps()}
onItemSelected={onItemSelected}
onRequestShowKeyboard={onRequestShowKeyboard}
useSafeArea={useSafeArea}
/>
</KeyboardTrackingView>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import ReactNative, {NativeModules, LayoutAnimation} from 'react-native';
const CustomInputControllerTemp = NativeModules.CustomInputControllerTemp;

export default class TextInputKeyboardManager {
static setInputComponent = (textInputRef, {component, initialProps}) => {
static setInputComponent = (textInputRef, {component, initialProps, useSafeArea}) => {
if (!textInputRef || !CustomInputControllerTemp) {
return;
}
const reactTag = findNodeHandle(textInputRef);
if (reactTag) {
CustomInputControllerTemp.presentCustomInputComponent(reactTag, {component, initialProps});
CustomInputControllerTemp.presentCustomInputComponent(reactTag, {component, initialProps, useSafeArea});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ -(UIView*)getFirstResponder:(UIView*)view
return nil;
}

- (BOOL)shouldUseSafeAreaFrom:(NSDictionary *)params {
return [params[@"useSafeArea"] isEqual:@(1)];
}

RCT_EXPORT_METHOD(presentCustomInputComponent:(nonnull NSNumber*)inputFieldTag params:(nonnull NSDictionary*)params)
{
RCTBridge* bridge = [self.bridge valueForKey:@"parentBridge"];
Expand All @@ -136,7 +140,8 @@ -(UIView*)getFirstResponder:(UIView*)view

self.customInputComponentPresented = NO;

RCTCustomKeyboardViewController* customKeyboardController = [[RCTCustomKeyboardViewController alloc] init];
BOOL useSafeArea = [self shouldUseSafeAreaFrom:params];
RCTCustomKeyboardViewController* customKeyboardController = [[RCTCustomKeyboardViewController alloc] initWithUsingSafeArea:useSafeArea];
customKeyboardController.rootView = rv;

_WXInputHelperView* helperView = [[_WXInputHelperView alloc] initWithFrame:CGRectZero];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@interface RCTCustomKeyboardViewController : UIInputViewController

- (void) setAllowsSelfSizing:(BOOL)allowsSelfSizing;
- (instancetype)initWithUsingSafeArea:(BOOL)useSafeArea;

@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
@property (nonatomic, strong) RCTRootView *rootView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
#import "RCTCustomKeyboardViewController.h"

#if __has_include(<KeyboardTrackingView/ObservingInputAccessoryViewTemp.h>)
#import <KeyboardTrackingView/ObservingInputAccessoryViewTemp.h>
#define ObservingInputAccessoryViewTemp_IsAvailable true
#import <KeyboardTrackingView/ObservingInputAccessoryViewTemp.h>
#define ObservingInputAccessoryViewTemp_IsAvailable true
#endif

@interface RCTCustomKeyboardViewController ()
@property (nonatomic, assign, getter=isUsingSafeArea) BOOL useSafeArea;
@end

@implementation RCTCustomKeyboardViewController

- (instancetype)init
- (instancetype)initWithUsingSafeArea:(BOOL)useSafeArea
{
self = [super init];

if(self)
{
self.inputView = [[UIInputView alloc] initWithFrame:CGRectZero inputViewStyle:UIInputViewStyleKeyboard];

self.heightConstraint = [self.inputView.heightAnchor constraintEqualToConstant:0];
self.useSafeArea = useSafeArea;

#ifdef ObservingInputAccessoryViewTemp_IsAvailable
ObservingInputAccessoryViewTemp *activeObservingInputAccessoryViewTemp = [ObservingInputAccessoryViewTempManager sharedInstance].activeObservingInputAccessoryViewTemp;
Expand Down Expand Up @@ -62,18 +67,33 @@ -(void)setRootView:(RCTRootView*)rootView
_rootView = rootView;
_rootView.translatesAutoresizingMaskIntoConstraints = NO;
[self.inputView addSubview:_rootView];


[self updateRootViewConstraints];
[self.inputView setNeedsLayout];
}

- (void)updateRootViewConstraints {
[_rootView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[_rootView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
[_rootView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;

NSLayoutYAxisAnchor *yAxisAnchor = [self bottomLayoutAnchorUsingSafeArea:self.isUsingSafeArea];
[_rootView.bottomAnchor constraintEqualToAnchor:yAxisAnchor].active = YES;
}

- (NSLayoutYAxisAnchor *)bottomLayoutAnchorUsingSafeArea:(BOOL)useSafeArea {
NSLayoutYAxisAnchor *yAxisAnchor = self.view.bottomAnchor;

if (!useSafeArea) {
return yAxisAnchor;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
if (@available(iOS 11.0, *)) {
yAxisAnchor = self.view.safeAreaLayoutGuide.bottomAnchor;
}
#endif
[_rootView.bottomAnchor constraintEqualToAnchor:yAxisAnchor].active = YES;
return yAxisAnchor;
}

@end
2 changes: 1 addition & 1 deletion src/components/actionBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ActionBar extends BaseComponent {
*/
useSafeArea: PropTypes.bool,
/**
* keep the action bar postion relative instead of it absolute position
* keep the action bar position relative instead of it absolute position
*/
keepRelative: PropTypes.bool,
/**
Expand Down