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

gateschema-form-react

v0.2.2

Published

A React component for generating forms from GateSchema.

Downloads

3

Readme

gateschema-form-react Build Status Coverage Status

A React component for generating forms from gateschema-js.

Features

  • GateSchema driven
  • Auto validation
  • Auto updating form data when user inputs value
  • Conditional fields
  • Able to change schema dynamically
  • Extendible, custom form component

How it works

It transforms a gateschema-js instance and the input value into a StateForm state, and use a StateForm implemetation to display the form.

Quick Start

In this example we use stateform-antd as StateForm layer

// file: GateSchemaForm.js

// stateform implementation
import createStateForm from '@stateform/antd'
import "@stateform/antd/dist/stateform-antd.css"

import { createForm } from 'gateschema-form-react'

// 1. create StateForm component
// see https://github.com/stateform/stateform-antd for more details
const StateForm = createStateForm({
  upload: {
    handleUpload(file, cb) {
      // custom implementation
      // cb must be called asynchronously
      setTimeout(() => {
        cb({
          status: 'done', // 'done' | 'error',
          url: 'http://....'
        })
      }, 1000)
    },
    handleRemove(file) {

    }
  },
  components: {
    // custom components
  }
})

// 2. create GateSchemaForm component
const GateSchemaForm = createForm({
  StateForm
})

export default GateSchemaForm
// file: App.jsx
import React from 'react'
import _ from 'gateschema'
// your schema
const schema = _
  .required
  .map({
    name: _
      .required
      .string
      .notEmpty,
    gender: _
      .required
      .enum({
      MALE: 0,
      FEMALE: 1
    }),
  age: _
    .optional
    .number,
  intro: _
    .optional
    .string
    .other('form', {
      component: 'Textarea'
      // StateForm options
      // see https://github.com/stateform/StateForm-Specification
    })
})

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      schema,
      value: {}
    }
  }
  handleSubmit() {
    console.log(this.state.value)
  }
  render() {
    const {schema, value} = this.state
    return (
      <GateSchemaForm
        schema={schema}
        value={value}
      />
    )
  }
}

N.B. As user fills the form, GateSchemaForm will mutate the properties of the passing value object.

Using with redux

// store.js

import { formReducer } from 'gateschema-form-react'
import {createStore, combineReducers} from 'redux'

export default store = createStore(combineReducers({
  form: formReducer
}))
// App.jsx
import React from 'react'
import {Provider} from 'react-redux'
import _ from 'gateschema'
import store from './store'
import GateSchemaForm from './GateSchemaForm'

const schema = _.map({
  //....
})

class App extends React.Component {
  handleSubmit() {
    console.log('submit')
  }
  render() {
    return (
      <Provider store={store}>
        <GateSchemaForm 
          name="myForm" 
          // now the form is binding to store.form.myForm
          schema={schema} 
          onSubmit={handleSubmit}
        >
        </GateSchemaForm>
      </Provider>
    )
  }
}

Live Expamples on CodeSandbox

Install

npm install gateschema-form-react --save  

Usage

Use the other keyword to pass your StateForm options.

Example

const schema = _
  .require
  .map({
    name: _
      .require
      .map({
        firstName: _
          .required
          .string
          .notEmpty
          // StateForm options
          .other('form', {
            placeholder: 'First Name',
            help: 'Your first name',
            cols: {
              item: 6,
              label: 0,
              wrapper: 18
            }
          }),
        lastName: _
          .required
          .string
          .notEmpty
          // StateForm options
          .other('form', {
            placeholder: 'Last Name',
            cols: {
              item: 8,
              label: 0,
              wrapper: 24
            }
          })
      })
    languages: _
      .enumList({
        c: 0,
        java: 1,
        python: 2,
        go: 3,
        js: 4
      })
      // StateForm options
      .other({
        component: 'Select', 
        cols: {
          label: 6,
          wrapper: 18
        }
      })
  })

Q&A

Custom validation ?

This form component is driven by gateschema. You should define your GateSchema keyword for custom validations

Conditional fields ?

Use switch keyword

const schema = _
  .map({
    type: _
      .enum({
        TYPE1: 1,
        TYPE2: 2
      }),
    field0: _
      .optional
      .string
      .switch('/type', [
        {
          case: _.value(1),
          // hidden when type = 1
          schema: _
            .other('form', {
              hidden: true
            })
        },
        {
          case: _.any,
          schema: _.any
        }
      ])
  })
  .switch('/type', [
    {
      case: _.value(1),
      // have field1 when type = 1
      schema: _
        .map({
          field1: _
            .required
            .number
        })
    },
    {
      case: _.value(2),
      schema: _
        .map({
          field2: _
            .required
            .string
            .notEmpty
        })
    }
  ])

Custom component ?

Extend the StateForm implementation

Changelog

See CHANGELOG

License

MIT