KFormModelItem.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <!--
  2. * @Description: 传入record数据,通过判断record.type,来渲染对应的组件
  3. * @Author: kcz
  4. * @Date: 2020-01-02 22:41:48
  5. * @LastEditors: kcz
  6. * @LastEditTime: 2021-05-14 17:30:17
  7. -->
  8. <template>
  9. <a-form-model-item
  10. v-if="
  11. [
  12. 'input',
  13. 'textarea',
  14. 'date',
  15. 'time',
  16. 'number',
  17. 'radio',
  18. 'checkbox',
  19. 'select',
  20. 'rate',
  21. 'switch',
  22. 'slider',
  23. 'uploadImg',
  24. 'uploadFile',
  25. 'cascader',
  26. 'treeSelect'
  27. ].includes(record.type)
  28. "
  29. :prop="`domains.${index}.${record.model}`"
  30. :rules="record.rules"
  31. >
  32. <!-- 多行文本 -->
  33. <a-textarea
  34. :style="`width:${record.options.width}`"
  35. v-if="record.type === 'textarea'"
  36. :autoSize="{
  37. minRows: record.options.minRows,
  38. maxRows: record.options.maxRows
  39. }"
  40. :disabled="record.options.disabled || parentDisabled"
  41. :placeholder="record.options.placeholder"
  42. :allowClear="record.options.clearable"
  43. :maxLength="record.options.maxLength"
  44. :rows="4"
  45. :value="value"
  46. @change="handleChange($event.target.value)"
  47. />
  48. <!-- 单选框 -->
  49. <a-radio-group
  50. v-else-if="record.type === 'radio'"
  51. :options="
  52. !record.options.dynamic
  53. ? record.options.options
  54. : dynamicData[record.options.dynamicKey]
  55. ? dynamicData[record.options.dynamicKey]
  56. : []
  57. "
  58. :disabled="record.options.disabled || parentDisabled"
  59. :placeholder="record.options.placeholder"
  60. :value="value"
  61. :checked="value"
  62. @change="handleChange($event.target.value)"
  63. />
  64. <!-- 多选框 -->
  65. <a-checkbox-group
  66. v-else-if="record.type === 'checkbox'"
  67. :options="
  68. !record.options.dynamic
  69. ? record.options.options
  70. : dynamicData[record.options.dynamicKey]
  71. ? dynamicData[record.options.dynamicKey]
  72. : []
  73. "
  74. :disabled="record.options.disabled || parentDisabled"
  75. :placeholder="record.options.placeholder"
  76. :value="value"
  77. @change="handleChange"
  78. />
  79. <!-- 滑块 -->
  80. <div
  81. v-else-if="record.type === 'slider'"
  82. :style="`width:${record.options.width}`"
  83. class="slider-box"
  84. >
  85. <div class="slider">
  86. <a-slider
  87. :disabled="record.options.disabled || parentDisabled"
  88. :min="record.options.min"
  89. :max="record.options.max"
  90. :step="record.options.step"
  91. :value="value"
  92. @change="handleChange"
  93. />
  94. </div>
  95. <div class="number" v-if="record.options.showInput">
  96. <a-input-number
  97. style="width:100%"
  98. :disabled="record.options.disabled || parentDisabled"
  99. :min="record.options.min"
  100. :max="record.options.max"
  101. :step="record.options.step"
  102. :value="value"
  103. @change="handleChange"
  104. />
  105. </div>
  106. </div>
  107. <component
  108. v-else
  109. :style="`width:${record.options.width}`"
  110. v-bind="componentOption"
  111. :min="
  112. record.options.min || record.options.min === 0
  113. ? record.options.min
  114. : -Infinity
  115. "
  116. :max="
  117. record.options.max || record.options.max === 0
  118. ? record.options.max
  119. : Infinity
  120. "
  121. :count="record.options.max"
  122. :precision="
  123. record.options.precision > 50 ||
  124. (!record.options.precision && record.options.precision !== 0)
  125. ? null
  126. : record.options.precision
  127. "
  128. :record="record"
  129. :config="config"
  130. :disabled="record.options.disabled || parentDisabled"
  131. :parentDisabled="record.options.disabled || parentDisabled"
  132. :allowClear="record.options.clearable"
  133. :dynamicData="dynamicData"
  134. :filterOption="
  135. record.options.showSearch
  136. ? (inputValue, option) => {
  137. return (
  138. option.componentOptions.children[0].text
  139. .toLowerCase()
  140. .indexOf(inputValue.toLowerCase()) >= 0
  141. );
  142. }
  143. : false
  144. "
  145. :treeData="
  146. !record.options.dynamic
  147. ? record.options.options
  148. : dynamicData[record.options.dynamicKey]
  149. ? dynamicData[record.options.dynamicKey]
  150. : []
  151. "
  152. :options="
  153. !record.options.dynamic
  154. ? record.options.options
  155. : dynamicData[record.options.dynamicKey]
  156. ? dynamicData[record.options.dynamicKey]
  157. : []
  158. "
  159. :mode="record.options.multiple ? 'multiple' : ''"
  160. :checked="value"
  161. :value="value"
  162. @change="handleChange($event)"
  163. :is="componentItem"
  164. ></component>
  165. </a-form-model-item>
  166. <!-- 文本 -->
  167. <a-form-model-item v-else-if="record.type === 'text'">
  168. <div :style="{ textAlign: record.options.textAlign }">
  169. <label
  170. :class="{ 'ant-form-item-required': record.options.showRequiredMark }"
  171. :style="{
  172. fontFamily: record.options.fontFamily,
  173. fontSize: record.options.fontSize,
  174. color: record.options.color
  175. }"
  176. v-text="record.label"
  177. ></label>
  178. </div>
  179. </a-form-model-item>
  180. <!-- html -->
  181. <div
  182. v-else-if="record.type === 'html'"
  183. v-html="record.options.defaultValue"
  184. ></div>
  185. <div v-else>
  186. <!-- 空 -->
  187. </div>
  188. </template>
  189. <script>
  190. /*
  191. * author kcz
  192. * date 2019-11-20
  193. */
  194. // import moment from "moment";
  195. import UploadFile from "../../UploadFile";
  196. import UploadImg from "../../UploadImg";
  197. import KDatePicker from "../../KDatePicker";
  198. import KTimePicker from "../../KTimePicker";
  199. import ComponentArray from "../../core/components_use";
  200. const _ = require("lodash/object");
  201. export default {
  202. name: "KFormItem",
  203. props: [
  204. "record",
  205. "domains",
  206. "index",
  207. "value",
  208. "parentDisabled",
  209. "dynamicData",
  210. "config"
  211. ],
  212. components: {
  213. UploadImg,
  214. UploadFile,
  215. KDatePicker,
  216. KTimePicker
  217. },
  218. computed: {
  219. componentItem() {
  220. return ComponentArray[this.record.type];
  221. },
  222. componentOption() {
  223. return _.omit(this.record.options, ["defaultValue", "disabled"]);
  224. }
  225. },
  226. methods: {
  227. handleChange(e) {
  228. let value = e;
  229. if (e.target) {
  230. value = e.target.value;
  231. }
  232. this.$emit("input", value);
  233. }
  234. }
  235. };
  236. </script>
  237. <style lang="less" scoped>
  238. .slider-box {
  239. display: flex;
  240. > .slider {
  241. flex: 1;
  242. margin-right: 16px;
  243. }
  244. > .number {
  245. width: 70px;
  246. }
  247. }
  248. </style>