Skip to content

Commit e42a033

Browse files
authored
use imageUtils in Image and Icon (#1803)
1 parent 8a22aab commit e42a033

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference types="react" />
2+
export interface SvgImageProps {
3+
data: any;
4+
}
5+
declare function SvgImage(props: SvgImageProps): JSX.Element | null;
6+
declare namespace SvgImage {
7+
var displayName: string;
8+
var isSvg: typeof import("../../utils/imageUtils").isSvg;
9+
}
10+
export default SvgImage;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { ImageProps } from 'react-native';
2+
export declare function isSvgUri(source: ImageProps['source']): any;
3+
export declare function isSvg(source: ImageProps['source']): any;
4+
export declare function getAsset(assetName?: string, assetGroup?: string): any;

src/components/icon/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import isUndefined from 'lodash/isUndefined';
2-
import get from 'lodash/get';
32
import React, {useMemo} from 'react';
43
import {Image, ImageProps, StyleSheet} from 'react-native';
54
import {asBaseComponent, BaseComponentInjectedProps, MarginModifiers, Constants} from '../../commons/new';
6-
import Assets from '../../assets';
5+
import {getAsset, isSvg} from '../../utils/imageUtils';
6+
import SvgImage from '../svgImage';
77

88
export type IconProps = ImageProps &
99
MarginModifiers & {
@@ -45,12 +45,14 @@ const Icon = (props: Props) => {
4545

4646
const iconSource = useMemo(() => {
4747
if (!isUndefined(assetName)) {
48-
return get(Assets, `${assetGroup}.${assetName}`);
48+
return getAsset(assetName, assetGroup);
4949
}
5050
return source;
5151
}, [source, assetGroup, assetName]);
5252

53-
return (
53+
return isSvg(source) ? (
54+
<SvgImage data={source} {...props}/>
55+
) : (
5456
<Image
5557
{...others}
5658
source={iconSource}

src/components/image/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import {
1111
ImageErrorEventData
1212
} from 'react-native';
1313
import {Constants, asBaseComponent, ForwardRefInjectedProps, BaseComponentInjectedProps, MarginModifiers} from '../../commons/new';
14-
// @ts-ignore
15-
import Assets from '../../assets';
14+
import {getAsset, isSvg} from '../../utils/imageUtils';
1615
import Overlay, {OverlayTypeType, OverlayIntensityType} from '../overlay';
17-
import SvgImage from './SvgImage';
16+
import SvgImage from '../svgImage';
1817
import View from '../view';
1918
import {Colors} from '../../style';
2019

@@ -146,7 +145,7 @@ class Image extends PureComponent<Props, State> {
146145
const {assetName, assetGroup, source} = this.props;
147146

148147
if (!_.isUndefined(assetName)) {
149-
return _.get(Assets, `${assetGroup}.${assetName}`);
148+
return getAsset(assetName, assetGroup);
150149
}
151150
if (this.sourceTransformer) {
152151
return this.sourceTransformer(this.props);
@@ -253,7 +252,7 @@ class Image extends PureComponent<Props, State> {
253252

254253
render() {
255254
const {source} = this.props;
256-
if (SvgImage.isSvg(source)) {
255+
if (isSvg(source)) {
257256
return this.renderSvg();
258257
} else {
259258
return this.renderRegularImage();

src/components/image/SvgImage.tsx renamed to src/components/svgImage/index.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import React from 'react';
2-
import {ImageProps} from 'react-native';
2+
import {isSvg, isSvgUri} from '../../utils/imageUtils';
33
import {SvgPackage} from '../../optionalDependencies';
44
const SvgXml = SvgPackage?.SvgXml;
55
const SvgCssUri = SvgPackage?.SvgCssUri;
66
// const SvgProps = SvgPackage?.SvgProps; TODO: not sure how (or if) we can use their props
77

8-
function isSvgUri(source: ImageProps['source']) {
9-
// @ts-expect-error
10-
return typeof source === 'object' && source?.uri?.endsWith('.svg');
11-
}
12-
13-
export function isSvg(source: ImageProps['source']) {
14-
return typeof source === 'string' || typeof source === 'function' || isSvgUri(source);
15-
}
16-
178
export interface SvgImageProps {
189
data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly
1910
}

src/utils/imageUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import get from 'lodash/get';
2+
import {ImageProps} from 'react-native';
3+
import Assets from '../assets';
4+
5+
export function isSvgUri(source: ImageProps['source']) {
6+
// @ts-expect-error
7+
return typeof source === 'object' && source?.uri?.endsWith('.svg');
8+
}
9+
10+
export function isSvg(source: ImageProps['source']) {
11+
return typeof source === 'string' || typeof source === 'function' || isSvgUri(source);
12+
}
13+
14+
export function getAsset(assetName = '', assetGroup = '') {
15+
return get(Assets, `${assetGroup}.${assetName}`);
16+
}

0 commit comments

Comments
 (0)