Skip to content

Commit 7820849

Browse files
committed
Refactor KeyboardView
1 parent 4bc5cfc commit 7820849

File tree

3 files changed

+51
-86
lines changed

3 files changed

+51
-86
lines changed

demo/src/screens/nativeComponentScreens/keyboardInput/KeyboardInputViewScreen.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import React, {PureComponent} from 'react';
23
import {ScrollView, StyleSheet, TextInput} from 'react-native';
34
import {
@@ -18,6 +19,7 @@ const KeyboardAccessoryView = Keyboard.KeyboardAccessoryView;
1819
const KeyboardUtils = Keyboard.KeyboardUtils;
1920

2021
import './demoKeyboards';
22+
const KeyboardRegistry = Keyboard.KeyboardRegistry;
2123

2224
const TrackInteractive = true;
2325

@@ -40,23 +42,25 @@ export default class KeyboardInputViewScreen extends PureComponent {
4042
};
4143

4244
getToolbarButtons() {
43-
return [
44-
{
45-
text: 'show1',
46-
testID: 'show1',
47-
onPress: () => this.showKeyboardView('KeyboardView', 'FIRST - 1 (passed prop)')
48-
},
49-
{
50-
text: 'show2',
51-
testID: 'show2',
52-
onPress: () => this.showKeyboardView('AnotherKeyboardView', 'SECOND - 2 (passed prop)')
53-
},
54-
{
55-
text: 'reset',
56-
testID: 'reset',
57-
onPress: () => this.resetKeyboardView()
58-
}
59-
];
45+
const keyboards = KeyboardRegistry.getAllKeyboards();
46+
const buttons = [];
47+
for (let index = 0; index < keyboards.length; ++index) {
48+
const string = `Show KB ${index + 1}`;
49+
const title = `Title ${index + 1} (passed prop)`;
50+
buttons.push({
51+
text: string,
52+
testID: string,
53+
onPress: () => this.showKeyboardView(keyboards[index].id, title)
54+
});
55+
}
56+
57+
buttons.push({
58+
text: 'reset',
59+
testID: 'reset',
60+
onPress: () => this.resetKeyboardView()
61+
});
62+
63+
return buttons;
6064
}
6165

6266
resetKeyboardView = () => {
Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
11
import React, {Component} from 'react';
22
import PropTypes from 'prop-types';
3-
import {ScrollView, StyleSheet} from 'react-native';
4-
import {Keyboard, Text, TouchableOpacity} from 'react-native-ui-lib';
5-
const KeyboardRegistry = Keyboard.KeyboardRegistry;
3+
import {View, Text, TouchableOpacity} from 'react-native-ui-lib';
64

75
export default class KeyboardView extends Component {
86
static propTypes = {
9-
title: PropTypes.string
7+
title: PropTypes.string,
8+
backgroundColor: PropTypes.string,
9+
onPress: PropTypes.func
1010
};
1111

12-
onButtonPress() {
13-
KeyboardRegistry.onItemSelected('KeyboardView', {
14-
message: 'item selected from KeyboardView'
15-
});
16-
}
17-
1812
render() {
13+
const {title, backgroundColor, onPress} = this.props;
14+
1915
return (
20-
<ScrollView contentContainerStyle={[styles.keyboardContainer, {backgroundColor: 'purple'}]}>
21-
<Text style={{color: 'white'}}>HELOOOO!!!</Text>
22-
<Text style={{color: 'white'}}>{this.props.title}</Text>
23-
<TouchableOpacity
24-
testID={'click-me'}
25-
style={{padding: 20, marginTop: 30, backgroundColor: 'white'}}
26-
onPress={() => this.onButtonPress()}
27-
>
16+
<View flex center style={backgroundColor && {backgroundColor}}>
17+
<Text white>{title}</Text>
18+
<TouchableOpacity testID={'click-me'} padding-20 marginT-30 bg-white onPress={onPress}>
2819
<Text>Click Me!</Text>
2920
</TouchableOpacity>
30-
</ScrollView>
21+
</View>
3122
);
3223
}
3324
}
34-
35-
const styles = StyleSheet.create({
36-
keyboardContainer: {
37-
flex: 1,
38-
flexDirection: 'column',
39-
alignItems: 'center',
40-
justifyContent: 'center'
41-
}
42-
});
43-
44-
KeyboardRegistry.registerKeyboard('KeyboardView', () => KeyboardView); // TODO:
Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,52 @@
11
import React, {Component} from 'react';
22
import PropTypes from 'prop-types';
3-
import {ScrollView, StyleSheet} from 'react-native';
4-
import {Keyboard, Text, TouchableOpacity} from 'react-native-ui-lib';
3+
import {Keyboard, Colors} from 'react-native-ui-lib';
54
const KeyboardRegistry = Keyboard.KeyboardRegistry;
5+
import KeyboardView from './KeyboardView';
66

7-
class KeyboardView extends Component {
7+
class KeyboardView1 extends Component {
88
static propTypes = {
99
title: PropTypes.string
1010
};
1111

1212
onButtonPress() {
13-
KeyboardRegistry.onItemSelected('KeyboardView', {
13+
KeyboardRegistry.onItemSelected('KeyboardView1', {
1414
message: 'item selected from KeyboardView'
1515
});
1616
}
1717

1818
render() {
19+
const {title} = this.props;
1920
return (
20-
<ScrollView contentContainerStyle={[styles.keyboardContainer, {backgroundColor: 'purple'}]}>
21-
<Text style={{color: 'white'}}>HELOOOO!!!</Text>
22-
<Text style={{color: 'white'}}>{this.props.title}</Text>
23-
<TouchableOpacity
24-
testID={'click-me'}
25-
style={{padding: 20, marginTop: 30, backgroundColor: 'white'}}
26-
onPress={() => this.onButtonPress()}
27-
>
28-
<Text>Click Me!</Text>
29-
</TouchableOpacity>
30-
</ScrollView>
21+
<KeyboardView
22+
title={title}
23+
backgroundColor={Colors.violet10}
24+
onPress={this.onButtonPress}
25+
/>
3126
);
3227
}
3328
}
3429

35-
class AnotherKeyboardView extends Component {
30+
class KeyboardView2 extends Component {
3631
static propTypes = {
3732
title: PropTypes.string
3833
};
3934

4035
onButtonPress() {
41-
KeyboardRegistry.toggleExpandedKeyboard('AnotherKeyboardView');
36+
KeyboardRegistry.toggleExpandedKeyboard('KeyboardView2');
4237
}
4338

4439
render() {
40+
const {title} = this.props;
4541
return (
46-
<ScrollView contentContainerStyle={[styles.keyboardContainer, {backgroundColor: 'orange'}]}>
47-
<Text>*** ANOTHER ONE ***</Text>
48-
<Text>{this.props.title}</Text>
49-
<TouchableOpacity
50-
testID={'toggle-fs'}
51-
style={{padding: 20, marginTop: 30, backgroundColor: 'white'}}
52-
onPress={() => this.onButtonPress()}
53-
>
54-
<Text>Toggle Full-Screen!</Text>
55-
</TouchableOpacity>
56-
</ScrollView>
42+
<KeyboardView
43+
title={title}
44+
backgroundColor={Colors.yellow20}
45+
onPress={this.onButtonPress}
46+
/>
5747
);
5848
}
5949
}
6050

61-
const styles = StyleSheet.create({
62-
keyboardContainer: {
63-
flex: 1,
64-
flexDirection: 'column',
65-
alignItems: 'center',
66-
justifyContent: 'center'
67-
}
68-
});
69-
70-
KeyboardRegistry.registerKeyboard('KeyboardView', () => KeyboardView);
71-
KeyboardRegistry.registerKeyboard('AnotherKeyboardView', () => AnotherKeyboardView);
51+
KeyboardRegistry.registerKeyboard('KeyboardView1', () => KeyboardView1);
52+
KeyboardRegistry.registerKeyboard('KeyboardView2', () => KeyboardView2);

0 commit comments

Comments
 (0)