npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@altiore/form

v4.5.6

Published

Form helper for building powerful forms

Downloads

740

Readme

Altiore Form

@altiore/form

Productive, flexible and extensible forms with easy-to-use validation and the most user-friendly API @altiore/form

Русская версия README.RU.md

Why?

Let's face it, forms in React are verbose and awkward. This library will help facilitate and speed up the work with forms. It solves the following main problems:

  1. Form validation
  2. Sending data management
  3. Convenient customization of reusable form components (inputs, selects,...)

Peculiarity:

This library, unlike most others, does not store the state of input fields. Within the @altiore/form library, we consider that the data entered into the form is stored on the page. If you need to provide data storage from inputs - you have complete freedom to implement this using your favorite state manager.

This feature means that if you hide the input fields, the data will not be stored.

In other words: hiding data should be equivalent to sending, and at the time of hiding data, you need to save them using your favorite state manager

Installation:

npm

npm i @altiore/form -S

yarn

yarn add @altiore/form

Simplest usage

import React, {useCallback} from 'react';

import {Form} from '@altiore/form';

const MyForm = () => {
  const handleSubmit = useCallback((values) => {
    console.log('form.values is', values);
  }, []);

  return (
    <Form onSubmit={handleSubmit}>
      <input name="name" />
      <button type="submit">Submit</button>
    </Form>
  );
};

Custom field

Allows you to customize the appearance of the input adds validation functionality and several other useful features. Custom Field in details You could use FieldArray for arrays

import React, {useCallback} from 'react';

import {createField, Form} from '@altiore/form';

/**
 * "error" here is added by createField
 * "name" and "label" comes from usage area
 */
const FieldView = ({fieldProps, inputProps, label}) => {
  return (
    <div>
      <label>{label}</label>
      <input {...inputProps} />
      <span>{fieldProps.error}</span>
    </div>
  );
};

export const Field = createField(FieldView);

const MyForm = () => {
  const handleSubmit = useCallback((values) => {
    console.log('form.values is', values);
  }, []);

  return (
    <Form onSubmit={handleSubmit}>
      <Field
        label="Label"
        name="name"
        validate={/* you can add validators here */}
      />
      <button type="submit">Submit</button>
    </Form>
  );
};

Validation

We prefer field-level validation. By analogy with as it is implemented in the browser. But you can also validate all data at the time of sending

import React, {useCallback} from 'react';

import {Form, isEmail, isRequired} from '@altiore/form';

const tooShort = (value) => {
  if (value.length < 5) {
    return 'Too short';
  }
};

const MyForm = () => {
  const handleSubmit = useCallback((values) => {
    console.log('form.values is', values);
  }, []);

  return (
    <Form onSubmit={handleSubmit}>
      <Field label="Email" name="email" validate={[isRequired(), isEmail()]} />
      <Field label="Long" name="long" validate={tooShort} />
      <button type="submit">Submit</button>
    </Form>
  );
};

You can also validate form values while sending them

import React, {useCallback} from 'react';

import {Form, isEmail, isRequired} from '@altiore/form';

const validate = (values) => {
  const errors = {};
  if (values.long?.length < 5) {
    errors.long = 'Too short';
  }

  return errors;
};

const MyForm = () => {
  const handleSubmit = useCallback((values, setErrors) => {
    const errors = validate(values);
    if (Object.keys(errors)?.length) {
      setErrors(errors);
      return;
    }
    console.log('Correct data for sending', values);
  }, []);

  return (
    <Form onSubmit={handleSubmit}>
      <Field label="Long" name="long" />
      <button type="submit">Submit</button>
    </Form>
  );
};

With Typescript

import React, {useCallback} from 'react';

import {FieldProps, createField, Form} from '@altiore/form';

interface IField {
  label: string;
}

/**
 * "error" here is added by createField
 * "name" and "label" comes from usage area
 */
const FieldView = ({fieldProps, inputProps, label}: FieldProps<IField>) => {
  return (
    <div>
      <label>{label}</label>
      <input {...inputProps} />
      <span>{fieldProps.error}</span>
    </div>
  );
};

export const Field = createField<IField>(FieldView);

interface FormState {
  name: string;
}

const MyForm = () => {
  const handleSubmit = useCallback((values: FormState) => {
    console.log('form.values is', values);
  }, []);

  return (
    <Form<FormState> onSubmit={handleSubmit}>
      <Field<FormState>
        label="Label"
        name="name"
        validate={/* you can add validators here */}
      />
      <button type="submit">Submit</button>
    </Form>
  );
};

Detailed Example

import React, {useCallback} from 'react';

import {
  FieldProps,
  createField,
  createFieldArray,
  createSubmit,
  Form,
  isRequired,
} from '@altiore/form';

interface IField {
  label: string;
}

const FieldView = createField<IField>(
  ({fieldProps, inputProps, label}: FieldProps<IField>) => {
    return (
      <div>
        <label>{label}</label>
        <input {...inputProps} />
        <span>{fieldProps.error}</span>
      </div>
    );
  },
);

export interface IFieldArray {
  label?: string;
}

export const FieldIngredients = createFieldArray<IFieldArray>(({list}) => {
  const renderIng = useCallback(({key, remove}) => {
    return (
      <div key={key}>
        <div>
          <div>
            <button onClick={remove} type="button">
              -
            </button>
          </div>
          <div>
            <Field label="Title" name="title" validate={[isRequired(null)]} />

            <Field label="Desc" name="desc" />
          </div>
        </div>
      </div>
    );
  }, []);

  return (
    <>
      {list.map(renderIng)}
      <button onClick={list.add} type="button">
        Add Ingredient
      </button>
    </>
  );
});

export interface ISubmit {
  children: string;
  className?: string;
  skipUntouched?: boolean;
}

export const Submit = createSubmit<ISubmit>(
  ({isInvalid, isSubmitting, isUntouched, ...props}: SubmitProps<ISubmit>) => {
    return (
      <button {...props} disabled={isInvalid || isSubmitting || isUntouched} />
    );
  },
);

interface FormState {
  name: string;
}

const MyForm = () => {
  const handleSubmit = useCallback((values: FormState) => {
    console.log('form.values is', values);
  }, []);

  return (
    <Form<FormState> onSubmit={handleSubmit}>
      <Field<FormState> label="Label" name="name" validate={isRequired(null)} />
      <FieldIngredients label="Ingredients" name="ingredients" />
      <Submit>Submit</Submit>
    </Form>
  );
};

Validation detailed example