Skip to content

Commit 734cd98

Browse files
gawrysiaksatya164
authored andcommitted
BREAKING: Drawer redesign (#377)
Drawer redesign for 2.0. Changes: - changed the active state styling - colors are now theme based, not fixed - theme based roundness - removed the "color" prop - it can be now controller with the primaryColor (see [the expo app example](https://github.com/callstack/react-native-paper/pull/377/files#diff-cf8d6e3323746c5c486de08cb0c333c3R59)) - added snapshot tests <img width="248" alt="screen shot 2018-05-16 at 23 59 54" src="https://user-images.githubusercontent.com/7827311/40146708-e58bf92c-5966-11e8-8916-e76cb42e2f86.png"> <img width="248" alt="screen shot 2018-05-17 at 00 00 02" src="https://user-images.githubusercontent.com/7827311/40146727-f26662a4-5966-11e8-9cee-dc01e3cd5599.png"> Fixes #358. Snapshot tests added. Run `yarn test`.
1 parent c696883 commit 734cd98

File tree

4 files changed

+347
-45
lines changed

4 files changed

+347
-45
lines changed

example/DrawerItems.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ class DrawerItems extends React.Component<Props, State> {
5656
<DrawerItem
5757
{...props}
5858
key={props.key}
59-
color={props.key === 3 ? Colors.tealA200 : undefined}
59+
theme={
60+
props.key === 3
61+
? { colors: { primary: Colors.tealA200 } }
62+
: undefined
63+
}
6064
active={this.state.drawerItemIndex === index}
6165
onPress={() => this._setDrawerItem(index)}
6266
/>

src/components/DrawerItem.js

+51-44
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as React from 'react';
55
import { View, StyleSheet } from 'react-native';
66
import Icon from './Icon';
77
import TouchableRipple from './TouchableRipple';
8-
import { grey300, grey700 } from '../styles/colors';
98
import withTheme from '../core/withTheme';
109
import Text from './Typography/Text';
1110
import type { Theme } from '../types';
@@ -28,10 +27,7 @@ type Props = {
2827
* Function to execute on press.
2928
*/
3029
onPress?: () => mixed,
31-
/**
32-
* Custom color for the drawer text and icon.
33-
*/
34-
color?: string,
30+
style?: any,
3531
/**
3632
* @optional
3733
*/
@@ -53,61 +49,72 @@ type Props = {
5349
*/
5450
class DrawerItem extends React.Component<Props> {
5551
render() {
56-
const {
57-
color: activeColor,
58-
icon,
59-
label,
60-
active,
61-
theme,
62-
...props
63-
} = this.props;
64-
const { colors, dark } = theme;
65-
const backgroundColor = active ? (dark ? grey700 : grey300) : 'transparent';
66-
const labelColor = active
67-
? activeColor || colors.text
68-
: color(colors.text)
69-
.alpha(0.54)
52+
const { icon, label, active, theme, style, onPress, ...rest } = this.props;
53+
const { colors, roundness } = theme;
54+
const backgroundColor = active
55+
? color(colors.primary)
56+
.alpha(0.12)
7057
.rgb()
71-
.string();
72-
const iconColor = active
73-
? activeColor || colors.text
58+
.string()
59+
: 'transparent';
60+
const contentColor = active
61+
? colors.primary
7462
: color(colors.text)
75-
.alpha(0.54)
63+
.alpha(0.68)
7664
.rgb()
7765
.string();
7866
const fontFamily = theme.fonts.medium;
7967
const labelMargin = icon ? 32 : 0;
8068

8169
return (
82-
<TouchableRipple {...props}>
83-
<View style={[styles.wrapper, { backgroundColor }]}>
84-
{icon && <Icon source={icon} size={24} color={iconColor} />}
85-
<Text
86-
numberOfLines={1}
87-
style={[
88-
{
89-
color: labelColor,
90-
fontFamily,
91-
marginLeft: labelMargin,
92-
},
93-
styles.label,
94-
]}
95-
>
96-
{label}
97-
</Text>
98-
</View>
99-
</TouchableRipple>
70+
<View
71+
{...rest}
72+
style={[
73+
styles.container,
74+
{ backgroundColor, borderRadius: roundness },
75+
style,
76+
]}
77+
>
78+
<TouchableRipple
79+
borderless
80+
delayPressIn={0}
81+
onPress={onPress}
82+
style={{ borderRadius: roundness }}
83+
>
84+
<View style={styles.wrapper}>
85+
{icon ? (
86+
<Icon source={icon} size={24} color={contentColor} />
87+
) : null}
88+
<Text
89+
numberOfLines={1}
90+
style={[
91+
styles.label,
92+
{
93+
color: contentColor,
94+
fontFamily,
95+
marginLeft: labelMargin,
96+
},
97+
]}
98+
>
99+
{label}
100+
</Text>
101+
</View>
102+
</TouchableRipple>
103+
</View>
100104
);
101105
}
102106
}
103107

104108
const styles = StyleSheet.create({
109+
container: {
110+
marginHorizontal: 10,
111+
marginVertical: 4,
112+
},
105113
wrapper: {
106114
flexDirection: 'row',
107115
alignItems: 'center',
108-
paddingHorizontal: 16,
109-
paddingVertical: 12,
110-
height: 48,
116+
padding: 8,
117+
height: 40,
111118
},
112119
label: {
113120
marginRight: 32,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* @flow */
2+
3+
import * as React from 'react';
4+
import renderer from 'react-test-renderer';
5+
import DrawerItem from '../DrawerItem';
6+
7+
it('renders basic DrawerItem', () => {
8+
const tree = renderer
9+
.create(<DrawerItem onPress={() => {}} label="Example item" />)
10+
.toJSON();
11+
12+
expect(tree).toMatchSnapshot();
13+
});
14+
15+
it('renders DrawerItem with icon', () => {
16+
const tree = renderer
17+
.create(<DrawerItem icon="info" label="Example item" />)
18+
.toJSON();
19+
20+
expect(tree).toMatchSnapshot();
21+
});
22+
23+
it('renders active DrawerItem', () => {
24+
const tree = renderer
25+
.create(<DrawerItem icon="info" active label="Example item" />)
26+
.toJSON();
27+
28+
expect(tree).toMatchSnapshot();
29+
});

0 commit comments

Comments
 (0)