-
Notifications
You must be signed in to change notification settings - Fork 65
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
[15팀 정채은] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #29
Open
zenna9
wants to merge
3
commits into
hanghae-plus:main
Choose a base branch
from
zenna9:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,41 @@ | ||
import { addEvent } from "./eventManager"; | ||
export function createElement(vNode) { | ||
if (vNode === null || vNode === undefined || typeof vNode === "boolean") { | ||
return document.createTextNode(""); | ||
} | ||
|
||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return document.createTextNode(vNode); | ||
} | ||
|
||
export function createElement(vNode) {} | ||
if(Array.isArray(vNode)){ | ||
const fragment = document.createDocumentFragment(); | ||
vNode.forEach((node) =>{ | ||
const doms = createNodeWithChildren(node); | ||
fragment.appendChild(doms); | ||
}) | ||
return fragment; | ||
}else{ // typeof "Object" | ||
return createNodeWithChildren(vNode); | ||
} | ||
} | ||
|
||
function updateAttributes($el, props) {} | ||
function updateAttributes($el, props) { | ||
|
||
} | ||
|
||
function createNodeWithChildren(node){ | ||
const domX = document.createElement(node.type); | ||
//props 삽입 | ||
for(let propK in node.props){ | ||
if (propK === 'className') { | ||
domX.setAttribute('class', node.props[propK]); | ||
} else { | ||
domX.setAttribute(propK, node.props[propK]); | ||
} | ||
} | ||
//child 삽입 | ||
node.children.forEach((child) =>{ | ||
domX.appendChild(createElement(child)); | ||
}) | ||
return domX; | ||
} |
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,3 +1,14 @@ | ||
export function createVNode(type, props, ...children) { | ||
return {}; | ||
return { | ||
type, | ||
props, | ||
// 기존 | ||
// children: (children.flat(Infinity)).filter((childX) => !!childX) | ||
// 개선 | ||
//개선1 : children: (children.flat(Infinity)).filter(Boolean) -> 이 경우 0을 falsy로 인식하여 리턴 제외함 | ||
//개선2 : 최종 | ||
children: children | ||
.flat(Infinity) | ||
.filter((childX) => (childX === 0 ? true : Boolean(childX))), // 완전 평탄화 | ||
}; | ||
} |
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,3 +1,38 @@ | ||
export function normalizeVNode(vNode) { | ||
return vNode; | ||
// null, undefined, boolean 값은 빈 문자열로 변환되어야 한다 | ||
if (vNode === null) { | ||
return ""; | ||
} | ||
switch (typeof vNode) { | ||
case "boolean": | ||
case "undefined": | ||
return ""; | ||
|
||
case "number": | ||
case "string": | ||
return `${vNode}`; | ||
|
||
case "function": // vNode가 함수일 경우 해당 함수를 호출해서 반환값을 얻되, 이 반환값이 또 다른 vNode일 가능성이 있으므로 재귀적으로 표준화. | ||
const result = vNode(); | ||
return normalizeVNode(result); // 함수가 반환한 결과 normalize | ||
|
||
default: | ||
|
||
if(typeof vNode.type === "function"){ | ||
|
||
const { type, props, children } = vNode; | ||
// const param = { props: vNode.props , children: vNode.children } | ||
|
||
const resultOfFunction = vNode.type({ ...props, children }); // 함수 호출 | ||
// const resultOfFunction = vNode.type(param); // 이건 왜 안될까... | ||
vNode = normalizeVNode(resultOfFunction); // 결과 표준화 | ||
|
||
} | ||
if (vNode.children && Array.isArray(vNode.children)) { | ||
vNode.children = vNode.children | ||
.map((child) => normalizeVNode(child)) | ||
.filter((childN)=> !!childN) | ||
} | ||
return vNode; | ||
} | ||
} |
Oops, something went wrong.
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.
와 채은님 이 조건문 저한텐 너무 신박한것같아요..! 0이어도 true, 0 아니면 Boolean 타서 truthy 필터 🙊