Skip to content

Commit

Permalink
📃 Added useForm (#40)
Browse files Browse the repository at this point in the history
* 📃 Added useForm

* 🦾 Linting Fixes
  • Loading branch information
aromalanil authored Dec 3, 2020
1 parent e7968ef commit 6496f5c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
37 changes: 37 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 📗 Index

- [useLocalStorage](#-uselocalstorage)
- [useForm](#-useform)
- [useStack](#-usestack)
- [useQueue](#-usequeue)
- [useDebounce](#-usedebounce)
Expand Down Expand Up @@ -51,6 +52,42 @@ const LocalValue = () => {

</br>

## 📃 useForm

Custom hook to create controlled form component.

### Usage

```jsx
import React from "react";
import { useForm } from "use-custom-hooks";

const Form = () => {
const [values, onChange] = useLocalStorage({name:"",age:12});

return (
<form>
<input type="text" name="name" value={values.name} onChange={onChange}/>
<input type="number" name="age" value={values.age} onChange={onChange}/>
</form>
);
};
```

### Parameters

1. `initialValue` (_Object_) : State object with name of each form input as keys and corresponding initial state as values.

### Return value

`[values,onChange]`

1. `values` (_Object_) : Values of input components.
2. `onChange` (_function_) : Function to be added to onChange event of input component.

</br>


## 📚 useStack
Hook for creating and managing Stack.

Expand Down
21 changes: 21 additions & 0 deletions hooks/useForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState } from 'react';

/**
*
* Custom hook to create controlled form component.
*
* @param {Object} initialState State object with name of each form input as keys and
* corresponding initial state as values.
* @return {Array} Array containing values and onChange function respectively.
*/
const useForm = (initialValues) => {
const [values, setValues] = useState(initialValues);

const onChange = (event) => {
setValues((previousValues) => ({ ...previousValues, [event.target.name]: event.target.value }));
};

return [values, onChange];
};

export default useForm;
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export { default as useForm } from './hooks/useForm';
export { default as useState } from './hooks/useStack';
export { default as useQueue } from './hooks/useQueue';
export { default as useToggle } from './hooks/useToggle';
export { default as useDarkMode } from './hooks/useDarkMode';
export { default as usePrevious } from './hooks/usePrevious';
export { default as useDebounce } from './hooks/useDebounce';
export { default as useGeoLocation } from './hooks/useGeoLocation';
export { default as useLocalStorage } from './hooks/useLocalStorage';
export { default as useMousePosition } from './hooks/useMousePosition';
export { default as usePrevious } from './hooks/usePrevious';
export { default as useQueue } from './hooks/useQueue';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-custom-hooks",
"version": "1.7.0",
"version": "1.8.0",
"description": "A collection of Custom Hooks for React.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 6496f5c

Please sign in to comment.