-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathMenuItem.tsx
257 lines (244 loc) · 6.09 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import * as React from 'react';
import {
AccessibilityState,
ColorValue,
GestureResponderEvent,
PressableAndroidRippleConfig,
StyleProp,
StyleSheet,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import {
getContentMaxWidth,
getMenuItemColor,
MAX_WIDTH,
MIN_WIDTH,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import Icon, { IconSource } from '../Icon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
export type Props = {
/**
* Title text for the `MenuItem`.
*/
title: React.ReactNode;
/**
* @renamed Renamed from 'icon' to 'leadingIcon' in v5.x
*
* Leading icon to display for the `MenuItem`.
*/
leadingIcon?: IconSource;
/**
* @supported Available in v5.x with theme version 3
*
* Trailing icon to display for the `MenuItem`.
*/
trailingIcon?: IconSource;
/**
* Whether the 'item' is disabled. A disabled 'item' is greyed out and `onPress` is not called on touch.
*/
disabled?: boolean;
/**
* @supported Available in v5.x with theme version 3
*
* Sets min height with densed layout.
*/
dense?: boolean;
/**
* Type of background drawabale to display the feedback (Android).
* https://reactnative.dev/docs/pressable#rippleconfig
*/
background?: PressableAndroidRippleConfig;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Specifies the largest possible scale a title font can reach.
*/
titleMaxFontSizeMultiplier?: number;
/**
* @optional
*/
style?: StyleProp<ViewStyle>;
contentStyle?: StyleProp<ViewStyle>;
titleStyle?: StyleProp<TextStyle>;
/**
* Color of the ripple effect.
*/
rippleColor?: ColorValue;
/**
* @optional
*/
theme?: ThemeProp;
/**
* TestID used for testing purposes
*/
testID?: string;
/**
* Accessibility label for the Touchable. This is read by the screen reader when the user taps the component.
*/
accessibilityLabel?: string;
/**
* Accessibility state for the Touchable. This is read by the screen reader when the user taps the component.
*/
accessibilityState?: AccessibilityState;
};
/**
* A component to show a single list item inside a Menu.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { Menu } from 'react-native-paper';
*
* const MyComponent = () => (
* <View style={{ flex: 1 }}>
* <Menu.Item leadingIcon="redo" onPress={() => {}} title="Redo" />
* <Menu.Item leadingIcon="undo" onPress={() => {}} title="Undo" />
* <Menu.Item leadingIcon="content-cut" onPress={() => {}} title="Cut" disabled />
* <Menu.Item leadingIcon="content-copy" onPress={() => {}} title="Copy" disabled />
* <Menu.Item leadingIcon="content-paste" onPress={() => {}} title="Paste" />
* </View>
* );
*
* export default MyComponent;
* ```
*/
const MenuItem = ({
leadingIcon,
trailingIcon,
dense,
title,
disabled,
background,
onPress,
style,
contentStyle,
titleStyle,
rippleColor: customRippleColor,
testID = 'menu-item',
accessibilityLabel,
accessibilityState,
theme: themeOverrides,
titleMaxFontSizeMultiplier = 1.5,
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const { titleColor, iconColor, rippleColor } = getMenuItemColor({
theme,
disabled,
customRippleColor,
});
const { isV3 } = theme;
const containerPadding = isV3 ? 12 : 8;
const iconWidth = isV3 ? 24 : 40;
const minWidth = MIN_WIDTH - (isV3 ? 12 : 16);
const maxWidth = getContentMaxWidth({
isV3,
iconWidth,
leadingIcon,
trailingIcon,
});
const titleTextStyle = {
color: titleColor,
...(isV3 ? theme.fonts.bodyLarge : {}),
};
const newAccessibilityState = { ...accessibilityState, disabled };
return (
<TouchableRipple
style={[
styles.container,
{ paddingHorizontal: containerPadding },
dense && styles.md3DenseContainer,
style,
]}
onPress={onPress}
disabled={disabled}
testID={testID}
background={background}
accessibilityLabel={accessibilityLabel}
accessibilityRole="menuitem"
accessibilityState={newAccessibilityState}
rippleColor={rippleColor}
>
<View style={styles.row}>
{leadingIcon ? (
<View
style={[!isV3 && styles.item, { width: iconWidth }]}
pointerEvents="box-none"
>
<Icon source={leadingIcon} size={24} color={iconColor} />
</View>
) : null}
<View
style={[
!isV3 && styles.item,
styles.content,
{ minWidth, maxWidth },
isV3 &&
(leadingIcon
? styles.md3LeadingIcon
: styles.md3WithoutLeadingIcon),
contentStyle,
]}
pointerEvents="none"
>
<Text
variant="bodyLarge"
selectable={false}
numberOfLines={1}
testID={`${testID}-title`}
style={[!isV3 && styles.title, titleTextStyle, titleStyle]}
maxFontSizeMultiplier={titleMaxFontSizeMultiplier}
>
{title}
</Text>
</View>
{isV3 && trailingIcon ? (
<View
style={[!isV3 && styles.item, { width: iconWidth }]}
pointerEvents="box-none"
>
<Icon source={trailingIcon} size={24} color={iconColor} />
</View>
) : null}
</View>
</TouchableRipple>
);
};
MenuItem.displayName = 'Menu.Item';
const styles = StyleSheet.create({
container: {
minWidth: MIN_WIDTH,
maxWidth: MAX_WIDTH,
height: 48,
justifyContent: 'center',
},
md3DenseContainer: {
height: 32,
},
row: {
flexDirection: 'row',
},
title: {
fontSize: 16,
},
item: {
marginHorizontal: 8,
},
content: {
justifyContent: 'center',
},
md3LeadingIcon: {
marginLeft: 12,
},
md3WithoutLeadingIcon: {
marginLeft: 4,
},
});
export default MenuItem;