Skip to content

Commit

Permalink
lint + remove double componentWillUnmount implementations in CustomKe…
Browse files Browse the repository at this point in the history
…yboardView
  • Loading branch information
artald committed Apr 16, 2017
1 parent fd98bc1 commit 80b1ff8
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react/forbid-prop-types": "off",
"react/jsx-space-before-closing": "off",
"react/jsx-tag-spacing": "off",
"max-len": [2, 125, 4, {"ignoreUrls": true}],
"max-len": [2, 135, 4, {"ignoreUrls": true}],
"class-methods-use-this": "off",
"arrow-body-style": "off",
"no-plusplus": "off",
Expand Down
23 changes: 10 additions & 13 deletions demo/demoApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
View,
ScrollView,
TouchableOpacity,
Keyboard,
PixelRatio,
Platform,
Alert
} from 'react-native';
import {AutoGrowingTextInput} from 'react-native-autogrow-textinput';
import {BlurView} from 'react-native-blur';
Expand All @@ -31,10 +29,15 @@ class AwesomeProject extends Component {
component: undefined,
initialProps: undefined,
},
receivedKeyboardData: undefined
receivedKeyboardData: undefined,
};
}

onKeyboardItemSelected(keyboardId, params) {
const receivedKeyboardData = `onItemSelected from "${keyboardId}"\nreceived params: ${JSON.stringify(params)}`;
this.setState({receivedKeyboardData});
}

getToolbarButtons() {
return [
{
Expand Down Expand Up @@ -97,11 +100,6 @@ class AwesomeProject extends Component {
);
}

onKeyboardItemSelected(keyboardId, params) {
const receivedKeyboardData = `onItemSelected from \"${keyboardId}\"\nreceived params: ${JSON.stringify(params)}`;
this.setState({receivedKeyboardData});
}

render() {
return (
<View style={styles.container}>
Expand Down Expand Up @@ -138,7 +136,7 @@ const styles = StyleSheet.create({
scrollContainer: {
justifyContent: 'center',
padding: 15,
flex: 1
flex: 1,
},
welcome: {
fontSize: 20,
Expand All @@ -148,17 +146,16 @@ const styles = StyleSheet.create({
paddingBottom: 50,
},
inputContainer: {
//flex: 1, // TODO is this needed on iOS?
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
blurContainer: {
...Platform.select({
ios: {
flex: 1
}
})
flex: 1,
},
}),
},
textInput: {
flex: 1,
Expand Down
4 changes: 2 additions & 2 deletions demo/demoKeyboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class KeyboardView extends Component {

onButtonPress() {
KeyboardRegistry.onItemSelected('KeyboardView', {
message: 'item selected from KeyboardView'
message: 'item selected from KeyboardView',
});
}

Expand Down Expand Up @@ -39,7 +39,7 @@ class AnotherKeyboardView extends Component {
onButtonPress() {
KeyboardRegistry.onItemSelected('AnotherKeyboardView', {
param1: 'some data',
param2: 10
param2: 10,
});
}

Expand Down
2 changes: 2 additions & 0 deletions ios/AwesomeProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@
"$(SRCROOT)/../node_modules/react-native/React/**",
);
INFOPLIST_FILE = AwesomeProject/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_LDFLAGS = (
"$(inherited)",
Expand All @@ -894,6 +895,7 @@
"$(SRCROOT)/../node_modules/react-native/React/**",
);
INFOPLIST_FILE = AwesomeProject/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ -(UIView*)getFirstResponder:(UIView*)view
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
if(inputField != nil && [self reactCanBecomeFirstResponder:inputField])
{
BOOL restoreFirstResponder = NO;
_WXInputHelperView* helperView = [inputField.superview viewWithTag:kHlperViewTag];
if(helperView != nil && [helperView isFirstResponder])
{//restore the first responder only if it was already the first responder to prevent the keyboard from opening again if not necessary
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
},
"dependencies": {
"lodash": "^4.17.4",
"react-native-keyboard-tracking-view": "^3.0.3"
"react-native-keyboard-tracking-view": "^3.0.4"
},
"peerDependencies": {
"react": "15.4.2",
"react-native": "^0.38.0"
},
"devDependencies": {
"babel-eslint": "^7.2.1",
"babel-jest": "^18.0.0",
"babel-preset-react-native": "1.9.1",
"eslint": "^3.0.0",
Expand Down
34 changes: 14 additions & 20 deletions src/CustomKeyboardView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component, PropTypes} from 'react';
import {View, Text, Platform, Dimensions, DeviceEventEmitter, requireNativeComponent} from 'react-native';
import {Platform, requireNativeComponent} from 'react-native';
import TextInputKeyboardManagerIOS from './TextInputKeyboardMangerIOS';
import TextInputKeyboardManagerAndroid from './TextInputKeyboardManagerAndroid';
import KeyboardRegistry from './KeyboardsRegistry';
Expand All @@ -20,7 +20,7 @@ export default class CustomKeyboardView extends Component {
constructor(props) {
super(props);

const {inputRef, component, initialProps, onItemSelected, onRequestShowKeyboard} = props;
const {inputRef, component, initialProps, onItemSelected} = props;
if (component) {
this.addOnItemSelectListener(onItemSelected, component);

Expand All @@ -32,18 +32,6 @@ export default class CustomKeyboardView extends Component {
}
}

componentWillUnmount() {
KeyboardRegistry.removeListeners('onRequestShowKeyboard');
}

addOnItemSelectListener(onItemSelected, component) {
if (onItemSelected) {
KeyboardRegistry.addListener(`${component}.onItemSelected`, (args) => {
onItemSelected(component, args);
});
}
}

async componentWillReceiveProps(nextProps) {
const {inputRef, component, initialProps, onRequestShowKeyboard} = nextProps;

Expand All @@ -70,15 +58,13 @@ export default class CustomKeyboardView extends Component {
this.registerListener(this.props, nextProps);
}

shouldComponentUpdate(nextProps, nextState) {
if (nextProps.component === this.props.component) {
return false;
}
return true;
shouldComponentUpdate(nextProps) {
return (nextProps.component !== this.props.component);
}


componentWillUnmount() {
KeyboardRegistry.removeListeners('onRequestShowKeyboard');

if (this.keyboardEventListeners) {
this.keyboardEventListeners.forEach(eventListener => eventListener.remove());
}
Expand All @@ -87,6 +73,14 @@ export default class CustomKeyboardView extends Component {
}
}

addOnItemSelectListener(onItemSelected, component) {
if (onItemSelected) {
KeyboardRegistry.addListener(`${component}.onItemSelected`, (args) => {
onItemSelected(component, args);
});
}
}

registerListener(props, nextProps) {
const {component, onItemSelected} = nextProps;
if (component && props.component !== component) {
Expand Down
52 changes: 26 additions & 26 deletions src/KeyboardAccessoryView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component, PropTypes} from 'react';
import {StyleSheet, View, Platform, Dimensions, NativeModules, NativeEventEmitter, DeviceEventEmitter, processColor} from 'react-native';
import {StyleSheet, Platform, Dimensions, NativeModules, NativeEventEmitter, DeviceEventEmitter, processColor} from 'react-native';
import {KeyboardTrackingView} from 'react-native-keyboard-tracking-view';
import CustomKeyboardView from './CustomKeyboardView';

Expand Down Expand Up @@ -34,48 +34,48 @@ export default class KeyboardAccessoryView extends Component {
this.registerForKeyboardResignedEvent();
}

registerForKeyboardResignedEvent() {
let eventEmitter = null;
if(IsIOS) {
if(NativeModules.CustomInputController) {
eventEmitter = new NativeEventEmitter(NativeModules.CustomInputController);
}
} else {
eventEmitter = DeviceEventEmitter;
}

if(eventEmitter !== null) {
this.customInputControllerEventsSubscriber = eventEmitter.addListener('kbdResigned', (params) => {
if(this.props.onKeyboardResigned) {
this.props.onKeyboardResigned();
}
});
}
}

componentWillUnmount() {
if(this.customInputControllerEventsSubscriber) {
if (this.customInputControllerEventsSubscriber) {
this.customInputControllerEventsSubscriber.remove();
}
}

onContainerComponentHeightChanged(event) {
if (this.props.onHeightChanged) {
this.props.onHeightChanged(event.nativeEvent.layout.height)
this.props.onHeightChanged(event.nativeEvent.layout.height)
}
}

getIOSTrackingScrollBehavior() {
let scrollBehavior = this.props.iOSScrollBehavior;
if(IsIOS && NativeModules.KeyboardTrackingViewManager && scrollBehavior === null) {
if (IsIOS && NativeModules.KeyboardTrackingViewManager && scrollBehavior === null) {
scrollBehavior = NativeModules.KeyboardTrackingViewManager.KeyboardTrackingScrollBehaviorFixedOffset;
}
return scrollBehavior;
}

registerForKeyboardResignedEvent() {
let eventEmitter = null;
if (IsIOS) {
if (NativeModules.CustomInputController) {
eventEmitter = new NativeEventEmitter(NativeModules.CustomInputController);
}
} else {
eventEmitter = DeviceEventEmitter;
}

if (eventEmitter !== null) {
this.customInputControllerEventsSubscriber = eventEmitter.addListener('kbdResigned', (params) => {
if (this.props.onKeyboardResigned) {
this.props.onKeyboardResigned();
}
});
}
}

processInitialProps() {
const processedProps = this.props.kbInitialProps;
if(IsIOS && processedProps && processedProps.backgroundColor) {
if (IsIOS && processedProps && processedProps.backgroundColor) {
processedProps.backgroundColor = processColor(processedProps.backgroundColor);
}
return processedProps;
Expand Down Expand Up @@ -110,7 +110,7 @@ const styles = StyleSheet.create({
position: 'absolute',
bottom: 0,
left: 0,
}
})
},
}),
},
});
7 changes: 3 additions & 4 deletions src/KeyboardRegistry.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {AppRegistry, View} from 'react-native';
import React from 'react';
import KeyboardRegistry from './KeyboardsRegistry';
import EventEmitterManager from './utils/EventEmitterManager';

describe('KeyboardRegistry - components', () => {
const mockComponent = 'test_component';
Expand All @@ -10,7 +9,7 @@ describe('KeyboardRegistry - components', () => {

beforeEach(() => {
AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
console.error = jest.fn()
console.error = jest.fn();
});

it('should register the component in the keyboard registry', () => {
Expand Down Expand Up @@ -62,7 +61,7 @@ describe('KeyboardRegistry - listeners', () => {
listenOn: jest.fn(),
emitEvent: jest.fn(),
removeListeners: jest.fn(),
}
};
});

it('should listen', () => {
Expand Down Expand Up @@ -97,4 +96,4 @@ describe('KeyboardRegistry - listeners', () => {
expect(KeyboardRegistry.eventEmitter.emitEvent.mock.calls[0][0]).toEqual('onRequestShowKeyboard');
expect(KeyboardRegistry.eventEmitter.emitEvent.mock.calls[0][1]).toEqual({keyboardId: mockId});
});
});
});
4 changes: 2 additions & 2 deletions src/KeyboardsRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default class KeyboardRegistry {
};

static getAllKeyboards = () => {
return Object.keys(KeyboardRegistry.registeredKeyboards).map(keyboardId => {
return Object.keys(KeyboardRegistry.registeredKeyboards).map((keyboardId) => {
return {
id: keyboardId,
...KeyboardRegistry.registeredKeyboards[keyboardId].params
...KeyboardRegistry.registeredKeyboards[keyboardId].params,
};
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/TextInputKeyboardManagerAndroid.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ReactNative, {NativeModules} from 'react-native';
import {NativeModules} from 'react-native';

const CustomKeyboardInput = NativeModules.CustomKeyboardInput;

export default class TextInputKeyboardManagerAndroid {
static reset = () => {
static reset = () => {
CustomKeyboardInput.reset();
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/EventEmitterManager.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const EventEmitterManager = require('./EventEmitterManager').default;

let EventEmitter;

describe('EventEmitterManager tests', () => {
Expand Down

0 comments on commit 80b1ff8

Please sign in to comment.