17.1.2 • Published 5 days ago

@cluetec/ngcx-tree v17.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 days ago

A reusable tree component for Angular based on the CDK Tree and the CDK Drag n Drop features.

Includes only peer dependencies to angular cdk, no other library included. So the lib size is pretty small.

Demo:

https://cluetec.github.io/ngcx-tree-demo

Feature Overview

  • Highly custumizable tree component
    • Configure your Angular Template for node rendering
    • Or configure your own component for node rendering
  • Selection state
    • configure which nodes are selectable
  • Drag and Drop
    • configure which nodes may be moved
    • configure where a node can be dropped
  • Events (always with detailed information about parents, next or prev. nodes):
    • On move
    • On click
    • On select/unselect
    • custom event may be emitted by custom component

Missing features? Let me know :)

Table of Content

Getting Started

  1. Install the library:
npm install @cluetec/ngcx-tree
  1. Import the component. Since it is standalone, either add it directly to another standlone component or import it into your existing NgModule:
import { NgcxTreeComponent } from '@cluetec/ngcx-tree';

@Component({
  standalone: true,
  imports: [NgcxTreeComponent],
})
import { NgcxTreeComponent } from '@cluetec/ngcx-tree';

@NgModule({
  declarations: [AppComponent],
  imports: [NgcxTreeComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}
<ngcx-tree [nodes]="nodes"></ngcx-tree>

Prerequisites

You need at least angular 16 to use the tree.

Inputs

PropertyTyperequiredDescription
nodesNgcxTreeNode[]nolist of nodes to show in the tree
configNgcxTreeConfignoused to render the node when no custom template or component is set

Outputs

Propertyevent content typeDescription
nodeMovedNgcxTreeNodeMovedEventfired when a node is moved
customEventanymay be fired by your own custom component
clickEventNgcxTreeNodeWrapperfired when node is clicked
selectEventNgcxTreeNodeWrapperfired when node is selected or un-selected. Clicking a selected node un-selects the node.

Model

NgcxTreeConfig

The component includes a model called NgcxTreeConfig with some basic optional settings.

  • allowDrag method that decides if a node can be dragged: (node: NgcxTreeNodeWrapper<T>) => boolean - all nodes are draggable by default
  • allowDrop method that decides if node can be dropped into another node (node: NgcxTreeNodeWrapper<T>, intoNode?: NgcxTreeNodeWrapper<T>) => boolean - every node may be draggable everywhere by default.
  • preventDropReason method that decides if node can be dropped into another node (node: NgcxTreeNodeWrapper<T>, intoNode?: NgcxTreeNodeWrapper<T>) => string - every node may be draggable everywhere by default. If a non empty string is returned, then the node cannot be dropped and the string will be displayed. One of the methods allowDrop or preventDropReason is sufficient to prevent the node to be dropped.
  • allowSelection method that decides if node can be selected (node: NgcxTreeNodeWrapper<T>) => boolean - nodes are not selectable by default
  • treeNodeContentTemplate Angular TemplateRef that will be used to render a node let-nodeWrapper="nodeWrapper" may be used to access the node wrapper to render the node
  • treeNodeContentComponent Angular Component that will be used to render a node. (use treeNodeContentComponent or treeNodeContentTemplate, but not both). see NgcxCustomComponent

NgcxTreeNode

If no data is passed to the component, it will simply display some mock data. Data is provided to the tree in the following format:

PropertyTyperequiredDescription
idstringyesnecessary unique id of the node
titlestringnoused to render the node when no custom template or component is set
faIconstringnofont awesome icon used to render the node when no custom template or component is set. You must include fontawesome on your own if you want
childrenNgcxTreeNode[]nochildren of the node

NgcxTreeNodeWrapper

Generic T is the same as the elements of the input nodes.

PropertyTypeDescription
idstringsame as NgcxTreeNode.id
dataTdata of the input nodes nodes
depthnumberdepth in the tree starting with 0
indexnumberindex of the node in it's parent
isSelectablebooleanif the node is selectable. Depending on the config.allowSelection method. (default: false)
isFirstChildbooleanis first node from the same parent
isLastChildbooleanis last node from the same parent
childrenNgcxTreeNodeWrapper[]list of children wrappers around the original nodes
parentNgcxTreeNodeWrapper | undefinedparent node
nextNgcxTreeNodeWrapper | undefinednode after this node in same parent, if one exists.
previousNgcxTreeNodeWrapper | undefinednode before this node in same parent, if one exists.

NgcxTreeNodeMovedEvent

PropertyTypeDescription
nodeNgcxTreeNodeWrapperthe moved node
parentNgcxTreeNodeWrapper | undefinedmoved into this parent node
afterNodeNgcxTreeNodeWrapper | undefinedmoved to position after this node
beforeNodeNgcxTreeNodeWrapper | undefinedmoved to position before this node

NgcxCustomComponent

Your component can implement this interface and can be set as Type<NgcxCustomComponent<T>> in the config.treeNodeContentComponent input.

Input

nodeWrapper the input to render the node. Type: NgcxTreeNodeWrapper

Output

customEvent EventEmitter<any> can be used to trigger the output 'customEvent'

TreeControl - Api

Access api like this

import { ViewChild, Component } from '@angular/core';
import { NgcxTreeComponent, NgcxTreeNode } from '@cluetec/ngcx-tree';

@Component({
  selector: 'app-expand-tree-sample',
  template: ```
  <button (click)="expandAll()"></button>
  <ngcx-tree #tree [nodes]="nodes"></ngcx-tree>
  ```,
  standalone: true,
  imports: [NgcxTreeComponent],
})
export class ExpandTreeSampleComponent {
  nodes = [];
  @ViewChild('tree', { static: false })
  ngcxTree: NgcxTreeComponent<NgcxTreeNode>;

  expandAll(): void {
    this.ngcxTree.treeControl.expandAll();
  }
}

treeControl

The treeControl extends the treeControl from Angular CDK (NestedTreeControl<NgcxTreeNodeWrapper<T>, string>) and can mainly be used to expand and collapse nodes.

Additional Helper methods

selectNodeById

Can be called to select a node by id. the selectEvent event is fired afterwards.

findNodeById

Can be used to get the NgcxTreeNodeWrapper<T> for an id. returns undefined if no node is available for the id.

Styling

Include Styles

styles.scss and styles.css contains all the parts described below in one file:

@import 'node_modules/@cluetec/ngcx-tree/styles/styles';

CSS Variables

Css Variables may be defined to customize the Look and feel like this:

  --ngcx-tree-color-no-drop: #ff0000;
Variable nameDescription
ngcx-tree-color-no-dropColor which is shown at the drop box dashed border or line to indicate that drop is not possible here. You may set to transparent if you don't want the border in this case

Common styling

you should set the width of cdk-drop-list to auto, otherwise, the node content may be on wrong place:

.ngcx-tree .cdk-drop-list {
  width: auto;
}

Or Include this:

@import 'node_modules/@cluetec/ngcx-tree/styles/ngcx-common';

Dotted tree lines

Import or copy the scss to show tree lines:

@import 'node_modules/@cluetec/ngcx-tree/styles/ngcx-doted-tree-line';

Selection highlighting

Import or copy the scss to show some selection styling:

@import 'node_modules/@cluetec/ngcx-tree/styles/ngcx-selection';

Icon color

Import or copy the scss to set the color of the node icon:

@import 'node_modules/@cluetec/ngcx-tree/styles/ngcx-icon-color';

Prevent drop reason tooltip

Import or copy the scss to style the tooltip, shown when config.preventDropReason is used:

@import 'node_modules/@cluetec/ngcx-tree/styles/ngcx-tooltip';

Font Awesome

Font Awesome is not included here, but to show icons for the nodes you may include font-awesome on your own and may use the node.faIcon property to set the icon.

Include like this: projects/ngcx-tree/stories/styles/styles.scss

Selection

Selected node can be styled like this:

.tree-node-content-container.selected .tree-node-content {
  background-color: #555;
  padding-left: 5px;
}

Hover effect on selectable node:

.ngcx-tree:not(.dragging)
  .tree-node-content-container.is-selectable:hover
  .tree-node-content {
  background-color: #fbfbfb;
}

Remove Selection css on dragging element:

.cdk-drag-preview .tree-node-content-container.selected .tree-node-content {
  background-color: inherit;
}

Simple Sample

import { Component } from '@angular/core';
import { NgcxTreeComponent } from '@cluetec/ngcx-tree';

@Component({
  selector: 'app-simple-tree-sample',
  template: '<ngcx-tree [nodes]="nodes"></ngcx-tree>',
  standalone: true,
  imports: [NgcxTreeComponent],
})
export class SimpleTreeSampleComponent {
  nodes = [
    {
      id: 'fru',
      title: 'Fruit',
      children: [
        {
          id: 'app',
          title: 'Apple',
        },
        {
          id: 'ban',
          title: 'Banana',
        },
      ],
    },
    {
      id: 'fish',
      title: 'Fish',
    },
  ];
}

Contributions

Contributions and improvement suggestions are always welcome!

Samples

You can run Storybook and see the samples there.

  1. npm run build
  2. npm run storybook
17.1.2

5 days ago

17.1.1

8 days ago

17.1.0

8 days ago

17.0.2

2 months ago

17.0.1

2 months ago

17.0.0

5 months ago

16.0.2

6 months ago

16.0.1

6 months ago

16.0.0

7 months ago

1.0.0

7 months ago

0.1.0-angular-15

8 months ago

0.1.0

8 months ago