-
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.
- Loading branch information
Showing
10 changed files
with
232 additions
and
29 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
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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
import { isString } from "../utils"; | ||
import { camelize, capitalize, isString } from "../utils"; | ||
import { render } from "./render"; | ||
import { h } from "./vnode"; | ||
|
||
let components; | ||
export function createApp(rootComponent) { | ||
components = rootComponent.components || {}; | ||
const app = { | ||
mount(el){ | ||
if(isString(el)){ | ||
mount(el) { | ||
if (isString(el)) { | ||
el = document.querySelector(el); | ||
} | ||
|
||
if(!rootComponent.render && !rootComponent.template){ | ||
if (!rootComponent.render && !rootComponent.template) { | ||
rootComponent.template = el.innerHTML; | ||
el.innerHTML = ''; | ||
} | ||
|
||
render(h(rootComponent), el); | ||
} | ||
} | ||
return app; | ||
} | ||
|
||
export function resolveComponent(name) { | ||
return components && (components[name] || components[camelize(name)] || components[capitalize((camelize(name)))]) | ||
} |
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,25 @@ | ||
import { isArray, isNumber, isObject, isString } from "../../utils"; | ||
|
||
export function renderList(source, renderItem) { | ||
const nodes = []; | ||
if (isNumber(source)) { | ||
for (let i = 0; i < source; i++) { | ||
// (item, index) => {} | ||
nodes.push(renderItem(i + 1, i)); | ||
} | ||
} | ||
else if (isString(source) || isArray(source)) { | ||
for (let i = 0; i < source.length; i++) { | ||
// (item, index) => {} | ||
nodes.push(renderItem(source[i], i)); | ||
} | ||
} | ||
else if (isObject(source)) { | ||
Object.keys(source).forEach((key, i) => { | ||
// (value, key, index) => {} | ||
nodes.push(renderItem(source[key], key, i)); | ||
}) | ||
} | ||
|
||
return nodes; | ||
} |
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,43 @@ | ||
import { isArray } from "../../utils"; | ||
|
||
export function withModel(tag, props, getter, setter) { | ||
props = props || {}; | ||
if (tag === 'input') { | ||
switch(props.type){ | ||
case 'radio': | ||
props.checked = props.value === getter(); | ||
props.onChange = (e) => setter(e.target.value); | ||
break; | ||
case 'checkbox': | ||
const modelValue = getter(); | ||
if(isArray(modelValue)){ | ||
props.checked = modelValue.includes(props.value); | ||
props.onChange = (e) => { | ||
const {value} = e.target; | ||
const values = new Set(getter()); | ||
if(values.has(value)){ | ||
values.delete(value); | ||
} | ||
else { | ||
values.add(value); | ||
} | ||
props.checked = values.has(props.value); | ||
setter([...values]); | ||
} | ||
} | ||
else { | ||
props.checked = modelValue; | ||
props.onChange = (e) => { | ||
props.checked = e.target.checked; | ||
setter(e.target.checked); | ||
}; | ||
} | ||
break; | ||
default: | ||
props.value = getter(); | ||
props.onInput = (e) => setter(e.target.value); | ||
break; | ||
} | ||
} | ||
return props; | ||
} |
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,4 +1,6 @@ | ||
export { h, Text, Fragment } from './vnode.js'; | ||
export { render } from './render.js'; | ||
export { nextTick } from './scheduler'; | ||
export {createApp} from './createApp'; | ||
export { createApp, resolveComponent } from './createApp'; | ||
export { renderList } from './helpers/renderList' | ||
export { withModel } from './helpers/vModel' |
Oops, something went wrong.