Open ScriptyChris opened 3 years ago
@ScriptyChris I would suggest create your own UI components instead of using the built-in UI components.
For example:
// note that you shouldn't use `activeKey` any more, and you need to create your own state
const [activeKeys, setActiveKeys] = useState([]);
const toggleItem = (key) => {
setActiveKeys(keys => {
if(keys.includes(key)) return keys.filter(activeKey => activeKey !== key);
return [...keys, key];
})
}
...
<TreeMenu
data={treeData}
onClickItem={({ key, label, ...props }) => {
toggleItem(key);
}}
{({ items }) => (
<ul>
{items.map(props => (
<li
onClick={props.onClick}
style={{
backgroundColor: activeKeys.includes(props.key) ? 'orange' : 'transparent'
}}>{props.label}</li>
))}
</ul>
)}
</TreeMenu>
Note: I didn't actually run the above code but just demonstrate how it might work 🙂
Thanks for answer, @iannbing. I assume that above approach is cleaner workaround than i did, but it's still workaround, since this library doesn't support multi selection?
Yes, multi-selection is not a built-in feature. But, this component is fully customizable (and this is a perfect example).
The activeKey
prop was designed for supporting keyboard browsing. To support multi-selection, it will require a separate state. I'd see this as a feature request 🙂
Is it possible to select (and deselect) multiple tree items? I can't see any "multiple" alike option/param/prop to be set in neither of internal components config in documentation.
I haven't dug into this library code yet, but if such feature is not currently implemented, would it conceptually be as simple as changing
activeKey
,initialActiveKey
and similar props that track active tree items from string to array along with adjusting logic accordingly?I did a workaround, which in my opinion is dirty: component that uses
<TreeMenu />
keeps track of selected tree items and highlights them outside of library's components control. Is it possible to be done in a simpler or cleaner way?Demo: https://codepen.io/ScriptyChris/pen/jOMZmdV?editors=1010
Code: