Skip to content

Commit

Permalink
support Radio
Browse files Browse the repository at this point in the history
support Radio
  • Loading branch information
icarusion committed Mar 1, 2017
1 parent fc7ef07 commit 0632251
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 214 deletions.
6 changes: 6 additions & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Input
使用 v-model
### RadioGroup
使用 v-model
### Radio
value 改为了 label,使用 v-model,废弃 checked
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- [x] Button
- [x] Icon
- [x] Input
- [ ] Radio
- [x] Radio
- [ ] Checkbox
- [ ] Switch
- [ ] Table
Expand Down
1 change: 1 addition & 0 deletions src/components/affix/affix.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}
export default {
name: 'Affix',
props: {
offsetTop: {
type: Number,
Expand Down
1 change: 1 addition & 0 deletions src/components/button/button-group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const prefixCls = 'ivu-btn-group';
export default {
name: 'buttonGroup',
props: {
size: {
validator (value) {
Expand Down
1 change: 1 addition & 0 deletions src/components/button/button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const prefixCls = 'ivu-btn';
export default {
name: 'Button',
components: { Icon },
props: {
type: {
Expand Down
1 change: 1 addition & 0 deletions src/components/icon/icon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const prefixCls = 'ivu-icon';
export default {
name: 'Icon',
props: {
type: String,
size: [Number, String],
Expand Down
1 change: 1 addition & 0 deletions src/components/input/input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
const prefixCls = 'ivu-input';
export default {
name: 'Input',
props: {
type: {
validator (value) {
Expand Down
29 changes: 18 additions & 11 deletions src/components/radio/radio-group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
export default {
name: 'radioGroup',
props: {
model: {
value: {
type: [String, Number],
default: ''
},
Expand All @@ -30,6 +30,11 @@
default: false
}
},
data () {
return {
currentValue: this.value
}
},
computed: {
classes () {
return [
Expand All @@ -42,27 +47,29 @@
];
}
},
compiled () {
this.updateModel();
mounted () {
this.updateValue();
},
methods: {
updateModel () {
const model = this.model;
updateValue () {
const value = this.value;
this.$children.forEach((child) => {
child.selected = model == child.value;
child.currentValue = value == child.label;
child.group = true;
});
},
change (data) {
this.model = data.value;
this.updateModel();
this.currentValue = data.value;
this.updateValue();
this.$emit('input', data.value);
this.$emit('on-change', data.value);
this.$dispatch('on-form-change', data.value);
// todo 事件
// this.$dispatch('on-form-change', data.value);
}
},
watch: {
model () {
this.updateModel();
value () {
this.updateValue();
}
}
};
Expand Down
47 changes: 25 additions & 22 deletions src/components/radio/radio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@
type="radio"
:class="inputClasses"
:disabled="disabled"
:checked="selected"
:checked="currentValue"
@change="change">
</span><slot>{{ value }}</slot>
</span><slot>{{ label }}</slot>
</label>
</template>
<script>
const prefixCls = 'ivu-radio';
export default {
name: 'Radio',
props: {
checked: {
value: {
type: Boolean,
default: false
},
label: {
type: [String, Number]
},
disabled: {
type: Boolean,
default: false
},
value: {
type: [String, Number]
}
},
data () {
return {
selected: false,
currentValue: this.value,
group: false
};
},
Expand All @@ -40,7 +41,7 @@
`${prefixCls}-wrapper`,
{
[`${prefixCls}-group-item`]: this.group,
[`${prefixCls}-wrapper-checked`]: this.selected,
[`${prefixCls}-wrapper-checked`]: this.currentValue,
[`${prefixCls}-wrapper-disabled`]: this.disabled
}
];
Expand All @@ -49,7 +50,7 @@
return [
`${prefixCls}`,
{
[`${prefixCls}-checked`]: this.selected,
[`${prefixCls}-checked`]: this.currentValue,
[`${prefixCls}-disabled`]: this.disabled
}
];
Expand All @@ -61,10 +62,10 @@
return `${prefixCls}-input`;
}
},
ready () {
mounted () {
if (this.$parent && this.$parent.$options.name === 'radioGroup') this.group = true;
if (!this.group) {
this.updateModel();
this.updateValue();
}
},
methods: {
Expand All @@ -73,25 +74,27 @@
return false;
}
this.selected = event.target.checked;
this.checked = this.selected;
const checked = event.target.checked;
this.currentValue = checked;
this.$emit('input', checked);
this.$emit('on-change', checked);
if (this.group && this.checked) {
if (this.group && this.label) {
this.$parent.change({
value: this.value,
checked: this.checked
value: this.label,
checked: this.value
});
}
if (!this.group) this.$dispatch('on-form-change', this.selected);
// todo 事件
// if (!this.group) this.$dispatch('on-form-change', checked);
},
updateModel () {
this.selected = this.checked;
updateValue () {
this.currentValue = this.value;
}
},
watch: {
checked () {
this.updateModel();
value () {
this.updateValue();
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Input from './components/input';
// import Page from './components/page';
// import Poptip from './components/poptip';
// import Progress from './components/progress';
// import Radio from './components/radio';
import Radio from './components/radio';
// import Rate from './components/rate';
// import Slider from './components/slider';
// import Spin from './components/spin';
Expand Down Expand Up @@ -89,8 +89,8 @@ const iview = {
// Panel: Collapse.Panel,
// Poptip,
// Progress,
// Radio,
// RadioGroup: Radio.Group,
Radio,
RadioGroup: Radio.Group,
// Rate,
Row,
// iSelect: Select,
Expand Down
1 change: 1 addition & 0 deletions test/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ li + li {
<li><router-link to="/grid">Grid</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="/radio">Radio</router-link></li>
</ul>
</nav>
<router-view></router-view>
Expand Down
4 changes: 4 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const router = new VueRouter({
{
path: '/input',
component: require('./routers/input.vue')
},
{
path: '/radio',
component: require('./routers/radio.vue')
}
]
});
Expand Down
Loading

0 comments on commit 0632251

Please sign in to comment.