Skip to content

Commit

Permalink
component GeekUniversity#1
Browse files Browse the repository at this point in the history
  • Loading branch information
RIGUANG JIN authored and RIGUANG JIN committed Sep 15, 2020
1 parent 518b03e commit 97fe3a0
Show file tree
Hide file tree
Showing 9 changed files with 2,007 additions and 90 deletions.
19 changes: 16 additions & 3 deletions week12/jsx/dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions week12/jsx/framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export function createElement(type, attributes, ...children){
let element;
if(typeof type === 'string')
element = new ElementWrapper(type);
else
element = new type;

for(let name in attributes) {
element.setAttribute(name, attributes[name]);
}

for(let child of children) {
if(typeof child === 'string') {
child = new TextWrapper(child)
}
element.appendChild(child)
}
return element;
}

export class Component {
constructor(type){

}

setAttribute(name, value){
this.root.setAttribute(name, value)
}

appendChild(child){
child.mountTo(this.root)
}
mountTo(parent){
parent.appendChild(this.root)
}
}


class ElementWrapper extends Component{

constructor(type){
this.root = document.createElement(type);
}

}

class TextWrapper extends Component{

constructor(content){
this.root = document.createTextNode(content);
}

}
File renamed without changes.
82 changes: 21 additions & 61 deletions week12/jsx/main.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,38 @@
function createElement(type, attributes, ...children){
let element;
if(typeof type === 'string')
element = new ElementWrapper(type);
else
element = new type;

for(let name in attributes) {
element.setAttribute(name, attributes[name]);
}

for(let child of children) {
if(typeof child === 'string') {
child = new TextWrapper(child)
}
element.appendChild(child)
}
return element;
}

class ElementWrapper{

constructor(type){
this.root = document.createElement(type);
}
setAttribute(name, value){
this.root.setAttribute(name, value)
}
appendChild(child){
child.mountTo(this.root)
}
mountTo(parent){
parent.appendChild(this.root)
}
}

class TextWrapper{
import {Component, createElement} from './framework';

constructor(content){
this.root = document.createTextNode(content);
}
setAttribute(name, value){
this.root.setAttribute(name, value)
}
appendChild(child){
child.mountTo(this.root)
}
mountTo(parent){
parent.appendChild(this.root)
}
}

class Div {
class Carousel extends Component {
constructor(){
this.root = document.createElement("div");
super();
this.attributes = Object.create(null);
}
setAttribute(name, value){
this.root.setAttribute(name, value)
this.attributes[name] = value;
}
appendChild(child){
child.mountTo(this.root)
render(){
this.root = document.createElement("div")

for(let record of this.attributes.src){
let child = document.createElement('img');
child.src = record;
this.root.appendChild(child)
}
return this.root;
}
mountTo(parent){
parent.appendChild(this.root)
parent.appendChild(this.render())
}
}


let a = <Div id="a">
<span>a</span>
<span>b</span>
<span>c</span>
</Div>

let d = [
'/statics/cat1.jpg',
'/statics/cat2.jpg',
'/statics/cat3.jpg'
]

// document.body.appendChild(a);

let a = <Carousel src={d}/>
a.mountTo(document.body)
Loading

0 comments on commit 97fe3a0

Please sign in to comment.