lbp-form-radio-group.js
3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* @Author: ly525
* @Date: 2019-11-23 12:35:43
* @LastEditors: ly525
* @LastEditTime: 2019-11-23 19:50:57
* @FilePath: /luban-h5/front-end/h5/src/components/plugins/lbp-form-radio-group.js
* @Github: https://github.com/ly525/luban-h5
* @Description: 表单单选组组件 #!en: radio group component
* @Copyright 2018 - 2019 luban-h5. All Rights Reserved
*/
import LbpFormRadio from './lbp-form-radio.js'
function getDefaultItems () {
// defaultItems.slice(0)[0] === defaultItems.slice(0)[0] -> true
// Object.assign(defaultItems)[0] === Object.assign(defaultItems)[0] -> true
// clone = (val) => JSON.parse(JSON.stringify(val))
// clone(defaultItems)[0] === clone(defaultItems)[0] -> false
const defaultItems = [
{
value: '选项A'
},
{
value: '选项B'
},
{
value: '选项C'
}
]
return defaultItems
}
export default {
name: 'lbp-form-radio-group',
props: {
aliasName: {
type: String,
default: `标题演示`,
editor: {
type: 'a-input',
label: '填写标题',
require: true
}
},
items: {
type: Array,
default: () => getDefaultItems(),
editor: {
type: 'lbs-prop-text-enum-editor',
label: '选项列表',
require: true
}
},
type: {
type: String,
default: 'radio',
editor: {
type: 'a-radio-group',
label: '选择模式',
require: true,
prop: {
options: [
{ label: '单选', value: 'radio' },
{ label: '多选', value: 'checkbox' }
],
name: 'mode'
}
}
}
},
data () {
return {
value: this.type === 'radio' ? '' : [],
uuid: undefined
}
},
computed: {
value_ () {
if (this.type === 'radio') {
return this.value
} else {
const value = (Array.isArray(this.value) && this.value) || []
return value.join(',')
}
}
},
watch: {
type (type) {
this.value = type === 'radio' ? '' : []
}
},
mounted () {
this.uuid = this.$el.dataset.uuid
},
methods: {
/**
* @param {String, Number} val radioValue or checkboxValue
*/
onChange (val) {
switch (this.type) {
case 'radio':
this.toggleRadio(val)
break
case 'checkbox':
this.toggleCheckbox(val)
break
default:
break
}
},
toggleCheckbox (val) {
const index = this.value.indexOf(val)
if (index === -1) {
this.value.push(val)
} else {
this.value.splice(index, 1)
}
},
toggleRadio (val) {
this.value = val
}
},
render () {
return (
<div>
<h3>{this.aliasName}</h3>
<input type="text" hidden value={this.value_} data-type="lbp-form-input" data-uuid={this.uuid} />
{
this.items.map(item => (
<LbpFormRadio
vertical
value={item.value}
checked={this.value === item.value}
aliasName={this.uuid}
type={this.type}
onChange={this.onChange}
>{item.value}
</LbpFormRadio>
))
}
</div>
)
}
}