-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMenuItem.tsx
132 lines (124 loc) · 2.79 KB
/
MenuItem.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
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
import * as React from 'react';
import {
GestureResponderEvent,
StyleSheet,
Text,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import { PlatformPressable } from '@react-navigation/elements';
import { useTheme } from '@react-navigation/native';
export type Props = {
/**
* Title text for the `MenuItem`.
*/
title: string;
/**
* Icon to display for the `MenuItem`.
*/
icon?: React.ReactNode | undefined;
/**
* Whether the 'item' is disabled. A disabled 'item' is greyed out and `onPress` is not called on touch.
*/
disabled?: boolean;
/**
* Function to execute on press.
*/
onPress?: null | ((params?: GestureResponderEvent) => void) | undefined;
/**
* @optional
*/
style?: ViewStyle;
titleStyle?: TextStyle;
/**
* TestID used for testing purposes
*/
testID?: string;
};
/**
* A component to show a single list item inside a Menu.
*/
export function MenuItem(props: Props) {
const { icon, title, disabled, onPress, style, titleStyle, testID } = props;
const {
dark,
colors: { text },
} = useTheme();
const disabledColor = dark ? styles.darkDisabled : styles.lightDisabled;
const titleColor = disabled ? disabledColor : { color: text };
const themePressColorAndroid = dark
? 'rgba(255, 255, 255, .32)'
: 'rgba(0, 0, 0, .32)';
return (
<PlatformPressable
style={[styles.container, style]}
onPress={onPress}
disabled={disabled}
testID={testID}
pressColor={themePressColorAndroid}
>
<View style={styles.row}>
{React.isValidElement(icon) && (
<View style={[styles.item, styles.icon]} pointerEvents="box-none">
{icon}
</View>
)}
<View
style={[
styles.item,
styles.content,
icon != null ? styles.widthWithIcon : undefined,
]}
pointerEvents="none"
>
<Text
selectable={false}
numberOfLines={1}
style={[styles.title, titleColor, titleStyle]}
>
{title}
</Text>
</View>
</View>
</PlatformPressable>
);
}
const minWidth = 112;
const maxWidth = 280;
const iconWidth = 25;
const styles = StyleSheet.create({
container: {
paddingHorizontal: 8,
minWidth,
maxWidth,
height: 48,
justifyContent: 'center',
},
row: {
flexDirection: 'row',
},
icon: {
width: iconWidth,
},
title: {
fontSize: 16,
},
item: {
marginHorizontal: 8,
},
content: {
justifyContent: 'center',
minWidth: minWidth - 16,
maxWidth: maxWidth - 16,
},
widthWithIcon: {
maxWidth: maxWidth - (iconWidth + 48),
},
lightDisabled: {
color: 'rgba(0, 0, 0, 0.32)',
},
darkDisabled: {
color: 'rgba(255, 255, 255, 0.32)',
},
});