Skip to content

Commit 182e351

Browse files
committed
feat: allow setting different layout functions
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent 05e2548 commit 182e351

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/BeautifulTree.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { edgesIterator, postOrderIterator } from './traversal'
22
import type { Tree } from './types'
3+
import type { WrappedTreeWithLayout } from './layouts'
34
import { computeLeftShiftLayout } from './layouts'
45

56
export interface BeautifulTreeProps {
@@ -10,14 +11,18 @@ export interface BeautifulTreeProps {
1011
readonly sizeUnit?: '%' | 'em' | 'px' | 'rem'
1112
}
1213
readonly tree: Tree
14+
readonly computeLayout?: (
15+
tree: Readonly<Tree>,
16+
) => Readonly<WrappedTreeWithLayout>
1317
}
1418

1519
export function BeautifulTree({
1620
id,
1721
svgProps,
1822
tree,
23+
computeLayout = computeLeftShiftLayout,
1924
}: Readonly<BeautifulTreeProps>): JSX.Element {
20-
const { tree: treeWithLayout, maxX, maxY } = computeLeftShiftLayout(tree)
25+
const { tree: treeWithLayout, maxX, maxY } = computeLayout(tree)
2126
const { width, height, sizeUnit = 'px' } = svgProps
2227

2328
const xDivisor = maxX + 2
@@ -34,6 +39,10 @@ export function BeautifulTree({
3439
}}
3540
className={'beautiful-tree-react'}
3641
>
42+
<style>{`
43+
line { stroke: black; }
44+
circle { stroke: black; fill: white; }
45+
`}</style>
3746
{/* TODO: introduce edge "styles" (straight, cornered, curved..., plus CSS styles) */}
3847
{[...edgesIterator(treeWithLayout)].map((edge, idx) => {
3948
return (
@@ -43,11 +52,9 @@ export function BeautifulTree({
4352
y1={((edge.start.y + 1) * height) / yDivisor}
4453
x2={((edge.end.x + 1) * width) / xDivisor}
4554
y2={((edge.end.y + 1) * height) / yDivisor}
46-
stroke="black"
4755
/>
4856
)
4957
})}
50-
5158
{[...postOrderIterator(treeWithLayout)].map((node, idx) => {
5259
const aX = node.meta.abstractPosition.x
5360
const aY = node.meta.abstractPosition.y
@@ -57,8 +64,6 @@ export function BeautifulTree({
5764
className={'beautiful-tree-node'}
5865
cx={((aX + 1) * width) / xDivisor}
5966
cy={((aY + 1) * height) / yDivisor}
60-
stroke="black"
61-
fill="white"
6267
r="5"
6368
/>
6469
)

0 commit comments

Comments
 (0)