GithubHelp home page GithubHelp logo

whizzes / svelte-forms Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 1021 KB

Form utilities for Svelte

Home Page: https://www.npmjs.com/package/@whizzes/svelte-forms

License: MIT License

JavaScript 1.84% TypeScript 98.16%
forms library svelte utility

svelte-forms's Introduction

Svelte Forms

Utilities for Svelte Forms Management

Discord Build Tests Lint Publish Version Downloads

Installation

# npm
npm install @whizzes/svelte-forms

# yarn
yarn add @whizzes/svelte-forms

# pnpm
pnpm add @whizzes/svelte-forms

Examples

Creating a new form

<script lang="ts">
  import * as Sentry from '@sentry/browser';
  import { newForm } from '@whizzes/svelte-forms';
  import * as Yup from 'yup';

  import { EmailAlreadyTaken } from '$lib/errors';
  import { agentService } from '$lib/services/agent';
  import { notificationStore } from '$lib/stores/notification';

  const { handleSubmit, values, errors, isSubmitting } = newForm({
    initialValues: {
      name: 'James'
      lastName: 'Bond',
      nickname: 'Agent 007',
      email: '[email protected]',
    },
    validationSchema: Yup.object({
      name: Yup.string().required(),
      lastName: Yup.string().required(),
      nickname: Yup.string().required(),
      email: Yup.string().email().required(),
    }),
    onSubmit: async (values, helpers) => {
      try {
        await agentService.register(values);
      } catch (error) {
        if (error instanceof EmailAlreadyTaken) {
          helpers.setFieldError(
            'email',
            'The email is already taken!');
          return;
        }

        Sentry.captureException(error);

        notificationStore.displayError({
          text: 'An error ocurred registering an agent.'
        });
      }
    },
  });
</script>

<form>
  <div>
    <label>Name</label>
    <input type="text" name="name" bind:value={$values.name} />
    <p class:hidden={!$errors.name}></p>
  </div>
  <div>
    <label>Last Name</label>
    <input type="text" name="lastName" bind:value={$values.lastName} />
    <p class:hidden={!$errors.lastName}></p>
  </div>
  <div>
    <label>Nickname</label>
    <input type="text" name="nickname" bind:value={$values.nickname} />
    <p class:hidden={!$errors.nickname}></p>
  </div>
  <div>
    <label>Email</label>
    <input type="text" name="email" bind:value={$values.email} />
    <p class:hidden={!$errors.email}></p>
  </div>
  <button type="submit" class:disabled={$isSubmitting} disabled={$isSubmitting}>
    Create Account
  </button>
</form>

Releasing

Whenever a tag is pushed a new release is created an the package is published to the NPM registry using GitHub Actions.

Bump the current version using npm as follows:

# for versions with breaking changes use `major`
npm version major

# for versions with non-breaking changes use `minor`
npm version minor

# for patch versions use `patch`
npm version patch

Then push the repository including tag metadata as follows

git push origin main --follow-tags

Contributions

Any contribution to this package is welcome! Don't hesitate on opening a PR or creating an issue!

svelte-forms's People

Contributors

dependabot[bot] avatar estebanborai avatar dararod avatar michael-liendo avatar

Stargazers

Pavel Rusakov avatar Augustine Madu avatar David Arenas avatar Nikolaus Schlemm avatar  avatar

Watchers

 avatar

Forkers

michael-liendo

svelte-forms's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.