Commit b66e6e4
Customize nodes with JSX views (#103)
* feat: Customize nodes with JSX views
A new 'view' property can now be passed down to graph nodes. This new
property will receive a view, which can include plain HTML or even
React components.
Please, note that this is only available as a low level property in
individual nodes and
cannot be defined in the config object. This feature is mostly thought
for dynamic content, so 'svg' property should be enough to cover any
scenario in which the nodes' content don't change depending on their
inner data.
Also, if the node has values for both 'svg' and 'view' properties,
'view' will always prevail. If we want 'svg' to be applied instead, we
must not assign a value to 'view'.
* fix: Wrapper for node's custom view
Inside of the 'foreignObject' tag, there is now a body tag which will
wrap around the defined custom view. This wrapper has the same dimensions that were set as node size in the
config object.
Because this wrapper has explicit dimensions now, the developer will be
able to use relative sizes (such as percentages) in their custom view's
parent element.
This fixes an issue where the custom component's height
and width were set to 100%, and it failed to retrieve the size for the
'foreignObject' tag. Having a real parent in the embedded HTML solves the problem, as our new view is no longer unable to retrieve its dimensions.
* feat: Custom view sample added to sandbox
The sandbox includes now a new example where the custom view property is
demonstrated.
In this case, each node represents a person and, for every one of them,
an additional set of data is included. The defined custom view displays
all of this data: name, gender and whether they're able to drive a bike
and / or a car.
The values for the 'customView' property are generated dynamically in the
'custom-node.data.js' file.
In order for the user to watch this example, they must enter the sandbox
with the following query param:
{default sandbox url}?data=custom-node
* fix: Error unmounting nodes with custom view in sandbox example
When trying to remove nodes in the sandbox example for the custom view
feature, the application threw the following error.
```
<body> tried to unmount. Because of cross-browser quirks it is
impossible to unmount some top-level components (eg <html>, <head>, and
<body>) reliably and efficiently. To fix this, have a single top-level
component that never unmounts render these elements.
```
This was introduced when I added a body tag as a wrapper for the
embedded code that serves as a custom view. Replacing that tag with a
lower-level one fixed the issue.
* feat: View generator function to customize nodes
The `view` property where we could assign an already generated view to
our nodes has been replaced by a new property `viewGenerator`.
This new property can be defined both at the global configuration object or at node
level, and it will consists on a function that receives a node object and uses its data to return a
view:
```
viewGenerator: (node) => <CustomViewComponent node={node}
```
With the previous method, the node view was not automatically updated
whenever the node data changed. The only way for this to happen was by
manually overriding the value of the the `view` property.
With the current approach, there are a couple of advantages:
* The view generator can be defined in the config object, so we don't
have to set a property for the view in every node. All of the nodes will
share this global setting.
* The library will be in charge of rendering the node custom view
whenever the data changes, as the generator method receives the current
node data as a parameter and returns a view generated using this values. This means the developer doesn't have to worry about updating a view property on each node everytime their data changes, as the view will be automatically updated anytime the Node component `render` method is called.
The sandbox example for this feature has also been updated to reflect the new
changes.
* docs: Fixed datatype for viewGenerator in config docs
* refactor: Inner property to override view generator
When building a node component props, a new property called
`overrideViewGenerator` is added. This property is set to `true` when
there's a svg provided for an specific node which doesn't have its own
view generator. In this case, we assume the svg should have precedence
over the default view generator set in the global settings.
In any other scenario, the view generator will always be applied instead
of the svg. Only when `overrideViewGenerator` is `true` will we assume
the `svg` property to be more important.
Before, this was done by setting the `viewGenerator` property to `null`,
but the new property makes the code more readable.
* refactor: Pass all node data to viewGenerator
Instead of passing only the node information entered by the user via the
`data` prop of the `Graph` component, the `viewGenerator` function will
now receive everything, including auto-generated data such as its
current coordinates in the graph or any values inherited from the
default config object.
* refactor: overrideViewGenerator property renamed to overrideGlobalViewGenerator
* test: buildNodeProps tests updated with new attributes
The `buildNodeProps` related tests were failing due to the new
properties added in this PR. This included both `viewGenerator`,
`overrideGlobalViewGenerator`, as well as other properties inherited
from the `that.node` object.
* test refactor: Implicit values inherited from node
Since some of the recently added values are directly inherited from the
`that.node` data object and `buildNodeProps` does not alter them in any
way, they are no longer hardcoded in the test `toEqual` check.
The object `that.node` contents are added via the spread operator
instead, which will make things easier if the sample data changes at some point.1 parent 1fc9733 commit b66e6e4
File tree
12 files changed
+512
-6
lines changed- sandbox/data/custom-node
- res
- images
- styles
- src/components
- graph
- node
- test/component/graph
12 files changed
+512
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
Loading
Loading
Loading
0 commit comments