defaultValue

pull/9/head
tangjinzhou 2017-11-02 12:07:20 +08:00
parent 4af93c1207
commit cf8809864b
4 changed files with 47 additions and 60 deletions

View File

@ -1,6 +1,6 @@
<template> <template>
<div :class="`${prefixCls}`"> <div :class="`${prefixCls}`">
<Radio v-for="item in options" :key="item.value" :checked="item.value === value" <Radio v-for="item in options" :key="item.value" :checked="item.value === stateValue"
:value="item.value" :disabled="item.disabled" @change="handleChange">{{item.label}}</Radio> :value="item.value" :disabled="item.disabled" @change="handleChange">{{item.label}}</Radio>
<slot v-if="options.length === 0"></slot> <slot v-if="options.length === 0"></slot>
</div> </div>
@ -14,7 +14,14 @@ export default {
default: 'ant-radio-group', default: 'ant-radio-group',
type: String, type: String,
}, },
value: [String, Number], defaultValue: {
default: undefined,
type: [String, Number],
},
value: {
default: undefined,
type: [String, Number],
},
size: { size: {
default: 'default', default: 'default',
validator (value) { validator (value) {
@ -27,8 +34,9 @@ export default {
}, },
}, },
data () { data () {
const { value, defaultValue } = this
return { return {
curValue: this.value, stateValue: value || defaultValue,
} }
}, },
model: { model: {
@ -46,19 +54,22 @@ export default {
return false return false
} }
const target = event.target const target = event.target
const { value } = target const { value: targetValue } = target
this.$emit('input', value) if (this.value === undefined) {
this.$emit('change', value) this.stateValue = targetValue
}
this.$emit('input', targetValue)
this.$emit('change', targetValue)
}, },
setChildRadio (children = []) { setChildRadio (children = []) {
const { options, $slots, value } = this const { options, $slots, stateValue } = this
if (options.length === 0 && $slots.default) { if (options.length === 0 && $slots.default) {
children.forEach(({ componentOptions = {}, children: newChildren }) => { children.forEach(({ componentOptions = {}, children: newChildren }) => {
const { Ctor, propsData } = componentOptions const { Ctor, propsData } = componentOptions
if (Ctor && Ctor.options.name === 'Radio') { if (Ctor && Ctor.options.name === 'Radio') {
propsData.isGroup = true propsData.isGroup = true
propsData.onGroupChange = this.handleChange propsData.onGroupChange = this.handleChange
propsData.checked = propsData.value === value propsData.checked = propsData.value === stateValue
} else { } else {
this.setChildRadio(newChildren) this.setChildRadio(newChildren)
} }
@ -71,6 +82,7 @@ export default {
}, },
watch: { watch: {
value (val) { value (val) {
this.stateValue = val
this.setChildRadio(this.$slots.default) this.setChildRadio(this.$slots.default)
}, },
}, },

View File

@ -2,7 +2,7 @@
<label :class="classes"> <label :class="classes">
<span :class="checkboxClass"> <span :class="checkboxClass">
<input :name="name" type="radio" :disabled="disabled" <input :name="name" type="radio" :disabled="disabled"
:class="`${prefixCls}-input`" :checked="!!checked" :class="`${prefixCls}-input`" :checked="stateChecked"
@change="handleChange" @change="handleChange"
/> />
<span :class="`${prefixCls}-inner`" /> <span :class="`${prefixCls}-inner`" />
@ -20,7 +20,8 @@ export default {
default: 'ant-radio', default: 'ant-radio',
type: String, type: String,
}, },
checked: Boolean, defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
disabled: Boolean, disabled: Boolean,
isGroup: Boolean, isGroup: Boolean,
value: [String, Number, Boolean], value: [String, Number, Boolean],
@ -30,35 +31,45 @@ export default {
model: { model: {
prop: 'checked', prop: 'checked',
}, },
data () {
const { checked, defaultChecked } = this
return {
stateChecked: checked === undefined ? defaultChecked : checked,
}
},
computed: { computed: {
hasDefaultSlot () { hasDefaultSlot () {
return !!this.$slots.default return !!this.$slots.default
}, },
classes () { classes () {
const { prefixCls, disabled, checked } = this const { prefixCls, disabled, stateChecked } = this
return { return {
[`${prefixCls}-wrapper`]: true, [`${prefixCls}-wrapper`]: true,
[`${prefixCls}-wrapper-checked`]: checked, [`${prefixCls}-wrapper-checked`]: stateChecked,
[`${prefixCls}-wrapper-disabled`]: disabled, [`${prefixCls}-wrapper-disabled`]: disabled,
} }
}, },
checkboxClass () { checkboxClass () {
const { prefixCls, disabled, checked } = this const { prefixCls, disabled, stateChecked } = this
return { return {
[`${prefixCls}`]: true, [`${prefixCls}`]: true,
[`${prefixCls}-checked`]: checked, [`${prefixCls}-checked`]: stateChecked,
[`${prefixCls}-disabled`]: disabled, [`${prefixCls}-disabled`]: disabled,
} }
}, },
}, },
methods: { methods: {
handleChange (event) { handleChange (event) {
const { name, value } = this const targetChecked = event.target.checked
this.$emit('input', true) const { name, value, checked } = this
if (checked === undefined) {
this.stateChecked = targetChecked
}
this.$emit('input', targetChecked)
const target = { const target = {
name, name,
value, value,
checked: true, checked: targetChecked,
} }
this.$emit('change', { this.$emit('change', {
target, target,
@ -74,5 +85,10 @@ export default {
} }
}, },
}, },
watch: {
checked (val) {
this.stateChecked = val
},
},
} }
</script> </script>

View File

@ -1,41 +0,0 @@
<template>
<div :class="`${prefixCls}`">
<Radio v-for="item in options" :key="item.value" :checked="item.value === value"
:value="item.value" :disabled="item.disabled" @change="handleChange">{{item.label}}</Radio>
<slot v-if="options.length === 0"></slot>
</div>
</template>
<script>
import Radio from './Radio.vue'
export default {
name: 'Radio',
props: {
prefixCls: {
default: 'ant-radio-button',
type: String,
},
value: [String, Number],
size: {
default: 'default',
validator (value) {
return ['large', 'default', 'small'].includes(value)
},
},
options: {
default: () => [],
type: Array,
},
},
data () {
return {
curValue: this.value,
}
},
model: {
prop: 'value',
},
components: {
Radio,
},
}
</script>

View File

@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<Radio v-model="checked" @change="change" name="test" value="123">Radio</Radio> <Radio @change="change" :defaultChecked="true" name="test" value="123">Radio</Radio>
<Radio :checked="false" @change="change" name="test2" value="222">Radio</Radio> <Radio :checked="false" @change="change" name="test2" value="222">Radio</Radio>
<RadioGroup v-model="value" @change="change"> <RadioGroup v-model="value" @change="change">
@ -14,7 +14,7 @@
</RadioGroup> </RadioGroup>
<RadioGroup :options="options" v-model="value1" @change="change"></RadioGroup> <RadioGroup :options="options" v-model="value1" @change="change"></RadioGroup>
<div> <div>
<RadioGroup v-model="value2" size="large"> <RadioGroup :defaultValue="'a'" size="large">
<RadioButton value="a">Hangzhou</RadioButton> <RadioButton value="a">Hangzhou</RadioButton>
<RadioButton value="b">Shanghai</RadioButton> <RadioButton value="b">Shanghai</RadioButton>
<RadioButton value="c">Beijing</RadioButton> <RadioButton value="c">Beijing</RadioButton>