-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: updateAttributes 함수 util 함수로 분리
- Loading branch information
Showing
4 changed files
with
50 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { addEvent, removeEvent } from "../lib/eventManager"; | ||
|
||
/** | ||
* 요소의 속성과 이벤트를 업데이트합니다. | ||
* | ||
* @param {HTMLElement} element - 속성을 업데이트할 DOM 요소. | ||
* @param {Object} oldProps - 이전 속성 객체. | ||
* @param {Object} newProps - 새로운 속성 객체. | ||
*/ | ||
|
||
export const handleUpdateAttr = (target, originNewProps, originOldProps) => { | ||
if (!originOldProps && !originNewProps) return; | ||
|
||
const newProps = originNewProps ?? {}; | ||
const oldProps = originOldProps ?? {}; | ||
|
||
// 속성 제거 처리 | ||
Object.entries(oldProps).forEach(([attr, value]) => { | ||
if (!newProps[attr]) { | ||
if (attr.startsWith("on")) { | ||
const eventType = attr.slice(2).toLowerCase(); | ||
removeEvent(target, eventType, value); | ||
} else { | ||
target?.removeAttribute(attr); | ||
} | ||
} | ||
}); | ||
|
||
// 속성 추가/갱신 처리 | ||
Object.entries(newProps).forEach(([attr, value]) => { | ||
if (oldProps[attr] !== value) { | ||
if (attr.startsWith("on") && typeof value === "function") { | ||
const eventType = attr.slice(2).toLowerCase(); | ||
|
||
if (typeof oldProps[attr] === "function") { | ||
removeEvent(target, eventType, oldProps[attr]); | ||
} | ||
|
||
addEvent(target, eventType, value); | ||
} else { | ||
target.setAttribute(attr === "className" ? "class" : attr, 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,2 +1,3 @@ | ||
export * from "./eventUtils"; | ||
export * from "./timeUtils"; | ||
export * from "./attrUtils"; |