-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
UsageNativeMenu.tsx
107 lines (99 loc) · 2.85 KB
/
UsageNativeMenu.tsx
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
import * as React from 'react';
import { Text, Platform, View } from 'react-native';
import {
HeaderButtons,
Item,
HiddenItemProps,
HiddenItem,
extractHiddenItemProps,
} from 'react-navigation-header-buttons';
import type { ScreenProps } from '../NavTypes';
import { MenuView } from '@react-native-menu/menu';
import inspectElements from 'react-to-imperative';
import type { MenuAction } from '@react-native-menu/menu';
import { ScreenBody } from '../components/ScreenBody';
import { MaterialHeaderButton } from '../components/MaterialHeaderButton';
type ExtractedProps = MenuAction & {
onPress?: HiddenItemProps['onPress'];
};
const extractOverflowButtonData = (
hiddenButtons: React.ReactNode
): ExtractedProps[] => {
const actions = inspectElements(hiddenButtons, extractHiddenItemProps);
return actions.map((it, index) => ({ ...it, id: String(index) }));
};
type HiddenItemWithMenuProps = HiddenItemProps & MenuAction;
const RightHeader = () => {
const actionElements = (
<>
<HiddenItem<HiddenItemWithMenuProps>
title="title 1"
onPress={() => alert('hidden 1')}
image={Platform.select({
ios: 'plus',
})}
/>
<HiddenItem<HiddenItemWithMenuProps>
title="share"
subtitle={'Share action'}
imageColor={'#46F289'}
onPress={() => alert('share')}
image={Platform.select({
ios: 'square.and.arrow.up',
})}
// @ts-expect-error
unsupportedProp={'123'}
/>
</>
);
const actions = extractOverflowButtonData(actionElements);
return (
<HeaderButtons HeaderButtonComponent={MaterialHeaderButton}>
<Item title="edit" onPress={() => alert('edit')} />
<MenuView
onPressAction={({ nativeEvent: { event } }) => {
actions[parseInt(event)]?.onPress?.();
}}
actions={actions}
>
<Item title="edit" iconName="more-vert" iconSize={23} />
</MenuView>
</HeaderButtons>
);
};
export function UsageNativeMenu({
navigation,
}: ScreenProps<'UsageNativeMenu'>) {
React.useLayoutEffect(() => {
navigation.setOptions({
headerRight: RightHeader,
});
}, [navigation]);
return (
<ScreenBody>
<View style={{ height: 250 }} />
<MenuView
title="Menu Title"
onPressAction={({ nativeEvent }) => {
console.warn(JSON.stringify(nativeEvent));
}}
actions={[
{
id: 'share',
title: 'Share Action',
titleColor: '#46F289',
subtitle: 'Share action on SNS',
image: Platform.select({
ios: 'square.and.arrow.up',
android: 'ic_menu_share',
}),
imageColor: '#46F289',
state: 'on',
},
]}
>
<Text>native menu overflow handler</Text>
</MenuView>
</ScreenBody>
);
}