Skip to content

Commit

Permalink
Add displayName
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasso committed Jan 9, 2017
1 parent e59d668 commit faad66b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ You can load styles in the way that you prefer but is important to keep in mind
For example, this is a valid one `.⚛App`.
So, pay attention that components' names and css selectors always match.
This is particular important if you have css-modules that modifies the original names, or code obfuscation.

The first ones for example need a syntax like this:

```css
Expand All @@ -76,6 +77,28 @@ The first ones for example need a syntax like this:
}
```

And the second one, considering for example a minification process with webpack's UglifyJsPlugin (see [here](https://github.com/facebook/react/issues/4915) for more information),
need a component with a displayName attribute like this:

```js
class ComponentName extends React.Component {

static propTypes = {
// propTypes...
}

// this
static displayName = 'ComponentName';

render() {
// render here...
}
}

// or this
// ComponentName.displayName = 'ComponentName';
```


### Adapting based on props

Expand All @@ -84,6 +107,9 @@ If you want to set styles based on props, you can do it in 2 ways:
- Set inline styles, as we can see in this example:
```js
class Button extends React.Component {

static displayName = 'Button';

render() {
return (
<button
Expand All @@ -103,6 +129,9 @@ class Button extends React.Component {
import styles from './Button.css';

export default class Button extends React.Component {

static displayName = 'Button';

render() {
return (
<button
Expand Down
2 changes: 1 addition & 1 deletion src/classInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function initClassInjector() {
const component = originalRender.apply(this);
let result = component;
if (component) {
const classToInject = `⚛${this.constructor.name}`;
const classToInject = `⚛${this.constructor.displayName || this.constructor.name}`;
if (typeof component.type === 'string') {
result = React.cloneElement(
component,
Expand Down
28 changes: 28 additions & 0 deletions test/classInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,32 @@ describe('Css class injector', () => {
className: '⚛Foo',
});
});

it('should inject displayName', () => {
//eslint-disable-next-line
class Foo extends React.Component {
static propTypes = {
style: React.PropTypes.string,
className: React.PropTypes.string,
}

static displayName = 'Bar';

render() {
return (
<div {...this.props}>
<p></p>
</div>
);
}
}

const renderer = ReactTestUtils.createRenderer();
renderer.render(<Foo style="bar" />);
const result = renderer.getRenderOutput();
expect(result.props).toMatch({
style: 'bar',
className: '⚛Bar ',
});
});
});

0 comments on commit faad66b

Please sign in to comment.