|
| 1 | +import baseField from './baseField'; |
| 2 | +export default { |
| 3 | + computed: { |
| 4 | + inputType(){ |
| 5 | + return this.to.inputType || 'checkbox'; |
| 6 | + } |
| 7 | + }, |
| 8 | + created(){ |
| 9 | + // set the default value to array |
| 10 | + if ( this.inputType === 'checkbox' && ( this.model[ this.field.key ].constructor !== Array || !this.model[ this.field.key ].constructor) ) this.$set( this.model, this.field.key, [] ); |
| 11 | + }, |
| 12 | + mixins: [baseField], |
| 13 | + render(h){ |
| 14 | + const children = []; |
| 15 | + const self = this; |
| 16 | + const isArray = this.inputType === 'checkbox'; |
| 17 | + |
| 18 | + // add the label if it needs it |
| 19 | + if ( this.to.label ) children.push( |
| 20 | + h('label', this.to.label ) |
| 21 | + ); |
| 22 | + |
| 23 | + // create each option |
| 24 | + if ( 'options' in this.field ){ |
| 25 | + this.field.options.forEach( option => { |
| 26 | + const optionLabel = option.hasOwnProperty('label') ? option.label : option; |
| 27 | + const optionVal = option.hasOwnProperty('value') ? option.value : option; |
| 28 | + const optionChecked = isArray ? this.model[ this.field.key ].indexOf( optionVal ) > -1 : this.model[ this.field.key ] === optionVal; |
| 29 | + |
| 30 | + children.push( |
| 31 | + // wrap each option in a label |
| 32 | + h('label', { |
| 33 | + 'class': this.to.labelClasses |
| 34 | + }, [ |
| 35 | + // create an input |
| 36 | + h('input', { |
| 37 | + attrs: { |
| 38 | + type: this.inputType, |
| 39 | + ...this.to.atts |
| 40 | + }, |
| 41 | + 'class': { |
| 42 | + ...this.to.classes |
| 43 | + }, |
| 44 | + domProps: { |
| 45 | + value: optionVal, |
| 46 | + checked: optionChecked |
| 47 | + }, |
| 48 | + on: { |
| 49 | + click: this.onClick, |
| 50 | + blur: this.onBlur, |
| 51 | + focus: this.onFocus, |
| 52 | + keyup: this.onKeyup, |
| 53 | + keydown: this.onKeydown, |
| 54 | + change(event){ |
| 55 | + const isChecked = event.target.checked; |
| 56 | + // we need to add/remove differently depending on the type of input we're using |
| 57 | + if ( isArray ){ |
| 58 | + if ( isChecked ){ |
| 59 | + // if it's a checkbox, and hence an array, push it |
| 60 | + self.model[ self.field.key ].push( event.target.value ); |
| 61 | + } else { |
| 62 | + // otherwise remove it |
| 63 | + const valueIdx = self.model[ self.field.key ].indexOf( event.target.value ); |
| 64 | + if ( valueIdx > -1 ) self.model[ self.field.key ].splice( valueIdx, 1 ); |
| 65 | + } |
| 66 | + } else { |
| 67 | + self.model[ self.field.key ] = isChecked ? event.target.value : null; |
| 68 | + } |
| 69 | + self.$emit('change', event.target.value); |
| 70 | + if ( typeof self.onChange === 'function' ) self.onChange(event); |
| 71 | + } |
| 72 | + } |
| 73 | + }), |
| 74 | + // display the label |
| 75 | + optionLabel |
| 76 | + ]) |
| 77 | + ); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // add the error element |
| 82 | + children.push( |
| 83 | + h('error-display', { |
| 84 | + props: { |
| 85 | + form: this.form, |
| 86 | + field: this.field.key |
| 87 | + } |
| 88 | + }) |
| 89 | + ); |
| 90 | + |
| 91 | + // create the wrapper element |
| 92 | + return h('div', { |
| 93 | + 'class': [ |
| 94 | + 'form-group formly-list', |
| 95 | + this.to.wrapperClasses, |
| 96 | + { |
| 97 | + 'has-error has-danger': this.hasError |
| 98 | + } |
| 99 | + ] |
| 100 | + }, children); |
| 101 | + } |
| 102 | +} |
0 commit comments