Implement a Dynamic Form Builder based on a JSON schema.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
<template>
<form>
<div v-for="field in schema" :key="field.name">
<label>{{ field.label }}</label>
<component
:is="field.type === 'text' ? 'input' : 'select'"
v-model="model[field.name]"
>
<option v-if="field.type==='select'" v-for="opt in field.options" :value="opt">{{opt}}</option>
</component>
</div>
</form>
</template>
<script setup>
import { reactive } from 'vue';
const schema = [
{ name: 'username', label: 'Username', type: 'text' },
{ name: 'role', label: 'Role', type: 'select', options: ['Admin', 'User'] }
];
const model = reactive({ username: '', role: 'User' });
</script>