0.0.23 • Published 2 years ago

@svelte-parts/form v0.0.23

Weekly downloads
25
License
MIT
Repository
github
Last release
2 years ago

@svelte-parts/form

Yet another form component.

The idea here is to define the fields in json. The form definition can then come from the server or be modified on the fly.

Try it out in the svelte REPL

Install

npm install @svelte-parts/form

Usage

<script>
  import Form from '@svelte-parts/form'

  const fields = [
    { type: 'text', property: 'username', label: 'User name', pattern: '[a-z0-9-]+', minLength: 5 },
    { type: 'date', property: 'birthDate', label: 'Birth date' },
    { type: 'email', property: 'email', label: 'Email' },
    { type: 'checkbox', property: 'wantsSpam', label: 'Subscribe to the newsletter', value: true },
  ]
  const onSubmit = data => alert(JSON.stringify(data))
</script>

<Form fields={fields} onSubmit={onSubmit}>

Properties

  • fields the form definition. It is the only required field (see type definition)
  • onSubmit a callback triggered when the form is submitted
  • submitButton is a string to use as a label for the submit button if you don not want to use the browser default
  • errorMessage is a string that will be shown between the last form field and the submit button

Types

<script lang="ts">
  import type { Field } from '@svelte-parts/form'
  import Form from '@svelte-parts/form'

  const fields: Field[] = [
    { type: 'text', property: 'username', label: 'User name', pattern: '[a-z0-9-]+', minLength: 5 },
    { type: 'date', property: 'birthDate', label: 'Birth date' },
    { type: 'email', property: 'email', label: 'Email' },
    { type: 'checkbox', property: 'wantsSpam', label: 'Subscribe to the newsletter', value: true },
  ]
  const onSubmit = data => alert(JSON.stringify(data))
</script>

<Form fields={fields} onSubmit={onSubmit} />
0.0.23

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago