Skip to content

Commit

Permalink
[RN mobile] fix className style in SVG primitive (#10556)
Browse files Browse the repository at this point in the history
* building an object out of the passed style/className concatenated string

* fix array level indirection when looking into styles object

* removed merge conflict comments and using correct variable safeProps

* removed width/height check as defaults are provided now

* fixed code style lint warnings

* added spaces in paren
  • Loading branch information
mzorz committed Oct 12, 2018
1 parent 7fbb3e0 commit b1c05b3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/components/src/primitives/svg/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
import { omit } from 'lodash';
import { Svg } from 'react-native-svg';

/**
* Internal dependencies
*/
import styles from '../../dashicon/style.scss';

export {
G,
Path,
Polygon,
} from 'react-native-svg';

export const SVG = ( props ) => {
const appliedProps = omit( props, 'className' );
// We're using the react-native-classname-to-style plugin, so when a `className` prop is passed it gets converted to `style` here.
// Given it carries a string (as it was originally className) but an object is expected for `style`,
// we need to check whether `style` exists and is a string, and convert it to an object
let styleKeys = new Array();
const styleValues = new Array();
if ( typeof props.style === 'string' || props.style instanceof String ) {
styleKeys = props.style.split( ' ' );
styleKeys.forEach( ( element ) => {
const oneStyle = styles[ element ];
if ( oneStyle !== undefined ) {
styleValues.push( oneStyle );
}
} );
}

const safeProps = styleValues.length === 0 ? { ...omit( props, [ 'style' ] ) } : { ...props, style: styleValues };
return (
<Svg
height="100%"
width="100%"
{ ...appliedProps }
{ ...safeProps }
/>
);
};

0 comments on commit b1c05b3

Please sign in to comment.