import classNames from "classnames"; import { callBoth } from "./call-both"; import { mergeStyle } from "./merge-style"; export function attachProps< A extends Record, B extends Record >(rawProps: A, newProps: B): A & B { const props = {}; for (const [propName, propValue] of Object.entries(newProps)) { if (typeof propValue === "function") { // 子节点之前已经有处理函数,则同时调用 if (rawProps[propName]) { props[propName] = callBoth(propValue, rawProps[propName]); } else { props[propName] = propValue; } } else if (propName === "className") { props[propName] = classNames(rawProps.className, propValue); } else if (propName === "style") { props[propName] = mergeStyle(rawProps.style, propValue); } else { props[propName] = propValue; } } return props as A & B; }