fix: 修复json文本框未转换为对象问题

This commit is contained in:
shaohuzhang1 2024-10-25 14:36:57 +08:00 committed by shaohuzhang1
parent 76138dfcd6
commit da96ccfe43
4 changed files with 63 additions and 22 deletions

View File

@ -21,8 +21,10 @@
:render_data="form_item_list" :render_data="form_item_list"
ref="dynamicsFormRef" ref="dynamicsFormRef"
> >
</DynamicsForm> </el-card </DynamicsForm>
></el-col> <el-button @click="validate">校验</el-button>
</el-card></el-col
>
</el-row> </el-row>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -30,6 +32,7 @@ import { ref } from 'vue'
import DynamicsFormConstructor from '@/components/dynamics-form/constructor/index.vue' import DynamicsFormConstructor from '@/components/dynamics-form/constructor/index.vue'
import DynamicsForm from '@/components/dynamics-form/index.vue' import DynamicsForm from '@/components/dynamics-form/index.vue'
const DynamicsFormConstructorRef = ref<InstanceType<typeof DynamicsFormConstructor>>() const DynamicsFormConstructorRef = ref<InstanceType<typeof DynamicsFormConstructor>>()
const form_item_list = ref<Array<any>>([]) const form_item_list = ref<Array<any>>([])
const add_field = () => { const add_field = () => {
if (DynamicsFormConstructorRef.value) { if (DynamicsFormConstructorRef.value) {
@ -38,8 +41,20 @@ const add_field = () => {
}) })
} }
} }
const form_data = ref({}) const form_data = ref({})
const item = ref({}) const item = ref({})
const dynamicsFormRef = ref<InstanceType<typeof DynamicsForm>>() const dynamicsFormRef = ref<InstanceType<typeof DynamicsForm>>()
const validate = () => {
console.log('asda')
dynamicsFormRef.value
?.validate()
.then((ok) => {
console.log('ok')
})
.catch((e) => {
console.log(e)
})
}
</script> </script>
<style lang="scss"></style> <style lang="scss"></style>

View File

@ -114,6 +114,7 @@ const errMsg = computed(() => {
const to_rule = (rule: any) => { const to_rule = (rule: any) => {
if (rule.validator) { if (rule.validator) {
let validator = (rule: any, value: string, callback: any) => {} let validator = (rule: any, value: string, callback: any) => {}
eval(rule.validator)
return { ...rule, validator } return { ...rule, validator }
} }
return rule return rule

View File

@ -5,15 +5,16 @@
prop="default_value" prop="default_value"
:rules="[default_value_rule]" :rules="[default_value_rule]"
> >
<JsonInput v-model="formValue.default_value"> </JsonInput> <JsonInput ref="jsonInputRef" v-model="formValue.default_value"> </JsonInput>
</el-form-item> </el-form-item>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, onMounted } from 'vue' import { computed, onMounted, ref } from 'vue'
import JsonInput from '@/components/dynamics-form/items/JsonInput.vue' import JsonInput from '@/components/dynamics-form/items/JsonInput.vue'
const props = defineProps<{ const props = defineProps<{
modelValue: any modelValue: any
}>() }>()
const formField = ref<any>({})
const emit = defineEmits(['update:modelValue']) const emit = defineEmits(['update:modelValue'])
const formValue = computed({ const formValue = computed({
set: (item) => { set: (item) => {
@ -23,8 +24,9 @@ const formValue = computed({
return props.modelValue return props.modelValue
} }
}) })
const jsonInputRef = ref<InstanceType<typeof JsonInput>>()
const getData = () => { const getData = () => {
console.log('xx')
return { return {
input_type: 'JsonInput', input_type: 'JsonInput',
attrs: {}, attrs: {},
@ -33,11 +35,8 @@ const getData = () => {
{ {
required: true, required: true,
validator: `validator = (rule, value, callback) => { validator: `validator = (rule, value, callback) => {
try { return componentFormRef.value?.validate_rules(rule, value, callback);
JSON.parse(value)
} catch (e) {
callback(new Error('JSON格式不正确'))
}
}`, }`,
trigger: 'blur' trigger: 'blur'
} }
@ -50,11 +49,7 @@ const getData = () => {
const default_value_rule = { const default_value_rule = {
required: true, required: true,
validator: (rule: any, value: any, callback: any) => { validator: (rule: any, value: any, callback: any) => {
try { jsonInputRef.value?.validate_rules(rule, value, callback)
JSON.parse(value)
} catch (e) {
callback(new Error('JSON格式不正确'))
}
return true return true
}, },
trigger: 'blur' trigger: 'blur'
@ -65,7 +60,7 @@ const rander = (form_data: any) => {
} }
defineExpose({ getData, rander }) defineExpose({ getData, rander })
onMounted(() => { onMounted(() => {
formValue.value.default_value = '{}' formValue.value.default_value = {}
}) })
</script> </script>
<style lang="scss"></style> <style lang="scss"></style>

View File

@ -43,46 +43,76 @@ import { oneDark } from '@codemirror/theme-one-dark'
import { Codemirror } from 'vue-codemirror' import { Codemirror } from 'vue-codemirror'
import { linter } from '@codemirror/lint' import { linter } from '@codemirror/lint'
import { computed, ref } from 'vue' import { computed, ref } from 'vue'
const props = withDefaults(defineProps<{ modelValue?: string }>(), { modelValue: '{}' }) const props = withDefaults(defineProps<{ modelValue?: any }>(), { modelValue: () => {} })
const emit = defineEmits(['update:modelValue']) const emit = defineEmits(['update:modelValue'])
const cache_model_value_str = ref<string>()
const model_value = computed({ const model_value = computed({
get: () => { get: () => {
if (!props.modelValue) { if (cache_model_value_str.value) {
emit('update:modelValue', '{}') return cache_model_value_str.value
} }
return props.modelValue return JSON.stringify(props.modelValue, null, 4)
}, },
set: (v: string) => { set: (v: string) => {
if (!v) { if (!v) {
emit('update:modelValue', '{}') emit('update:modelValue', JSON.parse('{}'))
} else { } else {
emit('update:modelValue', v) try {
cache_model_value_str.value = v
const result = JSON.parse(v)
emit('update:modelValue', result)
} catch (e) {}
} }
} }
}) })
const extensions = [json(), linter(jsonParseLinter()), oneDark] const extensions = [json(), linter(jsonParseLinter()), oneDark]
const codemirrorStyle = { const codemirrorStyle = {
height: '210px!important', height: '210px!important',
width: '100%' width: '100%'
} }
// //
const dialogVisible = ref<boolean>(false) const dialogVisible = ref<boolean>(false)
const cloneContent = ref<string>('') const cloneContent = ref<string>('')
const openCodemirrorDialog = () => { const openCodemirrorDialog = () => {
cloneContent.value = model_value.value cloneContent.value = model_value.value
dialogVisible.value = true dialogVisible.value = true
} }
const format = () => { const format = () => {
try { try {
const json_str = JSON.parse(model_value.value) const json_str = JSON.parse(model_value.value)
model_value.value = JSON.stringify(json_str, null, 4) model_value.value = JSON.stringify(json_str, null, 4)
} catch (e) {} } catch (e) {}
} }
function submitDialog() { function submitDialog() {
model_value.value = cloneContent.value model_value.value = cloneContent.value
dialogVisible.value = false dialogVisible.value = false
} }
/**
* 校验格式
* @param rule
* @param value
* @param callback
*/
const validate_rules = (rule: any, value: any, callback: any) => {
if (model_value.value) {
try {
JSON.parse(model_value.value)
} catch (e) {
callback(new Error('JSON格式不正确'))
}
}
}
defineExpose({ validate_rules: validate_rules })
</script> </script>
<style lang="scss"> <style lang="scss">
.function-CodemirrorEditor__footer { .function-CodemirrorEditor__footer {