Last active
July 19, 2021 00:18
-
-
Save 34fame/d9f96cf43ff5008d4d9a5911d1faf18c to your computer and use it in GitHub Desktop.
Dynamically Render Form Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export default { | |
name: 'MyFormField', | |
props: { | |
field: { | |
type: Object, | |
required: true, | |
}, | |
input: { | |
type: Function, | |
required: true, | |
}, | |
model: { | |
type: Object, | |
required: true, | |
} | |
}, | |
methods: { | |
renderLabel(h) { | |
return h('div', { | |
class: ['text-caption'], | |
},[ | |
this.field.label | |
]) | |
}, | |
renderField(h) { | |
return h(this.field.component, { | |
class: [ | |
...this.field.fieldOptions.class, | |
], | |
attrs: { | |
...this.field.fieldOptions.attrs, | |
}, | |
props: { | |
...this.field.fieldOptions.props, | |
value: this.model[this.field.model] | |
}, | |
style: { | |
...this.field.fieldOptions.style, | |
}, | |
on: { | |
input: (value) => { | |
this.input(value, this.field.model) | |
} | |
} | |
}) | |
}, | |
}, | |
render(h) { | |
return h('div', { | |
class: ['column', 'q-gutter-xs'], | |
}, [ | |
this.renderLabel(h), | |
this.renderField(h), | |
]) | |
}, | |
} | |
</script> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<q-form class="q-gutter-md"> | |
<my-form-field | |
v-for="field in fields" | |
:key="field.model" | |
:field="field" | |
:input="(value,field) => handleInput(value,field)" | |
:model="model" | |
/> | |
</q-form> | |
</template> | |
<script> | |
export default { | |
name: 'MyForm', | |
data() { | |
return { | |
model: {}, | |
fields: [ | |
{ | |
component: 'q-input', | |
label: 'Name', | |
model: 'name', | |
fieldOptions: { | |
class: [], | |
on: ['input'], | |
attrs: { | |
placeholder: 'Enter your full name', | |
}, | |
props: { | |
filled: true, | |
}, | |
style: { | |
width: '320px', | |
}, | |
}, | |
}, | |
{ | |
component: 'q-select', | |
label: 'Gender', | |
model: 'gender', | |
fieldOptions: { | |
class: [], | |
on: ['input'], | |
attrs: {}, | |
props: { | |
filled: true, | |
options: [ 'Female', 'Male' ] | |
}, | |
style: { | |
width: '200px', | |
}, | |
}, | |
}, | |
], | |
} | |
}, | |
methods: { | |
handleInput(value, field) { | |
this.$set(this.model, field, value) | |
} | |
}, | |
components: { | |
MyFormField: () => import('components/MyFormField.vue'), | |
}, | |
} | |
</script> | |
<style scoped> | |
</style> |
Additional details for Quasar:
In this gist, you can see in the "MyForm.vue" that I refer to components 'q-input' and 'q-select'.
When using the Vue render method we have to manually include these Quasar components in the quasar.conf.js file under "framework -> components". By using render() we are bypassing the setup automatically created with the quasar create
command.
So you would need something like this:
framework: {
components: [
'QInput',
'QSelect'
],
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the greatest thing I've built in months. So stoked and thank you to @metalsadman for helping me get it the last mile. This completely changes how I will manage my data models going forward!
General idea is that you define your form fields as schema. Your Form component will iterate these field definitions and the Field component will render the specified UI component with all the settings specified. It's beautiful in action. You can use ANY UI component you want.
Warning for Quasar users: you will need to include any UI component you render in your quasar.conf file. Even if you have importStrategy set to 'auto'. That only works when the components are visible in the template when being rendered. Since this is dynamic that no longer works.