From 7b48e00e5e9d3df34d67b67b175e4d2a85e0a94c Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Thu, 13 Dec 2018 12:30:26 +0100 Subject: [PATCH] [styles] Fix certain components not being considered mui elements --- .../src/hoistInternalStatics.js | 17 +++++++++++++++++ packages/material-ui-styles/src/withStyles.js | 3 +++ packages/material-ui-styles/src/withTheme.js | 3 +++ 3 files changed, 23 insertions(+) create mode 100644 packages/material-ui-styles/src/hoistInternalStatics.js diff --git a/packages/material-ui-styles/src/hoistInternalStatics.js b/packages/material-ui-styles/src/hoistInternalStatics.js new file mode 100644 index 00000000000000..dd4fb5943c57e7 --- /dev/null +++ b/packages/material-ui-styles/src/hoistInternalStatics.js @@ -0,0 +1,17 @@ +/** + * Copies internal immediate statics from material-ui from source to target + */ +export default function hoistStatics(target, source) { + const internals = ['muiName']; + + for (let i = 0; i < internals.length; i += 1) { + const key = internals[i]; + const descriptor = Object.getOwnPropertyDescriptor(source, key); + try { + // Avoid failures from read-only properties and undefined descriptors + Object.defineProperty(target, key, descriptor); + } catch (e) {} + } + + return target; +} diff --git a/packages/material-ui-styles/src/withStyles.js b/packages/material-ui-styles/src/withStyles.js index 50abe1d508b670..cff81d1d9be324 100644 --- a/packages/material-ui-styles/src/withStyles.js +++ b/packages/material-ui-styles/src/withStyles.js @@ -8,6 +8,7 @@ import mergeClasses from './mergeClasses'; import multiKeyStore from './multiKeyStore'; import getStylesCreator from './getStylesCreator'; import getThemeProps from './getThemeProps'; +import hoistStatics from './hoistInternalStatics'; import { StylesContext } from './StylesProvider'; import { ThemeContext } from './ThemeProvider'; @@ -332,6 +333,8 @@ const withStyles = (stylesOrCreator, options = {}) => Component => { WithStyles.displayName = `WithStyles(${getDisplayName(Component)})`; } + hoistStatics(WithStyles, Component); + if (process.env.NODE_ENV !== 'production') { // Exposed for test purposes. WithStyles.Naked = Component; diff --git a/packages/material-ui-styles/src/withTheme.js b/packages/material-ui-styles/src/withTheme.js index 69612aafefa6a1..820630f3e38113 100644 --- a/packages/material-ui-styles/src/withTheme.js +++ b/packages/material-ui-styles/src/withTheme.js @@ -1,6 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { getDisplayName } from '@material-ui/utils'; +import hoistStatics from './hoistInternalStatics'; import { ThemeContext } from './ThemeProvider'; // Provide the theme object as a property to the input component. @@ -25,6 +26,8 @@ const withTheme = () => Component => { WithTheme.displayName = `WithTheme(${getDisplayName(Component)})`; } + hoistStatics(WithTheme, Component); + return WithTheme; };