-
Notifications
You must be signed in to change notification settings - Fork 18
/
Icon.js
81 lines (70 loc) · 1.75 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
import React from "react";
import PropTypes from "prop-types";
import { View, TouchableOpacity } from "react-native"
import { Svg, Path } from "react-native-svg";
import { findIconDefinition } from "@fortawesome/fontawesome-svg-core";
import { prefixTypes } from "./config";
const DEFAULT_ICON = "question-circle";
const Icon = ( { name, size, color, type, containerStyle, iconStyle, onPress, activeOpacity } ) => {
const prefix = prefixTypes[type];
let icon = findIconDefinition( { prefix, iconName: name } );
if ( !icon ) {
icon = findIconDefinition( { prefix, iconName: DEFAULT_ICON } );
}
const iconData = icon.icon;
const path = iconData[4];
const viewBox = [ 0, 0, iconData[0], iconData[1] ].join( " " );
const iconContent = (
<Svg
height={size}
version="1.1"
viewBox={viewBox}
width={size}
x="0"
y="0"
style={iconStyle}
>
<Path
d={path}
fill={color}
/>
</Svg>
);
if ( onPress || containerStyle ) {
return (
<TouchableOpacity
onPress={onPress}
disabled={!onPress}
style={containerStyle}
activeOpacity={activeOpacity}
>
{iconContent}
</TouchableOpacity>
);
}
return iconContent;
};
Icon.propTypes = {
name: PropTypes.string,
size: PropTypes.number,
color: PropTypes.string,
onPress: PropTypes.func,
type: PropTypes.oneOf( Object.keys( prefixTypes ) ),
iconStyle: PropTypes.oneOfType( [
PropTypes.object,
PropTypes.array
] ),
containerStyle: PropTypes.oneOfType( [
PropTypes.object,
PropTypes.array
] ),
activeOpacity: PropTypes.number
};
Icon.defaultProps = {
name: "",
size: 20,
color: "black",
type: "regular",
activeOpacity: 0.2
};
export default Icon;