Skip to content

Commit d0fc33c

Browse files
simonhoyoschinesedfan
authored andcommitted
refactor: use styled-components in user-list-item (#601)
* refactor: use styled-components in user-list-item * Changed BorderBottom prop's name * fix(style): Fix invalid css prop
1 parent 1947f1c commit d0fc33c

File tree

1 file changed

+92
-80
lines changed

1 file changed

+92
-80
lines changed

src/components/user-list-item.component.js

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,76 @@
11
import React, { Component } from 'react';
22
import { connect } from 'react-redux';
3-
import {
4-
StyleSheet,
5-
View,
6-
TouchableHighlight,
7-
TouchableOpacity,
8-
Text,
9-
Image,
10-
} from 'react-native';
3+
import { View, TouchableHighlight } from 'react-native';
114
import { Icon } from 'react-native-elements';
5+
import styled, { css } from 'styled-components';
126

137
import { colors, fonts, normalize } from 'config';
148

15-
const styles = StyleSheet.create({
16-
borderContainer: {
17-
borderBottomColor: colors.greyLight,
18-
borderBottomWidth: 1,
19-
},
20-
wrapper: {
21-
padding: 10,
22-
flexDirection: 'row',
23-
},
24-
userInfo: {
25-
flex: 1,
26-
flexDirection: 'row',
27-
alignItems: 'center',
28-
},
29-
avatarContainer: {
30-
backgroundColor: colors.greyLight,
31-
borderRadius: 17,
32-
width: 34,
33-
height: 34,
34-
},
35-
avatar: {
36-
borderRadius: 17,
37-
width: 34,
38-
height: 34,
39-
},
40-
titleSubtitleContainer: {
41-
justifyContent: 'center',
42-
flex: 1,
43-
},
44-
title: {
45-
color: colors.black,
46-
...fonts.fontPrimary,
47-
fontSize: normalize(14),
48-
marginLeft: 10,
49-
},
50-
subtitle: {
51-
color: colors.greyDark,
52-
fontSize: normalize(10),
53-
marginTop: 1,
54-
...fonts.fontPrimarySemiBold,
55-
marginLeft: 10,
56-
},
57-
iconContainer: {
58-
flex: 0.15,
59-
alignItems: 'flex-end',
60-
justifyContent: 'center',
61-
},
62-
});
9+
const ViewBorderContainer = styled.View`
10+
${props =>
11+
props.hasBorderBottom &&
12+
css`
13+
border-bottom-color: ${colors.greyLight};
14+
border-bottom-width: 1;
15+
`};
16+
`;
17+
const TouchableBorderContainer = ViewBorderContainer.withComponent(
18+
TouchableHighlight
19+
);
20+
21+
const Wrapper = styled.View`
22+
padding: 10px;
23+
flex-direction: row;
24+
`;
25+
26+
const TouchableUserInfo = styled.TouchableOpacity`
27+
flex: 1;
28+
flex-direction: row;
29+
align-items: center;
30+
`;
31+
const ViewUserInfo = TouchableUserInfo.withComponent(View);
32+
33+
const TouchableAvatarContainer = styled.TouchableOpacity`
34+
background-color: ${colors.greyLight};
35+
border-radius: 17;
36+
width: 34;
37+
height: 34;
38+
`;
39+
const ViewAvatarContainer = TouchableAvatarContainer.withComponent(View);
40+
41+
const Avatar = styled.Image`
42+
border-radius: 17;
43+
width: 34;
44+
height: 34;
45+
`;
46+
47+
const TitleSubtitleContainer = styled.View`
48+
justify-content: center;
49+
flex: 1;
50+
`;
51+
52+
const Title = styled.Text`
53+
color: ${colors.black};
54+
${fonts.fontPrimary};
55+
font-size: ${normalize(14)};
56+
margin-left: 10;
57+
${props => props.titleStyle};
58+
`;
59+
60+
const Subtitle = styled.Text`
61+
color: ${colors.greyDark};
62+
font-size: ${normalize(10)};
63+
margin-top: 1;
64+
${fonts.fontPrimarySemiBold};
65+
margin-left: 10;
66+
`;
67+
68+
const TouchableIconContainer = styled.TouchableOpacity`
69+
flex: 0.15;
70+
align-items: flex-end;
71+
justify-content: center;
72+
`;
73+
const ViewIconContainer = TouchableIconContainer.withComponent(View);
6374

6475
const mapStateToProps = state => ({
6576
authUser: state.auth.user,
@@ -94,11 +105,17 @@ class UserListItemComponent extends Component {
94105
} = this.props;
95106

96107
const ContainerComponent =
97-
iconAction || onlyImageNavigate ? View : TouchableHighlight;
108+
iconAction || onlyImageNavigate
109+
? ViewBorderContainer
110+
: TouchableBorderContainer;
98111
const UserComponent =
99-
iconAction && !onlyImageNavigate ? TouchableOpacity : View;
100-
const ImageContainerComponent = onlyImageNavigate ? TouchableOpacity : View;
101-
const IconComponent = iconAction ? TouchableOpacity : View;
112+
iconAction && !onlyImageNavigate ? TouchableUserInfo : ViewUserInfo;
113+
const ImageContainerComponent = onlyImageNavigate
114+
? TouchableAvatarContainer
115+
: ViewAvatarContainer;
116+
const IconComponent = iconAction
117+
? TouchableIconContainer
118+
: ViewIconContainer;
102119

103120
const userScreen =
104121
authUser.login === user.login ? 'AuthProfile' : 'Profile';
@@ -109,54 +126,49 @@ class UserListItemComponent extends Component {
109126
navigation.navigate(
110127
user.type === 'User' ? userScreen : 'Organization',
111128
user.type === 'User' ? { user } : { organization: user }
112-
)}
129+
)
130+
}
113131
underlayColor={colors.greyLight}
114-
style={!noBorderBottom && styles.borderContainer}
132+
hasBorderBottom={!noBorderBottom}
115133
>
116-
<View style={styles.wrapper}>
134+
<Wrapper>
117135
<UserComponent
118-
style={styles.userInfo}
119136
onPress={() =>
120137
navigation.navigate(userScreen, {
121138
user,
122-
})}
139+
})
140+
}
123141
>
124142
<ImageContainerComponent
125-
style={styles.avatarContainer}
126143
onPress={() =>
127144
navigation.navigate(userScreen, {
128145
user,
129-
})}
146+
})
147+
}
130148
>
131-
<Image
132-
style={styles.avatar}
149+
<Avatar
133150
source={{
134151
uri: user.avatar_url,
135152
}}
136153
/>
137154
</ImageContainerComponent>
138155

139-
<View style={styles.titleSubtitleContainer}>
140-
<Text style={[styles.title, titleStyle && titleStyle]}>
141-
{title || user.login}
142-
</Text>
156+
<TitleSubtitleContainer>
157+
<Title titleStyle={titleStyle}>{title || user.login}</Title>
143158

144-
{subtitle && <Text style={styles.subtitle}>{subtitle}</Text>}
145-
</View>
159+
{subtitle && <Subtitle>{subtitle}</Subtitle>}
160+
</TitleSubtitleContainer>
146161
</UserComponent>
147162

148-
<IconComponent
149-
style={styles.iconContainer}
150-
onPress={() => iconAction(user.login)}
151-
>
163+
<IconComponent onPress={() => iconAction(user.login)}>
152164
<Icon
153165
color={colors.grey}
154166
size={icon ? 24 : 28}
155167
name={icon || 'chevron-right'}
156168
type={icon && 'octicon'}
157169
/>
158170
</IconComponent>
159-
</View>
171+
</Wrapper>
160172
</ContainerComponent>
161173
);
162174
}

0 commit comments

Comments
 (0)