1.0.21 • Published 9 months ago

canathus v1.0.21

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

Canathus

Custom form hook for React

Documentation: https://oflint-1.github.io/canathus/

Getting Started

A guide to getting started with using canathus in your projects.

--Installation--

Canathus can be installed into your react application with one command:

npm install canathus

--Basic Example--

This is a small example usage, to get up and running with canathus

import { useInput, validate } from "canathus";

export function App() {
    // Setup input with validator
    const [title, setTitle] = useInput<string>("", (value) => {
        const valid = value.length > 0;
        return {
            valid,
            errorMsg: valid ? "" : "This field is required",
        };
    });

    const handleSubmit = (e: any) => {
    e.preventDefault();
    // Validate fields
    console.log(validate({ title }));
    };

    return (
    <form onSubmit={(e) => handleSubmit(e)}>
        <input
            value={title.value}
            onChange={(e) => setTitle(e.target.value)}
            placeholder="Enter a title"
        />
        <button>submit</button>
    </form>
    );
}

--Creating Input Data--

In canathus, you use the useInput hook to create input data. It takes two arguments, a default value and then a validator function. The validator function is run whenever you validate your input.

const [data, setData] = useInput("test", validator);

Here, we are setting the default value as "test", and setting a validator function.

--Custom Validators--

Canathus is built to use custom validation for input data. The validator should return an object containing whether the current value is valid, and what the error message is.

Below is an example validator, which checks the length of a string:

const validator = (value) => {
    const valid = value.length > 0;
    
    return {
        valid,
        errorMsg: valid ? "" : "This field is required",
    };
};

--Recommended Workflow--

For readability purposes, it is recommended to store your validator functions in a separate directory called validators. These can then be imported into your react component containing the form.

First we can create our validator:

/* validators/lengthValidator.ts */
export const lengthValidator = (value: string) => {
    const valid = value.length > 0;
    
    return {
        valid,
        errorMsg: valid ? "" : "This field is required",
    };
};

Now we can use this within our form:

/* App.ts */
import { useInput, validate } from "canathus";
import { lengthValidator } from "./validators/lengthValidator.js"

export function App() {
    // Setup input with validator
    const [title, setTitle] = useInput<string>("", lengthValidator);

    const handleSubmit = (e: any) => {
    e.preventDefault();
    // Validate fields
    console.log(validate({ title }));
    };

    return (
    <form onSubmit={(e) => handleSubmit(e)}>
        <input
            value={title.value}
            onChange={(e) => setTitle(e.target.value)}
            placeholder="Enter a title"
        />
        <button>submit</button>
    </form>
    );
}
1.0.21

9 months ago

1.0.20

9 months ago

1.0.19

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago