• Stars
    star
    228
  • Rank 175,267 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The simplest way to handle forms in React

Simple React Form

travis-ci npm version

Simple React Form is the simplest way to handle forms in React. It helps you make reusable form components in React and React Native.

This is just a framework, you must create the form components or install a package with fields that you will use.

To use with react native check here

Installation

Install the base package

npm install --save simple-react-form

Example

import React from 'react'
import {Form, Field} from 'simple-react-form'
import DatePicker from './myFields/DatePicker'
import Text from './myFields/Text'

class PostsCreate extends React.Component {
  state = {}

  render() {
    return (
      <div>
        <Form state={this.state} onChange={state => this.setState(state)}>
          <Field fieldName="name" label="Name" type={Text} />
          <Field fieldName="date" label="A Date" type={DatePicker} />
        </Form>
        <p>My name is {this.state.name}</p>
      </div>
    )
  }
}

Contributions

Docs

Using with state

In this example, the current value of the form will be stored in this.state

import React from 'react'
import {Form, Field} from 'simple-react-form'
import DatePicker from './myFields/DatePicker'
import Text from './myFields/Text'

class PostsCreate extends React.Component {
  state = {}

  render() {
    return (
      <div>
        <Form state={this.state} onChange={state => this.setState(state)}>
          <Field fieldName="name" label="Name" type={Text} />
          <Field fieldName="date" label="A Date" type={DatePicker} />
        </Form>
        <p>My name is {this.state.name}</p>
      </div>
    )
  }
}

Using without state

In this example, the current value of the form will be stored inside the Form component and passed in the onSubmit function. The difference on this is that the state prop does not change over time.

import React from 'react'
import {Form, Field} from 'simple-react-form'
import DatePicker from './myFields/DatePicker'
import Text from './myFields/Text'

class PostsCreate extends React.Component {
  state = {}

  onSubmit({name, date}) {}

  render() {
    return (
      <div>
        <Form ref="form" state={this.props.initialDoc} onSubmit={this.onSubmit}>
          <Field fieldName="name" label="Name" type={Text} />
          <Field fieldName="date" label="A Date" type={DatePicker} />
        </Form>
        <button onClick={() => this.refs.form.submit()}>Submit</button>
      </div>
    )
  }
}

Field Types

React Simple Form is built from the idea that you can create custom components easily.

Basically this consist in a component that have the prop value and the prop onChange. You must render the value and call onChange passing the new value when the value has changed.

import UploadImage from '../components/my-fields/upload'

<Field fieldName='picture' type={UploadImage} squareOnly={true}/>

Creating the field type

You must create a React component.

import React from 'react'

export default class UploadImage extends React.Component {
  render() {
    return (
      <div>
        <p>{this.props.label}</p>
        <div>
          <img src={this.props.value} />
        </div>
        <TextField
          value={this.props.value}
          hintText="Image Url"
          onChange={event => this.props.onChange(event.target.value)}
        />
        <p>{this.props.errorMessage}</p>
      </div>
    )
  }
}

You can view the full list of props here.

React Native

With React Native the api is the same, but you must enclose the inner content of the form with a View component.

Example:

import React from 'react'
import {View, TextInput} from 'react-native'

export default class TextFieldComponent extends React.Component {
  render() {
    return (
      <View>
        <TextInput
          style={{height: 40, borderColor: 'gray', borderWidth: 1}}
          onChangeText={this.props.onChange}
          value={this.props.value}
        />
      </View>
    )
  }
}

Render the form in the component you want

import Text from '../components/my-fields/text'

<Form state={this.state} onChange={changes => this.setState(changes)}>
  <View>
    <Field fieldName='email' type={Text}/>
    <Field fieldName='password' type={Text}/>
  </View>
</Form>

You should always render your fields inside a View when using react native.

WithValue High order component

If you need to get the current value of the form in the children you can use the WithValue component

import React from 'react'
import {Form, Field, WithValue} from 'simple-react-form'

export default class Example extends React.Component {
  render() {
    return (
      <div>
        <Form>
          <WithValue>
            {value => (
              <div>
                <p>The name is {value.name}</p>
                <Field fieldName="name" label="Name" type={Text} />
              </div>
            )}
          </WithValue>
        </Form>
      </div>
    )
  }
}

Contributors

More Repositories

1

searchable

A php trait to search laravel models
PHP
2,011
star
2

roles

The most advanced roles package for meteor
JavaScript
88
star
3

react-deploy-s3

Deploy create react app's in AWS S3
JavaScript
67
star
4

meteor-router-layer

A layer for Meteor Routers
JavaScript
48
star
5

atom-graphql-autocomplete

Autocomplete and lint from a GraphQL endpoint in atom.
JavaScript
48
star
6

react-apollo-decorators

Better decorators for Apollo and React
JavaScript
39
star
7

react-meteor-data

Fetch Meteor data in React using decorators
JavaScript
38
star
8

simple-react-form-material-ui

A set of fields for simple-react-form that use material-ui
JavaScript
34
star
9

meteor-react-form

Automatic forms creation with Simple Schema and React
JavaScript
34
star
10

reactive-templates

Create templates that you can override in meteor
JavaScript
29
star
11

server-boilerplate

Orionsoft Meteor GraphQL Server Boilerplate
JavaScript
20
star
12

react-linkedin-login

React LinkedIn Login button
JavaScript
15
star
13

waveshosting

JavaScript
13
star
14

graphql-loader

A ecosystem for package authors to inject schemas to apollo apps
JavaScript
11
star
15

graphql-compiler

Compile .graphql files in Meteor
JavaScript
10
star
16

stripe-graphql

Simple Stripe and GraphQL integration
JavaScript
9
star
17

excel-export

Export excel files with Meteor
JavaScript
8
star
18

orion-exporter

Export and import all your Orion data
JavaScript
8
star
19

orion-ga

Google analytics made for orion
JavaScript
7
star
20

simple-react-form-examples

Examples for Simple React Form
JavaScript
6
star
21

options

Reactive app options for meteor
JavaScript
5
star
22

orion-admin-only-bootstrap

Load boostrap and fontawesome only in the admin
JavaScript
5
star
23

s3-static-proxy

Server side rendering solution for create react apps
JavaScript
4
star
24

apollo-hooks

TypeScript
3
star
25

email-queue

Add emails to a queue and send them one by one
JavaScript
3
star
26

orion-users-with-roles-attribute

Users attribute with roles filter
JavaScript
3
star
27

material-blog

JavaScript
2
star
28

orion-stripe

Orion integration with stripe payments
JavaScript
2
star
29

flow

Pagos Flow Chile (webpay) para meteor
JavaScript
2
star
30

uai-auxiliares

Objective-C
1
star
31

files-service-orion-provider

Orion filesystem provider for files-service
JavaScript
1
star
32

laravel-angular-cms

PHP
1
star
33

fuzzyfilepath-remake

JavaScript
1
star
34

uai-android

Java
1
star
35

orion-rut-input

Rut input for orion
JavaScript
1
star
36

printer

Electron app to connect to thermal printers
JavaScript
1
star
37

react-app-events

Use global events in your React app with hooks
TypeScript
1
star