Skip to content

Commit

Permalink
fix: treat node with empty children as leaf (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhao authored Oct 22, 2022
1 parent fdaaacd commit 08ddeaa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Node/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,27 @@ describe('<Node />', () => {
).toBe(data.__rd3t.id);
});

it('applies correct base className if `data.children` is defined', () => {
it('applies correct base className if `data.children` is defined and not empty', () => {
const leafNodeComponent = shallow(<Node {...mockProps} />);
const nodeComponent = shallow(<Node {...mockProps} data={{ ...data, children: [] }} />);
const leafNodeComponentWithEmptyChildren = shallow(
<Node {...mockProps} data={{ ...data, children: [] }} />
);
const nodeComponent = shallow(
<Node {...mockProps} data={{ ...data, children: [{ name: 'leaf' }] }} />
);

expect(
leafNodeComponent
.find('g')
.first()
.prop('className')
).toBe('rd3t-leaf-node');
expect(
leafNodeComponentWithEmptyChildren
.find('g')
.first()
.prop('className')
).toBe('rd3t-leaf-node');
expect(
nodeComponent
.find('g')
Expand Down
7 changes: 6 additions & 1 deletion src/Node/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ export default class Node extends React.Component<NodeProps, NodeState> {
this.nodeRef = n;
}}
style={this.state.initialStyle}
className={[data.children ? 'rd3t-node' : 'rd3t-leaf-node', nodeClassName].join(' ').trim()}
className={[
data.children && data.children.length > 0 ? 'rd3t-node' : 'rd3t-leaf-node',
nodeClassName,
]
.join(' ')
.trim()}
transform={this.state.transform}
>
{this.renderNodeElement()}
Expand Down

0 comments on commit 08ddeaa

Please sign in to comment.