Skip to content

Commit 2532803

Browse files
authored
Update README
Use updated example with Typescript
1 parent 72c61d7 commit 2532803

File tree

1 file changed

+84
-28
lines changed

1 file changed

+84
-28
lines changed

README.md

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,103 @@ yarn add react-native-enhanced-popup-menu
3131

3232
## Usage example
3333

34-
```jsx
34+
```tsx
3535
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,
3845
MenuItem,
3946
MenuDivider,
40-
Position
47+
Position,
4148
} from 'react-native-enhanced-popup-menu';
4249

43-
const App = props => {
44-
let textRef = React.createRef();
45-
let menuRef = null;
50+
declare const global: {HermesInternal: null | {}};
4651

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+
}
5155

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+
}
5382

83+
const Button = ({title, style, onPress}: ButtonProps) => {
5484
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} />
7187
</View>
7288
);
7389
};
7490

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+
75131
export default App;
76132
```
77133

0 commit comments

Comments
 (0)