|
| 1 | +/** |
| 2 | + * Represents a tree node (not DOM node) in the data tree. |
| 3 | + * It can have the following properties: |
| 4 | + * |
| 5 | + * id {string|optional} User defined id, comes from `data` passed to the component. |
| 6 | + * _id {string} Internal id, auto generated if `id` is not defined, otherwise set to `id`. |
| 7 | + * rootPrefixId {string} The id of the component's instance. |
| 8 | + * parent {Node} Parent node, if a child. |
| 9 | + * label {string|required} Checkbox label |
| 10 | + * value {string|required} Checkbox value |
| 11 | + * children {array<node>|optional} Array of child nodes |
| 12 | + * checked {bool|optional} Initial state of checkbox. if true, checkbox is selected and corresponding pill is rendered. |
| 13 | + * disabled {bool|optional} Selectable state of checkbox. if true, the checkbox is disabled and the node is not selectable. |
| 14 | + * expanded {bool|optional} If true, the node is expanded (children of children nodes are not expanded by default unless children nodes also have expanded: true). |
| 15 | + * className {string|optional} Additional css class for the node. This is helpful to style the nodes your way |
| 16 | + * tagClassName {string|optional} Css class for the corresponding tag. Use this to add custom style the pill corresponding to the node. |
| 17 | + * actions {array<object>|optional} An array of extra action on the node (such as displaying an info icon or any custom icons/elements) |
| 18 | + * dataset {object|optional} Allows data-* attributes to be set on the node and tag elements |
| 19 | + * isDefaultValue {bool|optional} Indicate if a node is a default value. When true, the dropdown will automatically select the node(s) when there is no other selected node. Can be used on more than one node. |
| 20 | + * |
| 21 | + */ |
| 22 | +export default class Node { |
| 23 | + constructor({ depth, rootPrefixId, parent, index, ...dataProps }) { |
| 24 | + // first copy all props coming from data |
| 25 | + Object.assign(this, dataProps) |
| 26 | + |
| 27 | + // then assign basic ones |
| 28 | + this._depth = depth |
| 29 | + this.rootPrefixId = rootPrefixId |
| 30 | + |
| 31 | + if (parent) { |
| 32 | + this._id = this.id || `${parent._id}-${index}` |
| 33 | + this._parent = parent._id |
| 34 | + parent._children.push(this._id) |
| 35 | + } else { |
| 36 | + this._id = this.id || `${rootPrefixId ? `${rootPrefixId}-${index}` : index}` |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Given an element, generate a DOM Id that's unique across instances |
| 42 | + */ |
| 43 | + getDOMId = element => { |
| 44 | + // if user has defined id, then ensure it's unique across instances |
| 45 | + if (this.id) return `${this.rootPrefixId}_${this.id}_${element}` |
| 46 | + return `${this._id}_${element}` |
| 47 | + } |
| 48 | +} |
0 commit comments