# stimulus-nested-form

> Stimulus Controller to manage nested form fields

Latest version **3.0.1** (published 2021-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install stimulus-nested-form
pnpm add stimulus-nested-form
yarn add stimulus-nested-form
bun add stimulus-nested-form
```

## 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 | 3.0.1 |
| Published | 2021-11-22 |
| First published | 2019-09-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Olivier |
| Maintainers | olimart |

## Links

- npm: https://www.npmjs.com/package/stimulus-nested-form
- Repository: https://github.com/olimart/stimulus-nested-form
- npm.io page: https://npm.io/package/stimulus-nested-form

## Dependencies (1)

- [@hotwired/stimulus](https://npm.io/package/@hotwired/stimulus.md) >=3.0.0

## Recent versions

- 3.0.1 (latest) — 2021-11-22
- 3.0.0 — 2021-11-22
- 2.0.0 — 2020-12-08
- 1.0.3 — 2019-09-18
- 1.0.2 — 2019-09-17
- 1.0.1 — 2019-09-17

## README

# Stimulus nested form controller

Credit goes to [GoRails.com](https://gorails.com/episodes/dynamic-nested-forms-with-stimulus-js) and
[DriftingRuby](https://www.driftingruby.com/episodes/creating-and-publishing-a-stimulus-controller-library)

## Installation

This assumes StimulusJS is already [installed](https://stimulus.hotwired.dev/handbook/installing).

1. Add package `yarn add stimulus-nested-form`
2. Import package in your application

```javascript
import NestedForm from "stimulus-nested-form"
application.register("nested-form", NestedForm)
```

## Example

```html
<div data-controller="nested-form">
  <template data-nested-form-target="template">
    <%= f.fields_for :options, ProductOption.new, child_index: "NEW_RECORD" do |ff| %>
      <%= render "product_options/option_fields", field: ff %>
    <% end %>
  </template>

  <%= f.fields_for :options do |ff| %>
    <%= render "product_options/option_fields", field: ff %>
  <% end %>
  <p data-nested-form-target="links">
    <%= link_to t("product_option.add"), "#",
        class: "btn btn-link",
        data: { action: "click->nested-form#add_association" } %>
  </p>
</div>

<!-- product_options/_option_fields.html.erb -->
<%= tag.div class: 'nested-fields', data: { new_record: field.object.new_record? } do %>
  <div class='row'>
    <div class='col-md-6'><%= field.input :name %></div>
    <div class='col-md-2 d-flex align-items-center'>
      <%= link_to t('product_option.remove'), '#', data: { action: 'click->nested-form#remove_association' }, class: 'text-danger' %>
    </div>
  </div>
  <%= field.hidden_field :_destroy %>
<% end %>
```

## Publish new version

Install Microbundle if not present yet `npm -g install microbundle`

1. Run `yarn build`
2. Update `CHANGELOG.md`
3. Run `yarn publish`

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