Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ethicdevs/react-forms-hook

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ethicdevs/react-forms-hook

A small and simple way to deal with Forms in React, looks a bit like formik, but simpler

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

react-forms-hook

NPM MIT License Average issue resolution time Number of open issues

A small and simple way to deal with Forms in React, looks a bit like formik, but much much simpler.

Installation

$ yarn add @ethicdevs/react-forms-hook
# or
$ npm i @ethicdevs/react-forms-hook

Usage

This example is made with react-native components (i.e. View) but this lib also works with regular react apps!

import React, { FC } from "react";
import { TextInput, View } from "react-native";
import { Form, useForm } from "@ethicdevs/react-forms-hook";

type MyScreenFormValues = {
  foo: string;
  bar: string;
  baz: string;
  quxx: string;
};

const FORM_ID = "my-super-form";

const MyScreenWithAForm: FC = () => {
  const { registerField, handleSubmit } = useForm<MyScreenFormValues>(FORM_ID);

  const onFormSubmit = (_, formValues) => {
    console.log("Form was submitted:");
    console.table(formValues);
  };

  return (
    <View>
      <Form id={FORM_ID}>
        {/* Foo */}
        <TextInput
          label={"Foo:"}
          placeholder={"Enter value for foo..."}
          {...registerField("foo", {
            nextField: "bar",
          })}
        />
        {/* Bar */}
        <TextInput
          label={"Bar:"}
          placeholder={"Enter value for bar..."}
          {...registerField("bar", {
            nextField: "baz",
          })}
        />
        {/* Baz */}
        <TextInput
          label={"Baz:"}
          placeholder={"Enter value for baz..."}
          {...registerField("baz", {
            nextField: "quxx",
          })}
        />
        {/* Quxx */}
        <TextInput
          label={"Quxx:"}
          placeholder={"Enter value for quxx..."}
          {...registerField("quxx", {
            returnKeyType: "done",
            returnSubmitsForm: true,
          })}
        />
        {/* Submit button */}
        <Button text={"Submit form"} onPress={handleSubmit(onFormSubmit)} />
      </Form>
    </View>
  );
};

Configuration

The useForm hook and the Form components both accepts an id argument/property in order to support multi-forms.

The useForm hook also takes some options:

  • opts.nextField: Default: undefined. If set to an input name, on input submit (keyboard return key in react-native, enter key in react/browser) the specified field will be focused.
  • opts.returnKeyType: Default: 'next'. React Native specific, allows to set the keyboard return key type.
  • opts.returnSubmitsForm: Default: false. If set to true, when last input gets submitted then it will call the form submit handler (set during the useForm()#handleSubmit execution on first render).

License

MIT

Keywords

FAQs

Package last updated on 08 Jun 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc