Skip to content

Commit da96ccf

Browse files
committed
fix: 修复json文本框未转换为对象问题
1 parent 76138df commit da96ccf

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
:render_data="form_item_list"
2222
ref="dynamicsFormRef"
2323
>
24-
</DynamicsForm> </el-card
25-
></el-col>
24+
</DynamicsForm>
25+
<el-button @click="validate">校验</el-button>
26+
</el-card></el-col
27+
>
2628
</el-row>
2729
</template>
2830
<script setup lang="ts">
2931
import { ref } from 'vue'
3032
import DynamicsFormConstructor from '@/components/dynamics-form/constructor/index.vue'
3133
import DynamicsForm from '@/components/dynamics-form/index.vue'
3234
const DynamicsFormConstructorRef = ref<InstanceType<typeof DynamicsFormConstructor>>()
35+
3336
const form_item_list = ref<Array<any>>([])
3437
const add_field = () => {
3538
if (DynamicsFormConstructorRef.value) {
@@ -38,8 +41,20 @@ const add_field = () => {
3841
})
3942
}
4043
}
44+
4145
const form_data = ref({})
4246
const item = ref({})
4347
const dynamicsFormRef = ref<InstanceType<typeof DynamicsForm>>()
48+
const validate = () => {
49+
console.log('asda')
50+
dynamicsFormRef.value
51+
?.validate()
52+
.then((ok) => {
53+
console.log('ok')
54+
})
55+
.catch((e) => {
56+
console.log(e)
57+
})
58+
}
4459
</script>
4560
<style lang="scss"></style>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const errMsg = computed(() => {
114114
const to_rule = (rule: any) => {
115115
if (rule.validator) {
116116
let validator = (rule: any, value: string, callback: any) => {}
117+
eval(rule.validator)
117118
return { ...rule, validator }
118119
}
119120
return rule

ui/src/components/dynamics-form/constructor/items/JsonInputConstructor.vue

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
prop="default_value"
66
:rules="[default_value_rule]"
77
>
8-
<JsonInput v-model="formValue.default_value"> </JsonInput>
8+
<JsonInput ref="jsonInputRef" v-model="formValue.default_value"> </JsonInput>
99
</el-form-item>
1010
</template>
1111
<script setup lang="ts">
12-
import { computed, onMounted } from 'vue'
12+
import { computed, onMounted, ref } from 'vue'
1313
import JsonInput from '@/components/dynamics-form/items/JsonInput.vue'
1414
const props = defineProps<{
1515
modelValue: any
1616
}>()
17+
const formField = ref<any>({})
1718
const emit = defineEmits(['update:modelValue'])
1819
const formValue = computed({
1920
set: (item) => {
@@ -23,8 +24,9 @@ const formValue = computed({
2324
return props.modelValue
2425
}
2526
})
26-
27+
const jsonInputRef = ref<InstanceType<typeof JsonInput>>()
2728
const getData = () => {
29+
console.log('xx')
2830
return {
2931
input_type: 'JsonInput',
3032
attrs: {},
@@ -33,11 +35,8 @@ const getData = () => {
3335
{
3436
required: true,
3537
validator: `validator = (rule, value, callback) => {
36-
try {
37-
JSON.parse(value)
38-
} catch (e) {
39-
callback(new Error('JSON格式不正确'))
40-
}
38+
return componentFormRef.value?.validate_rules(rule, value, callback);
39+
4140
}`,
4241
trigger: 'blur'
4342
}
@@ -50,11 +49,7 @@ const getData = () => {
5049
const default_value_rule = {
5150
required: true,
5251
validator: (rule: any, value: any, callback: any) => {
53-
try {
54-
JSON.parse(value)
55-
} catch (e) {
56-
callback(new Error('JSON格式不正确'))
57-
}
52+
jsonInputRef.value?.validate_rules(rule, value, callback)
5853
return true
5954
},
6055
trigger: 'blur'
@@ -65,7 +60,7 @@ const rander = (form_data: any) => {
6560
}
6661
defineExpose({ getData, rander })
6762
onMounted(() => {
68-
formValue.value.default_value = '{}'
63+
formValue.value.default_value = {}
6964
})
7065
</script>
7166
<style lang="scss"></style>

ui/src/components/dynamics-form/items/JsonInput.vue

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,76 @@ import { oneDark } from '@codemirror/theme-one-dark'
4343
import { Codemirror } from 'vue-codemirror'
4444
import { linter } from '@codemirror/lint'
4545
import { computed, ref } from 'vue'
46-
const props = withDefaults(defineProps<{ modelValue?: string }>(), { modelValue: '{}' })
46+
const props = withDefaults(defineProps<{ modelValue?: any }>(), { modelValue: () => {} })
4747
const emit = defineEmits(['update:modelValue'])
48+
49+
const cache_model_value_str = ref<string>()
50+
4851
const model_value = computed({
4952
get: () => {
50-
if (!props.modelValue) {
51-
emit('update:modelValue', '{}')
53+
if (cache_model_value_str.value) {
54+
return cache_model_value_str.value
5255
}
53-
return props.modelValue
56+
return JSON.stringify(props.modelValue, null, 4)
5457
},
5558
set: (v: string) => {
5659
if (!v) {
57-
emit('update:modelValue', '{}')
60+
emit('update:modelValue', JSON.parse('{}'))
5861
} else {
59-
emit('update:modelValue', v)
62+
try {
63+
cache_model_value_str.value = v
64+
const result = JSON.parse(v)
65+
emit('update:modelValue', result)
66+
} catch (e) {}
6067
}
6168
}
6269
})
6370
6471
const extensions = [json(), linter(jsonParseLinter()), oneDark]
72+
6573
const codemirrorStyle = {
6674
height: '210px!important',
6775
width: '100%'
6876
}
77+
6978
// 弹出框相关代码
7079
const dialogVisible = ref<boolean>(false)
80+
7181
const cloneContent = ref<string>('')
82+
7283
const openCodemirrorDialog = () => {
7384
cloneContent.value = model_value.value
7485
dialogVisible.value = true
7586
}
87+
7688
const format = () => {
7789
try {
7890
const json_str = JSON.parse(model_value.value)
7991
model_value.value = JSON.stringify(json_str, null, 4)
8092
} catch (e) {}
8193
}
94+
8295
function submitDialog() {
8396
model_value.value = cloneContent.value
8497
dialogVisible.value = false
8598
}
99+
/**
100+
* 校验格式
101+
* @param rule
102+
* @param value
103+
* @param callback
104+
*/
105+
const validate_rules = (rule: any, value: any, callback: any) => {
106+
if (model_value.value) {
107+
try {
108+
JSON.parse(model_value.value)
109+
} catch (e) {
110+
callback(new Error('JSON格式不正确'))
111+
}
112+
}
113+
}
114+
115+
defineExpose({ validate_rules: validate_rules })
86116
</script>
87117
<style lang="scss">
88118
.function-CodemirrorEditor__footer {

0 commit comments

Comments
 (0)