-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support JSX as a pragma #9
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -376,6 +376,83 @@ See [`@cycle/react-native`](https://github.com/cyclejs/react-native). | |
</p> | ||
</details> | ||
|
||
<details> | ||
<summary><strong>JSX support</strong> (click here)</summary> | ||
|
||
<p> | ||
|
||
### Babel | ||
|
||
Add the following to your webpack config: | ||
|
||
```js | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.jsx?$/, | ||
loader: 'babel-loader', | ||
options: { | ||
plugins: [ | ||
['transform-react-jsx', { pragma: 'jsxFactory.createElement' }], | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
``` | ||
|
||
If you used `create-cycle-app` you may have to eject to modify the config. | ||
|
||
### Automatically providing jsxFactory | ||
|
||
You can avoid having to import `jsxFactory` in every jsx file by allowing webpack to provide it: | ||
|
||
```js | ||
plugins: [ | ||
new webpack.ProvidePlugin({ | ||
jsxFactory: ['@cycle/react', 'jsxFactory'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was originally jsxFactory: ['react-dom', 'jsxFactory'] But I believe |
||
}) | ||
], | ||
``` | ||
|
||
### Typescript | ||
|
||
Add the following to your `tsconfig.json`: | ||
|
||
```js | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"jsxFactory": "jsxFactory.createElement" | ||
} | ||
} | ||
``` | ||
|
||
If webpack is providing `jsxFactory` you will need to add typings to `custom-typings.d.ts`: | ||
|
||
```js | ||
declare var jsxFactory: any; | ||
``` | ||
|
||
|
||
### Usage | ||
|
||
```js | ||
import { jsxFactory } from '@cycle/react'; | ||
function view(state$: Stream<State>): Stream<ReactElement> { | ||
return state$.map(({ count }) => ( | ||
<div> | ||
<h2>Counter: {count}</h2> | ||
<button sel="add">Add</button> | ||
<button sel="subtract">Subtract</button> | ||
</div> | ||
)); | ||
} | ||
``` | ||
|
||
</p> | ||
</details> | ||
|
||
## License | ||
|
||
MIT, Copyright Andre 'Staltz' Medeiros 2018 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {createElement, ReactElement, ReactType} from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is a copy and paste of https://github.com/cyclejs/react-dom/blob/229e592ab80998ace5bf4fd0c12eee36f4499731/src/jsx-factory.ts, except for import path of The original filename was kebab case ( |
||
import {incorporate} from './incorporate'; | ||
export {Attributes} from 'react'; | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicAttributes { | ||
sel?: string | symbol; | ||
} | ||
} | ||
namespace React { | ||
interface ClassAttributes<T> extends Attributes { | ||
sel?: string | symbol; | ||
} | ||
} | ||
} | ||
|
||
type PropsExtensions = { | ||
sel?: string | symbol; | ||
} | ||
|
||
function createIncorporatedElement<P = any>( | ||
type: ReactType<P>, | ||
props: P & PropsExtensions | null, | ||
...children: Array<string | ReactElement<any>> | ||
): ReactElement<P> { | ||
if (!props || !props.sel) { | ||
return createElement(type, props, ...children); | ||
} else { | ||
return createElement(incorporate(type), props, ...children); | ||
} | ||
} | ||
|
||
export default { | ||
createElement: createIncorporatedElement | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test cases have been almost rewritten in a DOM-free way (using Any good ideas? |
||
import * as Adapter from 'enzyme-adapter-react-16'; | ||
import * as Enzyme from 'enzyme'; | ||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider turning on |
||
const assert = require('assert'); | ||
import {jsxFactory, h, ReactSource, makeCycleReactComponent} from '../src'; | ||
|
||
Enzyme.configure({adapter: new Adapter()}); | ||
|
||
const {shallow} = Enzyme; | ||
|
||
describe('jsxFactory', function () { | ||
it('w/ nothing', () => { | ||
const wrapper = shallow(<h1></h1>); | ||
|
||
assert.strictEqual(wrapper.find('h1').length, 1); | ||
}); | ||
|
||
it('w/ text child', () => { | ||
const wrapper = shallow(<h1>heading 1</h1>); | ||
|
||
const h1 = wrapper.find('h1'); | ||
assert.strictEqual(h1.text(), 'heading 1'); | ||
}); | ||
|
||
it('w/ children array', () => { | ||
const wrapper = shallow( | ||
<section> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
); | ||
|
||
const section = wrapper.find('section'); | ||
assert.strictEqual(section.children().length, 3); | ||
}); | ||
|
||
it('w/ props', () => { | ||
const wrapper = shallow(<section data-foo='bar' />); | ||
|
||
assert(wrapper.exists('[data-foo="bar"]')); | ||
}); | ||
|
||
it('w/ props and children', () => { | ||
const wrapper = shallow( | ||
<section data-foo="bar"> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
); | ||
|
||
const section = wrapper.first(); | ||
assert.strictEqual(section.length, 1); | ||
assert(section.exists('[data-foo="bar"]')); | ||
assert.deepStrictEqual(section.children().length, 3); | ||
}); | ||
|
||
it('w/ className', () => { | ||
const wrapper = shallow(<section className="foo" />); | ||
|
||
assert(wrapper.hasClass('foo')); | ||
}); | ||
|
||
it('renders functional component', () => { | ||
const Test = () => <h1>Functional</h1>; | ||
const wrapper = shallow(<Test />); | ||
|
||
assert.strictEqual(wrapper.first().type(), 'h1'); | ||
}); | ||
|
||
it('renders class component', () => { | ||
class Test extends React.Component { | ||
render() { | ||
return <h1>Class</h1>; | ||
} | ||
} | ||
|
||
const wrapper = shallow(<Test />); | ||
|
||
assert.strictEqual(wrapper.first().type(), 'h1'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ | |
"rootDir": "src/", | ||
"outDir": "lib/cjs/", | ||
"skipLibCheck": true, | ||
"lib": ["dom", "es5", "scripthost", "es2015"] | ||
"lib": ["dom", "es5", "scripthost", "es2015"], | ||
"jsx": "react", | ||
"jsxFactory": "jsxFactory.createElement" | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of configuring |
||
}, | ||
"files": ["src/index.ts"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: GitHub's "Display the rich diff" will help.