-
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
[5팀 박소미] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #49
Open
confidential-nt
wants to merge
7
commits into
hanghae-plus:main
Choose a base branch
from
confidential-nt:confidential-nt
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.
+338
−25
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23120fa
feat: createVNode 구현
confidential-nt fbf6b05
feat: normalizeVNode 구현
confidential-nt 32b80ad
feat: createElement 구현
confidential-nt 70ee566
feat: eventManager 구현
confidential-nt f8bb333
feat: renderElement 구현
confidential-nt 18ffc9e
feat: updateElement 구현
confidential-nt 9d30226
feat: 심화 과제 구현
confidential-nt 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,57 @@ | ||
/** @jsx createVNode */ | ||
import { createVNode } from "../../lib"; | ||
import { globalStore } from "../../stores/globalStore.js"; | ||
|
||
export const PostForm = () => { | ||
return ( | ||
<div className="mb-4 bg-white rounded-lg shadow p-4"> | ||
<textarea | ||
id="post-content" | ||
placeholder="무슨 생각을 하고 계신가요?" | ||
className="w-full p-2 border rounded" | ||
/> | ||
<button | ||
id="post-submit" | ||
className="mt-2 bg-blue-600 text-white px-4 py-2 rounded" | ||
> | ||
게시 | ||
</button> | ||
</div> | ||
); | ||
const { loggedIn, currentUser } = globalStore.getState(); | ||
const { setState } = globalStore; | ||
|
||
let postContent = ""; | ||
|
||
const handleTextareaChange = (e) => { | ||
postContent = e.target.value; // 텍스트를 업데이트 | ||
}; | ||
|
||
const handlePostSubmit = () => { | ||
if (!postContent.trim()) { | ||
alert("내용을 입력해주세요!"); | ||
return; | ||
} | ||
|
||
const newPost = { | ||
id: Date.now(), // 고유 ID | ||
author: currentUser.username, // 로그인한 사용자 | ||
time: Date.now(), // 현재 시간 | ||
content: postContent.trim(), // 작성된 내용 | ||
likeUsers: [], // 좋아요 초기화 | ||
}; | ||
|
||
// 상태 업데이트 | ||
const updatedPosts = [newPost, ...globalStore.getState().posts]; | ||
setState({ posts: updatedPosts }); | ||
|
||
// 텍스트 초기화 | ||
document.querySelector("#post-content").value = ""; | ||
postContent = ""; | ||
}; | ||
|
||
if (loggedIn) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그인의 여부를 컴포넌트보다 페이지 단위에서 아는것이 더 낫지 않을까? 생각하는데 어떠실까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HomePage에서 글로벌 스토어를 가져와서 로그인 상태를 통해 렌더링하는 방식으로
|
||
return ( | ||
<div className="mb-4 bg-white rounded-lg shadow p-4"> | ||
<textarea | ||
id="post-content" | ||
placeholder="무슨 생각을 하고 계신가요?" | ||
className="w-full p-2 border rounded" | ||
onChange={handleTextareaChange} | ||
/> | ||
<button | ||
id="post-submit" | ||
className="mt-2 bg-blue-600 text-white px-4 py-2 rounded" | ||
onClick={handlePostSubmit} | ||
> | ||
게시 | ||
</button> | ||
</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 |
---|---|---|
@@ -1,5 +1,48 @@ | ||
import { addEvent } from "./eventManager"; | ||
|
||
export function createElement(vNode) {} | ||
export function createElement(vNode) { | ||
if (typeof vNode === "function") { | ||
throw Error("컴포넌트는 인자로 올 수 없습니다."); | ||
} | ||
if (vNode == null || typeof vNode === "boolean") { | ||
return document.createTextNode(""); | ||
} | ||
|
||
function updateAttributes($el, props) {} | ||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return document.createTextNode(String(vNode)); | ||
} | ||
|
||
if (Array.isArray(vNode)) { | ||
const fragment = document.createDocumentFragment(); | ||
vNode.forEach((child) => { | ||
fragment.appendChild(createElement(child)); // 자식 노드들을 재귀적으로 추가 | ||
}); | ||
return fragment; | ||
} | ||
|
||
const { type, props, children } = vNode; | ||
|
||
const element = document.createElement(type); | ||
|
||
if (props) { | ||
updateAttributes(element, props); | ||
} | ||
|
||
children.forEach((child) => { | ||
element.appendChild(createElement(child)); | ||
}); | ||
|
||
return element; | ||
} | ||
|
||
function updateAttributes($el, props) { | ||
Object.entries(props).forEach(([key, value]) => { | ||
if (key.startsWith("on")) { | ||
addEvent($el, key.slice(2).toLowerCase(), value); | ||
} else if (key === "className") { | ||
$el.setAttribute("class", value); | ||
} else { | ||
$el.setAttribute(key, value); | ||
} | ||
}); | ||
} |
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,11 @@ | ||
export function createVNode(type, props, ...children) { | ||
return {}; | ||
const flatChildren = children | ||
.flat(Infinity) | ||
.filter((child) => child || child === 0); | ||
|
||
return { | ||
type, | ||
props, | ||
children: flatChildren, | ||
}; | ||
} |
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,81 @@ | ||
export function setupEventListeners(root) {} | ||
// addEvent와 removeEvent를 통해 element에 대한 이벤트 함수를 어딘가에 | ||
// 저장하거나 삭제합니다. | ||
|
||
export function addEvent(element, eventType, handler) {} | ||
// setupEventListeners를 이용해서 이벤트 함수를 가져와서 | ||
// 한 번에 root에 이벤트를 등록합니다. | ||
|
||
export function removeEvent(element, eventType, handler) {} | ||
// 이벤트 저장 | ||
const eventStorage = {}; | ||
// const eventStorage = new Map(); | ||
|
||
export function setupEventListeners(root) { | ||
Object.keys(eventStorage).forEach((eventType) => { | ||
root.addEventListener(eventType, eventHandlers); | ||
}); | ||
} | ||
// export function setupEventListeners(root) { | ||
// eventStorage.forEach((handlerMap, eventType) => { | ||
// root.addEventListener(eventType, eventHandlers); | ||
// }); | ||
// } | ||
|
||
export function addEvent(element, eventType, handler) { | ||
if (!eventStorage[eventType]) { | ||
eventStorage[eventType] = new Map(); | ||
} | ||
|
||
const eventsMap = eventStorage[eventType]; | ||
eventsMap.set(element, handler); | ||
|
||
// if (!eventStorage.has(eventType)) { | ||
// eventStorage.set(eventType, new Map()); | ||
// } | ||
|
||
// const handlerMap = eventStorage.get(eventType); | ||
// handlerMap.set(element, handler); | ||
} | ||
|
||
export function removeEvent(element, eventType) { | ||
if (eventStorage[eventType]) { | ||
const eventsMap = eventStorage[eventType]; | ||
eventsMap.delete(element); | ||
|
||
if (eventsMap.size === 0) { | ||
delete eventStorage[eventType]; | ||
} | ||
} | ||
} | ||
// export function removeEvent(element, eventType) { | ||
// if (eventStorage[eventType]) { | ||
// const handlerMap = eventStorage[eventType]; | ||
// handlerMap.delete(element); | ||
|
||
// if (handlerMap.size === 0) { | ||
// delete eventStorage[eventType]; | ||
// } | ||
// } | ||
// } | ||
|
||
const eventHandlers = (e) => { | ||
if (!eventStorage[e.type]) { | ||
return; | ||
} | ||
const handlerGroup = eventStorage[e.type]; | ||
const handler = handlerGroup.get(e.target); | ||
|
||
if (handler) { | ||
handler(e); | ||
} | ||
}; | ||
|
||
// const eventHandlers = (e) => { | ||
// if (!eventStorage[e.type]) { | ||
// return; | ||
// } | ||
// const handlerMap = eventStorage[e.type]; | ||
// const handler = handlerMap.get(e.target); | ||
|
||
// if (handler) { | ||
// handler(e); | ||
// } | ||
// }; |
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,32 @@ | ||
export function normalizeVNode(vNode) { | ||
if (vNode == null || typeof vNode === "boolean") { | ||
return ""; | ||
} | ||
|
||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return String(vNode); | ||
} | ||
|
||
if (typeof vNode.type === "function") { | ||
return normalizeVNode( | ||
vNode.type({ children: vNode.children, ...vNode.props }), | ||
); | ||
} | ||
|
||
if (typeof vNode === "object") { | ||
const { type, props, children } = vNode; | ||
|
||
const normalizedChildren = children | ||
.flat(Infinity) | ||
.map(normalizeVNode) | ||
.filter((child) => child !== "" && child != null); | ||
|
||
return { | ||
type, | ||
props: props, | ||
children: normalizedChildren, | ||
}; | ||
} | ||
|
||
return vNode; | ||
} |
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
Oops, something went wrong.
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.
리액트 렌더링 알고리즘에 따르면 텍스트 초기화를 element를 직접 조작하는것보다 state를 통해 관리하는 방식이 어떨까? 제안드려봅니다 ㅎㅎ