Skip to content

Commit b4a3b0a

Browse files
committed
使用函数式组件优化checkbox
1 parent c848ed6 commit b4a3b0a

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

src/search-node.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export default {
5252
}} onClick={e => this.handlerExpand(e)}>
5353
<path d="M151.476947 199.553918l718.53082 0c39.763632 0 71.922053 31.909757 71.922053 71.675436 0 18.485003-7.095605 35.205826-18.486026 47.872311L568.114019 793.227056c-23.810289 31.400151-68.641333 37.993313-100.29731 14.183024-5.570879-4.052293-10.384511-8.873088-14.183024-14.190187L94.235245 314.041416c-23.547299-31.407314-17.217127-76.479859 14.436804-100.041484 12.922311-9.881045 27.864628-14.43885 42.804898-14.43885l0 0L151.476947 199.553918zM151.476947 199.553918" p-id="2223" fill="#c0c4cc"></path>
5454
</svg>
55-
{ root.showCheckbox && <zCheckbox value={data.checked} disabled={data[disabled]} indeterminate={this.indeterminate} onChange={e => this.handlerChecked(e)} class="tree-checkbox point"></zCheckbox> }
55+
{ root.showCheckbox && <zCheckbox
56+
class="point"
57+
value={data.checked}
58+
disabled={data[disabled]}
59+
indeterminate={this.indeterminate}
60+
onClick={e => this.handlerChecked(e)}
61+
></zCheckbox> }
5662
<div class="tree-content point" onClick={e => {
5763
root.expandOnClickNode && this.handlerExpand(e)
5864
root.checkOnClickNode && this.handlerChecked(e)
@@ -105,9 +111,6 @@ export default {
105111
align-items: center;
106112
transform: translate(0);
107113
}
108-
.tree-checkbox {
109-
margin: 0 0 0 4px;
110-
}
111114
.tree-content {
112115
width: 100%;
113116
margin: 0 0 0 3px;

src/z-checkbox.vue

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
<script>
22
export default {
33
name: 'zCheckbox',
4-
props: {
5-
value: Boolean,
6-
indeterminate: Boolean,
7-
disabled: Boolean
8-
},
9-
data() {
10-
return {
11-
isChecked: this.value
12-
}
13-
},
14-
watch: {
15-
value (val) {
16-
this.isChecked = val
17-
}
18-
},
19-
methods: {
20-
renderIcon () {
21-
if (this.indeterminate) return <svg width="12" height="12">
4+
functional: true,
5+
render (h, ctx) {
6+
const { props } = ctx
7+
const renderIcon = function (props) {
8+
if (props.indeterminate) return <svg width="12" height="12">
229
<rect x="0" y="0" width="12" height="12" stroke="#2080f0" fill="#2080f0" stroke-width="2"/>
2310
<rect x="1.5" y="5" width="9" height="2" stroke="#2080f0" fill="#fff" stroke-width="0"/>
2411
</svg>
25-
if (this.isChecked) return <svg width="12" height="12">
12+
if (props.value) return <svg width="12" height="12">
2613
<rect x="0" y="0" width="12" height="12" stroke="#2080f0" fill="#2080f0" stroke-width="2"/>
2714
<line x1="1.5" x2="5.5" y1="5.5" y2="10.5" stroke="#fff" fill="transparent" stroke-width="1.5"/>
2815
<line x1="10.5" x2="5" y1="2" y2="10" stroke="#fff" fill="transparent" stroke-width="1.5"/>
@@ -31,17 +18,10 @@ export default {
3118
<rect x="0" y="0" width="12" height="12" stroke="#2080f0" fill="transparent" stroke-width="2"/>
3219
</svg>
3320
}
34-
},
35-
render () {
36-
const { data, root } = this.$parent
37-
const { nodeKey } = root
3821
return <div style={{
39-
filter: `grayscale(${this.disabled ? 100 : 0}%)`,
40-
opacity: this.disabled ? 0.7 : 1
41-
}} onClick={e => {
42-
this.$emit('input', this.isChecked)
43-
this.$emit('change', e)
44-
}}>{ this.renderIcon() }</div>
22+
filter: `grayscale(${props.disabled ? 100 : 0}%)`,
23+
opacity: props.disabled ? 0.7 : 1
24+
}} onClick={e => ctx.listeners.click(e)}>{renderIcon(props)}</div>
4525
}
4626
}
4727
</script>
@@ -52,6 +32,7 @@ div {
5232
width: 12px;
5333
height: 12px;
5434
cursor: pointer;
35+
margin: 0 0 0 3px;
5536
}
5637
svg {
5738
vertical-align: super;

0 commit comments

Comments
 (0)