|
| 1 | +<template> |
| 2 | + <CFormGroup v-bind="{validFeedback, invalidFeedback, |
| 3 | + tooltipFeedback, description, |
| 4 | + wrapperClasses, class: computedClasses}" |
| 5 | + > |
| 6 | + <template slot="label"> |
| 7 | + <slot name="label"> |
| 8 | + <label v-if="label" :for="safeId" :class="labelClasses">{{label}}</label> |
| 9 | + </slot> |
| 10 | + </template> |
| 11 | + |
| 12 | + |
| 13 | + <template slot="input"> |
| 14 | + <input v-bind="$attrs" |
| 15 | + :id="safeId" |
| 16 | + :class="inputClasses" |
| 17 | + type="file" |
| 18 | + @change="onChange($event)" |
| 19 | + /> |
| 20 | + <label v-if="custom" :for="safeId" class="custom-file-label"> |
| 21 | + {{typeof custom === 'string' ? custom : multiple ? 'Choose files...' : 'Choose file...'}} |
| 22 | + </label> |
| 23 | + </template> |
| 24 | + |
| 25 | + |
| 26 | + <template v-for="slot in ['labelAfterInput','validFeedback', |
| 27 | + 'invalidFeedback','description']" |
| 28 | + :slot="slot" |
| 29 | + > |
| 30 | + <slot :name="slot"></slot> |
| 31 | + </template> |
| 32 | + </CFormGroup> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script> |
| 36 | +import * as allFormMixins from './formMixins' |
| 37 | +const mixins = Object.values(allFormMixins) |
| 38 | +import { formFileProps as props } from './formProps' |
| 39 | +import CFormGroup from './CFormGroup' |
| 40 | +export default { |
| 41 | + name: 'CFormFile', |
| 42 | + inheritAttrs: false, |
| 43 | + components: { CFormGroup }, |
| 44 | + mixins, |
| 45 | + props, |
| 46 | + // { |
| 47 | + // // Html props: disabled, required, accept |
| 48 | + // label: String, |
| 49 | + // id: String, |
| 50 | + // wasValidated: Boolean, |
| 51 | + // size: { |
| 52 | + // type: String, |
| 53 | + // validator: str => ['','sm','lg'].includes(str) |
| 54 | + // }, |
| 55 | + // horizontal: [Boolean, Object], |
| 56 | + // validFeedback: String, |
| 57 | + // invalidFeedback: String, |
| 58 | + // tooltipFeedback: Boolean, |
| 59 | + // description: String, |
| 60 | + // isValid: { |
| 61 | + // type: Boolean, |
| 62 | + // default: null |
| 63 | + // }, |
| 64 | + // multiple: Boolean, |
| 65 | + // custom: [Boolean, String], |
| 66 | + // addInputClasses: String, |
| 67 | + // addLabelClasses: String, |
| 68 | + // addWrapperClasses: String, |
| 69 | + // }, |
| 70 | + data () { |
| 71 | + return { |
| 72 | + state: null, |
| 73 | + } |
| 74 | + }, |
| 75 | + computed: { |
| 76 | + // classesComputedProps mixin |
| 77 | + haveCustomSize () { |
| 78 | + return ['','sm','lg'].includes(this.size) && |
| 79 | + Boolean(this.size) && !Boolean(this.custom) |
| 80 | + }, |
| 81 | + // haveCustomSize () { |
| 82 | + // return ['','sm','lg'].includes(this.size) && Boolean(this.size) |
| 83 | + // }, |
| 84 | + computedClasses () { |
| 85 | + return [ |
| 86 | + this.isHorizontal ? 'form-row': |
| 87 | + this.custom ? 'custom-file' : 'form-group position-relative', |
| 88 | + { |
| 89 | + 'was-validated': this.wasValidated |
| 90 | + } |
| 91 | + ] |
| 92 | + }, |
| 93 | + // computedClasses () { |
| 94 | + // return [ |
| 95 | + // this.isHorizontal ? 'form-row': 'form-group', |
| 96 | + // { |
| 97 | + // 'was-validated': this.wasValidated |
| 98 | + // } |
| 99 | + // ] |
| 100 | + // }, |
| 101 | + // labelClasses () { |
| 102 | + // return [ this.addLabelClasses, { |
| 103 | + // 'col-form-label': this.isHorizontal, |
| 104 | + // [this.horizontal.label || 'col-2']: this.isHorizontal, |
| 105 | + // [`col-form-label-${this.size}`]: this.haveCustomSize, |
| 106 | + // }] |
| 107 | + // }, |
| 108 | + // customSizeClass () { |
| 109 | + // return this.haveCustomSize ? `form-control-${this.size}` : null |
| 110 | + // }, |
| 111 | + inputClass () { |
| 112 | + return this.custom ? 'custom-file-input' : 'form-control-file' |
| 113 | + }, |
| 114 | + // inputClasses () { |
| 115 | + // return [ |
| 116 | + // this.inputClass || 'form-control', |
| 117 | + // this.stateClass, |
| 118 | + // this.addInputClasses, |
| 119 | + // this.customSizeClass |
| 120 | + // ] |
| 121 | + // }, |
| 122 | +
|
| 123 | +
|
| 124 | + // validationComputedProps mixin |
| 125 | + // computedIsValid () { |
| 126 | + // if (typeof this.isValid === 'function') |
| 127 | + // return this.isValid(this.state) |
| 128 | + // return this.isValid |
| 129 | + // }, |
| 130 | + // validationClass () { |
| 131 | + // if (this.computedIsValid === null) |
| 132 | + // return |
| 133 | + // return this.computedIsValid ? 'is-valid' : 'is-invalid' |
| 134 | + // } |
| 135 | +
|
| 136 | + //wrapperComputedProps mixin |
| 137 | + // isHorizontal () { |
| 138 | + // return Boolean(this.horizontal) |
| 139 | + // }, |
| 140 | + // haveInputGroup () { |
| 141 | + // return Boolean(this.tooltipFeedback || this.append || |
| 142 | + // this.prepend || this.$slots.append || this.$slots.prepend) |
| 143 | + // }, |
| 144 | + haveInputGroup () { |
| 145 | + return false |
| 146 | + } |
| 147 | + // haveWrapper () { |
| 148 | + // return this.haveInputGroup || Boolean(this.addWrapperClasses || this.isHorizontal) |
| 149 | + // }, |
| 150 | + // wrapperClasses () { |
| 151 | + // if(this.haveWrapper) |
| 152 | + // return [ this.addWrapperClasses, { |
| 153 | + // [this.horizontal.input || 'col-10'] : this.isHorizontal, |
| 154 | + // 'input-group' : this.haveInputGroup |
| 155 | + // }] |
| 156 | + // } |
| 157 | + }, |
| 158 | + methods: { |
| 159 | + onChange (e) { |
| 160 | + this.state = e.target.files |
| 161 | + this.$emit('change', e.target.files, e) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | +</script> |
0 commit comments