This document explains how to use the useTree composable to convert between the LinPulsar tree model and the Quasar
q-tree component format.
The useTree composable provides utility functions to translate between:
- The
TreeNode<T>structure used by the application layer - The
QTreeNodestructure expected by Quasar'sq-treecomponent
TreeNode<T> is a generic type where T represents the type of the value field carried by each node.
It ensures consistent node mapping, key handling, and recursive children conversion across the application.
import { useTree } from '@linagora/lin-pulsar-core';
const { toQTreeNodes } = useTree();| Method | Description |
|---|---|
toQTreeNodes |
Converts an array of TreeNode<T> to Quasar-compatible nodes |
Represents a node in the tree structure. The generic parameter T defines the type of the value field.
type TreeNode<T> = {
type: string;
key: string;
value: T;
nodes: TreeNode<T>[];
extraActions?: string[];
};| Field | Type | Description |
|---|---|---|
type |
string |
The type of the node (matches a TreeNodeType) |
key |
string |
Unique identifier within the tree |
value |
T |
The value carried by the node |
nodes |
TreeNode<T>[] |
Child nodes |
extraActions |
string[]? |
Optional additional actions specific to this node |
Describes a node type: its identifier and the default actions available to every node of this type.
type TreeNodeType = {
type: string;
actions?: string[];
selectable?: boolean;
};| Field | Type | Description |
|---|---|---|
type |
string |
The type of the node |
actions |
string[]? |
Default actions of the nodes of this type, merged with their extraActions |
selectable |
boolean? |
Whether the nodes of this type can be selected (true by default) |
Converts an array of TreeNode<T> to an array of Quasar-compatible QTreeNode.
function toQTreeNodes<T>(nodes: TreeNode<T>[]): QTreeNode[];Example with string values:
import type { TreeNode } from '@linagora/lin-pulsar-core';
const nodes: TreeNode<string>[] = [
{ type: 'one', key: 'a', value: 'Element A', nodes: [], extraActions: [] },
{
type: 'two',
key: 'b',
value: 'Element B',
nodes: [],
extraActions: ['import'],
},
];
const qNodes = toQTreeNodes<string>(nodes);
// Result:
// [
// { key: 'a', value: 'Element A', type: 'one', children: [] },
// { key: 'b', value: 'Element B', type: 'two', children: [] },
// ]Example with object values:
import type { TreeNode } from '@linagora/lin-pulsar-core';
type User = { id: number; name: string };
const nodes: TreeNode<User>[] = [
{
type: 'user',
key: 'u-1',
value: { id: 1, name: 'Alice' },
nodes: [],
extraActions: ['edit'],
},
];
const qNodes = toQTreeNodes<User>(nodes);
// Result:
// [
// { key: 'u-1', value: { id: 1, name: 'Alice' }, type: 'user', children: [] },
// ]