@slimr/forms
TypeScript icon, indicating that this package has built-in type declarations

5.0.6 • Public • Published

🪶 @slimr/forms npm package

A slim (< 1kB), enhanced HTML form with auto-disabling, auto-reset, error handling, more JS events, and context to its children.

  • optimizes for vanilla, uncontrolled input elements
  • does not cause re-renders
  • disables form elements while submitting
  • can auto-reset the form is desired
  • uses browser accessibility APIs to share errors to form elements
  • wraps onSubmit to
    • auto call event.preventsDefault()
    • pass form values as an Object
  • provide JS events and React context for rejected, submitted, accepted states
  • < 1kB when bundled+gzipped with a broader application

Context

@slimr is a set of slim React (hence '@slimr') libs. Check them all out on github!

Usage

<SForm onSubmit={(_, vals) => console.log(vals)}>
  {[
    'checkbox',
    'color',
    'date',
    'email',
    'number',
    'password',
    'search',
    'text',
    'textarea',
    'tel',
    'url',
  ].map(type => (
    <InputBox key={type} label={type} name={type} type={type} required />
  ))}
  <RadioBox
    label="Radios"
    name="radio1"
    options={[
      {label: 'Choice 1', value: 'choice1'},
      {label: 'Choice 2', value: 'choice2'},
      {label: 'Choice 3', value: 'choice3'},
    ]}
    required
  />
  <SelectBox
    label={'Select Single'}
    name="select1"
    options={[
      {label: '--', value: ''},
      {label: 'Choice 1', value: 'choice1'},
      {label: 'Choice 2', value: 'choice2'},
      {label: 'Choice 3', value: 'choice3'},
    ]}
    required
  />
  <SelectBox
    label={'Select multiple'}
    multiple
    name="select2"
    options={[
      {label: 'Choice 1', value: 'choice1'},
      {label: 'Choice 2', value: 'choice2'},
      {label: 'Choice 3', value: 'choice3'},
    ]}
    required
  />
  <FormFooter />
  <RenderCheck />
</SForm>

API

SForm

An enhanced HTML form with auto-disabling, auto-reset, error handling, more JS events, and context to its children.

SFormError

An extension of Error that accepts an errorSet as a constructor property. It is used to share errors to form elements

throw new FormError({field1: 'This field is invalid'})

useSFormContext

A hook to get the current state of the form

Readme

Keywords

Package Sidebar

Install

npm i @slimr/forms

Weekly Downloads

1

Version

5.0.6

License

ISC

Unpacked Size

46.9 kB

Total Files

12

Last publish

Collaborators

  • bdombro