Commit df14dc74a1509f4968ed2a924f6f93a1a80d712d
1 parent
e264e432
add checkbox plugin
Showing
2 changed files
with
189 additions
and
3 deletions
front-end/h5/src/components/plugins/lbp-form-checkbox-group.js
0 → 100644
| 1 | +import LbpFormRadio from './lbp-form-radio.js' | ||
| 2 | + | ||
| 3 | +const defaultItems = [ | ||
| 4 | + { | ||
| 5 | + value: '选项A-value' | ||
| 6 | + }, | ||
| 7 | + { | ||
| 8 | + value: '选项B-value' | ||
| 9 | + }, | ||
| 10 | + { | ||
| 11 | + value: '选项C-value' | ||
| 12 | + } | ||
| 13 | +] | ||
| 14 | + | ||
| 15 | +export default { | ||
| 16 | + name: 'lbp-form-checkbox-group', | ||
| 17 | + components: { | ||
| 18 | + LbpFormRadio | ||
| 19 | + }, | ||
| 20 | + props: { | ||
| 21 | + aliasName: { | ||
| 22 | + type: String, | ||
| 23 | + default: '标题演示' | ||
| 24 | + }, | ||
| 25 | + items: { | ||
| 26 | + type: Array, | ||
| 27 | + default: () => defaultItems | ||
| 28 | + }, | ||
| 29 | + type: { | ||
| 30 | + type: String, | ||
| 31 | + default: 'checkbox' | ||
| 32 | + } | ||
| 33 | + }, | ||
| 34 | + data () { | ||
| 35 | + return { | ||
| 36 | + value: this.type === 'radio' ? '' : [], | ||
| 37 | + uuid: undefined | ||
| 38 | + } | ||
| 39 | + }, | ||
| 40 | + computed: { | ||
| 41 | + value_ () { | ||
| 42 | + if (this.type === 'radio') { | ||
| 43 | + return this.value | ||
| 44 | + } else { | ||
| 45 | + const value = (Array.isArray(this.value) && this.value) || [] | ||
| 46 | + return value.join(',') | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + }, | ||
| 50 | + watch: { | ||
| 51 | + type (type) { | ||
| 52 | + this.value = type === 'radio' ? '' : [] | ||
| 53 | + } | ||
| 54 | + }, | ||
| 55 | + editorConfig: { | ||
| 56 | + propsConfig: { | ||
| 57 | + items: { | ||
| 58 | + type: 'lbs-form-radio-items-editor', | ||
| 59 | + label: '选项列表', | ||
| 60 | + require: true, | ||
| 61 | + defaultPropValue: defaultItems | ||
| 62 | + }, | ||
| 63 | + aliasName: { | ||
| 64 | + type: 'a-input', | ||
| 65 | + label: '填写标题', | ||
| 66 | + require: true, | ||
| 67 | + defaultPropValue: '标题演示' | ||
| 68 | + }, | ||
| 69 | + type: { | ||
| 70 | + type: 'a-radio-group', | ||
| 71 | + label: '选择模式', | ||
| 72 | + require: true, | ||
| 73 | + prop: { | ||
| 74 | + options: [ | ||
| 75 | + { label: '单选', value: 'radio' }, | ||
| 76 | + { label: '多选', value: 'checkbox' } | ||
| 77 | + ], | ||
| 78 | + name: 'mode' | ||
| 79 | + }, | ||
| 80 | + defaultPropValue: 'checkbox' | ||
| 81 | + } | ||
| 82 | + }, | ||
| 83 | + components: { | ||
| 84 | + 'lbs-form-radio-items-editor': { | ||
| 85 | + render () { | ||
| 86 | + return <div> | ||
| 87 | + { | ||
| 88 | + this.value_.map((item, index) => ( | ||
| 89 | + <div> | ||
| 90 | + <a-input value={item.value} onChange={e => { item.value = e.target.value }}></a-input> | ||
| 91 | + <a-button type="dashed" shape="circle" icon="plus" onClick={this.add} /> | ||
| 92 | + <a-button type="dashed" shape="circle" icon="minus" onClick={(item, index) => this.minus(item, index)} /> | ||
| 93 | + </div> | ||
| 94 | + )) | ||
| 95 | + } | ||
| 96 | + </div> | ||
| 97 | + }, | ||
| 98 | + props: { | ||
| 99 | + value: { | ||
| 100 | + type: Array, | ||
| 101 | + default: () => defaultItems | ||
| 102 | + } | ||
| 103 | + }, | ||
| 104 | + computed: { | ||
| 105 | + value_: { | ||
| 106 | + get () { | ||
| 107 | + return this.value | ||
| 108 | + }, | ||
| 109 | + set (val) { | ||
| 110 | + this.$emit('input', val) | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + }, | ||
| 114 | + methods: { | ||
| 115 | + add () { | ||
| 116 | + console.log(this.value_.length) | ||
| 117 | + this.$emit('change', [ | ||
| 118 | + ...this.value_, | ||
| 119 | + { | ||
| 120 | + value: `选项${this.value_.length + 1}-value`, | ||
| 121 | + label: `选项${this.value_.length + 1}-label` | ||
| 122 | + } | ||
| 123 | + ]) | ||
| 124 | + }, | ||
| 125 | + minus (item, index) { | ||
| 126 | + const items = this.value_.slice(0) | ||
| 127 | + items.splice(index, 1) | ||
| 128 | + this.$emit('change', items) | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + }, | ||
| 134 | + mounted () { | ||
| 135 | + this.uuid = this.$el.dataset.uuid | ||
| 136 | + }, | ||
| 137 | + methods: { | ||
| 138 | + /** | ||
| 139 | + * @param {String, Number} val radioValue or checkboxValue | ||
| 140 | + */ | ||
| 141 | + onChange (val) { | ||
| 142 | + switch (this.type) { | ||
| 143 | + case 'radio': | ||
| 144 | + this.toggleRadio(val) | ||
| 145 | + break | ||
| 146 | + case 'checkbox': | ||
| 147 | + this.toggleCheckbox(val) | ||
| 148 | + break | ||
| 149 | + default: | ||
| 150 | + break | ||
| 151 | + } | ||
| 152 | + }, | ||
| 153 | + toggleCheckbox (val) { | ||
| 154 | + const index = this.value.indexOf(val) | ||
| 155 | + if (index === -1) { | ||
| 156 | + this.value.push(val) | ||
| 157 | + } else { | ||
| 158 | + this.value.splice(index, 1) | ||
| 159 | + } | ||
| 160 | + }, | ||
| 161 | + toggleRadio (val) { | ||
| 162 | + this.value = val | ||
| 163 | + } | ||
| 164 | + }, | ||
| 165 | + render () { | ||
| 166 | + return ( | ||
| 167 | + <div> | ||
| 168 | + <h3>{this.aliasName}{this.type}</h3> | ||
| 169 | + <input type="text" hidden value={this.value_} data-type="lbp-form-input" data-uuid={this.uuid} /> | ||
| 170 | + { | ||
| 171 | + this.items.map(item => ( | ||
| 172 | + <lbp-form-radio | ||
| 173 | + vertical | ||
| 174 | + value={item.value} | ||
| 175 | + checked={this.value === item.value} | ||
| 176 | + aliasName={this.aliasName} | ||
| 177 | + type={this.type} | ||
| 178 | + onChange={this.onChange} | ||
| 179 | + >{item.value}</lbp-form-radio> | ||
| 180 | + )) | ||
| 181 | + } | ||
| 182 | + </div> | ||
| 183 | + ) | ||
| 184 | + } | ||
| 185 | +} |
front-end/h5/src/mixins/load-plugins.js
| @@ -6,6 +6,7 @@ import LbpText from '../components/plugins/lbp-text' | @@ -6,6 +6,7 @@ import LbpText from '../components/plugins/lbp-text' | ||
| 6 | import LbpFormInput from '../components/plugins/lbp-form-input' | 6 | import LbpFormInput from '../components/plugins/lbp-form-input' |
| 7 | import LbpFormButton from '../components/plugins/lbp-form-button' | 7 | import LbpFormButton from '../components/plugins/lbp-form-button' |
| 8 | import LbpFormRadioGroup from '../components/plugins/lbp-form-radio-group' | 8 | import LbpFormRadioGroup from '../components/plugins/lbp-form-radio-group' |
| 9 | +import LbpFormCheckboxGroup from '../components/plugins/lbp-form-checkbox-group' | ||
| 9 | import LbpBackground from '../components/plugins/lbp-background' | 10 | import LbpBackground from '../components/plugins/lbp-background' |
| 10 | import LbpSlide from '../components/plugins/lbp-slide' | 11 | import LbpSlide from '../components/plugins/lbp-slide' |
| 11 | 12 | ||
| @@ -119,11 +120,11 @@ export const pluginsList = [ | @@ -119,11 +120,11 @@ export const pluginsList = [ | ||
| 119 | 'en-US': 'Form Checkbox', | 120 | 'en-US': 'Form Checkbox', |
| 120 | 'zh-CN': '表单多选' | 121 | 'zh-CN': '表单多选' |
| 121 | }, | 122 | }, |
| 122 | - title: '表单单选', | 123 | + title: '表单多选', |
| 123 | icon: 'check-square-o', | 124 | icon: 'check-square-o', |
| 124 | - component: LbpFormRadioGroup, | 125 | + component: LbpFormCheckboxGroup, |
| 125 | visible: true, | 126 | visible: true, |
| 126 | - name: LbpFormRadioGroup.name | 127 | + name: LbpFormCheckboxGroup.name |
| 127 | }, | 128 | }, |
| 128 | { | 129 | { |
| 129 | i18nTitle: { | 130 | i18nTitle: { |