Skip to content

Commit 691d3d7

Browse files
authored
Merge pull request #1170 from StoDevX/extract-components
Extract custom Cell-based components
2 parents 8db8b60 + 8029c43 commit 691d3d7

File tree

14 files changed

+208
-130
lines changed

14 files changed

+208
-130
lines changed

source/views/components/__tests__/cell-toggle.test.js renamed to source/views/components/cells/__tests__/toggle.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import React from 'react'
55
import {shallow} from 'enzyme'
66

7-
import {CellToggle} from '../cell-toggle'
7+
import {CellToggle} from '../toggle'
88
import noop from 'lodash/noop'
99

1010
test('renders', () => {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @flow
2+
3+
import React from 'react'
4+
import {StyleSheet, Text} from 'react-native'
5+
import {Cell} from 'react-native-tableview-simple'
6+
import * as c from '../../components/colors'
7+
8+
const styles = StyleSheet.create({
9+
title: {
10+
textAlign: 'left',
11+
},
12+
active: {
13+
color: c.infoBlue,
14+
},
15+
disabled: {
16+
color: c.iosDisabledText,
17+
},
18+
})
19+
20+
export function ButtonCell({
21+
indeterminate,
22+
disabled,
23+
onPress,
24+
title,
25+
}: {
26+
indeterminate?: boolean,
27+
disabled?: boolean,
28+
onPress: () => any,
29+
title: string,
30+
}) {
31+
return (
32+
<Cell
33+
titleTextStyle={styles.title}
34+
isDisabled={indeterminate || disabled}
35+
onPress={onPress}
36+
title={
37+
<Text
38+
style={[
39+
styles.text,
40+
indeterminate || disabled ? styles.disabled : styles.active,
41+
]}
42+
>
43+
{title}
44+
</Text>
45+
}
46+
/>
47+
)
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @flow
2+
3+
import React from 'react'
4+
import {StyleSheet, Alert} from 'react-native'
5+
import {Cell} from 'react-native-tableview-simple'
6+
import * as c from '../colors'
7+
8+
const deleteStyles = StyleSheet.create({
9+
text: {textAlign: 'center', color: c.red},
10+
})
11+
12+
export const DeleteButtonCell = ({
13+
title,
14+
skipConfirm = false,
15+
onPress,
16+
}: {
17+
title: string,
18+
skipConfirm?: boolean,
19+
onPress?: () => any,
20+
}) => {
21+
const onPressCallback = onPress ? onPress : () => {}
22+
23+
const callback = !skipConfirm
24+
? () =>
25+
Alert.alert(title, 'Are you sure you want to delete this?', [
26+
{text: 'Cancel', onPress: () => {}, style: 'cancel'},
27+
{text: 'Delete', onPress: onPressCallback, style: 'destructive'},
28+
])
29+
: onPressCallback
30+
31+
return (
32+
<Cell title={title} titleTextStyle={deleteStyles.text} onPress={callback} />
33+
)
34+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// @flow
2+
import React from 'react'
3+
import {StyleSheet, Text, Platform, TextInput} from 'react-native'
4+
import {Cell} from 'react-native-tableview-simple'
5+
import {Row} from '../../components/layout'
6+
import * as c from '../../components/colors'
7+
8+
const styles = StyleSheet.create({
9+
label: {
10+
width: 90,
11+
fontSize: 16,
12+
marginTop: Platform.OS === 'ios' ? -2 : 0, // lines the label up with the text on iOS
13+
alignSelf: 'center',
14+
},
15+
hiddenLabel: {
16+
width: 0,
17+
},
18+
customTextInput: {
19+
flex: 1,
20+
},
21+
loginCell: {
22+
height: Platform.OS === 'android' ? 65 : 44,
23+
alignItems: 'stretch',
24+
paddingTop: 0,
25+
paddingBottom: 0,
26+
},
27+
})
28+
29+
export class CellTextField extends React.Component {
30+
static defaultProps = {
31+
disabled: false,
32+
placeholder: '',
33+
_ref: () => {},
34+
returnKeyType: 'default',
35+
secureTextEntry: false,
36+
}
37+
38+
props: {
39+
label?: string,
40+
_ref: () => any,
41+
disabled: boolean,
42+
onChangeText: string => any,
43+
onSubmitEditing: string => any,
44+
placeholder: string,
45+
returnKeyType: 'done' | 'next' | 'default',
46+
secureTextEntry: boolean,
47+
value: string,
48+
labelWidth?: number,
49+
}
50+
51+
_input: any
52+
focusInput = () => this._input.focus()
53+
54+
cacheRef = (ref: any) => {
55+
this._input = ref
56+
this.props._ref(ref)
57+
}
58+
59+
onSubmit = () => {
60+
this.props.onSubmitEditing(this.props.value)
61+
}
62+
63+
render() {
64+
const labelWidthStyle = this.props.labelWidth !== null &&
65+
this.props.labelWidth !== undefined
66+
? {width: this.props.labelWidth}
67+
: null
68+
69+
const label = this.props.label
70+
? <Text onPress={this.focusInput} style={[styles.label, labelWidthStyle]}>
71+
{this.props.label}
72+
</Text>
73+
: <Text style={styles.hiddenLabel} />
74+
75+
return (
76+
<Cell
77+
contentContainerStyle={styles.loginCell}
78+
cellContentView={label}
79+
cellAccessoryView={
80+
<TextInput
81+
ref={this.cacheRef}
82+
autoCapitalize="none"
83+
autoCorrect={false}
84+
clearButtonMode="while-editing"
85+
disabled={this.props.disabled}
86+
onChangeText={this.props.onChangeText}
87+
onSubmitEditing={this.onSubmit}
88+
placeholder={this.props.placeholder}
89+
placeholderTextColor={c.iosPlaceholderText}
90+
returnKeyType={this.props.returnKeyType}
91+
secureTextEntry={this.props.secureTextEntry}
92+
style={[styles.customTextInput]}
93+
value={this.props.value}
94+
/>
95+
}
96+
/>
97+
)
98+
}
99+
}
File renamed without changes.

source/views/components/filter/section-toggle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React from 'react'
33
import type {ToggleType} from './types'
44
import {Section} from 'react-native-tableview-simple'
5-
import {CellToggle} from '../../components/cell-toggle'
5+
import {CellToggle} from '../../components/cells/toggle'
66

77
type PropsType = {
88
filter: ToggleType,

source/views/components/touchable.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ type PropsType = {
1414
children?: any,
1515
borderless?: boolean,
1616
style?: any,
17+
containerStyle?: any,
1718
}
1819
export const Touchable = ({
1920
children,
2021
style,
2122
highlight = true,
2223
borderless = false,
2324
onPress = () => {},
25+
containerStyle,
2426
...props
2527
}: PropsType) => {
2628
// The child <View> is required; the Touchable needs a View as its direct child.
@@ -34,7 +36,12 @@ export const Touchable = ({
3436
? {underlayColor: '#d9d9d9'}
3537
: {activeOpacity: 0.65}
3638
return (
37-
<Component onPress={onPress} {...innerProps} {...props}>
39+
<Component
40+
onPress={onPress}
41+
{...innerProps}
42+
style={containerStyle}
43+
{...props}
44+
>
3845
{content}
3946
</Component>
4047
)
@@ -48,6 +55,7 @@ export const Touchable = ({
4855
<TouchableNativeFeedback
4956
onPress={onPress}
5057
background={background}
58+
style={containerStyle}
5159
{...props}
5260
>
5361
{content}
Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
// @flow
2-
import React from 'react'
3-
import {StyleSheet, Text} from 'react-native'
4-
import {Cell} from 'react-native-tableview-simple'
5-
import * as c from '../../components/colors'
62

7-
const styles = StyleSheet.create({
8-
button: {
9-
justifyContent: 'flex-start',
10-
},
11-
text: {
12-
fontSize: 16,
13-
},
14-
active: {
15-
color: c.infoBlue,
16-
},
17-
disabled: {
18-
color: c.iosDisabledText,
19-
},
20-
})
3+
import React from 'react'
4+
import {ButtonCell} from '../../components/cells/button'
215

226
export function LoginButton({
237
loading,
@@ -32,22 +16,16 @@ export function LoginButton({
3216
onPress: () => any,
3317
label: string,
3418
}) {
35-
let loginTextStyle = loading || disabled ? styles.disabled : styles.active
36-
37-
const contents = (
38-
<Text style={[styles.text, loginTextStyle]}>
39-
{loading
40-
? `Logging in to ${label}…`
41-
: loggedIn ? `Sign Out of ${label}` : `Sign In to ${label}`}
42-
</Text>
43-
)
44-
4519
return (
46-
<Cell
47-
contentContainerStyle={styles.button}
48-
isDisabled={loading || disabled}
20+
<ButtonCell
21+
disabled={loading || disabled}
22+
indeterminate={loading}
4923
onPress={onPress}
50-
title={contents}
24+
title={
25+
loading
26+
? `Logging in to ${label}…`
27+
: loggedIn ? `Sign Out of ${label}` : `Sign In to ${label}`
28+
}
5129
/>
5230
)
5331
}

0 commit comments

Comments
 (0)