Skip to content

Commit 600f179

Browse files
rkpasiaHoussein Djirdeh
authored andcommitted
Refactor StateBadge component (#715)
* refactor: StateBadge refactor with styled-components #532 * refactor: Remove unused StyleSheet from issue-description component * test: Update StateBadge tests to match component updates * refactor: StateBadge color selection refactor for readability * refactor: Remove "/native" from styled-components import
1 parent 7bd0f14 commit 600f179

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed

__tests__/tests/components/StateBadge.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('<StateBadge />', () => {
2121
.childAt(0)
2222
.text()
2323
).toEqual('Open');
24-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.green);
24+
expect(wrapper.prop('color')).toEqual(colors.green);
2525
});
2626

2727
it('correctly renders with closed issue', () => {
@@ -33,7 +33,7 @@ describe('<StateBadge />', () => {
3333
.childAt(0)
3434
.text()
3535
).toEqual('Closed');
36-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.red);
36+
expect(wrapper.prop('color')).toEqual(colors.red);
3737
});
3838

3939
it('correctly renders with open pull request', () => {
@@ -45,7 +45,7 @@ describe('<StateBadge />', () => {
4545
.childAt(0)
4646
.text()
4747
).toEqual('Open');
48-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.green);
48+
expect(wrapper.prop('color')).toEqual(colors.green);
4949
});
5050

5151
it('correctly renders with closed pull request', () => {
@@ -57,7 +57,7 @@ describe('<StateBadge />', () => {
5757
.childAt(0)
5858
.text()
5959
).toEqual('Closed');
60-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.red);
60+
expect(wrapper.prop('color')).toEqual(colors.red);
6161
});
6262

6363
it('correctly renders with merged pull request', () => {
@@ -71,7 +71,7 @@ describe('<StateBadge />', () => {
7171
.childAt(0)
7272
.text()
7373
).toEqual('Merged');
74-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.purple);
74+
expect(wrapper.prop('color')).toEqual(colors.purple);
7575
});
7676

7777
it('correctly renders without issue', () => {
@@ -83,7 +83,7 @@ describe('<StateBadge />', () => {
8383
.childAt(0)
8484
.text()
8585
).toEqual('Merged');
86-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.purple);
86+
expect(wrapper.prop('color')).toEqual(colors.purple);
8787
});
8888

8989
it('correctly renders with open type', () => {
@@ -95,7 +95,7 @@ describe('<StateBadge />', () => {
9595
.childAt(0)
9696
.text()
9797
).toEqual('');
98-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.green);
98+
expect(wrapper.prop('color')).toEqual(colors.green);
9999
});
100100

101101
it('correctly renders with closed type', () => {
@@ -107,7 +107,7 @@ describe('<StateBadge />', () => {
107107
.childAt(0)
108108
.text()
109109
).toEqual('');
110-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.red);
110+
expect(wrapper.prop('color')).toEqual(colors.red);
111111
});
112112

113113
it('correctly renders with closed type', () => {
@@ -119,7 +119,7 @@ describe('<StateBadge />', () => {
119119
.childAt(0)
120120
.text()
121121
).toEqual('');
122-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.purple);
122+
expect(wrapper.prop('color')).toEqual(colors.purple);
123123
});
124124

125125
it('correctly renders with custom text', () => {
@@ -133,6 +133,6 @@ describe('<StateBadge />', () => {
133133
.childAt(0)
134134
.text()
135135
).toEqual('test text');
136-
expect(wrapper.prop('style')[2].backgroundColor).toEqual(colors.green);
136+
expect(wrapper.prop('color')).toEqual(colors.green);
137137
});
138138
});

src/components/issue-description.component.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { StyleSheet, ActivityIndicator } from 'react-native';
2+
import { ActivityIndicator } from 'react-native';
33
import { ListItem } from 'react-native-elements';
44
import Parse from 'parse-diff';
55
import styled from 'styled-components';
@@ -15,13 +15,6 @@ import { translate, relativeTimeToNow } from 'utils';
1515
import { colors, fonts, normalize } from 'config';
1616
import { v3 } from 'api';
1717

18-
const styles = StyleSheet.create({
19-
badge: {
20-
alignItems: 'flex-end',
21-
justifyContent: 'center',
22-
},
23-
});
24-
2518
const HeaderContainer = styled.View`
2619
flex-direction: row;
2720
align-items: center;
@@ -164,7 +157,6 @@ export class IssueDescription extends Component {
164157
(issue.pull_request &&
165158
!isPendingCheckMerge && (
166159
<StateBadge
167-
style={styles.badge}
168160
issue={issue}
169161
isMerged={isMerged && issue.pull_request}
170162
locale={locale}
Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Text, View, StyleSheet } from 'react-native';
2+
import styled from 'styled-components';
33

44
import { colors, fonts, normalize } from 'config';
55
import { translate } from 'utils';
@@ -13,28 +13,17 @@ type Props = {
1313
locale: string,
1414
};
1515

16-
const styles = StyleSheet.create({
17-
badge: {
18-
padding: 12,
19-
paddingTop: 3,
20-
paddingBottom: 3,
21-
borderRadius: 20,
22-
},
23-
mergedIssue: {
24-
backgroundColor: colors.purple,
25-
},
26-
openIssue: {
27-
backgroundColor: colors.green,
28-
},
29-
closedIssue: {
30-
backgroundColor: colors.red,
31-
},
32-
text: {
33-
fontSize: normalize(12),
34-
...fonts.fontPrimarySemiBold,
35-
color: colors.white,
36-
},
37-
});
16+
const Badge = styled.View`
17+
padding: 3px 12px;
18+
border-radius: 20px;
19+
background-color: ${props => props.color};
20+
`;
21+
22+
const BadgeText = styled.Text`
23+
color: ${colors.white};
24+
font-size: ${normalize(12)};
25+
${fonts.fontPrimarySemiBold};
26+
`;
3827

3928
export const StateBadge = ({
4029
issue,
@@ -58,19 +47,18 @@ export const StateBadge = ({
5847
issueText = translate('issue.main.states.closed', locale);
5948
}
6049

61-
let issueStyle = {};
62-
63-
if (issueState === 'merged') {
64-
issueStyle = styles.mergedIssue;
65-
} else if (issueState === 'open') {
66-
issueStyle = styles.openIssue;
67-
} else if (issueState === 'closed') {
68-
issueStyle = styles.closedIssue;
69-
}
50+
const stateColor = {
51+
merged: colors.purple,
52+
open: colors.green,
53+
closed: colors.red,
54+
};
55+
const issueStateColor = stateColor[issueState]
56+
? stateColor[issueState]
57+
: colors.gray;
7058

7159
return (
72-
<View style={[styles.badge, style, issueStyle]}>
73-
<Text style={styles.text}>{issueText}</Text>
74-
</View>
60+
<Badge color={issueStateColor} style={style}>
61+
<BadgeText>{issueText}</BadgeText>
62+
</Badge>
7563
);
7664
};

0 commit comments

Comments
 (0)