Skip to content

Commit 29e4e6b

Browse files
committed
simple json
1 parent 681b5dd commit 29e4e6b

File tree

7 files changed

+69
-24
lines changed

7 files changed

+69
-24
lines changed

src/components/fn-schema.vue

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script setup lang="ts">
2+
import naiveUISchemaParser, { NaiveUISchema } from "../module/schema/schema.parser.ts";
3+
import {defineAsyncComponent, h} from 'vue'
4+
5+
const props = defineProps<{
6+
schema: NaiveUISchema
7+
}>()
8+
9+
10+
const node = naiveUISchemaParser(props.schema)
11+
12+
</script>
13+
14+
<template>
15+
<div class="schema">
16+
<h1>Test</h1>
17+
<template v-for="i of node">
18+
<component :is="i"/>
19+
</template>
20+
</div>
21+
</template>
22+
23+
<style scoped>
24+
25+
</style>

src/components/test.vue

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
<script setup lang="ts">
2+
import FnSchema from "./fn-schema.vue";
3+
import { NaiveUISchema } from "../module/schema/schema.parser.ts";
4+
5+
const json = [
6+
{
7+
$type: 'input',
8+
value: 'test'
9+
}
10+
] as NaiveUISchema
211
312
</script>
413

514
<template>
615
<n-config-provider>
7-
16+
<fn-schema :schema="json"/>
817
</n-config-provider>
918
</template>
1019

src/helper/helper.path.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const toPascalCase = (str: string) => {
2+
return str.replace(/\w+/g, function(w){
3+
return w[0].toUpperCase() + w.slice(1).toLowerCase()
4+
})
5+
}

src/main.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { createApp } from 'vue'
2-
import { plugin, defaultConfig } from '@formkit/vue'
32
import './style.css'
43
import App from './App.vue'
54
import naive from 'naive-ui'
65

76
createApp(App)
87
.use(naive)
9-
.use(plugin, defaultConfig)
108
.mount('#app')

src/module/schema/schema.d.ts

-10
This file was deleted.

src/module/schema/schema.parser.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
import { NaiveUISchema, NaiveUITypes } from "./schema";
2-
import { defineAsyncComponent } from 'vue'
1+
// @ts-ignore
2+
import NaiveUISchema = FNScheme.NaiveUISchema;
3+
import {toPascalCase} from "../../helper/helper.path.ts";
4+
import {defineAsyncComponent, h} from "vue";
5+
export enum NaiveUITypes {
6+
Form = 'form',
7+
Input = 'input',
8+
}
9+
10+
export type NaiveUISchema = NaiveUISchemaEl[]
11+
export declare interface NaiveUISchemaEl {
12+
$type: NaiveUITypes
13+
$children: any[]
14+
}
315

16+
export const fnComponentPath = (name: string) => `/node_modules/naive-ui/es/${name}/src/${toPascalCase(name)}.js`
417
export default function naiveUISchemaParser(json: NaiveUISchema) {
5-
for (const _el of json) {
6-
switch (_el.$type) {
7-
case NaiveUITypes.Form:
18+
return json.map((_el: any) => {
19+
const index = Object.values(NaiveUITypes).indexOf(_el.$type as unknown as NaiveUITypes)
820

9-
default:
10-
throw new Error('Element property $type not defined')
21+
if (index === -1) {
22+
throw new Error(
23+
`Type ${_el.$type} is not support. Supported types: \n ${JSON.stringify(NaiveUITypes, null, 4)}`
24+
)
1125
}
12-
}
13-
}
14-
//
15-
export const fnComponent = (name: string) => defineAsyncComponent(() => import(`naive-ui/lib/${name}`))
26+
27+
const { $type, ...schemeEl } = _el
28+
const { $children, ...props} = schemeEl
29+
30+
return h(defineAsyncComponent(() => import(fnComponentPath(_el.$type))), props)
31+
})
32+
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"typeRoots": ["./src/types"],
34
"target": "ES2020",
45
"useDefineForClassFields": true,
56
"module": "ESNext",

0 commit comments

Comments
 (0)