提交 aa0c7e13 编写于 作者: Y yangkaixuan

fix: radio group bug

上级 b2196633
...@@ -154,7 +154,9 @@ npm i @nutui/babel-plugin-separate-import -D ...@@ -154,7 +154,9 @@ npm i @nutui/babel-plugin-separate-import -D
``` bash ``` bash
{ {
"plugins": [ "plugins": [
["@nutui/babel-plugin-separate-import", { ["@nutui/babel-plugin-separate-import", {
"libraryName": "@nutui/nutui-jdl",
"libraryDirectory": "dist/packages",
"style": "scss" "style": "scss"
}] }]
] ]
......
<template> <template>
<div class="demo-list"> <div class="demo-list">
<h4>基础样式</h4> <h4>基础样式</h4>
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:name="'test0'" :name="'test0'"
:styleType="'radio'" :styleType="'radio'"
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
/> />
<div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut0 }}</div> <div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut0 }}</div>
<h4>列表样式</h4> <h4>列表样式</h4>
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:style-type="'list'" :style-type="'list'"
:name="'test3'" :name="'test3'"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
/> />
<div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut2 }}</div> <div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut2 }}</div>
<h4>列表禁选样式</h4> <h4>列表禁选样式</h4>
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:style-type="'list'" :style-type="'list'"
:name="'test4'" :name="'test4'"
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
/> />
<div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut3 }}</div> <div class="cell-reslout"> 选择结果 {{ resloutdemo1 }}: {{ labelReslut3 }}</div>
<h4>按钮样式</h4> <h4>按钮样式</h4>
<radio-group <nut-radiogroup
:list="labelList2" :list="labelList2"
:name="'test'" :name="'test'"
:styleType="'label'" :styleType="'label'"
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
</template> </template>
<script> <script>
import group from './index.vue';
export default { export default {
data() { data() {
return { return {
...@@ -138,9 +137,6 @@ export default { ...@@ -138,9 +137,6 @@ export default {
radioDisable: false, radioDisable: false,
}; };
}, },
components: {
'radio-group': group,
},
watch: { watch: {
disableVal(val) { disableVal(val) {
if (!isNaN(val)) { if (!isNaN(val)) {
......
...@@ -50,7 +50,7 @@ export default { ...@@ -50,7 +50,7 @@ export default {
```html ```html
<nut-cell> <nut-cell>
<span slot="title"> <span slot="title">
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:name="'test0'" :name="'test0'"
:effect-key = "'value'" :effect-key = "'value'"
...@@ -60,7 +60,7 @@ export default { ...@@ -60,7 +60,7 @@ export default {
</span> </span>
</nut-cell> </nut-cell>
<nut-cell> <nut-cell>
<span slot="title"> 选择结果 {{labelReslut0}}</span> <span slot="title"> 选择结果 {{labelReslut0} }</span>
</nut-cell> </nut-cell>
``` ```
...@@ -69,7 +69,7 @@ export default { ...@@ -69,7 +69,7 @@ export default {
```html ```html
<nut-cell> <nut-cell>
<span slot="title"> <span slot="title">
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:name="'test'" :name="'test'"
:styleType="'label'" :styleType="'label'"
...@@ -82,7 +82,7 @@ export default { ...@@ -82,7 +82,7 @@ export default {
</span> </span>
</nut-cell> </nut-cell>
<nut-cell> <nut-cell>
<span slot="title"> 选择结果 {{labelReslut}}</span> <span slot="title"> 选择结果 {{labelReslut} }</span>
</nut-cell> </nut-cell>
``` ```
...@@ -91,7 +91,7 @@ export default { ...@@ -91,7 +91,7 @@ export default {
```html ```html
<nut-cell> <nut-cell>
<span slot="title"> <span slot="title">
<radio-group <nut-radiogroup
:list="labelList" :list="labelList"
:style-type="'list'" :style-type="'list'"
:name="'test3'" :name="'test3'"
...@@ -105,7 +105,7 @@ export default { ...@@ -105,7 +105,7 @@ export default {
</span> </span>
</nut-cell> </nut-cell>
<nut-cell> <nut-cell>
<span slot="title"> 选择结果 {{resloutdemo1}}: {{labelReslut2}}</span> <span slot="title"> 选择结果 {{resloutdemo1} }: {{labelReslut2} }</span>
</nut-cell> </nut-cell>
``` ```
......
.nut-radio {
.nut-radio-labels {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 18px 16px;
box-sizing: border-box;
.nut-radio-list-label {
margin: 5px 2.3px;
}
}
.nut-radio-lists {
}
}
<template>
<div class="nut-radio">
<div
:class="{
'nut-radio-labels': styleType === 'label',
'nut-radio-lists': styleType === 'list',
}"
v-if="list && list.length > 0"
>
<nut-radio
v-for="(item, index) in list"
:key="item[effectKey]"
:type="styleType"
:name="name"
:text="item[effectText]"
:value="item[effectKey]"
:radio-data="item"
:checked="checkedIndex === index"
@radioChange="radioChange"
:disabled="disabledValue | disabledFilter(index)"
/>
</div>
</div>
</template>
<script>
export default {
name: 'radio-group',
props: {
styleType: String, // label
type: String, // radio 展示
list: Array, // radio 需要的数据信息
checkedIndex: {
type: Number,
default: -1,
}, // 初始化选中数组第几个从 0 开始
effectKey: String, // 绑定有效值对应属性 key
effectText: String, // 有效显示文案对应属性 key
resloutAttr: String, // 选择结果展示属性值类型 key
name: String, // radio 标签 name
disabledValue: [String, Number, Boolean, Array], // 禁用的 radio 需要传一个数组
},
mounted() {
if (this.checkedIndex > -1 && this.list && this.list.length > 0) {
this.$emit('input', this.list[this.checkedIndex][this.inputReslout]);
}
},
methods: {
radioChange(obj) {
this.$emit('input', obj[this.inputReslout]);
},
},
watch: {
list(val) {
if (val && val.length > 0 && this.checkedIndex > -1) {
this.$emit('input', val[this.checkedIndex][this.inputReslout]);
}
},
resloutAttr(res) {
if (this.list && this.list.length > 0 && this.checkedIndex > -1) {
this.$emit('input', this.list[this.checkedIndex][res]);
}
},
},
filters: {
disabledFilter(propObj, index) {
if (Object.prototype.toString.apply(propObj) === '[object Number]') {
return propObj === index;
} else if (Object.prototype.toString.apply(propObj) === '[object Array]') {
return propObj.indexOf(index) > -1;
} else if (Object.prototype.toString.apply(propObj) === '[object String]') {
return propObj === 'all';
} else if (Object.prototype.toString.apply(propObj) === '[object Boolean]') {
return propObj;
} else {
return false;
}
},
},
computed: {
inputReslout() {
return this.resloutAttr || this.effectText;
},
},
};
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
.nut-radiogroup { .nut-radio {
.nut-radio-labels {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 18px 16px;
box-sizing: border-box;
.nut-radio-list-label {
margin: 5px 2.3px;
}
}
} }
<template> <template>
<div class="nut-radiogroup"> <div class="nut-radio">
<slot></slot> <div
:class="{
'nut-radio-labels': styleType === 'label',
'nut-radio-lists': styleType === 'list',
}"
v-if="list && list.length > 0"
>
<nut-radio
v-for="(item, index) in list"
:key="item[effectKey]"
:type="styleType"
:name="name"
:text="item[effectText]"
:value="item[effectKey]"
:radio-data="item"
:checked="checkedIndex === index"
@radioChange="radioChange"
:disabled="disabledValue | disabledFilter(index)"
/>
</div>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'nut-radiogroup', name: 'nut-radiogroup',
props: { props: {
value: { styleType: String, // label
type: [String, Number, Boolean], type: String, // radio 展示
default: false, list: Array, // radio 需要的数据信息
checkedIndex: {
type: Number,
default: -1,
}, // 初始化选中数组第几个从 0 开始
effectKey: String, // 绑定有效值对应属性 key
effectText: String, // 有效显示文案对应属性 key
resloutAttr: String, // 选择结果展示属性值类型 key
name: String, // radio 标签 name
disabledValue: [String, Number, Boolean, Array], // 禁用的 radio 需要传一个数组
},
mounted() {
if (this.checkedIndex > -1 && this.list && this.list.length > 0) {
this.$emit('input', this.list[this.checkedIndex][this.inputReslout]);
}
},
methods: {
radioChange(obj) {
this.$emit('input', obj[this.inputReslout]);
},
},
watch: {
list(val) {
if (val && val.length > 0 && this.checkedIndex > -1) {
this.$emit('input', val[this.checkedIndex][this.inputReslout]);
}
}, },
disabled: { resloutAttr(res) {
type: Boolean, if (this.list && this.list.length > 0 && this.checkedIndex > -1) {
default: false, this.$emit('input', this.list[this.checkedIndex][res]);
}
}, },
size: { },
type: String, filters: {
default: 'base', disabledFilter(propObj, index) {
if (Object.prototype.toString.apply(propObj) === '[object Number]') {
return propObj === index;
} else if (Object.prototype.toString.apply(propObj) === '[object Array]') {
return propObj.indexOf(index) > -1;
} else if (Object.prototype.toString.apply(propObj) === '[object String]') {
return propObj === 'all';
} else if (Object.prototype.toString.apply(propObj) === '[object Boolean]') {
return propObj;
} else {
return false;
}
}, },
animated: { },
type: Boolean, computed: {
default: true, inputReslout() {
return this.resloutAttr || this.effectText;
}, },
}, },
}; };
</script> </script>
<style lang="scss" scoped>
@import './radiogroup.scss';
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册