support Checkbox

support Checkbox
This commit is contained in:
梁灏 2017-03-01 17:58:40 +08:00
parent 06322514c6
commit cbe03a12b2
10 changed files with 146 additions and 29 deletions

View file

@ -4,3 +4,7 @@
使用 v-model 使用 v-model
### Radio ### Radio
value 改为了 label使用 v-model废弃 checked value 改为了 label使用 v-model废弃 checked
### Checkbox
使用 v-model
### CheckboxGroup
value 改为了 label使用 v-model废弃 checked

View file

@ -22,7 +22,7 @@
- [x] Icon - [x] Icon
- [x] Input - [x] Input
- [x] Radio - [x] Radio
- [ ] Checkbox - [x] Checkbox
- [ ] Switch - [ ] Switch
- [ ] Table - [ ] Table
- [ ] Select - [ ] Select

View file

@ -9,42 +9,49 @@
export default { export default {
name: 'checkboxGroup', name: 'checkboxGroup',
props: { props: {
model: { value: {
type: Array, type: Array,
default () { default () {
return []; return [];
} }
} }
}, },
data () {
return {
currentValue: this.value
};
},
computed: { computed: {
classes () { classes () {
return `${prefixCls}`; return `${prefixCls}`;
} }
}, },
compiled () { mounted () {
this.updateModel(true); this.updateModel(true);
}, },
methods: { methods: {
updateModel (update) { updateModel (update) {
const model = this.model; const value = this.value;
this.$children.forEach((child) => { this.$children.forEach((child) => {
child.model = model; child.model = value;
if (update) { if (update) {
child.selected = model.indexOf(child.value) >= 0; child.currentValue = value.indexOf(child.label) >= 0;
child.group = true; child.group = true;
} }
}); });
}, },
change (data) { change (data) {
this.model = data; this.currentValue = data;
this.$emit('input', data);
this.$emit('on-change', data); this.$emit('on-change', data);
this.$dispatch('on-form-change', data); // todo
// this.$dispatch('on-form-change', data);
} }
}, },
watch: { watch: {
model () { value () {
this.updateModel(true); this.updateModel(true);
} }
} }

View file

@ -7,7 +7,7 @@
type="checkbox" type="checkbox"
:class="inputClasses" :class="inputClasses"
:disabled="disabled" :disabled="disabled"
:value="value" :value="label"
v-model="model" v-model="model"
@change="change"> @change="change">
<input <input
@ -15,28 +15,29 @@
type="checkbox" type="checkbox"
:class="inputClasses" :class="inputClasses"
:disabled="disabled" :disabled="disabled"
v-model="checked" :checked="currentValue"
@change="change"> @change="change">
</span> </span>
<slot v-if="showSlot"><span v-el:slot>{{ value }}</span></slot> <slot v-if="showSlot"><span ref="slot">{{ label }}</span></slot>
</label> </label>
</template> </template>
<script> <script>
const prefixCls = 'ivu-checkbox'; const prefixCls = 'ivu-checkbox';
export default { export default {
name: 'Checkbox',
props: { props: {
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false default: false
}, },
value: { value: {
type: [String, Number, Boolean]
},
checked: {
type: Boolean, type: Boolean,
default: false default: false
}, },
label: {
type: [String, Number, Boolean]
},
indeterminate: { indeterminate: {
type: Boolean, type: Boolean,
default: false default: false
@ -45,7 +46,7 @@
data () { data () {
return { return {
model: [], model: [],
selected: false, currentValue: this.value,
group: false, group: false,
showSlot: true showSlot: true
}; };
@ -56,7 +57,7 @@
`${prefixCls}-wrapper`, `${prefixCls}-wrapper`,
{ {
[`${prefixCls}-group-item`]: this.group, [`${prefixCls}-group-item`]: this.group,
[`${prefixCls}-wrapper-checked`]: this.selected, [`${prefixCls}-wrapper-checked`]: this.currentValue,
[`${prefixCls}-wrapper-disabled`]: this.disabled [`${prefixCls}-wrapper-disabled`]: this.disabled
} }
]; ];
@ -65,7 +66,7 @@
return [ return [
`${prefixCls}`, `${prefixCls}`,
{ {
[`${prefixCls}-checked`]: this.selected, [`${prefixCls}-checked`]: this.currentValue,
[`${prefixCls}-disabled`]: this.disabled, [`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-indeterminate`]: this.indeterminate [`${prefixCls}-indeterminate`]: this.indeterminate
} }
@ -78,11 +79,12 @@
return `${prefixCls}-input`; return `${prefixCls}-input`;
} }
}, },
ready () { mounted () {
// todo 使 while
if (this.$parent && this.$parent.$options.name === 'checkboxGroup') this.group = true; if (this.$parent && this.$parent.$options.name === 'checkboxGroup') this.group = true;
if (!this.group) { if (!this.group) {
this.updateModel(); this.updateModel();
if (this.$els.slot && this.$els.slot.innerHTML === '') { if (this.$refs.slot && this.$refs.slot.innerHTML === '') {
this.showSlot = false; this.showSlot = false;
} }
} }
@ -93,21 +95,24 @@
return false; return false;
} }
this.selected = event.target.checked; const checked = event.target.checked;
this.currentValue = checked;
this.$emit('input', checked);
if (this.group) { if (this.group) {
this.$parent.change(this.model); this.$parent.change(this.model);
} else { } else {
this.$emit('on-change', this.checked); this.$emit('on-change', checked);
this.$dispatch('on-form-change', this.checked); // todo
// this.$dispatch('on-form-change', checked);
} }
}, },
updateModel () { updateModel () {
this.selected = this.checked; this.currentValue = this.value;
} }
}, },
watch: { watch: {
checked () { value () {
this.updateModel(); this.updateModel();
} }
} }

View file

@ -33,7 +33,7 @@
data () { data () {
return { return {
currentValue: this.value currentValue: this.value
} };
}, },
computed: { computed: {
classes () { classes () {

View file

@ -63,6 +63,7 @@
} }
}, },
mounted () { mounted () {
// todo 使 while
if (this.$parent && this.$parent.$options.name === 'radioGroup') this.group = true; if (this.$parent && this.$parent.$options.name === 'radioGroup') this.group = true;
if (!this.group) { if (!this.group) {
this.updateValue(); this.updateValue();

View file

@ -10,7 +10,7 @@ import Button from './components/button';
// import Card from './components/card'; // import Card from './components/card';
// import Carousel from './components/carousel'; // import Carousel from './components/carousel';
// import Cascader from './components/cascader'; // import Cascader from './components/cascader';
// import Checkbox from './components/checkbox'; import Checkbox from './components/checkbox';
// import Circle from './components/circle'; // import Circle from './components/circle';
// import Collapse from './components/collapse'; // import Collapse from './components/collapse';
// import DatePicker from './components/date-picker'; // import DatePicker from './components/date-picker';
@ -60,8 +60,8 @@ const iview = {
// Carousel, // Carousel,
// CarouselItem: Carousel.Item, // CarouselItem: Carousel.Item,
// Cascader, // Cascader,
// Checkbox, Checkbox,
// CheckboxGroup: Checkbox.Group, CheckboxGroup: Checkbox.Group,
// Circle, // Circle,
// DatePicker, // DatePicker,
// Dropdown, // Dropdown,

View file

@ -29,6 +29,7 @@ li + li {
<li><router-link to="/button">Button</router-link></li> <li><router-link to="/button">Button</router-link></li>
<li><router-link to="/input">Input</router-link></li> <li><router-link to="/input">Input</router-link></li>
<li><router-link to="/radio">Radio</router-link></li> <li><router-link to="/radio">Radio</router-link></li>
<li><router-link to="/checkbox">Checkbox</router-link></li>
</ul> </ul>
</nav> </nav>
<router-view></router-view> <router-view></router-view>

View file

@ -36,6 +36,10 @@ const router = new VueRouter({
{ {
path: '/radio', path: '/radio',
component: require('./routers/radio.vue') component: require('./routers/radio.vue')
},
{
path: '/checkbox',
component: require('./routers/checkbox.vue')
} }
] ]
}); });

95
test/routers/checkbox.vue Normal file
View file

@ -0,0 +1,95 @@
<template>
<div>
<Checkbox v-model="single" @on-change="s">Checkbox</Checkbox>
{{ single }}
<div @click="single = !single">single-change</div>
<br>
{{ social }}
<Checkbox-group v-model="social" @on-change="s">
<Checkbox label="twitter">
<Icon type="social-twitter"></Icon>
<span>Twitter</span>
</Checkbox>
<Checkbox label="facebook">
<Icon type="social-facebook"></Icon>
<span>Facebook</span>
</Checkbox>
<Checkbox label="github">
<Icon type="social-github"></Icon>
<span>Github</span>
</Checkbox>
<Checkbox label="snapchat">
<Icon type="social-snapchat"></Icon>
<span>Snapchat</span>
</Checkbox>
</Checkbox-group>
<br>
<div @click="c">修改1</div>
{{ fruit }}
<Checkbox-group v-model="fruit">
<Checkbox label="香蕉"></Checkbox>
<Checkbox label="苹果"></Checkbox>
<Checkbox label="西瓜"></Checkbox>
</Checkbox-group>
<br><br>
<div style="border-bottom: 1px solid #e9e9e9;padding-bottom:6px;margin-bottom:6px;">
<Checkbox
:indeterminate="indeterminate"
v-model="checkAll"
@click.prevent.native="handleCheckAll">全选</Checkbox>
</div>
<Checkbox-group v-model="checkAllGroup" @on-change="checkAllGroupChange">
<Checkbox label="香蕉"></Checkbox>
<Checkbox label="苹果"></Checkbox>
<Checkbox label="西瓜"></Checkbox>
</Checkbox-group>
</div>
</template>
<script>
export default {
data () {
return {
social: ['facebook', 'github'],
fruit: ['苹果'],
single: false,
indeterminate: true,
checkAll: false,
checkAllGroup: ['香蕉', '西瓜']
}
},
methods: {
c () {
this.social.splice(0, 1)
},
s (d) {
console.log(d)
},
handleCheckAll () {
if (this.indeterminate) {
this.checkAll = false;
} else {
this.checkAll = !this.checkAll;
}
this.indeterminate = false;
if (this.checkAll) {
this.checkAllGroup = ['香蕉', '苹果', '西瓜'];
} else {
this.checkAllGroup = [];
}
},
checkAllGroupChange (data) {
if (data.length === 3) {
this.indeterminate = false;
this.checkAll = true;
} else if (data.length > 0) {
this.indeterminate = true;
this.checkAll = false;
} else {
this.indeterminate = false;
this.checkAll = false;
}
}
}
}
</script>