Commit e724513cb285a0a5681cbe7325f888454effd658
1 parent
49891768
feat:引入自定义组件 JAddInput 主要用于数据流转其他属性 客户端属性 Headers
Showing
3 changed files
with
137 additions
and
0 deletions
| ... | ... | @@ -27,6 +27,8 @@ import { BasicUpload } from '/@/components/Upload'; |
| 27 | 27 | import { StrengthMeter } from '/@/components/StrengthMeter'; |
| 28 | 28 | import { IconPicker } from '/@/components/Icon'; |
| 29 | 29 | import { CountdownInput } from '/@/components/CountDown'; |
| 30 | +//自定义组件 | |
| 31 | +import JAddInput from './jeecg/components/JAddInput.vue'; | |
| 30 | 32 | |
| 31 | 33 | const componentMap = new Map<ComponentType, Component>(); |
| 32 | 34 | |
| ... | ... | @@ -61,6 +63,8 @@ componentMap.set('IconPicker', IconPicker); |
| 61 | 63 | componentMap.set('InputCountDown', CountdownInput); |
| 62 | 64 | |
| 63 | 65 | componentMap.set('Upload', BasicUpload); |
| 66 | +//注册自定义组件 | |
| 67 | +componentMap.set('JAddInput', JAddInput); | |
| 64 | 68 | |
| 65 | 69 | export function add(compName: ComponentType, component: Component) { |
| 66 | 70 | componentMap.set(compName, component); | ... | ... |
| 1 | +<template> | |
| 2 | + <div v-for="(param, index) in dynamicInput.params" :key="index" style="display: flex"> | |
| 3 | + <a-input | |
| 4 | + placeholder="请输入参数key" | |
| 5 | + v-model:value="param.label" | |
| 6 | + style="width: 30%; margin-bottom: 5px" | |
| 7 | + @input="emitChange" | |
| 8 | + /> | |
| 9 | + <a-input | |
| 10 | + placeholder="请输入参数value" | |
| 11 | + v-model:value="param.value" | |
| 12 | + style="width: 30%; margin: 0 0 5px 5px" | |
| 13 | + @input="emitChange" | |
| 14 | + /> | |
| 15 | + <MinusCircleOutlined | |
| 16 | + v-if="dynamicInput.params.length > min" | |
| 17 | + class="dynamic-delete-button" | |
| 18 | + @click="remove(param)" | |
| 19 | + style="width: 50px" | |
| 20 | + /> | |
| 21 | + </div> | |
| 22 | + <div> | |
| 23 | + <a-button type="dashed" style="width: 60%" @click="add"> | |
| 24 | + <PlusOutlined /> | |
| 25 | + 新增 | |
| 26 | + </a-button> | |
| 27 | + </div> | |
| 28 | +</template> | |
| 29 | +<script lang="ts"> | |
| 30 | + import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue'; | |
| 31 | + import { defineComponent, reactive, UnwrapRef, watchEffect } from 'vue'; | |
| 32 | + import { propTypes } from '/@/utils/propTypes'; | |
| 33 | + import { isEmpty } from '/@/utils/is'; | |
| 34 | + | |
| 35 | + interface Params { | |
| 36 | + label: string; | |
| 37 | + value: string; | |
| 38 | + } | |
| 39 | + | |
| 40 | + export default defineComponent({ | |
| 41 | + name: 'JAddInput', | |
| 42 | + components: { | |
| 43 | + MinusCircleOutlined, | |
| 44 | + PlusOutlined, | |
| 45 | + }, | |
| 46 | + //--------------不继承Antd Design Vue Input的所有属性 否则控制台报大片警告-------------- | |
| 47 | + inheritAttrs: false, | |
| 48 | + props: { | |
| 49 | + value: propTypes.string.def(''), | |
| 50 | + //自定义删除按钮多少才会显示 | |
| 51 | + min: propTypes.integer.def(1), | |
| 52 | + }, | |
| 53 | + emits: ['change', 'update:value'], | |
| 54 | + setup(props, { emit }) { | |
| 55 | + //input动态数据 | |
| 56 | + const dynamicInput: UnwrapRef<{ params: Params[] }> = reactive({ params: [] }); | |
| 57 | + //删除Input | |
| 58 | + const remove = (item: Params) => { | |
| 59 | + let index = dynamicInput.params.indexOf(item); | |
| 60 | + if (index !== -1) { | |
| 61 | + dynamicInput.params.splice(index, 1); | |
| 62 | + } | |
| 63 | + emitChange(); | |
| 64 | + }; | |
| 65 | + //新增Input | |
| 66 | + const add = () => { | |
| 67 | + dynamicInput.params.push({ | |
| 68 | + label: '', | |
| 69 | + value: '', | |
| 70 | + }); | |
| 71 | + emitChange(); | |
| 72 | + }; | |
| 73 | + | |
| 74 | + //监听传入数据value | |
| 75 | + watchEffect(() => { | |
| 76 | + initVal(); | |
| 77 | + }); | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * 初始化数值 | |
| 81 | + */ | |
| 82 | + function initVal() { | |
| 83 | + dynamicInput.params = []; | |
| 84 | + if (props.value && props.value.indexOf('{') == 0) { | |
| 85 | + let jsonObj = JSON.parse(props.value); | |
| 86 | + Object.keys(jsonObj).forEach((key) => { | |
| 87 | + dynamicInput.params.push({ label: key, value: jsonObj[key] }); | |
| 88 | + }); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + /** | |
| 92 | + * 数值改变 | |
| 93 | + */ | |
| 94 | + function emitChange() { | |
| 95 | + let obj = {}; | |
| 96 | + if (dynamicInput.params.length > 0) { | |
| 97 | + dynamicInput.params.forEach((item) => { | |
| 98 | + obj[item['label']] = item['value']; | |
| 99 | + }); | |
| 100 | + } | |
| 101 | + emit('change', isEmpty(obj) ? '' : JSON.stringify(obj)); | |
| 102 | + emit('update:value', isEmpty(obj) ? '' : JSON.stringify(obj)); | |
| 103 | + } | |
| 104 | + | |
| 105 | + return { | |
| 106 | + dynamicInput, | |
| 107 | + emitChange, | |
| 108 | + remove, | |
| 109 | + add, | |
| 110 | + }; | |
| 111 | + }, | |
| 112 | + }); | |
| 113 | +</script> | |
| 114 | +<style scoped> | |
| 115 | + .dynamic-delete-button { | |
| 116 | + cursor: pointer; | |
| 117 | + position: relative; | |
| 118 | + top: 4px; | |
| 119 | + font-size: 24px; | |
| 120 | + color: #999; | |
| 121 | + transition: all 0.3s; | |
| 122 | + } | |
| 123 | + | |
| 124 | + .dynamic-delete-button:hover { | |
| 125 | + color: #777; | |
| 126 | + } | |
| 127 | + | |
| 128 | + .dynamic-delete-button[disabled] { | |
| 129 | + cursor: not-allowed; | |
| 130 | + opacity: 0.5; | |
| 131 | + } | |
| 132 | +</style> | ... | ... |