|
1 |
| -import bFormSelect from './form-select.vue'; |
| 1 | +import { idMixin, formMixin, formSizeMixin, formStateMixin, formOptionsMixin, formCustomMixin } from '../../mixins'; |
| 2 | +import { warn } from '../../utils'; |
| 3 | +import { from as arrayFrom } from '../../utils/array'; |
2 | 4 |
|
3 |
| -export default bFormSelect; |
| 5 | +export default { |
| 6 | + mixins: [idMixin, formMixin, formSizeMixin, formStateMixin, formCustomMixin, formOptionsMixin], |
| 7 | + render(h) { |
| 8 | + const t = this; |
| 9 | + const $slots = t.$slots; |
| 10 | + const options = t.formOptions.map((option, index) => { |
| 11 | + return h( |
| 12 | + 'option', |
| 13 | + { |
| 14 | + key: `option_${index}_opt`, |
| 15 | + attrs: { disabled: Boolean(option.disabled) }, |
| 16 | + domProps: { innerHTML: option.text, value: option.value } |
| 17 | + } |
| 18 | + ); |
| 19 | + }); |
| 20 | + return h( |
| 21 | + 'select', |
| 22 | + { |
| 23 | + ref: 'input', |
| 24 | + class: t.inputClass, |
| 25 | + directives: [ |
| 26 | + { name: 'model', rawName: 'v-model', value: t.localValue, expression: 'localValue' } |
| 27 | + ], |
| 28 | + attrs: { |
| 29 | + id: t.safeId(), |
| 30 | + name: t.name, |
| 31 | + multiple: t.multiple || null, |
| 32 | + size: (t.multiple || t.selectSize > 1) ? t.selectSize : null, |
| 33 | + disabled: t.disabled, |
| 34 | + required: t.required, |
| 35 | + 'aria-required': t.required ? 'true' : null, |
| 36 | + 'aria-invalid': t.computedAriaInvalid |
| 37 | + }, |
| 38 | + on: { |
| 39 | + change: (evt) => { |
| 40 | + const target = evt.target; |
| 41 | + const selectedVal = arrayFrom(target.options) |
| 42 | + .filter(o => o.selected) |
| 43 | + .map(o => '_value' in o ? o._value : o.value) |
| 44 | + t.localValue = target.multiple ? selectedVal : selectedVal[0]; |
| 45 | + t.$emit('change', t.localValue); |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + [ $slots.first, options, $slots.default ] |
| 50 | + ); |
| 51 | + }, |
| 52 | + data() { |
| 53 | + return { |
| 54 | + localValue: this.value |
| 55 | + } |
| 56 | + }, |
| 57 | + watch: { |
| 58 | + value(newVal, oldVal) { |
| 59 | + this.localValue = newVal; |
| 60 | + }, |
| 61 | + localValue(newVal, oldVal) { |
| 62 | + this.$emit('input', this.localValue); |
| 63 | + } |
| 64 | + }, |
| 65 | + props: { |
| 66 | + value: {}, |
| 67 | + multiple: { |
| 68 | + type: Boolean, |
| 69 | + default: false |
| 70 | + }, |
| 71 | + selectSize: { |
| 72 | + // Browsers default size to 0, which shows 4 rows in most browsers in multiple mode |
| 73 | + // Size of 1 can bork out firefox |
| 74 | + type: Number, |
| 75 | + default: 0 |
| 76 | + }, |
| 77 | + ariaInvalid: { |
| 78 | + type: [Boolean, String], |
| 79 | + default: false |
| 80 | + } |
| 81 | + }, |
| 82 | + computed: { |
| 83 | + inputClass() { |
| 84 | + return [ |
| 85 | + 'form-control', |
| 86 | + this.stateClass, |
| 87 | + this.sizeFormClass, |
| 88 | + (this.plain || (!this.multiple && this.selectSize > 1)) ? null : 'custom-select' |
| 89 | + ]; |
| 90 | + }, |
| 91 | + computedAriaInvalid() { |
| 92 | + if (this.ariaInvalid === true || this.ariaInvalid === 'true') { |
| 93 | + return 'true'; |
| 94 | + } |
| 95 | + return this.stateClass == 'is-invalid' ? 'true' : null; |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
0 commit comments