forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIcon.js
147 lines (129 loc) · 3.77 KB
/
Icon.js
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
/* @flow */
import * as React from 'react';
import { Image, Text, StyleSheet, I18nManager } from 'react-native';
import type { ImageSource } from 'react-native/Libraries/Image/ImageSource';
let MaterialIcons;
try {
// Optionally require vector-icons
MaterialIcons = require('react-native-vector-icons/MaterialIcons').default;
} catch (e) {
if (global.__expo && global.__expo.Icon && global.__expo.Icon.MaterialIcons) {
// Snack doesn't properly bundle vector icons from subpath
// Use icons from the __expo global if available
MaterialIcons = global.__expo.Icon.MaterialIcons;
} else {
// Fallback component for icons
MaterialIcons = ({ name, color, size, ...rest }) => {
// eslint-disable-next-line no-console
console.warn(
`Tried to use the icon '${name}' in a component from 'react-native-paper', but 'react-native-vector-icons' is not installed. To remove this warning, install 'react-native-vector-icons' or use another method to specify icon: https://callstack.github.io/react-native-paper/icons.html.`
);
return (
<Text
{...rest}
style={[styles.icon, { color, fontSize: size }]}
pointerEvents="none"
>
□
</Text>
);
};
}
}
type IconSourceBase = string | ImageSource;
export type IconSource =
| IconSourceBase
| $ReadOnly<{ source: IconSourceBase, direction: 'rtl' | 'ltr' | 'auto' }>
| ((props: IconProps) => React.Node);
type IconProps = {
color: string,
size: number,
};
type Props = IconProps & {
source: IconSource,
};
const isImageSource = (source: any) =>
// source is an object with uri
(typeof source === 'object' &&
source !== null &&
(Object.prototype.hasOwnProperty.call(source, 'uri') &&
typeof source.uri === 'string')) ||
// source is a module, e.g. - require('image')
typeof source === 'number';
const getIconId = (source: any) => {
if (
typeof source === 'object' &&
source !== null &&
(Object.prototype.hasOwnProperty.call(source, 'uri') &&
typeof source.uri === 'string')
) {
return source.uri;
}
return source;
};
export const isValidIcon = (source: any) =>
typeof source === 'string' || isImageSource(source);
export const isEqualIcon = (a: any, b: any) =>
a === b || getIconId(a) === getIconId(b);
const Icon = ({ source, color, size, ...rest }: Props) => {
const direction =
typeof source === 'object' && source.direction && source.source
? source.direction === 'auto'
? I18nManager.isRTL
? 'rtl'
: 'ltr'
: source.direction
: null;
const s =
typeof source === 'object' && source.direction && source.source
? source.source
: source;
if (typeof s === 'string') {
return (
<MaterialIcons
{...rest}
name={s}
color={color}
size={size}
style={[
{
transform: [{ scaleX: direction === 'rtl' ? -1 : 1 }],
},
styles.icon,
]}
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
/>
);
} else if (isImageSource(s)) {
return (
<Image
{...rest}
source={s}
style={[
{
transform: [{ scaleX: direction === 'rtl' ? -1 : 1 }],
},
{
width: size,
height: size,
tintColor: color,
resizeMode: 'contain',
},
]}
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
/>
);
} else if (typeof s === 'function') {
return s({ color, size, direction });
}
return null;
};
export default Icon;
const styles = StyleSheet.create({
icon: {
backgroundColor: 'transparent',
},
});