All files / lib/mixins form-options.js

48.15% Statements 13/27
32.14% Branches 9/28
50% Functions 1/2
48.15% Lines 13/27
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            198x   198x   198x 596x 596x                                                         198x     5x 5x               5x                               5x     5x     5x     5x        
import { isArray } from '../utils/array';
import { keys } from '../utils/object';
 
export default {
    computed: {
        formOptions() {
            let options = this.options || {};
 
            Eif (isArray(options)) {
                // Normalize flat arrays to object Array
                options = options.map(option => {
                    Eif (typeof option === 'object') {
                        return {
                            value: option[this.valueField],
                            text: option[this.textField],
                            disabled: option.disabled || false
                        };
                    }
 
                    return {text: String(option), value: option || {}};
                });
            } else {
                // Normalize Objects keys to Array
                options = keys(options).map(value => {
                    let option = options[value] || {};
 
                    // Resolve text
                    if (typeof option !== 'object') {
                        option = {text: String(option)};
                    }
 
                    // Resolve value (uses key as value if not provided)
                    option.value = option[this.valueField] || value;
 
                    // Resolve text field (uses key as value if not provided)
                    option.text = option[this.textField] || value;
 
                    return option;
                });
            }
 
            return options;
        },
        selectedValue() {
            const formOptions = this.formOptions;
            Iif (this.returnObject && !this.multiple) {
                for (let i = 0; i < formOptions.length; i++) {
                    if (formOptions[i].value === this.localValue) {
                        return formOptions[i];
                    }
                }
                return null;
            } else {
                return this.localValue;
            }
        }
    },
    props: {
        valueField: {
            type: String,
            default: 'value'
        },
        textField: {
            type: String,
            default: 'text'
        }
    },
    watch: {
        localValue(value, old_value) {
            Iif (value === old_value) {
                return;
            }
            this.$emit('input', this.selectedValue);
        },
        value(value, old_value) {
            Iif (value === old_value) {
                return;
            }
            this.localValue = value;
        }
    }
};