# react-form-with-layout

> A helper to build form layout

Latest version **2.1.0** (published 2018-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-form-with-layout
pnpm add react-form-with-layout
yarn add react-form-with-layout
bun add react-form-with-layout
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-05-19 |
| First published | 2016-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 238 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | code@alvinsj.net |
| Maintainers | alvinsj |
| Keywords | reactjs, form, layout, builder, react-component |

## Links

- npm: https://www.npmjs.com/package/react-form-with-layout
- Repository: https://github.com/blacktangent/react-layout-builder
- Issues: https://github.com/blacktangent/react-layout-builder/issues
- npm.io page: https://npm.io/package/react-form-with-layout

## Dependencies (5)

- [classnames](https://npm.io/package/classnames.md) ^2.1.3
- [object-assign](https://npm.io/package/object-assign.md) ^4.0.1
- [object.assign](https://npm.io/package/object.assign.md) ^4.0.4
- [react-form-utils](https://npm.io/package/react-form-utils.md) ^2.0.0
- [react-layout-builder](https://npm.io/package/react-layout-builder.md) ^2.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-05-19
- 2.0.0 — 2018-05-18
- 1.2.0 — 2018-05-18
- 2.0.0-beta.1 — 2017-11-07
- 2.0.0-alpha.9 — 2017-10-17
- 2.0.0-alpha.8 — 2017-10-17
- 2.0.0-alpha.7 — 2017-10-17
- 2.0.0-alpha.6 — 2017-10-12
- 2.0.0-alpha.5 — 2017-10-12
- 2.0.0-alpha.4 — 2017-10-12
- 2.0.0-alpha.3 — 2017-09-20
- 2.0.0-alpha.2 — 2017-09-20
- 2.0.0-alpha.1 — 2017-09-20
- 1.1.7 — 2017-08-17
- 1.0.15 — 2017-08-17
- … 9 more at https://npm.io/package/react-form-with-layout/versions

## README

# `<FormWithLayout />`

[![Build Status](https://travis-ci.org/blacktangent/react-layout-builder.svg?branch=master)](https://travis-ci.org/blacktangent/react-layout-builder)

`<FormWithLayout />` is a React component to facilitate building of the form fields with a fixed layout.

## Usage

```
$ yarn add react-form-with-layout
```

```
import FormWithLayout from 'react-form-with-layout';
```

### PropTypes
Argument    | Type        | Example
:-----------| :-----------| :-----------
`renderLayout `| `function(builder)`| required. renders layout
`getFieldProps `| `function(name)`      | required. retrieves field props
`renderField `| `function(name, props)`      | required. renders field
`defaultValues `| `object`      | required. values of the form fields
`onSubmit `| `function(e)`      | required. handles form submission
`renderExpandedLayout` | `function(builder)` | similar to `renderLayout`, for layout with more fields
`showAll` | `boolean` | hide/show fields. if it's false, use `renderLayout`. otherwise, use `renderExpandedLayout`
`renderButtons` | `function(props)` | renders buttons at the bottom of form

### Example

```jsx
import React from 'react';
import FormWithLayout from 'react-form-with-layout';
import {formSerialize} from 'react-form-utils';

class FormContainer extends React.Component {
    constructor(props){
        super(props);
        this._handleSubmit = this._handleSubmit.bind(this);
        this.state = {
            values: null,
            errorMessage: null
        };
    }

    render(){
        return (
            <div>
                {this.state.errorMessage}
                <FeedbackForm onSubmit={this._handleSubmit} defaultValues={this.state.defaultValues} />
            </div>);
    }

    _handleSubmit(e){
        if(e) e.preventDefault();

        const values = formSerialize(e.target);
        this.setState({defaultValues: values});

        this.props.postToServer(
            values
        ).then((res) => {
            // navigateToSummary();
        }).catch((err) => {
            this.setState({errorMessage: err.message});
        }});
    }
}

const FeedbackForm = (props) => {
    const renderlayout = (builder) => {
        const {layout, section} = builder;
        const col = (type, ...children) => builder.col(`col-xs-${type}`, ...children);

        return layout(
            section('Section 1',
                [col(6, 'first_name', 'last_name')], // first row: two 6 cols
                [col(5, 'email', 'id_no'), col(2, 'age')], // second row: two 5 cols, one 2 cols
                <button key="submit" onClick={this.submit}>Submit</button>
            )
        );
    };

    const getFieldProps = (name) => {
        return {
            first_name: { type: 'text' },
            last_name: { type: 'text' },
            email: { type: 'email' },
            id_no: { type: 'text' },
            age: { type: 'number' }
        }[name];
    };

    const renderField = (name, fieldProps) => {
        return <input
              key={name}
              type={fieldProps.type}
              name={fieldProps.name}
              defaultValue={fieldProps.defaultValue} />;
    };

    const {defaultValues, onSubmit} = props;
    const formProps = {
        renderLayout,
        getFieldProps,
        renderField,
        defaultValues,
        onSubmit
    };

    return <FormWithLayout {...formProps} />;
}

FeedbackForm.propTypes = {
    defaultValues: PropTypes.object.isRequired,
    onSubmit: PropTypes.func.isRequired
}
```

### DSL for layout
`builder` provides `{layout, section, col}` as helpers. e.g.

```jsx
const {layout, section} = builder;
const col = (type, ...children) =>
    builder.column(`col-${type}`, ...children);

return layout(
    section('Section 1',
        [col(6, 'first_name', 'last_name')], // first row
        [col(5, 'email', 'id_no'), col(2, 'age')], // second row
        <button key="submit" onClick={this.submit}>Submit</button> // regular
    )
);

```

#### builder.`layout`
```jsx
/*
 * @param {node} mainHeader
 * @param {node} section
 * @return {node}
 */
layout(mainHeader, section, section, ...)
// <div className="layout">{sections}</div>
```
#### builder.`section`
```jsx
/*
 * @param {node} sectionHeader
 * @param {node} row
 * @return {node}
 */
section(sectionHeader, row, row, row, ...)
// <section className="section">{rows}</section>
```

#### builder.`col`
```jsx
/*
 * @param {string} className - the group identifier for all columns within.
 * @param {string} fieldName - the name for lookup with `getFieldProps`+`renderField`
 * @return {node}
 */
builder.col(className, fieldName, fieldName, fieldName, ...)

// override the default arguments
const col = (type, ...children) =>
    builder.column(`col-${type}`, ...children);

col('6', fieldName, fieldName, fieldName, ...)
// <div className="col-6">
//   {renderField(fieldName, getFieldProps(name))}
// </div>

```

---
_Source: https://npm.io/package/react-form-with-layout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
