lbp-form-checkbox-group.js
2.97 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
import PropTypes from '@luban-h5/plugin-common-props'
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 {
extra: {
defaultStyle: {
width: 120,
height: 120
}
},
name: 'lbp-form-checkbox-group',
components: {
LbpFormRadio
},
props: {
aliasName: PropTypes.string({
defaultValue: '标题演示',
label: '填写标题'
}),
items: PropTypes.textOptions({
label: '选项列表',
defaultValue: () => getDefaultItems()
}),
// TODO 抽离 radio-group 至 common-props
type: {
type: String,
default: 'checkbox',
editor: {
type: 'a-radio-group',
label: '选择模式',
require: true,
props: {
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 => (
<lbp-form-radio
vertical
value={item.value}
checked={this.type === 'radio' ? this.value === item.value : this.value.includes(item.value)}
aliasName={this.uuid}
type={this.type}
onChange={this.onChange}
>{item.value}</lbp-form-radio>
))
}
</div>
)
}
}