1.0.78 • Published 11 months ago

use-inputs v1.0.78

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

useInputs

A useful react hook for managing inputs

  • Great flexibility and super easyToUse :)))

  • Written in TypeScript for better ide suggestion

Installation

yarn add use-inputs

Or if you prefer npm :

npm i use-inputs

Usage

import React from 'react';
import useInputs from 'use-inputs';

const App = () => {
	const { register, Inputs } = useInputs();
	return (
		<form>
			<input {...register('name')} />

			<input {...register('email')} />

			<input {...register('password')} type='password' />

			<button onClick={() => console.log(Inputs)}>Log Inputs</button>
		</form>
	);
};

export default App;

Validation

Adding validation to inputs :

You can use regex or validator function (or even both at the same time) for validation

If you set require : true , the input value can not be empty

import React from 'react';
import useInputs from 'use-inputs';

const App = () => {
	const { register } = useInputs({
		validation: {
			email: {
				regex: /^[\w._%+-]+@[\w-]+\.+.[A-Za-z]{2,}$/,
				errorMsg: 'email is invalid',
			},
			password: {
				required: true,
				validator: value => value.length > 5,
				errorMsg: 'password length most be greater than 5',
			},
		},
	});

	return (
		<form>
			<input {...register('email')} />
			<input {...register('password')} />
			<button>Login</button>
		</form>
	);
};

export default App;

RSuite Supported !

https://rsuitejs.com/components/input/

You can use this hook for custom rsuite <Input> component like this :

import { Input } from 'rsuite';
import useInputs from 'use-inputs';

const App = () => {
	const { register } = useInputs({ isRsuite: true });
	return (
		<form>
			<Input {...register('email')} />
			<Input {...register('password')} />
			// or you can have both at same time !
			<input {...register('normalInput', undefined, false)} />
		</form>
	);
};

export default App;

Features & Methods

below useful methods and objects ,

can be destructed ( const { ... } = useInputs() ) from useInputs

register(name:string); //Register a new input to the hook
Inputs, setInputs(); //Handling inputs states
isDirty(name:string); //Return true if user modified the input
//Validation info about the input
const validation = validOf(name:string);

validation.msg
//Error message if the input is invalid

validation.isValid
//Validity of the input

validation.isValidDirty
//If the input is not modified : true
//If the input is modified & valid : true
//Otherwise : false
isInputsValid; //If all inputs are valid it's true
resetInputs(); // Reset all inputs
getInputsData();
// Get an object only including name and value of the inputs

{
	//Example for code in usage section:
	name:'my name',
	email:'myEmail@test.com',
	password:'mySecretPass'
}
addExtra(name:string,extra:object); // Add extra data to the input

Attention: If the extra object have property with below keys , it will overwrite the input state , so avoid using these keys to extra object :

value , dirty , validation

Tips

  • If you want to have callback on Inputs changes you can use useEffect hook
    useEffect(() => console.log(Inputs), [Inputs]);
1.0.78

11 months ago

1.0.77

12 months ago

1.0.76

12 months ago

1.0.75

2 years ago

1.0.74

2 years ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.68

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.67

2 years ago

1.0.51

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.56

2 years ago

1.0.49

2 years ago

1.0.50

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.15

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago