@@ -31,47 +31,103 @@ yarn add react-native-enhanced-popup-menu
31
31
32
32
## Usage example
33
33
34
- ``` jsx
34
+ ``` tsx
35
35
import React from ' react' ;
36
- import { Text , View , Button } from ' react-native' ;
37
- import Menu, {
36
+ import {
37
+ View ,
38
+ Text ,
39
+ Button as RNButton ,
40
+ StyleProp ,
41
+ ViewStyle ,
42
+ } from ' react-native' ;
43
+ import {
44
+ Menu ,
38
45
MenuItem ,
39
46
MenuDivider ,
40
- Position
47
+ Position ,
41
48
} from ' react-native-enhanced-popup-menu' ;
42
49
43
- const App = props => {
44
- let textRef = React .createRef ();
45
- let menuRef = null ;
50
+ declare const global: {HermesInternal: null | {}};
46
51
47
- const setMenuRef = ref => (menuRef = ref);
48
- const hideMenu = () => menuRef .hide ();
49
- const showMenu = () =>
50
- menuRef .show (textRef .current , (stickTo = Position .BOTTOM_CENTER ));
52
+ interface ElementToStickProps {
53
+ style? : StyleProp <ViewStyle >;
54
+ }
51
55
52
- const onPress = () => showMenu ();
56
+ const ElementToStick = React .forwardRef <View , ElementToStickProps >(
57
+ ({style }, ref ) => {
58
+ return (
59
+ <View
60
+ ref = { ref }
61
+ style = { [
62
+ {
63
+ padding: 16 ,
64
+ borderColor: ' grey' ,
65
+ borderWidth: 2 ,
66
+ justifyContent: ' center' ,
67
+ alignItems: ' center' ,
68
+ },
69
+ style ,
70
+ ]} >
71
+ <Text >Element to which menu is sticked</Text >
72
+ </View >
73
+ );
74
+ },
75
+ );
76
+
77
+ interface ButtonProps {
78
+ title: string ;
79
+ style? : StyleProp <ViewStyle >;
80
+ onPress: () => void ;
81
+ }
53
82
83
+ const Button = ({title , style , onPress }: ButtonProps ) => {
54
84
return (
55
- < View style= {{ flex: 1 , alignItems: ' center' , backgroundColor: ' white' }}>
56
- < Text ref= {textRef} style= {{ fontSize: 20 , textAlign: ' center' }}>
57
- Text component
58
- < / Text >
59
-
60
- < Button title= " Show menu" onPress= {onPress} / >
61
-
62
- < Menu ref= {setMenuRef}>
63
- < MenuItem onPress= {hideMenu}> Item 1 < / MenuItem>
64
- < MenuItem onPress= {hideMenu}> Item 2 < / MenuItem>
65
- < MenuItem onPress= {hideMenu} disabled>
66
- Item 3
67
- < / MenuItem>
68
- < MenuDivider / >
69
- < MenuItem onPress= {hideMenu}> Item 4 < / MenuItem>
70
- < / Menu>
85
+ <View style = { style } >
86
+ <RNButton title = { title } onPress = { onPress } />
71
87
</View >
72
88
);
73
89
};
74
90
91
+ const App = () => {
92
+ let elementRef = React .createRef <View >();
93
+ let menuRef: Menu | null = null ;
94
+
95
+ const setMenuRef: (instance : Menu | null ) => void = (ref ) => (menuRef = ref );
96
+ const hideMenu = () => menuRef ?.hide ();
97
+ const showMenu = () => {
98
+ menuRef ?.show (elementRef .current , Position .TOP_LEFT );
99
+ };
100
+
101
+ const onPress = () => showMenu ();
102
+ return (
103
+ <>
104
+ <View
105
+ style = { {
106
+ width: ' 100%' ,
107
+ height: ' 100%' ,
108
+ alignItems: ' center' ,
109
+ justifyContent: ' center' ,
110
+ }} >
111
+ <ElementToStick ref = { elementRef } />
112
+ <Button
113
+ style = { {position: ' absolute' , bottom: 64 }}
114
+ title = { ' Press to show menu' }
115
+ onPress = { onPress }
116
+ />
117
+ <Menu ref = { setMenuRef } >
118
+ <MenuItem onPress = { hideMenu } >Item 1</MenuItem >
119
+ <MenuItem onPress = { hideMenu } >Item 2</MenuItem >
120
+ <MenuItem onPress = { hideMenu } disabled >
121
+ Item 3
122
+ </MenuItem >
123
+ <MenuDivider />
124
+ <MenuItem onPress = { hideMenu } >Item 4</MenuItem >
125
+ </Menu >
126
+ </View >
127
+ </>
128
+ );
129
+ };
130
+
75
131
export default App ;
76
132
```
77
133
0 commit comments