diff --git a/common/changes/office-ui-fabric-react/icon-fix_2018-05-08-00-58.json b/common/changes/office-ui-fabric-react/icon-fix_2018-05-08-00-58.json new file mode 100644 index 00000000000000..7ede5691096b4c --- /dev/null +++ b/common/changes/office-ui-fabric-react/icon-fix_2018-05-08-00-58.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "office-ui-fabric-react", + "comment": "Icon: undoing breaking change with regards to making `IIconStyles.root` required, adding tests to ensure backwards compatibility.", + "type": "minor" + } + ], + "packageName": "office-ui-fabric-react", + "email": "dzearing@microsoft.com" +} \ No newline at end of file diff --git a/packages/office-ui-fabric-react/src/components/Icon/Icon.base.tsx b/packages/office-ui-fabric-react/src/components/Icon/Icon.base.tsx index 000eebb2cbd0bb..4493bce827b5f8 100644 --- a/packages/office-ui-fabric-react/src/components/Icon/Icon.base.tsx +++ b/packages/office-ui-fabric-react/src/components/Icon/Icon.base.tsx @@ -33,43 +33,48 @@ export class IconBase extends BaseComponent { getStyles, iconName, imageErrorAs, + styles, } = this.props; const isPlaceholder = typeof iconName === 'string' && iconName.length === 0; const isImage = this.props.iconType === IconType.image || this.props.iconType === IconType.Image; const { iconClassName, children } = this._getIconContent(iconName); - const classNames = getClassNames(getStyles, { className, isPlaceholder, isImage, iconClassName }); + const classNames = getClassNames(getStyles, { + className, + iconClassName, + isImage, + isPlaceholder, + styles + }); - const containerProps = ariaLabel ? { 'aria-label': ariaLabel, 'data-icon-name': iconName, } : { - role: 'presentation', - 'aria-hidden': true, - 'data-icon-name': iconName, - }; + const containerProps = ariaLabel ? + { + 'aria-label': ariaLabel, + } : { + role: 'presentation', + 'aria-hidden': true, + }; - if (this.props.iconType === IconType.image || this.props.iconType === IconType.Image) { + const RootType = isImage ? 'div' : 'i'; + const nativeProps = getNativeProps(this.props, htmlElementProperties); + const { imageLoadError } = this.state; + const imageProps = { ...this.props.imageProps, onLoadingStateChange: this.onImageLoadingStateChange }; + const ImageType = imageLoadError && imageErrorAs || Image; - const { imageLoadError } = this.state; - const imageProps = { ...this.props.imageProps, onLoadingStateChange: this.onImageLoadingStateChange }; - const ImageType = imageLoadError && imageErrorAs || Image; - return ( -
+ return ( + + { isImage ? ( -
- ); - } else { - return ( - - { children } - - ); - } + ) : ( + children + ) } + + ); } private onImageLoadingStateChange = (state: ImageLoadState): void => { diff --git a/packages/office-ui-fabric-react/src/components/Icon/Icon.styles.ts b/packages/office-ui-fabric-react/src/components/Icon/Icon.styles.ts index 01160c07574612..d6f1e3a1b4ac3a 100644 --- a/packages/office-ui-fabric-react/src/components/Icon/Icon.styles.ts +++ b/packages/office-ui-fabric-react/src/components/Icon/Icon.styles.ts @@ -1,7 +1,7 @@ import { IIconStyleProps, IIconStyles } from './Icon.types'; export const getStyles = (props: IIconStyleProps): IIconStyles => { - const { className, iconClassName, isPlaceholder, isImage } = props; + const { className, iconClassName, isPlaceholder, isImage, styles } = props; return { root: [ @@ -17,7 +17,9 @@ export const getStyles = (props: IIconStyleProps): IIconStyles => { overflow: 'hidden' }, iconClassName, - className + className, + styles && styles.root, + styles && styles.imageContainer ], }; }; diff --git a/packages/office-ui-fabric-react/src/components/Icon/Icon.test.tsx b/packages/office-ui-fabric-react/src/components/Icon/Icon.test.tsx index 0ac5e834dad2ff..eeec062ee647a2 100644 --- a/packages/office-ui-fabric-react/src/components/Icon/Icon.test.tsx +++ b/packages/office-ui-fabric-react/src/components/Icon/Icon.test.tsx @@ -5,7 +5,7 @@ import { Icon } from './index'; describe('Icon', () => { it('renders Icon correctly', () => { - const component = renderer.create(); + const component = renderer.create(); const tree = component.toJSON(); expect(tree).toMatchSnapshot(); }); @@ -16,4 +16,26 @@ describe('Icon', () => { expect(tree).toMatchSnapshot(); }); + it('renders Icon with custom styles', () => { + const component = renderer.create( + + ); + expect(component.toJSON()).toMatchSnapshot(); + }); + + it('renders Icon with getStyles', () => { + const customStyles = (props: {}) => ({ root: 'root', imageContainer: 'imageContainer' }); + + const component = renderer.create( + + ); + expect(component.toJSON()).toMatchSnapshot(); + }); }); \ No newline at end of file diff --git a/packages/office-ui-fabric-react/src/components/Icon/Icon.types.ts b/packages/office-ui-fabric-react/src/components/Icon/Icon.types.ts index 56d88da735bb7e..c3ac2426bffad7 100644 --- a/packages/office-ui-fabric-react/src/components/Icon/Icon.types.ts +++ b/packages/office-ui-fabric-react/src/components/Icon/Icon.types.ts @@ -35,11 +35,6 @@ export interface IIconProps extends IBaseProps, React.HTMLAttributes; + + /** + * Deprecated: use getStyles. + * @deprecated + */ + styles?: IIconStyles; } export interface IIconStyleProps { @@ -77,9 +78,15 @@ export interface IIconStyleProps { iconClassName?: string; isPlaceholder: boolean; isImage: boolean; + styles?: Partial; } export interface IIconStyles { - root: IStyle; + root?: IStyle; + + /** + * Deprecated. Use 'root'. + * @deprecated + */ imageContainer?: IStyle; } diff --git a/packages/office-ui-fabric-react/src/components/Icon/__snapshots__/Icon.test.tsx.snap b/packages/office-ui-fabric-react/src/components/Icon/__snapshots__/Icon.test.tsx.snap index 71341f81864b8c..fede124af17e5c 100644 --- a/packages/office-ui-fabric-react/src/components/Icon/__snapshots__/Icon.test.tsx.snap +++ b/packages/office-ui-fabric-react/src/components/Icon/__snapshots__/Icon.test.tsx.snap @@ -2,7 +2,7 @@ exports[`Icon renders Icon correctly 1`] = ` @@ -41,3 +40,47 @@ exports[`Icon renders Icon correctly using iconName 1`] = `  `; + +exports[`Icon renders Icon with custom styles 1`] = ` + +  + +`; + +exports[`Icon renders Icon with getStyles 1`] = ` + +  + +`;