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

@interdan/react-validator

v0.0.15

Published

Check is the form valid, shows/hides error messages

Downloads

6

Readme

React validator

Library allows to achieve three main goals for your React application:

  • check the input value for correctness, show/hide an error message

  • check whether or not all the inputs contain valid values

  • show all the errors to the user, scroll automatically to the first input that contains the error

Installation

npm install @interdan/react-validator

Example

import React, { useState } from 'react';
import Validator, { withFormValidation } from '@interdan/react-validator';

const SimpleTextInput = ({ label, checker, errorMessage }) => {
  const [value, setTextValue] = useState('');

  const onChange = (e: any) => setTextValue(e.target.value);

  return (
    <div className="form-control">
      <div className="label">{label}</div>
      <div className="input">
        <Validator errorText={errorMessage} checker={checker}>
          <input type="text" value={value} onChange={onChange} />
        </Validator>
      </div>
    </div>
  );
};

...

const NOT_EMPTY_VALUE_ERROR_TEXT = "Value can't be empty";
const NUMBER_REQUIRED_ERROR_TEXT = 'Please, provide a positive integer';

const notEmptyChecker = (value) => !!value;

const isPositiveNumber = (value) => {
  const intValue = parseInt(value, 10);
  return !!intValue && intValue > 0;
};

const Form = ({ isFormValid, makeErrorsVisible, proceed }) => {
  const handleOnProceed = () => {
    if (!isFormValid) {
      makeErrorsVisible();
      return;
    }

    proceed();
  };

  return (
    <div className="form">
      <SimpleTextInput checker={notEmptyChecker} label="First name" errorMessage={NOT_EMPTY_VALUE_ERROR_TEXT} />
      <SimpleTextInput checker={notEmptyChecker} label="Last name" errorMessage={NOT_EMPTY_VALUE_ERROR_TEXT} />
      <SimpleTextInput checker={isPositiveNumber} label="Your age" errorMessage={NUMBER_REQUIRED_ERROR_TEXT} />
      <button onClick={handleOnProceed}>Proceed</button>
    </div>
  );
};

export default withFormValidation(Form);

withFormValidation is a higher-order component (HOC), that provide your component with two props:

  • isFormValid - boolean variable that indicates whether or not all the inputs contain valid values;
  • makeErrorsVisible - the parameterless method that shows all the errors, if it is necessary - scrolls (with animation) up to the topmost input with wrong data. Of course, you can just disable your "Proceed" button but it's not always obvious what is wrong with entered data for your end-user. The best example when a user gets confused is a situation when he didn't even try to enter any data (just never focused on an input, so the error is hidden). To achieve a better UX it's a good idea to leave your button available and just show what is wrong when the user clicks on it.

Validator - is the React component that should wrap the component the value of which you want to check. You must always provide errorText property with some string or React element value type. There is several ways of how you can use Validator within your app:

  • just with valueIsEmpty property: if this boolean prop is set - true means value is correct and false - value is incorrect. You can use this approach when you need to be sure that the select's value isn't empty, or chech checkbox input or some specific custom control. It's a trivial check of course, but the validator will show/hide error for your app, and will use this data for computing the "isFormValid" value;

  • with checker function. As you can see from the example this function gets only one parameter - input's value and must check whether it correct or not. The result of this checker is cast to boolean type under the hood. So both of the checker functions below are considered as valid:

// value of string type
const notEmptyChecker = value => !!value;

// or just
const notEmptyChecker = value => value;

With checker function you can test any React component that stores the value in a value prop. This value is passed to checker function.

Validator also has two more optional props: className and errorClass that you can use to set some custom css classes for the whole Validator container and error message container accordingly.