javascript - 在逐个节点的基础上设计 react-treebeard
问题描述
我react-treebeard
在我的react.js
项目中有。
当我尝试创建时,<TreeBeard />
我提供data
,onToggle
和style
具有适当值的属性(根据文档)。
但是我找不到如何逐个节点设置元素样式的方法。
例如,我有一棵这样的树:
ParentNode
-> ChildNode1
-> ChildNode2
-> ChildNode3
这是我的代码:
import React, { useState } from 'react'
import { Treebeard } from 'react-treebeard'
const MyComponent = ({ data }) => {
const [treeData, setTreeData] = useState(data)
const [cursor, setCursor] = useState(false)
const onToggle = (node, toggled) => {
if (cursor) {
cursor.active = false
}
node.active = true
if (node.children) {
node.toggled = toggled
}
setCursor(node)
setTreeData(Object.assign({}, data))
}
return <Treebeard
style={treeStyle}
data={treeData}
onToggle={onToggle} />
}
export default MyComponent
const treeStyle = {
tree: {
base: {
listStyle: 'none',
backgroundColor: 'white',
margin: 0,
padding: 0,
color: 'rgb(35,31,32)',
fontFamily: '"Helvetica Neue", "Open Sans", Arial, sans-serif',
fontSize: '1.3rem'
},
node: {
base: {
position: 'relative'
},
link: {
cursor: 'pointer',
position: 'relative',
padding: '0px 5px',
display: 'block'
},
activeLink: {
background: 'rgba(0, 0, 0, 0.04)'
},
toggle: {
base: {
position: 'relative',
display: 'inline-block',
verticalAlign: 'top',
marginLeft: '-5px',
height: '24px',
width: '24px'
},
wrapper: {
position: 'absolute',
top: '50%',
left: '50%',
margin: '-7px 0 0 -7px',
height: '14px'
},
height: 14,
width: 14,
arrow: {
fill: 'rgb(35,31,32)',
strokeWidth: 0
}
},
header: {
base: {
display: 'inline-block',
verticalAlign: 'top',
color: 'rgb(35,31,32)'
},
connector: {
width: '2px',
height: '12px',
borderLeft: 'solid 2px black',
borderBottom: 'solid 2px black',
position: 'absolute',
top: '0px',
left: '-21px'
},
title: {
lineHeight: '24px',
verticalAlign: 'middle'
}
},
subtree: {
listStyle: 'none',
paddingLeft: '19px'
},
loading: {
color: '#E2C089'
}
}
}
}
默认情况下,他们共享相同的样式,我想要的是为每个孩子设置不同的样式(例如不同的颜色/fontWeight/等)。
如果没有某种黑客,这件事甚至可能吗?如果可能的话,我该怎么做?
解决方案
尝试这个:
Treebeard.defaultProps = {
style: {
tree: {
base: {
listStyle: "none",
backgroundColor: "white",
margin: 0,
padding: 0,
color: "#00ff00",
fontFamily: "lucida grande ,tahoma,verdana,arial,sans-serif",
fontSize: "14px",
height: "100%",
width: "100%",
},
node: {
base: {
position: "relative",
},
link: {
cursor: "pointer",
position: "relative",
padding: "0px 5px",
display: "block",
},
activeLink: {
background: "#31363F",
},
toggle: {
base: {
position: "relative",
display: "inline-block",
verticalAlign: "top",
marginLeft: "-5px",
height: "24px",
width: "24px",
},
wrapper: {
position: "absolute",
top: "50%",
left: "50%",
margin: "-7px 0 0 -7px",
height: "14px",
},
height: 14,
width: 14,
arrow: {
fill: "#9DA5AB",
strokeWidth: 0,
},
},
header: {
base: {
display: "inline-block",
verticalAlign: "top",
color: "#9DA5AB",
},
connector: {
width: "2px",
height: "12px",
borderLeft: "solid 2px black",
borderBottom: "solid 2px black",
position: "absolute",
top: "0px",
left: "-21px",
},
title: {
lineHeight: "24px",
verticalAlign: "middle",
},
},
subtree: {
listStyle: "none",
paddingLeft: "19px",
},
loading: {
color: "#E2C089",
},
},
},
},
};
推荐阅读
- uno - 创建具有给定上下文的实例
- typescript - 与非空对象很好地相交的空对象类型
- javascript - 如何通过 react-query 在客户端显示服务器端验证消息?
- python - 如何呈现用户复选框,然后将列表保存到选中的复选框?
- vba - 如何在VBA中仅替换字符串起始行
- python - 尝试在 flat_map 中加载数据集时出现错误“TypeError:预期的 str、字节或 os.PathLike 对象,而不是张量”
- mongodb - Mongoose:从一堆文件中更新一个文件
- angular - 主页中的控制组件(Angular/Ionic)
- python - 我正在尝试使用 pysimplegui,但我无法重新运行窗口
- c - 为什么使用gets()函数的返回值会出现分段错误?