A very minimal tree viewer for electron webviews with virtual dom.
$ npm i electron-tree-view
const root = {
name: 'foo',
children: [{
name: 'bar',
children: [{
name: 'bar',
children: []
}, {
name: 'baz',
children: []
}]
}]
}
const tree = require('electron-tree-view')({
root,
container: document.querySelector('.container'),
children: c => c.children,
label: c => c.name
})
tree.on('selected', item => {
// adding a new children to every selected item
item.children.push({ name: 'foo', children: [] })
tree.loop.update({ root })
console.log('item selected')
})
creates a new tree view. the opts
object can contain:
root
: the root node of the tree data structure. requiredcontainer
: a DOM node to which the tree will be appended. requiredchildren
: by default the program checks for thechildren
property of a tree node to add children, but if it called something else, or you want custom behaviour, then implement thisfunction
that returns the children as anarray
.label
: by default the program checks for thename
property of a tree node to display a text for a node, but if it called something else, or you want custom behaviour, then implement thisfunction
that returns astring
to display.filter
: a function (Child => Boolean
) that can determine which child element to let through to display. This can be used to hide certain children nodes.- overwrite rendering of nodes - see overwriting rendering below.
fires when an item
has been clicked.
fires when an item
has been clicked again and it closed.
selects node
of the tree programatically.
overwrite the rendering of the root element.
hx
template string function used for rendering dom nodeschildren
string containing traversed children
default:
(hx, children) => {
return hx`<div class="tree-view">${children}</div>`
}
notes:
- make sure you include the
tree-view
class
overwrite the rendering of each node.
hx
template string function used for rendering dom nodesdata
data for the current node being renderedchildren
array of children below the current nodeloadHook
hook to setup the click handler properlyclickElem
click handler to attach to the anchorcreateChild
function used to render child nodes
default:
(hx, data, children, loadHook, clickElem, createChild) => {
return hx`<div class="elem" loaded=${loadHook}>
<a href="#" class="header" onclick=${clickElem}>
<div>
${children.length === 0 ? '' : hx`<img src="${__dirname + '/images/chevron.png'}" class="chevron" />`}
<span>${opts.label ? opts.label(data) : data.name}</span>
</div>
</a>
<ul>
${children.map(createChild)}
</ul>
</div>`
}
notes:
- make sure the parent element includes the
loaded
attribute set toloadHook
- make sure a clickable element has
onclick
attribute set toclickElem
- make sure the parent element has the class
elem
- you can't use
opts
here; label function will not be used
overwrite the rendering of a child node.
hx
template string function used for rendering dom nodeschildren
string containing traversed child
default:
(hx, children) => {
return hx`<li>${children}</li>`
}