mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-21 22:09:16 +08:00
radio component
This commit is contained in:
parent
36e1710e33
commit
5de064caf4
@ -12,11 +12,9 @@ export default {
|
|||||||
|
|
||||||
### 基础用法
|
### 基础用法
|
||||||
|
|
||||||
:::demo
|
|
||||||
```html
|
```html
|
||||||
<z-radio v-model="radio"></z-radio>
|
<z-radio v-model="radio">单选框</z-radio>
|
||||||
```
|
```
|
||||||
:::
|
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { Dialog } from 'src/index';
|
import ZanUI from 'src/index';
|
||||||
|
let { Dialog } = ZanUI;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
methods: {
|
methods: {
|
||||||
|
27
docs/examples/radio.vue
Normal file
27
docs/examples/radio.vue
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<template>
|
||||||
|
<div class="page-switch">
|
||||||
|
<h1 class="page-title">Radio</h1>
|
||||||
|
|
||||||
|
<h2 class="page-sub-title">基础用法</h2>
|
||||||
|
<z-radio-group v-model="radio1">
|
||||||
|
<z-radio name="1">单选框1</z-radio>
|
||||||
|
<z-radio name="2">单选框2</z-radio>
|
||||||
|
</z-radio-group>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
radio1: '1'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
radio1(val) {
|
||||||
|
console.log(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
@ -5,13 +5,7 @@ import navConfig from './nav.config.json';
|
|||||||
import routes from './router.config';
|
import routes from './router.config';
|
||||||
import SideNav from './components/side-nav';
|
import SideNav from './components/side-nav';
|
||||||
import Mobile from './components/mobile';
|
import Mobile from './components/mobile';
|
||||||
import ZanUI from '../src/index';
|
|
||||||
|
|
||||||
import '../packages/zanui-css/src/index.css';
|
|
||||||
|
|
||||||
import { Dialog } from '../src/index';
|
|
||||||
console.log(Dialog);
|
|
||||||
Vue.use(ZanUI);
|
|
||||||
Vue.use(VueRouter);
|
Vue.use(VueRouter);
|
||||||
Vue.component('side-nav', SideNav);
|
Vue.component('side-nav', SideNav);
|
||||||
Vue.component('mobile', Mobile);
|
Vue.component('mobile', Mobile);
|
||||||
|
@ -9,7 +9,14 @@ export default {
|
|||||||
name: 'z-radio-group',
|
name: 'z-radio-group',
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
value: [String, Number]
|
value: {},
|
||||||
|
disabled: Boolean
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
value(value) {
|
||||||
|
this.$emit('change', value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
@ -2,10 +2,15 @@
|
|||||||
<div
|
<div
|
||||||
class="z-radio"
|
class="z-radio"
|
||||||
:class="{
|
:class="{
|
||||||
'is-disabled': disabled
|
'is-disabled': isDisabled
|
||||||
}">
|
}">
|
||||||
<span class="z-radio__input">
|
<span class="z-radio__input">
|
||||||
<input type="radio" class="z-radio__control">
|
<input
|
||||||
|
:value="name"
|
||||||
|
v-model="currentValue"
|
||||||
|
type="radio"
|
||||||
|
class="z-radio__control"
|
||||||
|
:disabled="isDisabled">
|
||||||
<span class="z-radio__circle"></span>
|
<span class="z-radio__circle"></span>
|
||||||
</span>
|
</span>
|
||||||
<span class="z-radio__label">
|
<span class="z-radio__label">
|
||||||
@ -21,35 +26,50 @@ export default {
|
|||||||
props: {
|
props: {
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
value: {},
|
value: {},
|
||||||
parentGroup: null
|
name: [String, Number]
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
isGroup() {
|
isGroup() {
|
||||||
let parent = this.$parent;
|
return !!this.findRadioGroup()
|
||||||
while (parent) {
|
|
||||||
if (parent.$options.name === 'z-radio-group') {
|
|
||||||
this.parentGroup = parent;
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
parent = parent.$parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
model: {
|
currentValue: {
|
||||||
get() {
|
get() {
|
||||||
return this.isGroup ? this.parentGroup.value : this.value;
|
return this.isGroup ? this.parentGroup.value : this.value;
|
||||||
},
|
},
|
||||||
|
|
||||||
set(val) {
|
set(val) {
|
||||||
if (this.isGroup) {
|
if (this.isGroup) {
|
||||||
|
this.parentGroup.$emit('input', val);
|
||||||
} else {
|
} else {
|
||||||
this.$emit('input', val);
|
this.$emit('input', val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
isDisabled() {
|
||||||
|
return this.isGroup
|
||||||
|
? this.parentGroup.disabled || this.disabled
|
||||||
|
: this.disabled;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
findRadioGroup() {
|
||||||
|
if (this.parentGroup) return;
|
||||||
|
|
||||||
|
let parent = this.$parent;
|
||||||
|
while (parent) {
|
||||||
|
if (parent.$options.name === 'z-radio-group') {
|
||||||
|
this.parentGroup = parent;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
parent = parent.$parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.parentGroup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user