Skip to content

📋 Awesome React Hook. It supports controlled and uncontrolled inputs.

License

Notifications You must be signed in to change notification settings

SlDo/react-awesome-form

Repository files navigation

Install

npm install react-awesome-form

Links

Quickstart

import "./styles.css";
import { useInput } from "react-awesome-form";

export default function App() {
  const firstName = useInput(true, "", {
    regExp: /^[a-zA-Zа-яА-Я ,.'-]+$/i,
    required: true
  });

  const lastName = useInput(true, "", {
    regExp: /^[a-zA-Zа-яА-Я ,.'-]+$/i,
    required: true
  });

  return (
    <div className="App">
      <h1 class="title">React Awesome Form</h1>
      <form className="form">
        <label className="label">
          <input
            placeholder="Last Name"
            className="input"
            type="text"
            {...lastName.handlers}
            autoComplete={false}
          />
          {lastName.error && <span className="error">Incorrect last name</span>}
        </label>
        <button type="submit" className="button">
          Send
        </button>
      </form>
    </div>
  );
}

API

type InputValue = string | number | null | undefined;

interface InputProps {
  regExp?: RegExp,
  required?: boolean
}

interface IuseInputReturn {
  resetInput: () => void, 
  handlers: {
    onBlur: (e: any) => void, 
    onInput: (e: any) => void
  }, 
  setValue: (value: T) => void, 
  error: boolean | undefined, 
  value: () => InputValue
};

function useInput<T>(controlled: boolean, initial: T, options?: InputProps): IuseInputReturn

Main function useInput. The first argument is a boolean type (required argument), which declare controlled or uncontrolled input. The second one is a initial state of the input (required argument). The third one is an additional option for form (optional argument).

About

📋 Awesome React Hook. It supports controlled and uncontrolled inputs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published