{"id":22729785,"url":"https://github.com/gc-victor/react-form-core","last_synced_at":"2025-04-13T23:13:38.156Z","repository":{"id":57333309,"uuid":"156451599","full_name":"gc-victor/react-form-core","owner":"gc-victor","description":"React Form Core is an utility to create your own form elements components","archived":false,"fork":false,"pushed_at":"2020-06-26T12:22:36.000Z","size":511,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T23:13:24.850Z","etag":null,"topics":["error-messages","form","react","react-component","react-form","react-form-core","validator"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gc-victor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-06T21:40:33.000Z","updated_at":"2020-07-08T10:23:54.000Z","dependencies_parsed_at":"2022-08-24T18:50:23.974Z","dependency_job_id":null,"html_url":"https://github.com/gc-victor/react-form-core","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gc-victor%2Freact-form-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gc-victor%2Freact-form-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gc-victor%2Freact-form-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gc-victor%2Freact-form-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gc-victor","download_url":"https://codeload.github.com/gc-victor/react-form-core/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794569,"owners_count":21162615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-messages","form","react","react-component","react-form","react-form-core","validator"],"created_at":"2024-12-10T18:11:46.835Z","updated_at":"2025-04-13T23:13:38.135Z","avatar_url":"https://github.com/gc-victor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Form Core\n\nReact 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.\n\n## Demo\n\n[Demo CodeSandbox.](https://codesandbox.io/s/zkrql)\n\n[![typescript](https://badgen.net/badge/icon/typescript?icon=typescript\u0026label)](https://badgen.net/badge/icon/typescript?icon=typescript\u0026label)\n[![minified + gzip](https://badgen.net/bundlephobia/minzip/react-form-core@6.0.0)](https://badgen.net/bundlephobia/minzip/react-form-core@6.0.0)\n[![license](https://badgen.net/npm/license/react-form-core)](https://badgen.net/npm/license/react-form-core)\n\n## Install\n\nYou can use npm or yarn to install it.\n\n`$ npm install --save react-form-core`\n\n## Let's Play\n\nFirst of all, let's create a Validator component to validate the values of the fields if it is required.\n\n`./components/validator.js`\n\n```javascript\nimport React, { useContext } from 'react';\nimport { FormContext } from 'react-form-core';\n\nexport const Validator = ({ children, validation, name, ...rest }) =\u003e {\n    const { errors, setError, setSuccess, successes, values } = React.useContext(FormContext);\n    const element = children;\n    const onChange = (ev) =\u003e {\n        validation({\n            errors,\n            setError: (error) =\u003e setError(name, error),\n            setSuccess: (success) =\u003e setSuccess(name, success),\n            successes,\n            value: ev.target.value,\n            values,\n        });\n    };\n\n    return React.cloneElement(element, {\n        ...rest,\n        onChange,\n    });\n};\n```\n\nCreate 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.\n\n`./components/field.js`\n\n```javascript\nimport React, { useContext } from 'react';\nimport { FormContext } from 'react-form-core';\nimport { Validator } from './validator';\n\nexport const Field = ({ children, label, name, validation }) =\u003e {\n    const { errors } = useContext(FormContext);\n    const errorMessage = errors \u0026\u0026 errors[name];\n    const errorClassName = errorMessage ? 'has-error' : '';\n\n    return (\n        \u003clabel className={errorClassName}\u003e\n            \u003cspan\u003e{label}\u003c/span\u003e\n            {validation ? (\n                \u003cValidator name={name} validation={validation}\u003e\n                    {children}\n                \u003c/Validator\u003e\n            ) : (\n                children\n            )}\n            {errorMessage \u0026\u0026 \u003cspan\u003e{errorMessage}\u003c/span\u003e}\n        \u003c/label\u003e\n    );\n};\n```\n\nUse the Field component to create an input.\n\n`./components/input.js`\n\n```javascript\nimport React from 'react';\nimport { Field } from './components/field';\n\nexport const Input = ({ label, validation, ...rest }) =\u003e {\n    return (\n        \u003cField label={label} validation={validation} {...rest}\u003e\n            \u003cinput {...rest} /\u003e\n        \u003c/Field\u003e\n    );\n};\n```\n\nAs you will need to submit the form, let's create a submit button and disabled it if there is an error. \n\n`./components/submit.js`\n\n```javascript\nimport React, { useContext } from 'react';\nimport { FormContext } from 'react-form-core';\n\nexport const Submit = ({ ...rest }) =\u003e {\n    const { errors } = useContext(FormContext);\n    const hasErrors = !!Object.keys(errors).length;\n\n    return (\n        \u003cbutton disabled={hasErrors} type=\"submit\" {...rest}\u003e\n            Submit\n        \u003c/button\u003e\n    );\n};\n```\n\nAdd the components to the form and create the application view.\n\n`./views/app.js` \n\n```javascript\nimport React, { useContext } from 'react';\nimport { Form } from 'react-form-core';\nimport { Input } from './components/input';\nimport { Submit } from './components/submit';\n\nexport const App = () =\u003e {\n    const validation = ({ value, setError }) =\u003e value \u0026\u0026 setError('Submit disabled ;)');\n    const onSubmit={({ ev, errors, values }) =\u003e {\n        // send the form using the values or the form event \n    }}\n\n    return (\n        \u003cForm onSubmit={onSubmit}\u003e\n            \u003cp\u003e\u003cInput label={'First name: '} name={'firstName'} validation={validation} /\u003e\u003c/p\u003e\n            \u003cp\u003e\u003cSubmit /\u003e\u003c/p\u003e\n        \u003c/Form\u003e\n    );\n}\n```\n\n## FormContext API\n\nAPI to get and set errors and successes messages, and get the values from the form.\n\n- errors:\n\n    Object of errors by field name\n\n    `{ \u003cname\u003e: \u003cerror_message\u003e }` \n\n- setError:\n\n    Set errors by field name\n\n    `setError(\u003cname\u003e, \u003cerror_message\u003e)`\n\n- setSuccess:\n    \n    Set successes by field name\n\n    `setError(\u003cname\u003e, \u003csuccess_message\u003e)`\n\n- successes:\n\n    Object of successes by field name\n\n    `{ \u003cname\u003e: \u003csuccess_message\u003e }`\n\n- values:\n\n    Object of values by field name\n\n    `{ \u003cname\u003e: \u003cvalue\u003e }`\n\n## Compatible Versioning\n\n### Summary\n\nGiven a version number MAJOR.MINOR, increment the:\n\n- MAJOR version when you make backwards-incompatible updates of any kind\n- MINOR version when you make 100% backwards-compatible updates\n\nAdditional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR format.\n\n[![ComVer](https://img.shields.io/badge/ComVer-compliant-brightgreen.svg)](https://github.com/staltz/comver)\n\n## Contribute\n\nFirst off, thanks for taking the time to contribute!\nNow, take a moment to be sure your contributions make sense to everyone else.\n\n### Reporting Issues\n\nFound a problem? Want a new feature? First of all, see if your issue or idea has [already been reported](../../issues).\nIf it hasn't, just open a [new clear and descriptive issue](../../issues/new).\n\n### Commit message conventions\n\nWe are following *AngularJS Git Commit Message Conventions*. This leads to more readable messages that are easy to follow when looking through the project history.\n\n- [AngularJS Git Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#heading=h.uyo6cb12dt6w)\n- [Commit Message Guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit)\n\n### Submitting pull requests\n\nPull requests are the greatest contributions, so be sure they are focused in scope and do avoid unrelated commits.\n\n-   Fork it!\n-   Clone your fork: `git clone http://github.com/\u003cyour-username\u003e/react-form-core`\n-   Navigate to the newly cloned directory: `cd react-form-core`\n-   Create a new branch for the new feature: `git checkout -b my-new-feature`\n-   Install the tools necessary for development: `npm install`\n-   Make your changes.\n-   `npm run build` to verify your change doesn't increase output size.\n-   `npm test` to make sure your change doesn't break anything.\n-   Commit your changes: `git commit -am 'Add some feature'`\n-   Push to the branch: `git push origin my-new-feature`\n-   Submit a pull request with full remarks documenting your changes.\n\n## License\n\n[MIT License](https://github.com/gc-victor/react-form-core/blob/master/LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgc-victor%2Freact-form-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgc-victor%2Freact-form-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgc-victor%2Freact-form-core/lists"}