-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModuleCredential.tsx
172 lines (157 loc) · 4.12 KB
/
ModuleCredential.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
import React from "react";
import {
Image,
ImageSourcePropType,
ImageURISource,
StyleSheet
} from "react-native";
import Placeholder from "rn-placeholder";
import {
IOListItemVisualParams,
IOSelectionListItemVisualParams,
IOSpacer,
IOVisualCostants,
useIOTheme
} from "../../core";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { WithTestID } from "../../utils/types";
import { Badge } from "../badge";
import { IOIcons, Icon } from "../icons";
import { LoadingSpinner } from "../loadingSpinner";
import { HStack } from "../stack/Stack";
import { BodySmall } from "../typography";
import { ModuleStatic } from "./ModuleStatic";
import {
PressableModuleBase,
PressableModuleBaseProps
} from "./PressableModuleBase";
type ImageProps =
| { icon: IOIcons; image?: never }
| { icon?: never; image: ImageURISource | ImageSourcePropType };
type LoadingModuleProps = {
isLoading: true;
};
type BaseModuleProps = {
isLoading?: false;
label: string;
badge?: Badge;
isFetching?: boolean;
};
type ModuleCredentialProps =
| BaseModuleProps & ImageProps & PressableModuleBaseProps;
const ModuleCredential = (
props: WithTestID<LoadingModuleProps | ModuleCredentialProps>
) =>
props.isLoading ? (
<ModuleCredentialSkeleton />
) : (
<ModuleCredentialContent {...props} />
);
const ModuleCredentialContent = ({
testID,
icon,
image,
label,
onPress,
badge,
isFetching,
...pressableProps
}: WithTestID<ModuleCredentialProps>) => {
const theme = useIOTheme();
const { hugeFontEnabled } = useIOFontDynamicScale();
const iconComponent = icon && !hugeFontEnabled && (
<Icon
allowFontScaling
name={icon}
size={IOSelectionListItemVisualParams.iconSize}
color={theme["icon-decorative"]}
/>
);
const imageComponent = image && (
<Image
source={image}
style={styles.image}
accessibilityIgnoresInvertColors={true}
/>
);
const endComponent = React.useMemo(() => {
if (isFetching) {
return (
<LoadingSpinner
testID={testID ? `${testID}_activityIndicator` : undefined}
color={theme["interactiveElem-default"]}
/>
);
}
if (badge) {
return (
<Badge {...badge} testID={testID ? `${testID}_badge` : undefined} />
);
}
if (onPress) {
return (
<Icon
testID={testID ? `${testID}_icon` : undefined}
name="chevronRightListItem"
color={theme["interactiveElem-default"]}
size={IOListItemVisualParams.chevronSize}
/>
);
}
return null;
}, [testID, theme, isFetching, badge, onPress]);
const ModuleContent = () => (
<HStack space={8} style={{ alignItems: "center" }}>
<HStack
space={IOVisualCostants.iconMargin as IOSpacer}
style={{ flexGrow: 1, flexShrink: 1, alignItems: "center" }}
>
{/* Graphical assets */}
{iconComponent ?? imageComponent}
<BodySmall
color={theme["interactiveElem-default"]}
weight="Semibold"
numberOfLines={2}
lineBreakMode="middle"
style={{ flexShrink: 1 }}
>
{label}
</BodySmall>
</HStack>
{endComponent}
</HStack>
);
return onPress ? (
<PressableModuleBase {...pressableProps} testID={testID} onPress={onPress}>
<ModuleContent />
</PressableModuleBase>
) : (
<ModuleStatic>
<ModuleContent />
</ModuleStatic>
);
};
const ModuleCredentialSkeleton = () => (
<ModuleStatic
startBlock={
<HStack
style={{ alignItems: "center" }}
space={IOVisualCostants.iconMargin as IOSpacer}
>
<Placeholder.Box animate="fade" width={24} height={24} radius={8} />
<Placeholder.Box animate="fade" width={96} height={16} radius={8} />
</HStack>
}
endBlock={
<Placeholder.Box animate="fade" width={64} height={24} radius={16} />
}
/>
);
const styles = StyleSheet.create({
image: {
width: IOSelectionListItemVisualParams.iconSize,
height: IOSelectionListItemVisualParams.iconSize,
resizeMode: "contain"
}
});
export { ModuleCredential };