Skip to content

Commit 28e7b02

Browse files
committed
增加属性: 配置主题色, 相关私有辅助函数优化
1 parent bded526 commit 28e7b02

File tree

8 files changed

+296
-317
lines changed

8 files changed

+296
-317
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Vue.use(SearchTree)
3131
| :-: | :-: | :-: | :-:
3232
| data | 源数据 | Array | 必填项
3333
| node-key | 节点的唯一标识 | String | 'id'
34+
| theme-color | 主题色 | String | '#409eff'
3435
| search | 模糊搜索的关键词 | String | ''
3536
| hide-misses | 是否隐藏模糊搜索不匹配的节点 | Boolean | true
3637
| expand-misses | 是否展开模糊搜索不匹配的节点 | Boolean | false
@@ -83,4 +84,5 @@ Vue.use(SearchTree)
8384
| getTotalOfNodes | 获取所有满足条件的节点数量 | - | Function | 所有满足条件的节点数量
8485

8586
- **`getTotalOfNodes` 使用示例:**
87+
8688
- 获取所有名称包含'北京'的节点: `getTotalOfNodes(node => node.name.indexOf('北京') > -1)`

dist/index.common.js

Lines changed: 129 additions & 146 deletions
Large diffs are not rendered by default.

dist/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.umd.js

Lines changed: 129 additions & 146 deletions
Large diffs are not rendered by default.

dist/index.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/search-node.vue

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default {
3535
</svg>
3636
{
3737
root.showCheckbox && <zCheckbox
38+
theme-color={root.themeColor}
3839
value={data.checked}
3940
disabled={data[disabled]}
4041
indeterminate={data.indeterminate}
@@ -74,33 +75,36 @@ export default {
7475
_upwardUpdateChecked (checked) {
7576
const { data, root } = this
7677
const { children } = root.defaultProps
78+
let oneNodeIsIndeterminate = false, len = data[children].length
7779
// 获取所有选中节点的数量
78-
const checkedNum = data[children].reduce((num, item) => num += +item.checked, 0)
80+
const checkedNum = data[children].reduce((num, item) => {
81+
if (item.indeterminate) oneNodeIsIndeterminate = true
82+
return num += +item.checked
83+
}, 0)
7984
// 所有节点都被选中时checked=true, 反之false
80-
data.checked = checkedNum === data[children].length
81-
//
82-
if (checkedNum === data[children].length) {
83-
data.indeterminate = false
84-
} else {
85-
data.indeterminate = !!checkedNum || !!root._preorder(data[children], item => item.checked)
86-
}
85+
data.checked = checkedNum === len
86+
// 如果全部节点都选中了, 那么indeterminate必然等于false, 否则就看子节点是否有半选节点, 再看子节点是否有0个未选中
87+
data.indeterminate = checkedNum === len ? false : (oneNodeIsIndeterminate || !!checkedNum)
8788
this.$emit('check-change', data.checked)
8889
},
8990
handlerChecked (e) {
9091
const { data, root } = this
9192
const { children, disabled } = root.defaultProps
92-
let checked = !data.checked
9393
if (data[disabled]) return false
94+
let checked = !data.checked
9495
// 过滤所有disabled=false的叶子节点, 如果有没选中的就重写checked
9596
data[children].length && checked && (
9697
checked = !!root._levelOrder(data[children], item => !item[disabled] && !item.checked)
9798
)
99+
// 必须先向下改变状态
98100
root._downwardUpdateChecked(data, checked)
101+
// 再向上逐级传递状态
99102
this.$emit('check-change', checked)
100103
root.$emit('node-checked', e, deepCopy(data))
101104
},
102105
handlerExpand (e) {
103106
const { data, root } = this
107+
if (!data.children.length) return false
104108
data.expand = !data.expand
105109
root.$emit('node-expand', e, deepCopy(data))
106110
}

src/search-tree.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default {
1313
type: String,
1414
default: 'id'
1515
},
16+
themeColor: { // 主题色
17+
type: String,
18+
default: '#409eff'
19+
},
1620
search: { // 模糊搜索关键词
1721
type: String,
1822
default: ''

src/z-checkbox.vue

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@ export default {
44
functional: true,
55
render (h, ctx) {
66
const { props } = ctx
7-
const renderIcon = function (props) {
8-
if (props.indeterminate) return <svg t="1593660826979" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19241" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m42.666666 85.333333v597.333334h597.333334V213.333333H213.333333z m85.333334 256h426.666666v85.333334H298.666667v-85.333334z" p-id="19242" fill="#2080F0"></path></svg>
9-
if (props.value) return <svg t="1593660847492" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19489" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m298.794666 554.666667l301.653334-301.696-60.330667-60.330667-241.322667 241.365333-120.704-120.704-60.330666 60.330667L469.461333 682.666667z" p-id="19490" fill="#2080F0"></path></svg>
10-
return <svg t="1593660598710" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="18740" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m42.666666 85.333333v597.333334h597.333334V213.333333H213.333333z" p-id="18741" fill="#2080f0"></path></svg>
7+
const renderIcon = function (props, color) {
8+
if (props.indeterminate) return <svg t="1593660826979" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19241" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m42.666666 85.333333v597.333334h597.333334V213.333333H213.333333z m85.333334 256h426.666666v85.333334H298.666667v-85.333334z" p-id="19242" fill={color}></path></svg>
9+
if (props.value) return <svg t="1593660847492" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19489" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m298.794666 554.666667l301.653334-301.696-60.330667-60.330667-241.322667 241.365333-120.704-120.704-60.330666 60.330667L469.461333 682.666667z" p-id="19490" fill={color}></path></svg>
10+
return <svg t="1593660598710" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="18740" width="18" height="18"><path d="M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m42.666666 85.333333v597.333334h597.333334V213.333333H213.333333z" p-id="18741" fill={color}></path></svg>
1111
}
12-
return <div class="tree-checkbox" style={{
12+
return <div class={['tree-checkbox', {disabled: props.disabled}]} style={{
1313
filter: `grayscale(${props.disabled ? 100 : 0}%)`,
1414
opacity: props.disabled ? 0.7 : 1
15-
}} onClick={e => ctx.listeners.click(e)}>{renderIcon(props)}</div>
15+
}} onClick={e => ctx.listeners.click(e)}>{renderIcon(props, props.themeColor)}</div>
1616
}
1717
}
1818
</script>
1919

2020
<style scoped>
21-
div {
22-
display: block;
23-
width: 18px;
24-
height: 18px;
25-
cursor: pointer;
26-
margin: 0 0 0 3px;
27-
cursor: pointer;
28-
}
21+
div {
22+
display: block;
23+
width: 18px;
24+
height: 18px;
25+
cursor: pointer;
26+
margin: 0 0 0 3px;
27+
cursor: pointer;
28+
}
29+
.disabled {
30+
cursor: not-allowed;
31+
}
2932
</style>

0 commit comments

Comments
 (0)