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

use-input-value

v1.0.0

Published

React hook that allow us an easy validation and manage styles

Downloads

5

Readme

useInputValue

React hook that allow us an easy validation and manage styles.

Versión español

const email = useInputValue()

1. Paramaters

Receive an object with the following keys

| Option | Description | Type | | ------------------ | :--------------------------------------------------------------------------------------------- | --------- | | initialValue | The initial value of the input | any | | required | Says if the input is required | boolean | | regEx | A regular expression to validate | RegExp | | customValidation | A callback to perform a custom validation that return boolean | fn | | errorMessage | An object with messages for the three types of errors required, regExp, customValidation | fn |

Examples

Username validation

A username validation where the input is required and need to match with a regular expression

const regExpUsername = /^(?=.{5,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/
const username = useInputValue({
  initialValue: '',
  required: true,
  regExp: regExpUsername,
})

Password matches

A password validation where the password inputs must be equals perfomed by a customValidation

const password1 = useInputValue({
  initialValue: '',
  required: true,
  regExp: regExpPassword,
})

const password2 = useInputValue({
  initialValue: '',
  required: true,
  customValidation: (value) => {
    return value == password1.value
  },
})

2. Return

| Name | Description | Type | | ----------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | | value | The value of the field | any | | onChange | The function that update the state of the value | fn | | valid | Indicates if the field entry is valid | boolean | | showError | A variable that helps show error styles. Avoid displaying an error when the field has not been touched. | boolean | | errors | An object with the errors present in the validation, could be empty or coul be one or more of these required, regExp, customValidation | object | | updateShowError | An function to re-check if the input is valid, useful when an user submit the form and dont modified a field | fn |

Examples

With className

You can use the key showError to put a class to show the correct error styles. Or use showError, required, regExp, customValidation to show error messages, in this case show just one of the possible errors at the same time, and prefer to show the required error message when the field is empty above the other possible errors. If the field is not empty can display another error if exists.

<input
  type="password"
  {...password1}
  className={`input ${password1.showError? 'error': ''}`}
/>
{
  password1.errors.required && password1.showError ?
  <Error>{password1.errors.required}</Error>
  : null
}
{
  password1.errors.regExp && password1.showError &&
 !password1.errors.required ?
  <Error>{password1.errors.regExp}</Error>
 : null
}
<input
  type="password"
  {...password2}
  className={`input ${password1.showError? 'error': ''}`}
/>
{
  password2.errors.customValidation && password2.showError
  && !password2.errors.required ?
  <Error>{password2.errors.customValidation}</Error>
: null
}

With styled-components

You can pass directly the all the variables using the spread operation and use styled-components to show the correct styles.

<Input type="password" {...password1} />
{/*
 Code to show the specific message error
 */}
<Input type="password" {...password2} />
{/*
  Code to show the specific message error
 */}

showError and valid are provided by the hook.

export const Input = styled.input`
  height: 32px;
  margin-bottom: 6px;
  padding-left: 16px;
  border: 1px solid #382042;
  ${(props) =>
    props.showError &&
    css`
      box-shadow: ${(props) =>
        props.valid ? null : '0 0 3px 2px rgba(255, 30, 30, 0.97)'};
      border-color: ${(props) => (props.valid ? '#382042' : 'transparent')};
    `}
`
export const Error = styled.span`
  font-size: 0.87em;
  color: rgba(255, 30, 30, 0.97);
  padding-left: 7px;
`