npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@beeanco/svelte-form

v0.4.1

Published

Easy forms in svelte

Downloads

2

Readme

@beeanco/svelte-form

Easy forms in svelte

Installation

With node.js installed, use npm to install this package and its peer dependencies:

npm install @beeanco/svelte-form yup

Usage

Basically you only need to use the createForm function to create a form using a yup schema and an action in your svelte component, and use it with the <Form /> component:

<script>
  import { createForm, Form } from '@beeanco/svelte-form';
  import { object, string } from 'yup';

  // Create a yup schema for your form
  const schema = object().shape({
    username: string().required(),
    password: string().required(),
  });

  // The action to run
  async function action(values) {
    console.info(values);
  }

  // Create the form object
  const form = createForm({ schema, action });
</script>

<Form {form}>
  <!-- Your inputs... -->
</Form>

The easiest way to add inputs and buttons to this form is to use @beeanco/svelte-bulma, which contains svelte components for the bulma CSS framework. Don't forget to npm install @beeanco/svelte-bulma!

<!-- LoginForm.svelte -->
<script>
  import { createForm, Form, FieldContext } from '@beeanco/svelte-form';
  import { FormField, ErrorMessage, SubmitField } from '@beeanco/svelte-bulma';
  import { object, string } from 'yup';

  // Create a yup schema for your form
  // (https://www.npmjs.com/package/yup)
  const schema = object().shape({
    username: string().label('Username').required(),
    password: string().label('Password').required(),
  });

  // The function to call with the resulting object
  async function action(values) {
    console.info('A user wants to log in with these credentials:', values);

    // You can call your API here...
    await new Promise((resolve) => setTimeout(resolve, 200));

    // ...and errors thrown here are reported.
    if (Math.random() > 0.5) {
      throw new Error('nope...');
    }
  }

  // Create the form object
  const form = createForm({ schema, action });

  // Get the field stores
  const { value: nameValue, error: nameError } = form.fields.get('username');
  const { value: passwordValue, error: passwordError } = form.fields.get('password');
</script>

<h1>Login to continue</h1>

<Form {form}>
  <FormField name="username" placeholder="Your Username" />
  <FormField name="password" placeholder="Your Password" />

  <ErrorMessage />

  <SubmitField label="Submit" />
</Form>

Of course you can use this package with other CSS frameworks as well. Just use the stores returned by the createForm function.

Complete example without bulma:

<!-- LoginForm.svelte -->
<script>
  import { createForm, Form } from '@beeanco/svelte-form';
  import { object, string } from 'yup';

  // Create a yup schema for your form
  // (https://www.npmjs.com/package/yup)
  const schema = object().shape({
    username: string().required(),
    password: string().required(),
  });

  // The function to call with the resulting object
  async function action(values) {
    console.info('A user wants to log in with these credentials:', values);

    // You can call your API here...
    await new Promise((resolve) => setTimeout(resolve, 200));

    // ...and errors thrown here are reported.
    if (Math.random() > 0.5) {
      throw new Error('nope...');
    }
  }

  // Create the form object
  const form = createForm({ schema, action });

  // Get the field stores
  const { value: nameValue, error: nameError } = form.fields.get('username');
  const { value: passwordValue, error: passwordError } = form.fields.get('password');
</script>

<h1>Login to continue</h1>

<!-- The <Form /> component sets up the svelte contexts -->
<Form {form} let:error let:submitting>
  <!-- let:values is also available -->

  <label>
    Username
    <input type="text" bind:value={$nameValue} />

    {#if $nameError}
      <strong>{$nameError.message}</strong>
    {/if}
  </label>

  <label>
    Password
    <input type="password" bind:value={$passwordValue} />

    {#if $passwordError}
      <strong>{$passwordError.message}</strong>
    {/if}
  </label>

  <label>
    {#if error}
      <strong>Oops! {error.message}</strong>
    {/if}

    {#if submitting}
      <i>Submitting...</i>
    {:else}
      <button type="submit">log in</button>
    {/if}
  </label>
</Form>

<style>
  /* Minimal styling */
  label {
    display: block;
  }
</style>