[](https://travis-ci.org/adrianhelvik/json-form) [](https://coveralls.io/git
npm install @adrianhelvik/json-form

This package can be used to generate forms from a JSON object.
It has 100% test coverage. Contributions are welcome!
``javascript
import JsonForm from '@adrianhelvik/json-form'
const Form = JsonForm({
types: {
string: ({ onChange, value, label }) => (
const schema = {
authorName: 'string',
articles: [{
title: 'string',
content: 'text',
}]
}
Renders the following html:
`html
Author name:
Title:
Title:
...
`As you can see, the keys are transformed from camel case to
space delimited words, with the first letter in upper case.
$3
If you need to use a custom label, use the format below.
Specify a type as you normally would, but under a $type
key, and specify a label under the $label key.
`javascript
const schema = {
author: {
$type: 'string',
$label: 'Your name'
},
articles: {
$type: [{
title: 'string',
content: 'text',
}],
$label: 'A list of nice articles',
}
}
`$3
In some cases you might want to define multiple types of array
editors. One example that I need is a paginated list and a
non-paginated.
Custom array editors are denoted with a symbol in the types
object and with an array with its first item being the given
symbol in the schema.
`javascript
const types = {
string: StringEditor,
[Symbol.for('paginated')]: PaginatedEditor,
}
const schema = {
list: [Symbol.for('paginated'), {
title: 'string'
}]
}
`$3
It is often necessary to change the value of something based
on an independent variable. A rather poor, but quite simple
example of this is an input field with a max length.
`javascript
const types = {
string({ value, onChange, maxLength }) {
return (
value={value}
onChange={e => {
if (maxLength != null && e.target.value.length >= maxLength)
e.target.value = e.target.value.slice(0, maxLength)
onChange(e.target.value)
}}
/>
)
},
number({ value, onChange }) {
return (
type="number"
value={value == null ? '' : String(value)}
onChange={e => {
onChange(parseInt(e.target.value, 10))
}}
/>
)
}
}
const schema = {
text: {
$type: 'string',
$computedProps({ maxLength }) {
return { maxLength }
}
},
maxLength: 'number',
}
`Additionally you can use the prop
computedPropsRest on the
form component. (The one returned from JsonForm). this prop
should be an array. $computedProps is called with the value
as its first argument and the computedPropsRest` as the