# react-struct-form

> Form

Latest version **1.0.2** (published 2019-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-struct-form
pnpm add react-struct-form
yarn add react-struct-form
bun add react-struct-form
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-10-14 |
| First published | 2019-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >8.0.0 |
| Dependencies | 1 |
| Unpacked size | 134.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ruben Almeida |
| Maintainers | almeidarruben |
| Keywords | react, reactjs, forms, form |

## Links

- npm: https://www.npmjs.com/package/react-struct-form
- Repository: https://github.com/almeidarruben/
- npm.io page: https://npm.io/package/react-struct-form

## Dependencies (1)

- [deepmerge](https://npm.io/package/deepmerge.md) ^4.0.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

- 1.0.2 (latest) — 2019-10-14
- 1.0.1 — 2019-10-14

## README

# react-struct-form

React component and set of utilities to render and manage forms with complex data structures.

[![NPM registry](https://img.shields.io/npm/v/react-struct-form.svg?style=for-the-badge)](https://yarnpkg.com/en/package/react-struct-form) [![NPM license](https://img.shields.io/badge/license-mit-red.svg?style=for-the-badge)](LICENSE.md)

## Installation

```bash
# Yarn
yarn add react-struct-form

# NPM
npm install --save react-struct-form
```

## Usage

Form data can be accessed and managed using `FormContext` component or `useForm` hook.

More examples can be seen [here](/examples).

### `<FormContext.Consumer>`

```js
// PersonForm.js

import { Form, FormContext } from 'react-struct-form';

export default function PersonForm() {
  const formData = {
    firstName: 'John',
  };

  return (
    <Form initialFormData={formData}>
      <FormContext.Consumer>
        {({ getData, updateField }) => (
          <>
            <input
              type="text"
              placeholder="First name"
              value={getData('firstName')}
              onChange={event =>
                updateField('firstName', { value: event.target.value })
              }
            />
            <input
              type="text"
              placeholder="Last Name"
              value={getData('lastName')}
              onChange={event =>
                updateField('lastName', { value: event.target.value })
              }
            />
          </>
        )}
      </FormContext.Consumer>
    </Form>
  );
}
```

### `useForm()`

```js
// InputField.js

import { useForm } from 'react-struct-form';

export default function InputField({ name, placeholder }) {
  const { getData, updateField } = useForm();

  return (
    <input
      type="text"
      name={name}
      placeholder={placeholder}
      value={getData(name)}
      onChange={event => updateField(name, { value: event.target.value })}
    />
  );
}
```

```js
// PersonForm.js

import { Form } from 'react-struct-form';

const formData = {
  firstName: 'John',
};

export default function PersonForm() {
  return (
    <Form initialFormData={formData}>
      <InputField name="firstName" placeholder="First Name" />
      <InputField name="lastName" placeholder="Last Name" />
    </Form>
  );
}
```

## API

[To be done]

## License

MIT © [almeidarruben](https://github.com/almeidarruben)

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