Skip to content

Commit 629b57a

Browse files
committed
1 parent faca4bc commit 629b57a

File tree

6 files changed

+428
-21
lines changed

6 files changed

+428
-21
lines changed
File renamed without changes.

docs/assets/screenshots/fab-2.png

11 KB
Loading

example/src/FABExample.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ class ButtonExample extends React.Component<Props> {
2929
style={styles.fab}
3030
onPress={this._handlePress}
3131
/>
32-
<FAB icon="add" style={styles.fab} onPress={this._handlePress} />
32+
<FAB icon="favorite" style={styles.fab} onPress={this._handlePress} />
33+
<FAB
34+
icon="done"
35+
label="Extended FAB"
36+
style={styles.fab}
37+
onPress={this._handlePress}
38+
/>
3339
</View>
3440
</View>
3541
);

src/components/FAB.js

+47-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as React from 'react';
55
import { StyleSheet, View } from 'react-native';
66
import Paper from './Paper';
77
import Icon from './Icon';
8+
import Text from './Typography/Text';
89
import TouchableRipple from './TouchableRipple';
910
import { white } from '../styles/colors';
1011
import withTheme from '../core/withTheme';
@@ -13,19 +14,23 @@ import type { IconSource } from './Icon';
1314

1415
type Props = {
1516
/**
16-
* Whether FAB is mini-sized, used to create visual continuity with other elements.
17+
* Icon to display for the `FAB`.
18+
*/
19+
icon: IconSource,
20+
/**
21+
* Optional label for extended `FAB`.
22+
*/
23+
label?: string,
24+
/**
25+
* Whether FAB is mini-sized, used to create visual continuity with other elements. This has no effect if `label` is specified.
1726
*/
1827
small?: boolean,
1928
/**
2029
* Icon color of button, a dark button will render light text and vice-versa.
2130
*/
2231
dark?: boolean,
2332
/**
24-
* Icon to display for the `FAB`.
25-
*/
26-
icon: IconSource,
27-
/**
28-
* Custom color for the FAB.
33+
* Custom color for the `FAB`.
2934
*/
3035
color?: string,
3136
/**
@@ -43,7 +48,8 @@ type Props = {
4348
* A floating action button represents the primary action in an application.
4449
*
4550
* <div class="screenshots">
46-
* <img src="screenshots/fab.png" />
51+
* <img src="screenshots/fab-1.png" />
52+
* <img src="screenshots/fab-2.png" />
4753
* </div>
4854
*
4955
* ## Usage
@@ -66,12 +72,14 @@ class FAB extends React.Component<Props> {
6672
small,
6773
dark,
6874
icon,
75+
label,
6976
color: iconColor,
7077
onPress,
7178
theme,
7279
style,
7380
...rest
7481
} = this.props;
82+
7583
const backgroundColor = theme.colors.accent;
7684
const isDark =
7785
typeof dark === 'boolean' ? dark : !color(backgroundColor).light();
@@ -84,21 +92,31 @@ class FAB extends React.Component<Props> {
8492
return (
8593
<Paper
8694
{...rest}
87-
style={[
88-
{ backgroundColor, elevation: 12 },
89-
styles.content,
90-
small ? styles.small : styles.standard,
91-
style,
92-
]}
95+
style={[{ backgroundColor, elevation: 12 }, styles.container, style]}
9396
>
9497
<TouchableRipple
9598
borderless
9699
onPress={onPress}
97100
rippleColor={rippleColor}
98-
style={[styles.content, small ? styles.small : styles.standard]}
101+
style={styles.container}
99102
>
100-
<View>
103+
<View
104+
style={[
105+
styles.content,
106+
label ? styles.extended : small ? styles.small : styles.standard,
107+
]}
108+
>
101109
<Icon name={icon} size={24} color={textColor} />
110+
{label ? (
111+
<Text
112+
style={[
113+
styles.label,
114+
{ color: textColor, fontFamily: theme.fonts.medium },
115+
]}
116+
>
117+
{label.toUpperCase()}
118+
</Text>
119+
) : null}
102120
</View>
103121
</TouchableRipple>
104122
</Paper>
@@ -107,19 +125,28 @@ class FAB extends React.Component<Props> {
107125
}
108126

109127
const styles = StyleSheet.create({
110-
content: {
111-
alignItems: 'center',
112-
justifyContent: 'center',
128+
container: {
129+
borderRadius: 28,
113130
},
114131
standard: {
115132
height: 56,
116133
width: 56,
117-
borderRadius: 28,
118134
},
119135
small: {
120136
height: 40,
121137
width: 40,
122-
borderRadius: 20,
138+
},
139+
extended: {
140+
height: 48,
141+
paddingHorizontal: 16,
142+
},
143+
content: {
144+
flexDirection: 'row',
145+
alignItems: 'center',
146+
justifyContent: 'center',
147+
},
148+
label: {
149+
marginHorizontal: 8,
123150
},
124151
});
125152

src/components/__tests__/FAB.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* @flow */
2+
3+
import * as React from 'react';
4+
import renderer from 'react-test-renderer';
5+
import FAB from '../FAB';
6+
7+
it('renders normal FAB', () => {
8+
const tree = renderer.create(<FAB onPress={() => {}} icon="add" />).toJSON();
9+
10+
expect(tree).toMatchSnapshot();
11+
});
12+
13+
it('renders small FAB', () => {
14+
const tree = renderer
15+
.create(<FAB small onPress={() => {}} icon="add" />)
16+
.toJSON();
17+
18+
expect(tree).toMatchSnapshot();
19+
});
20+
21+
it('renders extended FAB', () => {
22+
const tree = renderer
23+
.create(<FAB onPress={() => {}} icon="add" label="Add items" />)
24+
.toJSON();
25+
26+
expect(tree).toMatchSnapshot();
27+
});

0 commit comments

Comments
 (0)