Socket
Socket
Sign inDemoInstall

formact

Package Overview
Dependencies
3
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    formact

A zero dependency and design agnostic Form library for React.


Version published
Weekly downloads
23
increased by475%
Maintainers
1
Install size
20.9 kB
Created
Weekly downloads
 

Readme

Source

Formact codecov

A zero dependency and design agnostic Form library for React.

Installation

In your terminal, add it to the project by running:

npm install --save formact

Usage

Creating fields

As formact is design agnostic, you'll need to create your own fields. You can it by using the hook useField in a function component.

This how a TextField would look like:
import { useField, FieldProps } from 'formact'

export default function TextField(props: FieldProps & { label: string }) {
  const field = useField<string>(props)

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'flex-start',
        margin: '5px 10px',
      }}>
      <div>
        <label htmlFor={props.name}>{props.label}</label>
      </div>
      <input
        // @ts-ignore
        data-testid={`field-${props.name}`}
        name={props.name}
        id={props.name}
        type={props.type}
        onBlur={(e) => {
          // call field.onBlur to make sure the field is dirty
          field.onBlur(e)
        }}
        // use field value
        value={field.fieldValue || ''}
        // update the field value
        onChange={(e) => field.update(e.target.value)}
        onKeyDown={(e) => {
          // Submit the form when pressing enter
          if (e.key === 'Enter') {
            field.submit()
          }
        }}
      />
      {field.showError ? (
        <div data-testid={`field-error-${props.name}`} style={{ color: 'red' }}>
          {field.errorMessage}
        </div>
      ) : null}
    </div>
  )
}
A submit button:
export default function SubmitButton() {
  const form = useForm()
  return (
    <button
      data-testid="submit-button"
      type="submit"
      disabled={!form.valid}
      onClick={form.submit}>
      {form.submitting ? 'Submitting...' : 'Submit'}
    </button>
  )
}
Using the fields

These fields become really powerful when wrapped in a Form component.

import Form from "formact"

<Form onChange=(console.log) onSubmit={console.log}>
  <TextField name="country" />
  <SubmitButton />
</Form>

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Keywords

FAQs

Last updated on 17 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc