From 128a8a0f10342fb984649a40db9c1930c2cc3d11 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Wed, 27 Jun 2018 20:26:18 +0200 Subject: [PATCH] add a component property --- docs/src/pages/style/icons/SvgIcons.js | 24 ++++++------ packages/material-ui/src/SvgIcon/SvgIcon.d.ts | 1 + packages/material-ui/src/SvgIcon/SvgIcon.js | 23 ++++++------ .../material-ui/src/SvgIcon/SvgIcon.test.js | 37 +++++++++++++------ 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/docs/src/pages/style/icons/SvgIcons.js b/docs/src/pages/style/icons/SvgIcons.js index b645c5545b02b9..ec8f3a0579a9e5 100644 --- a/docs/src/pages/style/icons/SvgIcons.js +++ b/docs/src/pages/style/icons/SvgIcons.js @@ -25,15 +25,11 @@ const styles = theme => ({ function HomeIcon(props) { return ( - + ); } -HomeIcon.propTypes = { - fill: PropTypes.string, -}; - function SvgIcons(props) { const { classes } = props; return ( @@ -48,13 +44,17 @@ function SvgIcons(props) { className={classes.icon} color="primary" style={{ fontSize: 36 }} - fill="url(#gradient1)" - defs={ - - - - - } + component={svgProps => ( + + + + + + + + {React.cloneElement(svgProps.children[0], { fill: 'url(#gradient1)' })} + + )} /> ); diff --git a/packages/material-ui/src/SvgIcon/SvgIcon.d.ts b/packages/material-ui/src/SvgIcon/SvgIcon.d.ts index 6b7235727d4b41..39a2b64659fa5c 100644 --- a/packages/material-ui/src/SvgIcon/SvgIcon.d.ts +++ b/packages/material-ui/src/SvgIcon/SvgIcon.d.ts @@ -4,6 +4,7 @@ import { StandardProps, PropTypes } from '..'; export interface SvgIconProps extends StandardProps, SvgIconClassKey> { color?: PropTypes.Color | 'action' | 'disabled' | 'error'; + component?: React.ReactType; fontSize?: 'inherit' | 'default'; nativeColor?: string; titleAccess?: string; diff --git a/packages/material-ui/src/SvgIcon/SvgIcon.js b/packages/material-ui/src/SvgIcon/SvgIcon.js index 5f1eccc28d0359..8dc722701b9ef3 100644 --- a/packages/material-ui/src/SvgIcon/SvgIcon.js +++ b/packages/material-ui/src/SvgIcon/SvgIcon.js @@ -43,7 +43,7 @@ function SvgIcon(props) { classes, className: classNameProp, color, - defs, + component: Component, fontSize, nativeColor, titleAccess, @@ -61,7 +61,7 @@ function SvgIcon(props) { ); return ( - - {defs ? {defs} : null} - {titleAccess ? {titleAccess} : null} {children} - + {titleAccess ? {titleAccess} : null} + ); } @@ -96,15 +95,14 @@ SvgIcon.propTypes = { */ color: PropTypes.oneOf(['inherit', 'primary', 'secondary', 'action', 'error', 'disabled']), /** -<<<<<<< HEAD + * The component used for the root node. + * Either a string to use a DOM element or a component. + */ + component: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]), + /** * The fontSize applied to the icon. Defaults to 24px, but can be configure to inherit font size. */ fontSize: PropTypes.oneOf(['inherit', 'default']), -======= - * Node passed into the top of the SVG element. - */ - defs: PropTypes.node, ->>>>>>> [SvgIcon] Add defs prop, test and demo to SvgIcon /** * Applies a color attribute to the SVG element. */ @@ -126,8 +124,9 @@ SvgIcon.propTypes = { SvgIcon.defaultProps = { color: 'inherit', - viewBox: '0 0 24 24', + component: 'svg', fontSize: 'default', + viewBox: '0 0 24 24', }; SvgIcon.muiName = 'SvgIcon'; diff --git a/packages/material-ui/src/SvgIcon/SvgIcon.test.js b/packages/material-ui/src/SvgIcon/SvgIcon.test.js index 4df7ed1bb6cdb5..0544ca3c7dfebc 100644 --- a/packages/material-ui/src/SvgIcon/SvgIcon.test.js +++ b/packages/material-ui/src/SvgIcon/SvgIcon.test.js @@ -2,20 +2,26 @@ import React from 'react'; import { assert } from 'chai'; -import { createShallow, getClasses } from '../test-utils'; +import { createShallow, createMount, getClasses } from '../test-utils'; import SvgIcon from './SvgIcon'; describe('', () => { let shallow; + let mount; let classes; let path; before(() => { shallow = createShallow({ dive: true }); + mount = createMount(); classes = getClasses(foo); path = ; }); + after(() => { + mount.cleanUp(); + }) + it('renders children by default', () => { const wrapper = shallow({path}); assert.strictEqual(wrapper.contains(path), true, 'should contain the children'); @@ -100,17 +106,26 @@ describe('', () => { }); }); - describe('prop: defs', () => { - it('should render defs before path', () => { - path = ; - const defs = ( - - - - + describe('prop: component', () => { + it('should render component before path', () => { + const wrapper = mount( + ( + + + + + + + + {props.children} + + )} + > + {path} + , ); - const wrapper = shallow({path}); - assert.strictEqual(wrapper.childAt(0).type(), 'defs'); + assert.strictEqual(wrapper.find('defs').length, 1); }); }); });