[英]How to draw an arrow from a parent div to all children divs?
I have a reusable React list component that can have nested children and I want to draw an arrow from the parent div to all direct children divs like the picture below.我有一个可重用的 React 列表组件,它可以有嵌套的子元素,我想绘制一个从父 div 到所有直接子 div 的箭头,如下图所示。
Here is an example of a nested list component:下面是一个嵌套列表组件的示例:
import React from 'react';
import ListElement from './ListElement.js';
const List = () => (
<>
<ListElement>
<ListElement>
<ListElement>
<ListElement />
<ListElement>
<ListElement />
</ListElement>
</ListElement>
<ListElement />
</ListElement>
<ListElement />
</ListElement>
<ListElement />
</>
);
export default List;
The ListElement component looks something like this: ListElement 组件看起来像这样:
import React from 'react';
const ListElement = props => {
const indentationStyle = { paddingLeft: `${3 * props.indent}rem`,
position: 'relative'};
const lineStyle = {
left: `${2 + 3 * (props.indent - 1.2)}rem`,
};
const tile = (
<div style={indentationStyle}>
{props.indent > 0 ? (
<div className={'arrow-line-container'} style={lineStyle}>
<div className={'arrow-line'}/>
<div className={'curve-arrow-line'}/>
</div>
) : null}
<div
style={{
border: '1px solid black',
padding: '1rem',
marginBottom: '1rem',
}}
>
I am a ListElement
</div>
</div>
);
const getChildren = () => {
let elements = React.Children.toArray(props.children);
// increase indent prop of each child and assign what number child it is in the list
elements = elements.map((element, index) => {
return React.cloneElement(element, {
...element.props,
indent: props.indent + 1,
childNumber: index,
});
});
return elements;
};
const childTiles = <div className={'child-tile'}>{getChildren()}</div>;
const arrowStyle = {
backgroundPosition: `${1.3 + 3 * (props.indent - 1)}rem`,
};
return (
<>
<ul className={'no-bullet'}>
<li
className={props.indent === 0 ? 'no-arrow' : 'arrow'}
style={arrowStyle}
>
{tile}
</li>
{props.children ? childTiles : null}
</ul>
</>
);
};
ListElement.defaultProps = {
childNumber: 0,
indent: 0,
};
export default ListElement;
and the css looks like this: css 看起来像这样:
ul.no-bullet {
list-style-type: none;
padding: 0;
margin: 0;
}
.arrow-line {
border-left: 2px solid #6a6969;
content: "";
position: absolute;
height: 65%;
}
li.arrow {
background: url("./arrow.png") no-repeat;
}
li.no-arrow {
display: block;
}
Currently, I am creating the list with <li>
elements and changing the bullet to an image of the arrow.目前,我正在使用
<li>
元素创建列表并将项目符号更改为箭头的图像。 I want to draw the lines from the parent and attach them to the arrows at each child.我想从父母那里画线并将它们附加到每个孩子的箭头上。 I am struggling with calculating the correct height of the line and the position for the top of the line.
我正在努力计算线的正确高度和线顶部的 position。 Any suggestions are appreciated.
任何建议表示赞赏。
Here is a link to the Plunker: https://plnkr.co/edit/GFrriWfJyeur7MRh?open=Hello.js&deferRun=1&preview这是 Plunker 的链接: https://plnkr.co/edit/GFrriWfJyeur7MRh?open=Hello.js&deferRun=1&preview
I managed to find a solution by only drawing one line at the last child and using offsetTop
and getBoundingClientRect().height
to calculate the position and height of the arrow-line.我设法通过仅在最后一个孩子处绘制一条线并使用
offsetTop
和getBoundingClientRect().height
来计算 position 和箭头线的高度来找到解决方案。 Working solution here: https://plnkr.co/edit/SFzgiZi1dckRa79C此处的工作解决方案: https://plnkr.co/edit/SFzgiZi1dckRa79C
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.