UNPKG

4.31 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import React from 'react';
13import { Platform, TouchableHighlight, View, StyleSheet, TouchableNativeFeedback, } from 'react-native';
14import Color from 'color';
15import getIconType from '../helpers/getIconType';
16import getIconStyle from '../helpers/getIconStyle';
17import { withTheme } from '../config';
18const Icon = (props) => {
19 var _a, _b;
20 const { type = 'material', name, size = 24, color: colorProp, iconStyle, iconProps, underlayColor = 'transparent', reverse = false, raised = false, containerStyle, reverseColor: reverseColorProp, disabled = false, disabledStyle, onPress, Component = onPress
21 ? Platform.select({
22 android: TouchableNativeFeedback,
23 default: TouchableHighlight,
24 })
25 : View, solid = false, brand = false, theme } = props, attributes = __rest(props, ["type", "name", "size", "color", "iconStyle", "iconProps", "underlayColor", "reverse", "raised", "containerStyle", "reverseColor", "disabled", "disabledStyle", "onPress", "Component", "solid", "brand", "theme"]);
26 const color = colorProp || ((_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.black);
27 const reverseColor = reverseColorProp || ((_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.white);
28 const IconComponent = getIconType(type);
29 const iconSpecificStyle = getIconStyle(type, { solid, brand });
30 const getBackgroundColor = () => {
31 var _a;
32 if (reverse) {
33 return color;
34 }
35 return raised ? (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.white : 'transparent';
36 };
37 const buttonStyles = {
38 borderRadius: size + 4,
39 height: size * 2 + 4,
40 width: size * 2 + 4,
41 };
42 if (Platform.OS === 'android' && !attributes.background) {
43 if (Platform.Version >= 21) {
44 attributes.background = TouchableNativeFeedback.Ripple(Color(color).alpha(0.2).rgb().string(), true);
45 }
46 }
47 return (<View style={StyleSheet.flatten([
48 styles.container,
49 (reverse || raised) && styles.button,
50 (reverse || raised) && buttonStyles,
51 raised && styles.raised,
52 iconStyle && iconStyle.borderRadius
53 ? {
54 borderRadius: iconStyle.borderRadius,
55 }
56 : {},
57 containerStyle && containerStyle,
58 ])}>
59 <Component {...attributes} {...(onPress && {
60 onPress,
61 disabled,
62 underlayColor: reverse ? color : underlayColor,
63 activeOpacity: 0.3,
64 })}>
65 <View style={StyleSheet.flatten([
66 (reverse || raised) && buttonStyles,
67 {
68 backgroundColor: getBackgroundColor(),
69 alignItems: 'center',
70 justifyContent: 'center',
71 },
72 disabled && styles.disabled,
73 disabled && disabledStyle,
74 ])}>
75 <IconComponent testID="iconIcon" style={StyleSheet.flatten([
76 { backgroundColor: 'transparent' },
77 iconStyle && iconStyle,
78 ])} size={size} name={name} color={reverse ? reverseColor : color} {...iconSpecificStyle} {...iconProps}/>
79 </View>
80 </Component>
81 </View>);
82};
83const styles = StyleSheet.create({
84 container: {
85 overflow: 'hidden',
86 },
87 button: {
88 margin: 7,
89 },
90 raised: Object.assign({}, Platform.select({
91 android: {
92 elevation: 2,
93 },
94 default: {
95 shadowColor: 'rgba(0,0,0, .4)',
96 shadowOffset: { height: 1, width: 1 },
97 shadowOpacity: 1,
98 shadowRadius: 1,
99 },
100 })),
101 disabled: {
102 backgroundColor: '#D1D5D8',
103 },
104});
105export { Icon };
106export default withTheme(Icon, 'Icon');
107
\No newline at end of file