-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathListItemAction.tsx
102 lines (94 loc) · 2.79 KB
/
ListItemAction.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
import React, { ComponentProps, useMemo } from "react";
import { GestureResponderEvent, Pressable, View } from "react-native";
import Animated from "react-native-reanimated";
import {
IOColors,
IOListItemStyles,
IOListItemVisualParams,
useIOTheme
} from "../../core";
import { useListItemAnimation } from "../../hooks";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { WithTestID } from "../../utils/types";
import { AnimatedIcon, IOIcons } from "../icons";
import { ButtonText } from "../typography/ButtonText";
export type ListItemAction = WithTestID<{
label: string;
variant: "primary" | "danger";
onPress: (event: GestureResponderEvent) => void;
icon?: IOIcons;
}> &
Pick<
ComponentProps<typeof Pressable>,
"accessibilityLabel" | "accessibilityHint"
>;
export const ListItemAction = ({
variant,
label,
onPress,
icon,
accessibilityLabel,
accessibilityHint,
testID
}: ListItemAction) => {
const { onPressIn, onPressOut, scaleAnimatedStyle, backgroundAnimatedStyle } =
useListItemAnimation();
const theme = useIOTheme();
const { dynamicFontScale, spacingScaleMultiplier } = useIOFontDynamicScale();
const listItemAccessibilityLabel = useMemo(
() => (accessibilityLabel ? accessibilityLabel : `${label}`),
[label, accessibilityLabel]
);
const mapForegroundColor: Record<
NonNullable<ListItemAction["variant"]>,
IOColors
> = {
primary: theme["interactiveElem-default"],
danger: theme.errorText
};
return (
<Pressable
onPress={onPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
onTouchEnd={onPressOut}
accessible={true}
accessibilityLabel={listItemAccessibilityLabel}
accessibilityHint={accessibilityHint}
accessibilityRole="button"
testID={testID}
>
<Animated.View
style={[IOListItemStyles.listItem, backgroundAnimatedStyle]}
importantForAccessibility="no-hide-descendants"
accessibilityElementsHidden
>
<Animated.View
style={[
IOListItemStyles.listItemInner,
{
columnGap:
IOListItemVisualParams.iconMargin *
dynamicFontScale *
spacingScaleMultiplier
},
scaleAnimatedStyle
]}
>
{icon && (
<AnimatedIcon
allowFontScaling
name={icon}
color={IOColors[mapForegroundColor[variant]]}
size={IOListItemVisualParams.iconSize}
/>
)}
<View style={{ flexGrow: 1, flexShrink: 1 }}>
<ButtonText color={mapForegroundColor[variant]}>{label}</ButtonText>
</View>
</Animated.View>
</Animated.View>
</Pressable>
);
};
export default ListItemAction;