# svelte-final-form

> 🏁 High performance subscription-based form state management for Svelte

Latest version **1.2.3** (published 2020-12-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install svelte-final-form
pnpm add svelte-final-form
yarn add svelte-final-form
bun add svelte-final-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.2.3 |
| Published | 2020-12-22 |
| First published | 2020-05-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Alexey Solilin |
| Maintainers | solilin |
| Keywords | svelte-final-form, final-form, forms, validation, svelte |

## Links

- npm: https://www.npmjs.com/package/svelte-final-form
- Repository: https://github.com/jetrockets/svelte-final-form
- Homepage: https://github.com/jetrockets/svelte-final-form#readme
- Issues: https://github.com/jetrockets/svelte-final-form/issues
- npm.io page: https://npm.io/package/svelte-final-form

## 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.2.3 (latest) — 2020-12-22
- 1.2.2 — 2020-11-13
- 1.2.1 — 2020-11-11
- 1.2.0 — 2020-11-10
- 1.1.5 — 2020-07-29
- 1.1.4 — 2020-07-29
- 1.1.3 — 2020-07-28
- 1.1.2 — 2020-07-28
- 1.1.1 — 2020-06-10
- 1.1.0 — 2020-06-08
- 1.0.3 — 2020-06-08
- 1.0.2 — 2020-05-07
- 1.0.1 — 2020-05-07

## README

# 🏁 Svelte Final Form

Svelte Final Form is a thin Svelte wrapper for [Final Form](https://final-form.org)

✅ Zero dependencies

✅ Only peer dependencies: Svelte and [🏁 Final Form](https://github.com/final-form/final-form#-final-form)

✅ Opt-in subscriptions - only update on the state you need!

---

Before we jump right into code, you might want to learn a little bit about the [philosophy](https://final-form.org/docs/react-final-form/philosophy) and origin story of Final Form.

## Installation

```bash
npm install --save final-form svelte-final-form
```

or

```bash
yarn add final-form svelte-final-form
```

### Architecture

Svelte Final Form is a thin Svelte wrapper for Final Form, which is a subscriptions-based form state management library that uses the Observer pattern.

By default, Svelte Final Form subscribes to all changes, but if you want to fine tune your form to optimized blazing-fast perfection, you may specify only the form state that you care about for rendering your gorgeous UI. You can think of it a little like GraphQL's feature of only fetching the data your component needs to render, and nothing else.

```html
<script>
  import { Form, Field } from "svelte-final-form";

  // Just for example
  import Select from "svelte-select";
  // Your custom form group adapter
  import FormGroup from "components/FormGroup";

  const selectItems = ["Green", "Red", "Black"];

  const initialValues = {
    firstName: "Alexey",
    lastName: "Solilin",
    color: "Red",
  };

  const onSubmit = async (values) => {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    console.log(JSON.stringify(values, undefined, 2));
  };

  const validate = (values) => {
    const errors = {};
    if (!values.firstName) {
      errors.firstName = "Required";
    }
    if (!values.lastName) {
      errors.lastName = "Required";
    }
    return errors;
  };
</script>

<Form {onSubmit} {validate} {initialValues} let:form let:state>
  <form on:submit|preventDefault={form.submit}>
    <Field name="firstName" let:input let:meta>
      <label for="firstName">First Name</label>
      <input
        name={input.name}
        on:blur={input.onBlur}
        on:focus={input.onFocus}
        on:input={(e) => input.onChange(e.target.value)}
        type="text"
        placeholder="Last Name"
        value={input.value} />
      {#if meta.touched && meta.error}
        <div>{meta.error}</div>
      {/if}
    </Field>

    <!-- You can prepare you Form Group Adapter with Label, Input, Errors -->
    <Field name="lastName" let:input let:meta>
      <FormGroup label="Last Name" type="text" {...input} {...meta} />
    </Field>

    <!-- Example for svelte-select -->
    <Field name="color" let:input let:meta>
      <Select
        items={selectItems}
        on:blur={input.onBlur}
        on:focus={input.onFocus}
        on:select={({ detail }) => input.onChange(detail.value)}
        selectedValue={input.value}
        name="color"
        />
    </Field>

    <button type="submit" disabled={state.submitting}>Submit</button>
    or
    <button disabled={state.pristine} on:click={() => form.reset(initialValues)}>Reset</button>
  </form>
</Form>
```

### TODO

- [ ] Write tests
- [ ] Final Form Arrays
- [ ] More docs and CodeSandbox examples for
  - [ ] Conditional Fields
  - [ ] Mutators
  - [ ] Decorators
  - [ ] Validation
  - [ ] Custom Form Adapters

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