forked from jaysoo/react-native-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.js
185 lines (180 loc) · 5.2 KB
/
Example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {
ScrollView,
StyleSheet,
Text,
View
} = ReactNative;
import Menu, {
MenuContext,
MenuOptions,
MenuOption,
MenuTrigger
} from 'react-native-menu';
const Example = React.createClass({
componentDidMount() {
// We can use the public context API to open/close/toggle the menu.
//setInterval(() => {
// this.refs.MenuContext.toggleMenu('menu1');
//}, 2000);
},
getInitialState() {
return {
message: 'Try clicking the top-right menus',
firstMenuDisabled: false,
dropdownSelection: '-- Choose --'
};
},
setMessage(value) {
if (typeof value === 'string') {
this.setState({ message: `You selected "${value}"` });
} else {
this.setState({ message: `Woah!\n\nYou selected an object:\n\n${JSON.stringify(value)}` });
}
return value !== 'do not close';
},
setFirstMenuDisabled(disabled) {
this.setState({
message: `First menu is ${disabled ? 'disabled' : 'enabled'}`,
firstMenuDisabled: disabled
});
return false;
},
render() {
return (
<MenuContext style={{ flex: 1 }} ref="MenuContext">
<View style={styles.topbar}>
<Menu onSelect={this.setMessage}>
<MenuTrigger disabled={this.state.firstMenuDisabled} style={styles.menuTrigger}>
<Text style={styles.menuTriggerText}>OPEN FIRST MENU</Text>
</MenuTrigger>
<MenuOptions style={styles.menuOptions}>
<MenuOption value="normal">
<Text>Normal option</Text>
</MenuOption>
<MenuOption value="do not close">
<Text>Does not close menu</Text>
</MenuOption>
<MenuOption value="disabled" disabled={true}>
<Text style={styles.disabled}>Disabled option</Text>
</MenuOption>
<View style={styles.divider}/>
<MenuOption value={{ message: 'Hello World!' }}>
<Text>Option with object value</Text>
</MenuOption>
</MenuOptions>
</Menu>
</View>
<View style={[styles.topbar, { backgroundColor: '#333' }]}>
<Menu onSelect={this.setFirstMenuDisabled}>
<MenuTrigger style={styles.menuTrigger}>
<Text style={styles.menuTriggerText}>OPEN SECOND MENU</Text>
</MenuTrigger>
<MenuOptions>
{
this.state.firstMenuDisabled
? (
<MenuOption value={false}>
<Text>enable first menu</Text>
</MenuOption>
)
: (
<MenuOption value={true}>
<Text>disable first menu</Text>
</MenuOption>
)
}
</MenuOptions>
</Menu>
</View>
<View style={styles.content}>
<Text style={styles.contentText}>
{ this.state.message }
</Text>
</View>
<View style={styles.content}>
<Text style={styles.contentText}>
You can also make it a dropdown
</Text>
<Menu style={styles.dropdown} onSelect={(value) => this.setState({ dropdownSelection: value })}>
<MenuTrigger>
<Text>{this.state.dropdownSelection}</Text>
</MenuTrigger>
<MenuOptions optionsContainerStyle={styles.dropdownOptions}
renderOptionsContainer={(options) => <ScrollView><Text>CHOOSE SOMETHING....</Text>{options}</ScrollView>}>
<MenuOption value="Option One">
<Text>Option One</Text>
</MenuOption>
<MenuOption value="Option Two">
<Text>Option Two</Text>
</MenuOption>
<MenuOption value="Option Three">
<Text>Option Three</Text>
</MenuOption>
<MenuOption value="Option Four">
<Text>Option Four</Text>
</MenuOption>
<MenuOption value="Option Five">
<Text>Option Five</Text>
</MenuOption>
</MenuOptions>
</Menu>
</View>
</MenuContext>
);
}
});
const styles = StyleSheet.create({
topbar: {
flexDirection: 'row',
justifyContent: 'flex-end',
backgroundColor: 'black',
paddingHorizontal: 5,
paddingVertical: 10
},
menuTrigger: {
flexDirection: 'row',
paddingHorizontal: 10
},
menuTriggerText: {
color: 'lightgrey',
fontWeight: '600',
fontSize: 20
},
disabled: {
color: '#ccc'
},
divider: {
marginVertical: 5,
marginHorizontal: 2,
borderBottomWidth: 1,
borderColor: '#ccc'
},
content: {
backgroundColor: 'white',
paddingHorizontal: 10,
paddingTop: 20,
paddingBottom: 30,
borderBottomWidth: 1,
borderColor: '#ccc'
},
contentText: {
fontSize: 18
},
dropdown: {
width: 300,
borderColor: '#999',
borderWidth: 1,
padding: 5
},
dropdownOptions: {
marginTop: 30,
borderColor: '#ccc',
borderWidth: 2,
width: 300,
height: 200
}
});
module.exports = Example;