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

@simosol/forms

v2.0.0

Published

These forms work only with react stateless components. React hooks should be available.

Downloads

39

Readme

Forms

Requirements

These forms work only with react stateless components. React hooks should be available.

Installation

npm install @simosol/forms --save

Basic usage

The example, which includes every use case and possible api call.

import * as React from 'react';
import { Form, useForm, useError, useValidated, ValidationRuleSimple } from '@simosol/forms';
import * as rules from '@simosol/forms/lib/rules';
import FormInput, { KeysOfType } from '@simosol/forms/lib/FormInput';

// prepare common rules
const required = rules.required('Field is required'); // accepts message, returns rule function
const email = rules.email('Invalid e-mail'); // accepts message, returns rule function
const ruleNumber = rules.number('Should be a number'); // accepts message, returns rule function

// custom simple validation rule, check min string length
const customRuleMinLength =
  (min: number, message: string): ValidationRuleSimple =>
    (value: any) => {
      if (typeof value !== 'string' || value.length < min) return message;
    };

// custom complex rule with dependence of other fields
// one field should be less than other field, if values are numeric
const customRuleLess =
  <T, >(otherField: keyof T, message: string) =>
    (value: T[keyof T], form: Form<T>) => {
      const fieldIsNotEmpty = rules.required()(value) === undefined;
      const fieldIsNumber = rules.number()(value) === undefined;
      const otherFieldValue = form.getValue(otherField);
      const otherFieldValidated = form.isValidated(otherField);
      const otherFieldIsNumber = rules.number()(otherFieldValue) === undefined;
      if (
        fieldIsNotEmpty &&
        otherFieldValidated &&
        otherFieldIsNumber &&
        fieldIsNumber &&
        Number(value) >= Number(otherFieldValue)
      ) {
        return message;
      }
    };

const CompanyRegister = () => {
  // prepare data
  const data = React.useState(() => ({
    companyName: 'Your company name', // pre-filled value
    email: '',
    revenue: '',
    profit: '',
    password: '',
    passwordConfirm: '',
  }))[0];

  // create form, using data and validation rules
  const form = useForm(
    data,
    {
      companyName: rules.required('Company name is required'), // custom required message
      email: [email],
      revenue: [required, ruleNumber],
      profit: [ruleNumber, customRuleLess('revenue', 'Should be less than revenue')],
      password: [rules.required('Password is required'), customRuleMinLength(6, 'Minimum password length is 6')],
      passwordConfirm: [
        required,
        rules.same('password', 'Should be same as password'),
      ],
    },
  );

  const onSubmitClick = () => {
    // if all fields are valid, then do something (send request etc.)
    if (form.validateAll()) {
      console.log('do something');
    }
  };
  const onErrorClick = () => {
    // this is needed, when something went wrong
    // show custom email error
    form.setError('email', 'E-mail already exists');
  };

  return (
    <div>
      <Field field={'companyName'} form={form} label={'Company name'}/>
      <Field field={'email'} form={form} label={'E-mail'}/>
      <Field field={'revenue'} form={form} label={'Revenue'}/>
      <Field field={'profit'} form={form} label={'Profit'}/>
      <Field field={'password'} form={form} label={'Password'}/>
      <Field field={'passwordConfirm'} form={form} label={'Confirm password'}/>
      <div>
        <button onClick={onSubmitClick}>Submit</button>
      </div>
      <div>
        <button onClick={onErrorClick}>Error</button>
      </div>
    </div>
  );
};

// simple field component with label and error
const Field = <T, >(
  props: { label: string, field: KeysOfType<T, string>, form: Form<T>},
) => {
  const { field, label, form } = props;
  const error = useError(form, field);
  const validated = useValidated(form, field); // if field was validated at least once
  const borderColor = validated ? (error ? 'red' : 'green') : 'none';
  return (
    <div style={{ paddingBottom: 16 }}>
      <div>{label}</div>
      <div>
        <FormInput style={{ borderColor }} field={field} form={form}/>
      </div>
      <div>{error}</div>
    </div>
  );
};

export default CompanyRegister;