Skip to content

Commit a0ad4c9

Browse files
committed
fix: 添加JSON文本框
1 parent 823bd9f commit a0ad4c9

File tree

5 files changed

+193
-6
lines changed

5 files changed

+193
-6
lines changed

ui/src/components/dynamics-form/Demo.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Dict } from '@/api/type/common'
2222
2323
const damo_data: Array<FormField> = [
2424
{ field: 'name', input_type: 'PasswordInput', label: '用戶名', required: false },
25+
{ field: 'json_text', input_type: 'JsonInput', label: 'aa', required: false },
2526
{
2627
field: 'array_object_card_field',
2728
input_type: 'ArrayObjectCard',

ui/src/components/dynamics-form/FormItem.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,23 @@ const errMsg = computed(() => {
107107
? props.formfield.label + '不能为空'
108108
: props.formfield.label.label + '不能为空'
109109
})
110-
110+
/**
111+
* 反序列化
112+
* @param rule
113+
*/
114+
const to_rule = (rule: any) => {
115+
if (rule.validator) {
116+
let validator = (rule: any, value: string, callback: any) => {}
117+
return { ...rule, validator }
118+
}
119+
return rule
120+
}
111121
/**
112122
* 校验
113123
*/
114124
const rules = computed(() => {
115125
return props_info.value.rules
116-
? props_info.value.rules
126+
? props_info.value.rules.map(to_rule)
117127
: {
118128
message: errMsg.value,
119129
trigger: 'blur',

ui/src/components/dynamics-form/constructor/index.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ const props = withDefaults(
5252
{ label: '滑块', value: 'SliderConstructor' },
5353
{ label: '开关', value: 'SwitchInputConstructor' },
5454
{ label: '单选框', value: 'SingleSelectConstructor' },
55-
{ label: '日期', value: 'DatePickerConstructor' }
55+
{ label: '日期', value: 'DatePickerConstructor' },
56+
{ label: 'JSON文本框', value: 'JsonInputConstructor' }
5657
]
5758
}
5859
)
@@ -69,8 +70,8 @@ const form_data = ref<any>({
6970
input_type: ''
7071
})
7172
const rules = {
72-
label: [{ required: true, message: '参数 为必填属性' }],
73-
field: [{ required: true, message: '显示名称 为必填属性' }],
73+
label: [{ required: true, message: '显示名称 为必填属性' }],
74+
field: [{ required: true, message: '参数 为必填属性' }],
7475
required: [{ required: true, message: '是否必填 为必填属性' }],
7576
input_type: [{ required: true, message: '组建类型 为必填属性' }]
7677
}
@@ -108,7 +109,10 @@ onMounted(() => {
108109
const rander = (data: any) => {
109110
form_data.value.required = data.required ? data.required : false
110111
form_data.value.field = data.field
111-
form_data.value.input_type = data.input_type + 'Constructor'
112+
if (data.input_type) {
113+
form_data.value.input_type = data.input_type + 'Constructor'
114+
}
115+
112116
if (data.label && data.label.input_type === 'TooltipLabel') {
113117
form_data.value.tooltip = data.label.attrs.tooltip
114118
form_data.value.label = data.label.label
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<template>
2+
<el-form-item
3+
label="默认值"
4+
:required="formValue.required"
5+
prop="default_value"
6+
:rules="[default_value_rule]"
7+
>
8+
<JsonInput v-model="formValue.default_value"> </JsonInput>
9+
</el-form-item>
10+
</template>
11+
<script setup lang="ts">
12+
import { computed, onMounted } from 'vue'
13+
import JsonInput from '@/components/dynamics-form/items/JsonInput.vue'
14+
const props = defineProps<{
15+
modelValue: any
16+
}>()
17+
const emit = defineEmits(['update:modelValue'])
18+
const formValue = computed({
19+
set: (item) => {
20+
emit('update:modelValue', item)
21+
},
22+
get: () => {
23+
return props.modelValue
24+
}
25+
})
26+
27+
const getData = () => {
28+
return {
29+
input_type: 'JsonInput',
30+
attrs: {},
31+
props_info: {
32+
rules: [
33+
{
34+
required: true,
35+
validator: `validator = (rule, value, callback) => {
36+
try {
37+
JSON.parse(value)
38+
} catch (e) {
39+
callback(new Error('JSON格式不正确'))
40+
}
41+
}`,
42+
trigger: 'blur'
43+
}
44+
]
45+
},
46+
default_value: formValue.value.default_value
47+
}
48+
}
49+
50+
const default_value_rule = {
51+
required: true,
52+
validator: (rule: any, value: any, callback: any) => {
53+
try {
54+
JSON.parse(value)
55+
} catch (e) {
56+
callback(new Error('JSON格式不正确'))
57+
}
58+
return true
59+
},
60+
trigger: 'blur'
61+
}
62+
63+
const rander = (form_data: any) => {
64+
formValue.value.default_value = form_data.default_value
65+
}
66+
defineExpose({ getData, rander })
67+
onMounted(() => {
68+
formValue.value.default_value = '{}'
69+
})
70+
</script>
71+
<style lang="scss"></style>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<template>
2+
<div style="width: 100%" class="function-CodemirrorEditor">
3+
<Codemirror
4+
v-bind="$attrs"
5+
ref="cmRef"
6+
v-model="model_value"
7+
:extensions="extensions"
8+
:style="codemirrorStyle"
9+
:tab-size="4"
10+
:autofocus="true"
11+
/>
12+
<div class="function-CodemirrorEditor__format">
13+
<el-button text type="info" @click="format" class="magnify">
14+
<el-icon><DocumentChecked /></el-icon
15+
></el-button>
16+
</div>
17+
<div class="function-CodemirrorEditor__footer">
18+
<el-button text type="info" @click="openCodemirrorDialog" class="magnify">
19+
<AppIcon iconName="app-magnify" style="font-size: 16px"></AppIcon>
20+
</el-button>
21+
</div>
22+
<!-- Codemirror 弹出层 -->
23+
<el-dialog v-model="dialogVisible" title="Python 代码" append-to-body>
24+
<Codemirror
25+
v-model="cloneContent"
26+
:extensions="extensions"
27+
:style="codemirrorStyle"
28+
:tab-size="4"
29+
:autofocus="true"
30+
style="height: 300px !important; border: 1px solid #bbbfc4; border-radius: 4px"
31+
/>
32+
<template #footer>
33+
<div class="dialog-footer mt-24">
34+
<el-button type="primary" @click="submitDialog"> 确认</el-button>
35+
</div>
36+
</template>
37+
</el-dialog>
38+
</div>
39+
</template>
40+
<script setup lang="ts">
41+
import { json, jsonParseLinter } from '@codemirror/lang-json'
42+
import { oneDark } from '@codemirror/theme-one-dark'
43+
import { Codemirror } from 'vue-codemirror'
44+
import { linter } from '@codemirror/lint'
45+
import { computed, ref } from 'vue'
46+
const props = withDefaults(defineProps<{ modelValue?: string }>(), { modelValue: '{}' })
47+
const emit = defineEmits(['update:modelValue'])
48+
const model_value = computed({
49+
get: () => {
50+
if (!props.modelValue) {
51+
emit('update:modelValue', '{}')
52+
}
53+
return props.modelValue
54+
},
55+
set: (v: string) => {
56+
if (!v) {
57+
emit('update:modelValue', '{}')
58+
} else {
59+
emit('update:modelValue', v)
60+
}
61+
}
62+
})
63+
64+
const extensions = [json(), linter(jsonParseLinter()), oneDark]
65+
const codemirrorStyle = {
66+
height: '210px!important',
67+
width: '100%'
68+
}
69+
// 弹出框相关代码
70+
const dialogVisible = ref<boolean>(false)
71+
const cloneContent = ref<string>('')
72+
const openCodemirrorDialog = () => {
73+
cloneContent.value = model_value.value
74+
dialogVisible.value = true
75+
}
76+
const format = () => {
77+
try {
78+
const json_str = JSON.parse(model_value.value)
79+
model_value.value = JSON.stringify(json_str, null, 4)
80+
} catch (e) {}
81+
}
82+
function submitDialog() {
83+
model_value.value = cloneContent.value
84+
dialogVisible.value = false
85+
}
86+
</script>
87+
<style lang="scss">
88+
.function-CodemirrorEditor__footer {
89+
position: absolute;
90+
bottom: 10px;
91+
right: 10px;
92+
}
93+
.function-CodemirrorEditor {
94+
position: relative;
95+
}
96+
.function-CodemirrorEditor__format {
97+
position: absolute;
98+
top: 10px;
99+
right: 10px;
100+
}
101+
</style>

0 commit comments

Comments
 (0)