|
| 1 | +<template> |
| 2 | + <validation-provider :rules="rules" v-slot="{ errors }"> |
| 3 | + <a-form-item |
| 4 | + :size="size" |
| 5 | + :required="isRequired" |
| 6 | + :help="errors[0]" |
| 7 | + :extra="tip" |
| 8 | + > |
| 9 | + <template v-slot:label> |
| 10 | + <span>{{ label }}</span> |
| 11 | + <span v-if="true"> |
| 12 | + <a-tooltip :title="tooltip" placement="top"> |
| 13 | + <a-icon type="question-circle" /> |
| 14 | + </a-tooltip> |
| 15 | + </span> |
| 16 | + </template> |
| 17 | + <component |
| 18 | + v-if="isSelect" |
| 19 | + :is="component" |
| 20 | + v-bind="props" |
| 21 | + :value="localValue" |
| 22 | + @change="updateAntLocalValue" |
| 23 | + > |
| 24 | + <a-select-option |
| 25 | + v-for="item in items" |
| 26 | + :key="item.value" |
| 27 | + :value="item.value" |
| 28 | + > |
| 29 | + {{ item.text }} |
| 30 | + </a-select-option> |
| 31 | + </component> |
| 32 | + <component |
| 33 | + v-else-if="isRadio" |
| 34 | + :is="component" |
| 35 | + v-bind="props" |
| 36 | + :value="localValue" |
| 37 | + @change="updateAntLocalValue" |
| 38 | + > |
| 39 | + <a-radio v-for="item in items" :key="item.value" :value="item.value"> |
| 40 | + {{ item.text }} |
| 41 | + </a-radio> |
| 42 | + </component> |
| 43 | + <component |
| 44 | + v-else-if="isCheckbox" |
| 45 | + :is="component" |
| 46 | + v-bind="props" |
| 47 | + :value="localValue" |
| 48 | + @change="updateAntLocalValue" |
| 49 | + > |
| 50 | + <a-checkbox v-for="item in items" :key="item.value" :value="item.value"> |
| 51 | + {{ item.text }} |
| 52 | + </a-checkbox> |
| 53 | + </component> |
| 54 | + <component |
| 55 | + v-else |
| 56 | + :is="component" |
| 57 | + v-bind="props" |
| 58 | + :value="localValue" |
| 59 | + @change="updateAntLocalValue" |
| 60 | + > |
| 61 | + </component> |
| 62 | + </a-form-item> |
| 63 | + </validation-provider> |
| 64 | +</template> |
| 65 | + |
| 66 | +<style lang="scss"></style> |
| 67 | + |
| 68 | +<script> |
| 69 | +import { useFormElement } from '@fext/vue-use'; |
| 70 | +
|
| 71 | +export default { |
| 72 | + name: 'ant-form-adaptor', |
| 73 | +
|
| 74 | + props: { |
| 75 | + tip: String, |
| 76 | + tooltip: String, |
| 77 | + name: String, |
| 78 | + size: { |
| 79 | + type: String, |
| 80 | + default: 'default' |
| 81 | + }, |
| 82 | + label: String, |
| 83 | + rules: { |
| 84 | + type: [String, Object] |
| 85 | + }, |
| 86 | + value: { |
| 87 | + required: false |
| 88 | + }, |
| 89 | + props: { |
| 90 | + type: Object, |
| 91 | + default() { |
| 92 | + return {}; |
| 93 | + }, |
| 94 | + required: false |
| 95 | + }, |
| 96 | + items: { |
| 97 | + type: Array, |
| 98 | + default() { |
| 99 | + return []; |
| 100 | + }, |
| 101 | + required: false |
| 102 | + }, |
| 103 | + extend: { |
| 104 | + type: Object, |
| 105 | + default() { |
| 106 | + return {}; |
| 107 | + } |
| 108 | + }, |
| 109 | + metadata: { |
| 110 | + type: Object, |
| 111 | + default() { |
| 112 | + return {}; |
| 113 | + } |
| 114 | + }, |
| 115 | + formValues: { |
| 116 | + type: Object, |
| 117 | + required: false |
| 118 | + } |
| 119 | + }, |
| 120 | +
|
| 121 | + setup(props, context) { |
| 122 | + const { |
| 123 | + dirty, |
| 124 | + isRequired, |
| 125 | + localValue, |
| 126 | + setInitialValue, |
| 127 | + updateLocalValue |
| 128 | + } = useFormElement(props, context); |
| 129 | + return { |
| 130 | + dirty, |
| 131 | + isRequired, |
| 132 | + localValue, |
| 133 | + setInitialValue, |
| 134 | + updateLocalValue |
| 135 | + }; |
| 136 | + }, |
| 137 | +
|
| 138 | + data() { |
| 139 | + return {}; |
| 140 | + }, |
| 141 | +
|
| 142 | + computed: { |
| 143 | + component() { |
| 144 | + return this.extend.component || 'a-input'; |
| 145 | + }, |
| 146 | +
|
| 147 | + isSelect() { |
| 148 | + return this.component === 'a-select'; |
| 149 | + }, |
| 150 | +
|
| 151 | + isMultipleSelect() { |
| 152 | + return this.isSelect && this.props.multiple; |
| 153 | + }, |
| 154 | +
|
| 155 | + isRadio() { |
| 156 | + return this.component === 'a-radio-group'; |
| 157 | + }, |
| 158 | +
|
| 159 | + isCheckbox() { |
| 160 | + return this.component === 'a-checkbox-group'; |
| 161 | + } |
| 162 | + }, |
| 163 | +
|
| 164 | + created() { |
| 165 | + const { localValue, isMultipleSelect, isCheckbox } = this; |
| 166 | +
|
| 167 | + if (localValue == null) { |
| 168 | + if (isMultipleSelect || isCheckbox) { |
| 169 | + this.setInitialValue([]); |
| 170 | + } |
| 171 | + } |
| 172 | + }, |
| 173 | +
|
| 174 | + methods: { |
| 175 | + updateAntLocalValue(source) { |
| 176 | + if (source.target) { |
| 177 | + this.updateLocalValue(source.target.value); |
| 178 | + } else { |
| 179 | + this.updateLocalValue(source); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +}; |
| 184 | +</script> |
0 commit comments