Skip to content

Commit 25064ed

Browse files
author
luchunyu
committed
fix(datepicker, table, checkbox): update datepicker, table, checkbox for more using
datepicker add shortcut , checkbox group add slot, table add td span setting
1 parent c77a129 commit 25064ed

9 files changed

Lines changed: 501 additions & 136 deletions

File tree

src/components/checkbox/checkbox-group.vue

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<template lang="pug">
22
div.c-checkbox-group
3-
c-checkbox(
4-
v-for="(option, index) in optionList"
5-
v-model="isChecked[index]"
6-
:key="index"
7-
:label="option.label"
8-
:disabled="option.disabled"
9-
@change="onItemChange($event, index)"
10-
)
3+
slot
4+
c-checkbox(
5+
v-for="option in optionList"
6+
:key="option.label"
7+
:label="option.value"
8+
:disabled="option.disabled"
9+
) {{ option.label }}
1110
em.c-error-msg(v-if="!validity.valid") {{validity.msg}}
1211
</template>
1312

@@ -42,7 +41,7 @@ const props = {
4241
maxItems: Number,
4342
options: {
4443
type: Array,
45-
required: true,
44+
required: false,
4645
default () { return [] }
4746
}
4847
}
@@ -54,12 +53,17 @@ export default {
5453
},
5554
props,
5655
mixins: [validatable],
56+
provide () {
57+
return {
58+
'$checkboxGroup': this
59+
}
60+
},
5761
inject: {
5862
$form: { default: null }
5963
},
6064
data () {
6165
return {
62-
isChecked: []
66+
checkedValues: []
6367
}
6468
},
6569
computed: {
@@ -87,28 +91,26 @@ export default {
8791
minItems: minItems.bind(this),
8892
maxItems: maxItems.bind(this)
8993
})
90-
this.updateChecked()
91-
this.$watch('options', this.updateChecked)
92-
this.$watch('value', this.updateChecked)
94+
this.onPropChange()
95+
this.$watch('options', this.onPropChange)
96+
this.$watch('value', this.onPropChange)
9397
},
9498
methods: {
95-
updateChecked () {
96-
const isChecked = this.optionList.map(option => {
97-
return this.value.indexOf(option.value) > -1
98-
})
99-
this.isChecked = isChecked
99+
onPropChange () {
100+
// todo check options
101+
this.checkedValues = [...this.value]
100102
},
101103
102-
onItemChange (checked, index) {
103-
const isChecked = [...this.isChecked]
104-
isChecked[index] = checked
105-
106-
const checkedValues = this.optionList
107-
.filter((_, i) => isChecked[i])
108-
.map(option => option.value)
109-
110-
this.$emit('change', checkedValues)
104+
updateCheckedValues (isChecked, value) {
105+
if (isChecked) {
106+
this.checkedValues.push(value)
107+
} else {
108+
const index = this.checkedValues.indexOf(value)
109+
this.checkedValues.splice(index, 1)
110+
}
111+
this.$emit('change', this.checkedValues)
111112
}
113+
112114
}
113115
}
114116
</script>

src/components/checkbox/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,37 @@ route: /component/checkbox
8080
</script>
8181
```
8282

83+
## 多选框组自定义内容
84+
85+
```html
86+
<c-checkbox-group
87+
v-model="selected"
88+
>
89+
<c-checkbox label="a" >选项1</c-checkbox>
90+
<c-checkbox label="选项2" ></c-checkbox>
91+
<c-checkbox label="选项3" ></c-checkbox>
92+
</c-checkbox-group>
93+
94+
<p>你选择了 {{ selected }}</p>
95+
96+
<c-button primary @click="resetSelected">重置</c-button>
97+
98+
<script>
99+
export default {
100+
data () {
101+
return {
102+
selected: ["选项2"]
103+
}
104+
},
105+
methods: {
106+
resetSelected () {
107+
this.selected = []
108+
}
109+
}
110+
}
111+
</script>
112+
```
113+
83114
### indeterminate 状态
84115

85116
`indeterminate` 通常用于展示一组**未全部被选中**(部分选中)的多选框组状态。

src/components/checkbox/index.vue

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
type="checkbox"
88
:name="name"
99
:disabled="disabled"
10-
:checked="value"
10+
:checked="checked"
1111
ref="input"
1212
)
1313
span.c-checkbox__box
14-
span.c-checkbox__label {{ label }}
14+
span.c-checkbox__label
15+
slot {{ label }}
1516
</template>
1617

1718
<script>
@@ -24,7 +25,7 @@ const name = 'c-checkbox'
2425
const props = {
2526
value: Boolean,
2627
name: String,
27-
label: String,
28+
label: [String, Number, Object],
2829
disabled: Boolean,
2930
size: String,
3031
indeterminate: Boolean
@@ -37,31 +38,60 @@ export default {
3738
},
3839
props,
3940
inject: {
40-
$form: { default: null }
41+
$form: { default: null },
42+
$checkboxGroup: { default: null }
4143
},
4244
mixins: [resettable, validatable],
45+
data () {
46+
return {
47+
checked: false
48+
}
49+
},
4350
computed: {
4451
classNames () {
4552
const { size, $form } = this
4653
const actualSize = size || ($form && $form.size)
4754
return actualSize ? `is-${actualSize}` : ''
55+
},
56+
groupCheckedValues () {
57+
return this.$checkboxGroup ? this.$checkboxGroup.checkedValues : []
4858
}
4959
},
5060
watch: {
61+
value: {
62+
handler (val) {
63+
this.checked = val
64+
},
65+
immediate: true
66+
},
5167
indeterminate (newVal) {
5268
if (this.$refs.input) {
5369
this.$refs.input.indeterminate = Boolean(newVal)
5470
}
71+
},
72+
groupCheckedValues (newVal, oldVal) {
73+
if (newVal) {
74+
this.initChecked()
75+
}
5576
}
5677
},
5778
mounted () {
5879
if (this.$refs.input) {
5980
this.$refs.input.indeterminate = this.indeterminate
6081
}
82+
if (this.$checkboxGroup) {
83+
this.initChecked()
84+
}
6185
},
6286
methods: {
6387
onChange (e) {
88+
this.$checkboxGroup && this.$checkboxGroup.updateCheckedValues(e.target.checked, this.label)
89+
this.checked = e.target.checked
6490
this.$emit('change', e.target.checked)
91+
},
92+
initChecked () {
93+
const isChecked = this.groupCheckedValues.indexOf(this.label) > -1
94+
this.checked = isChecked
6595
}
6696
}
6797
}

src/components/datepicker/index.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,49 @@
100100
right: 1em;
101101
bottom: 1.5em;
102102
}
103+
104+
.c-datepicker__sidebar {
105+
width: 110px;
106+
border-right: 1px solid #e4e4e4;
107+
box-sizing: border-box;
108+
padding-top: 6px;
109+
background-color: #fff;
110+
overflow: auto;
111+
float: left;
112+
margin-bottom:-10000px;
113+
padding-bottom:10000px;
114+
}
115+
116+
.c-datepicker__sidebar ul {
117+
padding: 0;
118+
}
119+
120+
.c-datepicker__sidebar .optionbtn {
121+
display: block;
122+
width: 100%;
123+
border: 0;
124+
background-color: transparent;
125+
line-height: 28px;
126+
font-size: 14px;
127+
color: #606266;
128+
padding-left: 12px;
129+
text-align: left;
130+
outline: none;
131+
cursor: pointer;
132+
}
133+
134+
.c-datepicker__sidebar + .c-datepicker__body {
135+
float: left;
136+
}
137+
138+
.c-datepicker__sidebar + .c-calendar {
139+
border: 0;
140+
float: left;
141+
}
142+
143+
.withSidebar {
144+
background: #fff;
145+
border: 1px solid rgb(214, 217, 219);
146+
border-radius: 0.2em;
147+
overflow: hidden;
148+
}

0 commit comments

Comments
 (0)