forked from alexlafroscia/ember-react-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decorator for creating the shim class
- Loading branch information
1 parent
e70353d
commit 27f2ab6
Showing
15 changed files
with
469 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,74 @@ | ||
ember-cli-react | ||
============================================================================== | ||
> Consume React components in Ember ??? | ||
[Short description of the addon.] | ||
This addon is a proof-of-concept for an approach to rendering React components in Ember. It is almost entirely inspired by [a blog post][blog-post] by [Sivakumar Kailasam][sivakumar], from which the general idea was mostly borrowed. | ||
|
||
Installation | ||
------------------------------------------------------------------------------ | ||
|
||
``` | ||
ember install ember-cli-react | ||
yarn add -D alexlafroscia/ember-cli-react react react-dom | ||
``` | ||
|
||
Note: `react` and `react-dom` are considered peer dependencies of this addon. You should install them in addition to the addon itself. | ||
|
||
|
||
Usage | ||
------------------------------------------------------------------------------ | ||
|
||
[Longer description of how to use the addon in apps.] | ||
This addon provides an ES6 class decorator that allows a React element to be rendered in Ember. | ||
|
||
As an example, you can create a component like this: | ||
|
||
Contributing | ||
------------------------------------------------------------------------------ | ||
|
||
### Installation | ||
```javascript | ||
// app/components/my-react-component.js | ||
import React from 'react'; | ||
import WithEmberSupport from 'ember-cli-react'; | ||
|
||
@WithEmberSupport | ||
export default class extends React.Component { | ||
render() { | ||
const { name } = this.props; | ||
|
||
return ( | ||
<p>Hello, {name}</p> | ||
); | ||
} | ||
} | ||
``` | ||
|
||
And render it like this: | ||
|
||
* `git clone <repository-url>` | ||
* `cd ember-cli-react` | ||
* `npm install` | ||
```handlebars | ||
{{my-react-component name='Alex'}} | ||
``` | ||
|
||
That would create a component that renders `Hello, Alex`. | ||
|
||
### Linting | ||
Got'chas | ||
------------------------------------------------------------------------------ | ||
|
||
* `npm run lint:js` | ||
* `npm run lint:js -- --fix` | ||
* Any time a property on `my-react-component` changes, it will blow away the React component entirely and re-render it. You will lose any temporal state. | ||
|
||
### Running tests | ||
What all is this addon doing? | ||
------------------------------------------------------------------------------ | ||
|
||
* `ember test` – Runs the test suite on the current Ember version | ||
* `ember test --server` – Runs the test suite in "watch mode" | ||
* `npm test` – Runs `ember try:each` to test your addon against multiple Ember versions | ||
* Provides imports for `react` and `react-dom` | ||
* Hooks up a bunch of necessary `babel` transforms | ||
* Includes the decorator for creating a React-Ember-component Frankenstein-ian monster class that does the "heavy lifting" to bridge the two frameworks | ||
|
||
### Running the dummy application | ||
Is this production ready? | ||
------------------------------------------------------------------------------ | ||
|
||
* `ember serve` | ||
* Visit the dummy application at [http://localhost:4200](http://localhost:4200). | ||
O god, please do not use this | ||
|
||
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). | ||
|
||
License | ||
------------------------------------------------------------------------------ | ||
|
||
This project is licensed under the [MIT License](LICENSE.md). | ||
|
||
[blog-post]: https://medium.com/@sivakumar_k/using-react-components-in-your-ember-app-8f7805d409b0 | ||
[sivakumar]: https://github.com/sivakumar-kailasam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Component from '@ember/component'; | ||
import { get } from '@ember/object'; | ||
import { schedule } from '@ember/runloop'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
export default function WithEmberSupport(Klass) { | ||
return class extends Component { | ||
constructor() { | ||
super(); | ||
|
||
for (const key in Klass.prototype) { | ||
this[key] = Klass.prototype[key]; | ||
} | ||
} | ||
|
||
_getPropsForReact() { | ||
return Object.keys(this.attrs).reduce((acc, key) => { | ||
const value = get(this, key); | ||
|
||
acc[key] = value; | ||
|
||
return acc; | ||
}, {}); | ||
} | ||
|
||
_mountElement() { | ||
this._reactElement = ReactDOM.render( | ||
<Klass {...this._getPropsForReact()} />, | ||
this.element | ||
); | ||
} | ||
|
||
didUpdateAttrs() { | ||
schedule('render', () => { | ||
this._mountElement(); | ||
}); | ||
} | ||
|
||
didInsertElement() { | ||
super.didInsertElement(...arguments); | ||
|
||
this._mountElement(); | ||
} | ||
|
||
willDestroyElement() { | ||
ReactDOM.unmountComponentAtNode(this.element); | ||
|
||
super.willDestroyElement(...arguments); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function(/* environment, appConfig */) { | ||
return { }; | ||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import WithEmberSupport from 'ember-cli-react'; | ||
|
||
@WithEmberSupport | ||
export default class FromMixin extends React.Component { | ||
render() { | ||
return <h1>Hello from React</h1>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import WithEmberSupport from 'ember-cli-react'; | ||
|
||
@WithEmberSupport | ||
export default class WithProperties extends React.Component { | ||
render() { | ||
const { foo } = this.props; | ||
|
||
return <p>foo equals {foo}</p>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
<h2 id="title">Welcome to Ember</h2> | ||
<h2 id="title">Hello from Ember</h2> | ||
|
||
{{outlet}} | ||
<button {{action (mut showReactComponent) true}}> | ||
Show react component | ||
</button> | ||
|
||
<button {{action (mut showReactComponent) false}}> | ||
Hide react component | ||
</button> | ||
|
||
{{#if showReactComponent}} | ||
{{from-mixin}} | ||
{{/if}} |
Empty file.
Oops, something went wrong.