# stimulus-form-state

> A Stimulus controller for tracking changes on form elements.

Latest version **0.1.2** (published 2022-08-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-08-14 |
| First published | 2022-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Matthew Kennedy |
| Maintainers | matthewkennedy |
| Keywords | stimulus, stimulusjs |

## Links

- npm: https://www.npmjs.com/package/stimulus-form-state
- Repository: git@github.com:matthewkennedy/stimulus-form-state
- Homepage: https://github.com/matthewkennedy/stimulus-form-state
- Issues: https://github.com/matthewkennedy/stimulus-form-state/issues
- npm.io page: https://npm.io/package/stimulus-form-state

## Recent versions

- 0.1.2 (latest) — 2022-08-14
- 0.1.1 — 2022-08-04
- 0.1.0 — 2022-07-29
- 0.0.6 — 2022-07-29
- 0.0.5 — 2022-07-28
- 0.0.4 — 2022-07-27
- 0.0.3 — 2022-07-27
- 0.0.2 — 2022-07-27

## README

# Stimulus Form State
[![Test](https://github.com/MatthewKennedy/stimulus-form-state/actions/workflows/test.yml/badge.svg)](https://github.com/MatthewKennedy/stimulus-form-state/actions/workflows/test.yml)
[![Lint](https://github.com/MatthewKennedy/stimulus-form-state/actions/workflows/lint.yml/badge.svg)](https://github.com/MatthewKennedy/stimulus-form-state/actions/workflows/lint.yml)

## Getting started

A Stimulus controller for tracking changes to form elements such as: `<input>`, `<select>`, `<textarea>`
allowing you to manipulate the form submit `<button>` or other DOM elements if a form element has been changed from its original state.

## 📚 Documentation

### Install
```bash
yarn add stimulus-form-state
```

### Extending Controller
```javascript
// javascript/controllers/form_state_controller.js

import StimulusFormState from 'stimulus-form-state'

export default class extends StimulusFormState {

  // Override the change methods to manipulate the DOM as you wish.
  enableChangeControles () {
    // When a form element has been changed.
    document.getElementById('inputStateSubmitButton').style.display = 'inline'
    super.enableChangeControles()
  }

  disableChangeControles () {
    // If a previously changed form element is then set back to its original state.
    document.getElementById('inputStateSubmitButton').style.display = 'none'
    super.disableChangeControles()
  }
}
```

### In The DOM

Add `data-controller="form-state"` to the form element wrapping the elements you want to track the state of, including the submit `<button>`.

Add `data-form-state-target="watch"` to the `<select>`, `<textarea>` or `<input>` elements that you want to track state of. The correct stimulus data-action will be added to the watched element unless a preexisting data-action is present.

Add `data-form-state-target="saveButton"` to the from submit button, by default the button will be disabled unless a watched element is changed.

An example:
```html
<form data-controller="form-state" action="/customer" method="post">
  <input data-form-state-target="watch" type="text" value="Joe" name="first_name">
  <input data-form-state-target="watch" type="text" value="Blogs" name="last_name">
  <textarea data-form-state-target="watch" name="description">Some Value.</textarea>

  <button type="submit" data-form-state-target="saveButton">Save</button>
</form>
```

## 👷‍♂️ Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

## 📝 License

This project is released under the [MIT](http://opensource.org/licenses/MIT) license.

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