-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
export default class Component {} | ||
import { reconcile } from "./reconciler"; | ||
|
||
export class Component { | ||
constructor(props) { | ||
this.props = props; | ||
this.state = this.state || {}; | ||
} | ||
|
||
setState(partialState) { | ||
this.state = Object.assign({}, this.state, partialState); | ||
updateInstance(this.__internalInstance); | ||
} | ||
} | ||
|
||
function updateInstance(internalInstance) { | ||
const parentDom = internalInstance.dom.parentNode; | ||
const element = internalInstance.element; | ||
reconcile(parentDom, internalInstance, element); | ||
} | ||
|
||
export function createPublicInstance(element, internalInstance) { | ||
const { type, props } = element; | ||
const publicInstance = new type(props); | ||
publicInstance.__internalInstance = internalInstance; | ||
return publicInstance; | ||
} |
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,15 +1,11 @@ | ||
import { createElement } from './element'; | ||
import Component from './component'; | ||
import { render } from './reconciler'; | ||
import { createElement } from "./element"; | ||
import { Component } from "./component"; | ||
import { render } from "./reconciler"; | ||
|
||
export default { | ||
createElement, | ||
Component, | ||
render | ||
createElement, | ||
Component, | ||
render | ||
}; | ||
|
||
export { | ||
createElement, | ||
Component, | ||
render | ||
}; | ||
export { createElement, Component, render }; |
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,39 @@ | ||
import test from "ava"; | ||
import browserEnv from "browser-env"; | ||
/** @jsx createElement */ | ||
import { render, createElement, Component } from "../src/didact"; | ||
|
||
// Create document global var | ||
browserEnv(["document"]); | ||
|
||
test.beforeEach(t => { | ||
let root = document.getElementById("root"); | ||
if (!root) { | ||
root = document.createElement("div"); | ||
root.id = "root"; | ||
document.body.appendChild(root); | ||
} | ||
t.context.root = root; | ||
}); | ||
|
||
test("render component", t => { | ||
const root = t.context.root; | ||
class FooComponent extends Component { | ||
render() { | ||
return <div><b /><a href="foo" /></div>; | ||
} | ||
} | ||
render(<FooComponent />, root); | ||
t.is(root.innerHTML, '<div><b></b><a href="foo"></a></div>'); | ||
}); | ||
|
||
test("render component with props", t => { | ||
const root = t.context.root; | ||
class FooComponent extends Component { | ||
render() { | ||
return <div><b>{this.props.name}</b><a href="foo" /></div>; | ||
} | ||
} | ||
render(<FooComponent name="Bar" />, root); | ||
t.is(root.innerHTML, '<div><b>Bar</b><a href="foo"></a></div>'); | ||
}); |
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,49 @@ | ||
import test from "ava"; | ||
import browserEnv from "browser-env"; | ||
/** @jsx createElement */ | ||
import { render, createElement, Component } from "../src/didact"; | ||
|
||
// Create document global var | ||
browserEnv(["document"]); | ||
|
||
test.beforeEach(t => { | ||
let root = document.getElementById("root"); | ||
if (!root) { | ||
root = document.createElement("div"); | ||
root.id = "root"; | ||
document.body.appendChild(root); | ||
} | ||
t.context.root = root; | ||
}); | ||
|
||
test("change state on click", t => { | ||
const root = t.context.root; | ||
class FooComponent extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
count: 0 | ||
}; | ||
} | ||
|
||
handleClick() { | ||
this.setState({ | ||
count: this.state.count + 1 | ||
}); | ||
} | ||
|
||
render() { | ||
return <div onClick={e => this.handleClick()}>{this.state.count}</div>; | ||
} | ||
} | ||
render(<FooComponent />, root); | ||
t.is(root.innerHTML, "<div>0</div>"); | ||
click(root.firstChild); | ||
t.is(root.innerHTML, "<div>1</div>"); | ||
}); | ||
|
||
function click(dom) { | ||
var evt = document.createEvent("MouseEvent"); | ||
evt.initEvent("click", false, true); | ||
dom.dispatchEvent(evt); | ||
} |