-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add JSX pragma #3
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e06d1cb
Upgrade to @cycle/[email protected]
sliptype 7d1ab25
Merge branch 'master' of github.com:cyclejs/react-dom
sliptype cd46cc1
Upgrade typescript
sliptype 9ed1bbe
Add jsx-factory
sliptype a2c0a39
Add tests
sliptype 056ed6d
Update readme
sliptype 1153592
Remove unused import
sliptype 26eafda
Rename JsxFactory -> jsxFactory
sliptype b7f5b87
Fix typo in readme
sliptype 27832ba
Fix dangling quote
sliptype File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,36 @@ | ||
import { createElement, ReactElement, ReactType } from 'react'; | ||
import { incorporate } from '@cycle/react'; | ||
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 | ||
} |
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,231 @@ | ||
import { createElement, Attributes, ReactElement, ReactType } from 'react'; | ||
const assert = require('assert'); | ||
import {ReactSource} from '@cycle/react'; | ||
import {makeDOMDriver, jsxFactory} from '../src/index'; | ||
import {run} from '@cycle/run'; | ||
import xs from 'xstream'; | ||
|
||
function createRenderTarget(id: string | null = null) { | ||
const element = document.createElement('div'); | ||
element.className = 'cycletest'; | ||
if (id) { | ||
element.id = id; | ||
} | ||
document.body.appendChild(element); | ||
return element; | ||
} | ||
|
||
describe('jsx-factory', function() { | ||
it('w/ nothing', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of(<h1></h1>), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const h1 = target.querySelector('h1') as HTMLElement; | ||
assert.strictEqual(!!h1, true); | ||
assert.strictEqual(h1.tagName, 'H1'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ text child', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of(<h1>heading 1</h1>), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const h1 = target.querySelector('h1') as HTMLElement; | ||
assert.strictEqual(!!h1, true); | ||
assert.strictEqual(h1.innerHTML, 'heading 1'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ children array', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of( | ||
<section> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const section = target.querySelector('section') as HTMLElement; | ||
assert.strictEqual(!!section, true); | ||
assert.strictEqual(section.children.length, 3); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ props', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of(<section data-foo="bar"/>), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const section = target.querySelector('section') as HTMLElement; | ||
assert.strictEqual(!!section, true); | ||
assert.strictEqual(section.dataset.foo, 'bar'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ props and children', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of( | ||
<section data-foo="bar"> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const section = target.querySelector('section') as HTMLElement; | ||
assert.strictEqual(!!section, true); | ||
assert.strictEqual(section.dataset.foo, 'bar'); | ||
assert.strictEqual(section.children.length, 3); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ className', done => { | ||
function main(sources: {react: ReactSource}) { | ||
return { | ||
react: xs.of(<section className="foo"/>), | ||
}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const section = target.querySelector('section') as HTMLElement; | ||
assert.strictEqual(!!section, true); | ||
assert.strictEqual(section.className, 'foo'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('w/ symbol selector', done => { | ||
function main(sources: {react: ReactSource}) { | ||
|
||
const inc = Symbol(); | ||
const inc$ = sources.react.select(inc).events('click'); | ||
const count$ = inc$.fold((acc: number, x: any) => acc + 1, 0); | ||
const vdom$ = count$.map((i: number) => ( | ||
<div> | ||
<h1>{'' + i}</h1> | ||
<button sel={inc}/> | ||
</div> | ||
)); | ||
|
||
return {react: vdom$}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const button = target.querySelector('button') as HTMLElement; | ||
const h1 = target.querySelector('h1') as HTMLElement; | ||
assert.strictEqual(!!button, true); | ||
assert.strictEqual(!!h1, true); | ||
assert.strictEqual(h1.innerHTML, '0'); | ||
button.click(); | ||
setTimeout(() => { | ||
assert.strictEqual(h1.innerHTML, '1'); | ||
done(); | ||
}, 100); | ||
}, 100); | ||
}); | ||
|
||
it('renders functional component', done => { | ||
const Test = () => (<h1>Functional</h1>); | ||
|
||
function main() { | ||
const vdom$ = xs.of(<Test/>); | ||
return {react: vdom$}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const h1 = target.querySelector('h1') as HTMLElement; | ||
assert.strictEqual(!!h1, true); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('renders class component', done => { | ||
class Test extends React.Component { | ||
render() { | ||
return (<h1>Class</h1>); | ||
} | ||
} | ||
|
||
function main() { | ||
const vdom$ = xs.of(<Test/>); | ||
return {react: vdom$}; | ||
} | ||
|
||
const target = createRenderTarget(); | ||
run(main, { | ||
react: makeDOMDriver(target), | ||
}); | ||
|
||
setTimeout(() => { | ||
const h1 = target.querySelector('h1') as HTMLElement; | ||
assert.strictEqual(!!h1, true); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is a dangling single quote in this line that is breaking the markdown syntax afterwards
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.
@sliptype
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.
@sliptype good catch! fixed in 27832ba