"use strict";
const React = require("react");
const asExpression = require("as-expression");
const defaultStyle = require("./style.css");
const useTheme = require("../../util/themeable");
const ListItemContext = require("../../contexts/list-item");
const comparePath = require("../../util/compare-path");
const useClickTimer = require("../../util/use-click-timer");
function Expander({ expanded, onClick }) {
let { withTheme } = useTheme({ control: "list", defaultStyle });
return (
{(expanded === true)
? "▼"
: "▶"}
);
}
function ExpanderPlaceholder() {
let { withTheme } = useTheme({ control: "list", defaultStyle });
return ;
}
module.exports = function ListItem({ label, id, children }) {
let { onClick, onDoubleClick, path, selectedPath } = React.useContext(ListItemContext);
let [ expanded, setExpanded ] = React.useState(true);
let { withTheme } = useTheme({ control: "list", defaultStyle });
let doubleClickTimer = useClickTimer(500);
let depth = path.length;
let indent = depth * 11; // FIXME: Figure out a way to do this with CSS instead, eg. via value exports?
let ownPath = path.concat([ id ]);
let isSelected = comparePath(selectedPath, ownPath);
function handleClick() {
if (doubleClickTimer.click()) {
if (onDoubleClick != null) {
onDoubleClick(ownPath);
} else if (children != null) {
// TODO: Check whether we really only want to do this when no onDoubleClick handler is specified, ie. only as a default behaviour
toggleExpanded();
}
}
}
function handleMouseDown() {
if (doubleClickTimer.mouseDown()) {
onClick(ownPath);
}
}
function toggleExpanded() {
setExpanded((expanded) => !expanded);
}
let style = {
paddingLeft: `${indent}px`
};
return (
{/* FIXME: item_selected capitalization */}
{(children != null)
?
:
}
{label}
{(children != null && expanded === true)
? asExpression(() => {
let itemContext = {
path: ownPath,
selectedPath: selectedPath,
onClick: onClick,
onDoubleClick: onDoubleClick
};
return (
{children}
);
})
: null}
);
};