@dhmk/dom-utils
A collection of DOM related utils.
list<T>(parent: Element, childFactory: (data: T, index: number) => Child, getKey?: (data: T) => Key): (newData: T[]) => void
type Child<T> = {
node: Element;
update?: (data: T, index: number) => void;
dispose?: () => void;
insert?: (parent: Element, node: Element, ref: Element | null) => void;
remove?: (node: Element) => void;
};
const defaultKey = (x) => ("id" in x ? x.id : x);
Syncs data list with DOM elements.
Usage:
const sync = list(document.getElementById("items"), (data) => {
const node = document.createElement('div')
return {
node,
update(newData, index) {}
dispose() {}
insert(parent, node, refNode) {}
remove(node) {}
}
})
sync([1, 2, 3])
sync([3, 1])