|
| 1 | +import { Autocomplete } from 'element-ui' |
| 2 | +import noop from 'lodash/noop' |
| 3 | +import isNull from 'lodash/isNull' |
| 4 | +import ConnectedControlMixin from '../mixins/ConnectedControl' |
| 5 | + |
| 6 | +const ConnectedAutocomplete = { |
| 7 | + props: { |
| 8 | + name: { |
| 9 | + type: String, |
| 10 | + required: true, |
| 11 | + }, |
| 12 | + |
| 13 | + value: { |
| 14 | + type: [String, Number], |
| 15 | + default: '', |
| 16 | + }, |
| 17 | + |
| 18 | + labelKey: { |
| 19 | + type: String, |
| 20 | + default: 'name', |
| 21 | + }, |
| 22 | + |
| 23 | + valueKey: { |
| 24 | + type: String, |
| 25 | + default: 'id', |
| 26 | + }, |
| 27 | + |
| 28 | + placeholder: String, |
| 29 | + clearable: Boolean, |
| 30 | + disabled: Boolean, |
| 31 | + debounce: Number, |
| 32 | + placement: String, |
| 33 | + popperClass: String, |
| 34 | + triggerOnFocus: Boolean, |
| 35 | + selectWhenUnmatched: Boolean, |
| 36 | + prefixIcon: String, |
| 37 | + suffixIcon: String, |
| 38 | + hideLoading: Boolean, |
| 39 | + popperAppendToBody: Boolean, |
| 40 | + tabindex: Number, |
| 41 | + autofocus: Boolean, |
| 42 | + autosize: Boolean, |
| 43 | + autocomplete: { |
| 44 | + type: String, |
| 45 | + default: 'off', |
| 46 | + }, |
| 47 | + size: String, |
| 48 | + |
| 49 | + validators: Array, |
| 50 | + asyncValidators: Array, |
| 51 | + |
| 52 | + fetchSuggestions: { |
| 53 | + type: Function, |
| 54 | + required: true, |
| 55 | + }, |
| 56 | + |
| 57 | + handleFocus: { |
| 58 | + type: Function, |
| 59 | + default: noop, |
| 60 | + }, |
| 61 | + |
| 62 | + handleBlur: { |
| 63 | + type: Function, |
| 64 | + default: noop, |
| 65 | + }, |
| 66 | + |
| 67 | + handleSelect: { |
| 68 | + type: Function, |
| 69 | + default: noop, |
| 70 | + }, |
| 71 | + |
| 72 | + /* FormItem Props */ |
| 73 | + label: [String, Boolean], |
| 74 | + formItem: Boolean, |
| 75 | + labelWidth: String, |
| 76 | + }, |
| 77 | + |
| 78 | + mixins: [ConnectedControlMixin], |
| 79 | + |
| 80 | + data() { |
| 81 | + return { |
| 82 | + viewValue: this.value, |
| 83 | + } |
| 84 | + }, |
| 85 | + |
| 86 | + methods: { |
| 87 | + handleFieldSelect(value) { |
| 88 | + const nextValue = isNull(this.valueKey) ? value : value[this.valueKey] |
| 89 | + |
| 90 | + this.setTouched() |
| 91 | + this.handleSelect(nextValue) |
| 92 | + |
| 93 | + this.viewValue = isNull(this.labelKey) ? value : value[this.labelKey] |
| 94 | + |
| 95 | + const [, setValue] = this.state |
| 96 | + setValue(nextValue) |
| 97 | + }, |
| 98 | + |
| 99 | + renderComponent() { |
| 100 | + return ( |
| 101 | + <Autocomplete |
| 102 | + class={this.class} |
| 103 | + name={this.name} |
| 104 | + type={this.type} |
| 105 | + value={this.viewValue} |
| 106 | + placeholder={this.placeholder} |
| 107 | + clearable={this.clearable} |
| 108 | + disabled={this.disabled} |
| 109 | + value-key={this.labelKey} |
| 110 | + debounce={this.debounce} |
| 111 | + placement={this.placement} |
| 112 | + popper-class={this.popperClass} |
| 113 | + trigger-on-focus={this.triggerOnFocus} |
| 114 | + select-when-unmatched={this.selectWhenUnmatched} |
| 115 | + prefix-icon={this.prefixIcon} |
| 116 | + suffix-icon={this.suffixIcon} |
| 117 | + hide-loading={this.hideLoading} |
| 118 | + popper-append-to-body={this.popperAppendToBody} |
| 119 | + tabindex={this.tabindex} |
| 120 | + autofocus={this.autofocus} |
| 121 | + autosize={this.autosize} |
| 122 | + autocomplete={this.autocomplete} |
| 123 | + size={this.size} |
| 124 | + fetch-suggestions={this.fetchSuggestions} |
| 125 | + on-focus={this.handleFocus} |
| 126 | + on-blur={this.handleFieldBlur} |
| 127 | + on-select={this.handleFieldSelect}> |
| 128 | + {Boolean(this.prefix) && <template slot="prefix">{this.prefix}</template>} |
| 129 | + {Boolean(this.suffix) && <template slot="suffix">{this.suffix}</template>} |
| 130 | + {Boolean(this.append) && <template slot="append">{this.append}</template>} |
| 131 | + {Boolean(this.prepend) && <template slot="prepend">{this.prepend}</template>} |
| 132 | + </Autocomplete> |
| 133 | + ) |
| 134 | + }, |
| 135 | + }, |
| 136 | +} |
| 137 | + |
| 138 | +export default ConnectedAutocomplete |
0 commit comments