Tree
Form integration of the Tree component.
NOTE
The Tree component has already been covered in this part of the documentation. Here we'll only look at the specifics of the form component. For the base usage of tree, please refer to its documentation.
Basic usage
A bt-form-tree
works for the most part just like a bt-tree
. It adds a checkbox for each level to make it selectable.
Selecting a level will select all the sub levels. Here is an example:
As you can see, there are two differences from a normal Tree:
- Like any other form component, it expects either a form control (
control=".."
) or a variable (v-model=".."
) to hold the value. - A
nodes-value
prop is used to define the name of the property to use as value for the form control.
Node value
By default, if nodes-value
is not defined, so the original object describing the node is used as value.
For example:
As you can see, a new value-identifier
prop has been added.
This is because the form control now receive an array of objects as value because no value is defined for nodes-value
.
In our example, we preselect the node Node 1.1.2
by doing:
public selected: any[] = [{id: 4}];
But {id: 4}
is not the same object as the one declared in content ({id: 4, label: 'Node 1.1.2'}
).
This could work though, because the tree does compare the objects deeply, so if we add the label it would work:
public selected: any[] = [{id: 4, label: 'Node 1.1.2'}];
Both objects must be strictly equal though, which is unpractical. And more importantly, it can be very slow if you have many big objects.
That's why value-identifier
exists. You are not forced to use it, but it's a better practice.
Its role is to uniquely identify each node with a
TIP
Like all these kind of props, value-identifier
can take a function that will be called for each node, and that must return the value to use for comparison:
<bt-form-tree [...] :value-identifier="(n) => n.id"></bt-form-tree>
Excluding values
In our previous example, you can see that the whole object is used as a value. Even the children, if the node has some.
What if we only want nodes with no children to contribute to the form's value?
You can control this behavior via nodes-value
. If you say for example, that the property to use as value is value
,
if a node has no value
attribute defined, nothing will be added to the form control when selected. Here is an example:
But as you can see, if we want the label as value like we do above, we have to copy this new value
attribute, so we can differentiate the nodes. Annoying.
You can workaround this issue by giving a function to nodes-value
and test if the node has children.
If not, returning undefined
will prevent the tree from adding a value for this node.
It works the same, but we only have one label in our tree 🙂.
Props
Method | Description | Default |
---|---|---|
Bidirectional binding for the value of the control. | undefined | |
The original value given by the end-user through the html element. | undefined | |
The label of the field. Overridden by the default slot. | null | |
A placeholder value to show when there is no value selected. | null | |
A help text to show to the user. Overridden by the help slot. | null | |
If true the errors will appear as an icon on the right side of the input that shows a popover.This value is overridden to true internally if the control is in a group to preserve layout integrity. | true | |
If true the help text will appear as an icon on the right side of the input that shows a popover.This value is overridden to true internally if the control is in a group to preserve layout integrity. | false | |
If true , a little asterisk extra is shown, indicating to the user that the field is mandatory. | false | |
For external control of the disabled state of the control. | false | |
For external control of the busy state of the control. | false | |
If true , show the debug overlay. | false | |
HTML tab index (for keyboard navigation). | 0 | |
If true , the control will try to gain focus when mounted. | false | |
Defines how to resolve the nodes' value. Can be:
| null | |
Define the name of the property (or get the value if a function) to use as identifier for the value. Required if the value is not a primitive because a primitive is required to preselect items of the tree from the value of the FormControl . | null |
Slots
Name | Description |
---|---|
default or label | Defines the label of the field. This slot takes priority over the Props: none |
help | Defines the help text of the field. This slot takes priority over the Props: none |
node | Slot that is rendered for each node of the tree. Props:
|
Theming
Below the list of all CSS variables and selectors available to customize the appearance of the component. If you're not familiar on how the theming works, please refer to the VueThemes documentation.
Variables
Variable | Default | Comment |
---|---|---|
fontFamily | var(--bt-font-family-base) | - |
fontSize | var(--bt-font-size-base) | - |
fontWeight | var(--bt-font-weight-normal) | - |
textColor | var(--bt-text-color-base) | - |
helpTextColor | var(--bt-text-color-light) | - |
helpFontSize | var(--bt-font-size-sm) | - |
errorTextColor | var(--bt-color-red-500) | - |
errorFontSize | var(--bt-font-size-sm) | - |
disabledOpacity | 0.5 | - |
addonBackgroundColor | var(--bt-color-gray-50) | - |
addonTextColor | var(--bt-text-color-light) | - |
Selectors
Name | Comment |
---|---|
root | The root container. |
label | The container of the label. |
help | The container of the help text. |
inputGroup | A container that only include the form field. |
input | The actual form control. |
item | The individual container of each node. |
itemsWrapper | The container a single level of the tree. |
itemTitle | Title of a node. |
itemAddon | The icon that appear on the left of a node. |
Examples:
cssSelectors: {
item: {
background: '...'
},
'item:hover': {
background: '...'
}
}