1 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2 |
|
3 | import * as React from 'react';
|
4 | import { Image, I18nManager, Platform } from 'react-native';
|
5 | import { Consumer as SettingsConsumer } from '../core/settings';
|
6 | import { accessibilityProps } from './MaterialCommunityIcon';
|
7 | import { withTheme } from '../core/theming';
|
8 |
|
9 | const isImageSource = source =>
|
10 | typeof source === 'object' && source !== null && Object.prototype.hasOwnProperty.call(source, 'uri') && typeof source.uri === 'string' ||
|
11 | typeof source === 'number' ||
|
12 | Platform.OS === 'web' && typeof source === 'string' && (source.startsWith('data:image') || /\.(bmp|jpg|jpeg|png|gif|svg)$/.test(source));
|
13 |
|
14 | const getIconId = source => {
|
15 | if (typeof source === 'object' && source !== null && Object.prototype.hasOwnProperty.call(source, 'uri') && typeof source.uri === 'string') {
|
16 | return source.uri;
|
17 | }
|
18 |
|
19 | return source;
|
20 | };
|
21 |
|
22 | export const isValidIcon = source => typeof source === 'string' || typeof source === 'function' || isImageSource(source);
|
23 | export const isEqualIcon = (a, b) => a === b || getIconId(a) === getIconId(b);
|
24 |
|
25 | const Icon = _ref => {
|
26 | let {
|
27 | source,
|
28 | color,
|
29 | size,
|
30 | theme,
|
31 | ...rest
|
32 | } = _ref;
|
33 | const direction = typeof source === 'object' && source.direction && source.source ? source.direction === 'auto' ? I18nManager.isRTL ? 'rtl' : 'ltr' : source.direction : null;
|
34 | const s = typeof source === 'object' && source.direction && source.source ? source.source : source;
|
35 | const iconColor = color || theme.colors.text;
|
36 |
|
37 | if (isImageSource(s)) {
|
38 | return React.createElement(Image, _extends({}, rest, {
|
39 | source: s,
|
40 | style: [{
|
41 | transform: [{
|
42 | scaleX: direction === 'rtl' ? -1 : 1
|
43 | }]
|
44 | },
|
45 | {
|
46 | width: size,
|
47 | height: size,
|
48 | tintColor: color,
|
49 | resizeMode: 'contain'
|
50 | }]
|
51 | }, accessibilityProps));
|
52 | } else if (typeof s === 'string') {
|
53 | return React.createElement(SettingsConsumer, null, _ref2 => {
|
54 | let {
|
55 | icon
|
56 | } = _ref2;
|
57 | return icon({
|
58 | name: s,
|
59 | color: iconColor,
|
60 | size,
|
61 | direction
|
62 | });
|
63 | });
|
64 | } else if (typeof s === 'function') {
|
65 | return s({
|
66 | color: iconColor,
|
67 | size,
|
68 | direction
|
69 | });
|
70 | }
|
71 |
|
72 | return null;
|
73 | };
|
74 |
|
75 | export default withTheme(Icon);
|
76 |
|
\ | No newline at end of file |