https://github.com/gc-victor/react-form-core
React Form Core is an utility to create your own form elements components
https://github.com/gc-victor/react-form-core
error-messages form react react-component react-form react-form-core validator
Last synced: about 1 year ago
JSON representation
React Form Core is an utility to create your own form elements components
- Host: GitHub
- URL: https://github.com/gc-victor/react-form-core
- Owner: gc-victor
- License: mit
- Created: 2018-11-06T21:40:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-06-26T12:22:36.000Z (almost 6 years ago)
- Last Synced: 2025-04-13T23:13:24.850Z (about 1 year ago)
- Topics: error-messages, form, react, react-component, react-form, react-form-core, validator
- Language: TypeScript
- Size: 499 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# React Form Core
React Form Core is a lightweight, only 1.1 kB, utility to create your form components easily. Just use the Form component, and it works out of the box.
## Demo
[Demo CodeSandbox.](https://codesandbox.io/s/zkrql)
[](https://badgen.net/badge/icon/typescript?icon=typescript&label)
[](https://badgen.net/bundlephobia/minzip/react-form-core@6.0.0)
[](https://badgen.net/npm/license/react-form-core)
## Install
You can use npm or yarn to install it.
`$ npm install --save react-form-core`
## Let's Play
First of all, let's create a Validator component to validate the values of the fields if it is required.
`./components/validator.js`
```javascript
import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';
export const Validator = ({ children, validation, name, ...rest }) => {
const { errors, setError, setSuccess, successes, values } = React.useContext(FormContext);
const element = children;
const onChange = (ev) => {
validation({
errors,
setError: (error) => setError(name, error),
setSuccess: (success) => setSuccess(name, success),
successes,
value: ev.target.value,
values,
});
};
return React.cloneElement(element, {
...rest,
onChange,
});
};
```
Create a simple Field component wrapper with an error message. Use the FormContext to get and set data to the context of the form, and the Validation component to validate the field values.
`./components/field.js`
```javascript
import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';
import { Validator } from './validator';
export const Field = ({ children, label, name, validation }) => {
const { errors } = useContext(FormContext);
const errorMessage = errors && errors[name];
const errorClassName = errorMessage ? 'has-error' : '';
return (
{label}
{validation ? (
{children}
) : (
children
)}
{errorMessage && {errorMessage}}
);
};
```
Use the Field component to create an input.
`./components/input.js`
```javascript
import React from 'react';
import { Field } from './components/field';
export const Input = ({ label, validation, ...rest }) => {
return (
);
};
```
As you will need to submit the form, let's create a submit button and disabled it if there is an error.
`./components/submit.js`
```javascript
import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';
export const Submit = ({ ...rest }) => {
const { errors } = useContext(FormContext);
const hasErrors = !!Object.keys(errors).length;
return (
Submit
);
};
```
Add the components to the form and create the application view.
`./views/app.js`
```javascript
import React, { useContext } from 'react';
import { Form } from 'react-form-core';
import { Input } from './components/input';
import { Submit } from './components/submit';
export const App = () => {
const validation = ({ value, setError }) => value && setError('Submit disabled ;)');
const onSubmit={({ ev, errors, values }) => {
// send the form using the values or the form event
}}
return (
);
}
```
## FormContext API
API to get and set errors and successes messages, and get the values from the form.
- errors:
Object of errors by field name
`{ : }`
- setError:
Set errors by field name
`setError(, )`
- setSuccess:
Set successes by field name
`setError(, )`
- successes:
Object of successes by field name
`{ : }`
- values:
Object of values by field name
`{ : }`
## Compatible Versioning
### Summary
Given a version number MAJOR.MINOR, increment the:
- MAJOR version when you make backwards-incompatible updates of any kind
- MINOR version when you make 100% backwards-compatible updates
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR format.
[](https://github.com/staltz/comver)
## Contribute
First off, thanks for taking the time to contribute!
Now, take a moment to be sure your contributions make sense to everyone else.
### Reporting Issues
Found a problem? Want a new feature? First of all, see if your issue or idea has [already been reported](../../issues).
If it hasn't, just open a [new clear and descriptive issue](../../issues/new).
### Commit message conventions
We are following *AngularJS Git Commit Message Conventions*. This leads to more readable messages that are easy to follow when looking through the project history.
- [AngularJS Git Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#heading=h.uyo6cb12dt6w)
- [Commit Message Guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit)
### Submitting pull requests
Pull requests are the greatest contributions, so be sure they are focused in scope and do avoid unrelated commits.
- Fork it!
- Clone your fork: `git clone http://github.com//react-form-core`
- Navigate to the newly cloned directory: `cd react-form-core`
- Create a new branch for the new feature: `git checkout -b my-new-feature`
- Install the tools necessary for development: `npm install`
- Make your changes.
- `npm run build` to verify your change doesn't increase output size.
- `npm test` to make sure your change doesn't break anything.
- Commit your changes: `git commit -am 'Add some feature'`
- Push to the branch: `git push origin my-new-feature`
- Submit a pull request with full remarks documenting your changes.
## License
[MIT License](https://github.com/gc-victor/react-form-core/blob/master/LICENSE.md)