Skip to content

Commit c0f7f90

Browse files
a-morenovallsferrannp
authored andcommitted
feat: ToggleButton component (#575)
1 parent 0ad98f3 commit c0f7f90

File tree

13 files changed

+996
-212
lines changed

13 files changed

+996
-212
lines changed
838 KB
Loading
5.35 KB
Loading

docs/pages/src/Home.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,5 @@ const screenshots = [
170170
'gallery/typography.png',
171171
'gallery/bottom-navigation.png',
172172
'gallery/fab.png',
173+
'gallery/toggle-button.png',
173174
];

example/src/ExampleList.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import SurfaceExample from './SurfaceExample';
2626
import SwitchExample from './SwitchExample';
2727
import TextExample from './TextExample';
2828
import TextInputExample from './TextInputExample';
29+
import ToggleButtonExample from './ToggleButtonExample';
2930
import TouchableRippleExample from './TouchableRippleExample';
3031

3132
type Props = {
@@ -57,6 +58,7 @@ export const examples = {
5758
switch: SwitchExample,
5859
text: TextExample,
5960
textInput: TextInputExample,
61+
toggleButton: ToggleButtonExample,
6062
touchableRipple: TouchableRippleExample,
6163
};
6264

example/src/ToggleButtonExample.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* @flow */
2+
3+
import * as React from 'react';
4+
import { View, StyleSheet, ImageBackground } from 'react-native';
5+
import { withTheme, ToggleButton, List, type Theme } from 'react-native-paper';
6+
7+
type Props = {
8+
theme: Theme,
9+
};
10+
11+
type State = {
12+
first: string,
13+
second: string,
14+
fruit: string,
15+
status: 'checked' | 'unchecked',
16+
};
17+
18+
class ToggleButtonExample extends React.Component<Props, State> {
19+
static title = 'Toggle Button';
20+
21+
state = {
22+
first: 'bold',
23+
second: 'left',
24+
fruit: 'watermelon',
25+
status: 'checked',
26+
};
27+
28+
render() {
29+
const {
30+
theme: {
31+
colors: { background },
32+
},
33+
} = this.props;
34+
return (
35+
<View style={[styles.container, { backgroundColor: background }]}>
36+
<List.Section title="Single">
37+
<View style={styles.row}>
38+
<ToggleButton
39+
icon="android"
40+
value="android"
41+
status={this.state.status}
42+
onPress={status => {
43+
this.setState({
44+
status: status === 'checked' ? 'unchecked' : 'checked',
45+
});
46+
}}
47+
/>
48+
</View>
49+
</List.Section>
50+
<List.Section title="Group">
51+
<View style={styles.toggleGroup}>
52+
<ToggleButton.Group
53+
value={this.state.first}
54+
onValueChange={value =>
55+
this.setState({
56+
first: value,
57+
})
58+
}
59+
>
60+
<ToggleButton disabled icon="format-italic" value="italic" />
61+
<ToggleButton icon="format-bold" value="bold" />
62+
<ToggleButton icon="format-underlined" value="underlined" />
63+
<ToggleButton icon="format-color-text" value="format-color" />
64+
</ToggleButton.Group>
65+
</View>
66+
</List.Section>
67+
<List.Section title="Custom">
68+
<View style={styles.group}>
69+
<ToggleButton.Group
70+
value={this.state.fruit}
71+
onValueChange={value =>
72+
this.setState({
73+
fruit: value,
74+
})
75+
}
76+
>
77+
<ImageBackground
78+
style={{
79+
width: 143,
80+
height: 153,
81+
margin: 2,
82+
}}
83+
source={{
84+
uri:
85+
'https://images.pexels.com/photos/1068534/pexels-photo-1068534.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260',
86+
}}
87+
>
88+
<ToggleButton
89+
borderless
90+
value="watermelon"
91+
size={24}
92+
style={{
93+
position: 'absolute',
94+
right: 0,
95+
}}
96+
color="white"
97+
icon={
98+
this.state.fruit === 'watermelon'
99+
? 'favorite'
100+
: 'favorite-border'
101+
}
102+
/>
103+
</ImageBackground>
104+
<ImageBackground
105+
style={{
106+
width: 143,
107+
height: 153,
108+
margin: 2,
109+
}}
110+
source={{
111+
uri:
112+
'https://images.pexels.com/photos/46174/strawberries-berries-fruit-freshness-46174.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260',
113+
}}
114+
>
115+
<ToggleButton
116+
borderless
117+
value="strawberries"
118+
size={24}
119+
style={{
120+
position: 'absolute',
121+
right: 0,
122+
}}
123+
color="white"
124+
icon={
125+
this.state.fruit === 'strawberries'
126+
? 'favorite'
127+
: 'favorite-border'
128+
}
129+
/>
130+
</ImageBackground>
131+
</ToggleButton.Group>
132+
</View>
133+
</List.Section>
134+
</View>
135+
);
136+
}
137+
}
138+
139+
const styles = StyleSheet.create({
140+
container: {
141+
flex: 1,
142+
},
143+
toggleGroup: {
144+
flexDirection: 'row',
145+
paddingHorizontal: 16,
146+
},
147+
group: {
148+
flexDirection: 'row',
149+
marginLeft: 5,
150+
},
151+
row: {
152+
flexDirection: 'row',
153+
flexWrap: 'wrap',
154+
paddingHorizontal: 16,
155+
},
156+
});
157+
158+
export default withTheme(ToggleButtonExample);

0 commit comments

Comments
 (0)