forked from instea/react-native-popup-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.js
163 lines (150 loc) · 4.88 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
import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import Menu, {
MenuProvider,
MenuTrigger,
MenuOptions,
MenuOption,
renderers,
} from 'react-native-popup-menu';
let unique = 0;
const { SlideInMenu } = renderers;
export default class Example extends Component {
constructor(props, ctx) {
super(props, ctx);
this.state = { log: [] };
}
selectNumber(value) {
this.addLog(`selecting number: ${value}`);
}
selectOptionType(value) {
const v = typeof value === 'object' ? JSON.stringify(value) : value;
this.addLog(`selecting type: ${v}`);
return value !== 'Do not close';
}
addLog(value) {
this.setState({
log: [...this.state.log, {
value,
id: ++unique,
}],
});
}
toggleHighlight(id) {
const log = this.state.log.map(l => {
if (l.id === id) {
return Object.assign({}, l, {highlighted: !l.highlighted});
}
return l;
})
this.setState({ log });
}
deleteLogItem(id) {
const log = this.state.log.filter(l => l.id !== id);
this.setState({ log });
}
render() {
return (
<MenuProvider style={{flex: 1}}>
<View style={styles.container}>
<View style={styles.topbar}>
<Menu name="numbers" renderer={SlideInMenu} onSelect={value => this.selectNumber(value)}>
<MenuTrigger style={styles.trigger}>
<Text style={[styles.text, styles.triggerText]}>Slide-in menu...</Text>
</MenuTrigger>
<MenuOptions customStyles={{ optionText: [styles.text, styles.slideInOption] }}>
<MenuOption value={1} text='Option one' />
<MenuOption value={2} text='Option two' />
<MenuOption value={3} text='Option three' />
<MenuOption value={4} text='Option four' />
{ null /* conditional not rendered option */ }
<MenuOption value={5} text='Option five' />
</MenuOptions>
</Menu>
<View style={{flex:1}}></View>
<Menu name="types" onSelect={value => this.selectOptionType(value)}
onBackdropPress={() => this.addLog('menu will be closed by backdrop')}
onOpen={() => this.addLog('menu is opening')}
onClose={() => this.addLog('menu is closing')}
>
<MenuTrigger
onAlternativeAction={() => this.addLog('trigger longpressed')}
style={styles.trigger}>
<Text style={[styles.text, styles.triggerText]}>Context menu...</Text>
</MenuTrigger>
<MenuOptions customStyles={{ optionText: styles.text }}>
<MenuOption value="Normal" text='Normal' />
<MenuOption value="N/A" disabled={true} text='Disabled' />
<MenuOption value="N/A" disableTouchable={true} text='Non-selectable' />
<MenuOption value="Do not close" text='Do not close' />
<View style={styles.divider}/>
<MenuOption value={{ text: 'Hello world!' }} text='Object as value' />
</MenuOptions>
</Menu>
</View>
<ScrollView style={styles.logView}>
{this.state.log.map((l, i) => {
const wrapperStyle = {backgroundColor: i % 2 ? 'white' : 'whitesmoke'};
const textStyle = {color: l.highlighted ? 'red' : 'gray'};
return (
<View style={[styles.logItem, wrapperStyle]} key={l.id}>
<Text style={[styles.text, textStyle]}>{l.value}</Text>
<View style={{flex:1}}></View>
<Menu>
<MenuTrigger text='edit' customStyles={{ triggerText: styles.text }} />
<MenuOptions customStyles={{ optionText: styles.text }}>
<MenuOption onSelect={() => this.toggleHighlight(l.id)} text={l.highlighted ? 'Unhighlight' : 'Highlight'} />
<MenuOption onSelect={() => this.deleteLogItem(l.id)} text='Delete' />
</MenuOptions>
</Menu>
</View>
);
})}
</ScrollView>
</View>
</MenuProvider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'lightgray',
},
topbar: {
flexDirection: 'row',
backgroundColor: 'dimgray',
paddingTop : 15,
},
trigger: {
padding: 5,
margin: 5,
},
triggerText: {
color: 'white',
},
disabled: {
color: '#ccc',
},
divider: {
marginVertical: 5,
marginHorizontal: 2,
borderBottomWidth: 1,
borderColor: '#ccc',
},
logView: {
flex: 1,
flexDirection: 'column',
},
logItem: {
flexDirection: 'row',
padding: 8,
},
slideInOption: {
padding: 5,
},
text: {
fontSize: 18,
},
});