npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adrianhelvik/json-form

v3.0.1

Published

[![Build Status](https://travis-ci.org/adrianhelvik/json-form.svg?branch=master)](https://travis-ci.org/adrianhelvik/json-form) [![Coverage Status](https://coveralls.io/repos/github/adrianhelvik/json-form/badge.svg?branch=master)](https://coveralls.io/git

Downloads

75

Readme

@adrianhelvik/json-form

Build Status Coverage Status

This package can be used to generate forms from a JSON object. It has 100% test coverage. Contributions are welcome!

Note

This is still experimental.

Example

import JsonForm from '@adrianhelvik/json-form'

const Form = JsonForm({
  types: {
    string: ({ onChange, value, label }) => (
      <div>
        {label}: <input onChange={e => onChange(e.target.value)} value={value} />
      </div>
    ),
    text: ({ onChange, value, label }) => (
      <div>
        {label}: <textarea onChange={e => onChange(e.target.value)} value={value} />
      </div>
    )
  }
})

const schema = {
  authorName: 'string',
  articles: [{
    title: 'string',
    content: 'text',
  }]
}

<Form schema={schema} value={...} onChange={...} />

Renders the following html:

<div>
  Author name: <input>
</div>
<div>
  <div>
    Title: <input>
  </div>
  <div>
    Title: <input>
  </div>
  ...
  <button>Add item</button>
</div>

As you can see, the keys are transformed from camel case to space delimited words, with the first letter in upper case.

Explicit labels

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.

const schema = {
  author: {
    $type: 'string',
    $label: 'Your name'
  },
  articles: {
    $type: [{
      title: 'string',
      content: 'text',
    }],
    $label: 'A list of nice articles',
  }
}

Multiple array editors

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.

const types = {
  string: StringEditor,
  [Symbol.for('paginated')]: PaginatedEditor,
}
const schema = {
  list: [Symbol.for('paginated'), {
    title: 'string'
  }]
}

Dynamic editors with $computedProps

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.

const types = {
  string({ value, onChange, maxLength }) {
    return (
      <input
        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 (
      <input
        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 rest argument. See the tests for more info on this.

Licence

MIT, see LICENCE file