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