As a Developer I need a component XForm
that generates a form defined in schema and can edit an object.
Acceptance criteria
- it accepts object in v-model
- it accepts array of objects in
fields
property - it renders
<form>
with same amount of child elements as length offields
array - each
field
in fields describes a configuration for a child -
field.component
gets the name of the component (or tag) that should be rendered (for example it can beinput
, orselect
, ordiv
, etc.). Default:input
. -
field.field
get the name of the key of the v-model object to edit -
field.field
value is used asname
attribute of the child - all other properties are binded as html attributes (such as
type
,placeholder
, etc.) - when value of input is changed, it updates v-model
- examples in
App.vue
work as expected:XForm
with paramteresmyObjectN
andmyFieldsN
should render into htmlmyResultN
Example
Let's say that
object = {
name: 'Alex',
dateOfBirth: '06.01.1989',
eyeColor: 'blue',
bio: 'I love apples'
}
and
schema = [
{
field: 'name',
component: 'input',
type: 'text',
placeholder: 'Name',
},
{
field: 'dateOfBirth',
component: 'input',
type: 'datetime-local'
},
{
field: 'eyeColor'
},
{
field: 'bio',
component: 'textarea'
}
]
Given Vue Template should result into Resulting HTML.
Vue Template
<XForm v-model="object" :fields="schema" />
Resulting HTML
<form>
<input name="name" placeholder="Name" />
<input name="dateOfBirth" type="datetime-local" />
<input name="eyeColor" />
<textarea name="bio" />
</form>
How to submit?
- Write in the comments "Challenge accepted".
- Fork stackblitz project and send a new link with your solution as a reply to your original comment.
Unit tests are good to have, but optional.
Share useful articles in the comments.
In a meantime I will start working on a tutorial and a solution. Don't miss it - subscribe to discussion and follow me.
Tips
- If you are new to Vuejs, start with tutorial
Top comments (9)
Challenge accepted
stackblitz.com/edit/vitejs-vite-61...
challenge accepted
stackblitz.com/edit/vitejs-vite-n5...
Challenge Accepted
challenge accepted
stackblitz.com/edit/vitejs-vite-mt...
challenge accepted
stackblitz.com/edit/vitejs-vite-st...