Skip to content

Infra/ imageUtils #1803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions generatedTypes/src/components/svgImage/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="react" />
export interface SvgImageProps {
data: any;
}
declare function SvgImage(props: SvgImageProps): JSX.Element | null;
declare namespace SvgImage {
var displayName: string;
var isSvg: typeof import("../../utils/imageUtils").isSvg;
}
export default SvgImage;
4 changes: 4 additions & 0 deletions generatedTypes/src/utils/imageUtils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ImageProps } from 'react-native';
export declare function isSvgUri(source: ImageProps['source']): any;
export declare function isSvg(source: ImageProps['source']): any;
export declare function getAsset(assetName?: string, assetGroup?: string): any;
10 changes: 6 additions & 4 deletions src/components/icon/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isUndefined from 'lodash/isUndefined';
import get from 'lodash/get';
import React, {useMemo} from 'react';
import {Image, ImageProps, StyleSheet} from 'react-native';
import {asBaseComponent, BaseComponentInjectedProps, MarginModifiers, Constants} from '../../commons/new';
import Assets from '../../assets';
import {getAsset, isSvg} from '../../utils/imageUtils';
import SvgImage from '../svgImage';

export type IconProps = ImageProps &
MarginModifiers & {
Expand Down Expand Up @@ -45,12 +45,14 @@ const Icon = (props: Props) => {

const iconSource = useMemo(() => {
if (!isUndefined(assetName)) {
return get(Assets, `${assetGroup}.${assetName}`);
return getAsset(assetName, assetGroup);
}
return source;
}, [source, assetGroup, assetName]);

return (
return isSvg(source) ? (
<SvgImage data={source} {...props}/>
) : (
<Image
{...others}
source={iconSource}
Expand Down
9 changes: 4 additions & 5 deletions src/components/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import {
ImageErrorEventData
} from 'react-native';
import {Constants, asBaseComponent, ForwardRefInjectedProps, BaseComponentInjectedProps, MarginModifiers} from '../../commons/new';
// @ts-ignore
import Assets from '../../assets';
import {getAsset, isSvg} from '../../utils/imageUtils';
import Overlay, {OverlayTypeType, OverlayIntensityType} from '../overlay';
import SvgImage from './SvgImage';
import SvgImage from '../svgImage';
import View from '../view';
import {Colors} from '../../style';

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

if (!_.isUndefined(assetName)) {
return _.get(Assets, `${assetGroup}.${assetName}`);
return getAsset(assetName, assetGroup);
}
if (this.sourceTransformer) {
return this.sourceTransformer(this.props);
Expand Down Expand Up @@ -253,7 +252,7 @@ class Image extends PureComponent<Props, State> {

render() {
const {source} = this.props;
if (SvgImage.isSvg(source)) {
if (isSvg(source)) {
return this.renderSvg();
} else {
return this.renderRegularImage();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import React from 'react';
import {ImageProps} from 'react-native';
import {isSvg, isSvgUri} from '../../utils/imageUtils';
import {SvgPackage} from '../../optionalDependencies';
const SvgXml = SvgPackage?.SvgXml;
const SvgCssUri = SvgPackage?.SvgCssUri;
// const SvgProps = SvgPackage?.SvgProps; TODO: not sure how (or if) we can use their props

function isSvgUri(source: ImageProps['source']) {
// @ts-expect-error
return typeof source === 'object' && source?.uri?.endsWith('.svg');
}

export function isSvg(source: ImageProps['source']) {
return typeof source === 'string' || typeof source === 'function' || isSvgUri(source);
}

export interface SvgImageProps {
data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/imageUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import get from 'lodash/get';
import {ImageProps} from 'react-native';
import Assets from '../assets';

export function isSvgUri(source: ImageProps['source']) {
// @ts-expect-error
return typeof source === 'object' && source?.uri?.endsWith('.svg');
}

export function isSvg(source: ImageProps['source']) {
return typeof source === 'string' || typeof source === 'function' || isSvgUri(source);
}

export function getAsset(assetName = '', assetGroup = '') {
return get(Assets, `${assetGroup}.${assetName}`);
}