{"id":13481311,"url":"https://github.com/ericvaladas/react-uncontrolled-form","last_synced_at":"2025-03-27T12:30:45.691Z","repository":{"id":40827533,"uuid":"66894698","full_name":"ericvaladas/react-uncontrolled-form","owner":"ericvaladas","description":"A 2kb library for building forms and validation with uncontrolled fields in React.","archived":false,"fork":false,"pushed_at":"2023-01-03T16:34:58.000Z","size":832,"stargazers_count":6,"open_issues_count":17,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-02T13:39:30.196Z","etag":null,"topics":["fields","form","react","uncontrolled","validation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/ericvaladas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-30T01:24:49.000Z","updated_at":"2022-09-25T20:02:48.000Z","dependencies_parsed_at":"2023-02-01T07:46:15.169Z","dependency_job_id":null,"html_url":"https://github.com/ericvaladas/react-uncontrolled-form","commit_stats":null,"previous_names":["ericvaladas/formwood"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericvaladas%2Freact-uncontrolled-form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericvaladas%2Freact-uncontrolled-form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericvaladas%2Freact-uncontrolled-form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericvaladas%2Freact-uncontrolled-form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericvaladas","download_url":"https://codeload.github.com/ericvaladas/react-uncontrolled-form/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245844801,"owners_count":20681779,"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":["fields","form","react","uncontrolled","validation"],"created_at":"2024-07-31T17:00:50.712Z","updated_at":"2025-03-27T12:30:45.381Z","avatar_url":"https://github.com/ericvaladas.png","language":"JavaScript","readme":"# React Uncontrolled Form\n\nA 2kb library for building forms and validation with uncontrolled fields in React.\n\n[![npm version](https://badge.fury.io/js/react-uncontrolled-form.svg)](https://badge.fury.io/js/react-uncontrolled-form)\n[![Travis build status](http://img.shields.io/travis/ericvaladas/react-uncontrolled-form.svg)](https://travis-ci.org/ericvaladas/react-uncontrolled-form)\n[![Coverage Status](https://coveralls.io/repos/github/ericvaladas/react-uncontrolled-form/badge.svg?branch=master)](https://coveralls.io/github/ericvaladas/react-uncontrolled-form?branch=master)\n[![Dependency Status](https://david-dm.org/ericvaladas/react-uncontrolled-form.svg)](https://david-dm.org/ericvaladas/react-uncontrolled-form)\n[![devDependency Status](https://david-dm.org/ericvaladas/react-uncontrolled-form/dev-status.svg)](https://david-dm.org/ericvaladas/react-uncontrolled-form?type=dev)\n[![gzip size](http://img.badgesize.io/https://unpkg.com/react-uncontrolled-form/dist/react-uncontrolled-form.min.js?compression=gzip\u0026label=gzip)](https://unpkg.com/react-uncontrolled-form/dist/react-uncontrolled-form.min.js)\n\n## Usage\n\nInstall the package with npm.\n```sh\nnpm install --save react-uncontrolled-form\n```\n\nImport the `Field` and `Form` components.\n```js\nimport { Field, Form } from 'react-uncontrolled-form';\n```\n\n### Example\n```js\nclass MyForm extends React.Component {\n  handleSubmit(form) {\n  }\n\n  render() {\n    return (\n      \u003cForm onSubmit={form =\u003e this.handleSubmit(form)}\u003e\n        \u003cField\u003e\n          {() =\u003e\n            \u003cinput name=\"email\" type=\"email\"/\u003e\n          }\n        \u003c/Field\u003e\n        \u003cField validators={[minLength(6)]}\u003e\n          {state =\u003e\n            \u003cdiv\u003e\n              \u003cinput name=\"password\" type=\"password\"/\u003e\n              \u003cdiv\u003e{state.message}\u003c/div\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n        \u003cbutton\u003eSubmit\u003c/button\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n### CodeSandbox Examples\n- [Field Validation](https://codesandbox.io/s/vvryvlrn95)\n- [Form Validation](https://codesandbox.io/s/o9085ml0y9)\n- [Inital Values](https://codesandbox.io/s/48w9noq1lx)\n\n### Field Component\nThe `Field` component requires its children to be a function that returns JSX. When a `Field` renders it will call the children function, passing `state` and `validate` as arguments, and render the output. During the render, if an element with a `name` attribute is found, it will be registered with the `Form` and have the necessary props passed to it.\n\n### Form Component\nThe `Form` component will render a `\u003cform\u003e` element and validate its `Field` components on submission. A `form` prop, containing essential field registration functions, is automatically passed to all child components to be used by the `Field` component. If you nest a `Field` component inside another component, you must pass along the `form` prop to it.\n\n### Form Submission\nWhen a form is submitted, all fields will have their validators run. The `onSubmit` event handler is passed an object containing the form's validity, values, and invalid fields.\n```js\nclass MyForm extends React.Component {\n  handleSubmit(form) {\n    if (form.valid) {\n      console.log('Form values:', form.values);\n    }\n  },\n\n  render() {\n    return (\n      \u003cForm onSubmit={form =\u003e this.handleSubmit(form)}\u003e\n        \u003cField\u003e\n          {() =\u003e\n            \u003cinput name=\"username\"/\u003e\n          }\n        \u003c/Field\u003e\n        \u003cbutton\u003eSubmit\u003c/button\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n### Initial Values\nYou can pass initial values to your fields by adding the `values` prop to the `Form`.\n```js\nclass MyForm extends React.Component {\n  render() {\n    const values = {\n      firstName: 'Eric',\n      lastName: 'Valadas'\n    };\n\n    return (\n      \u003cForm values={values}\u003e\n        \u003cField\u003e\n          {() =\u003e\n            \u003cinput name=\"firstName\"/\u003e\n          }\n        \u003c/Field\u003e\n        \u003cField\u003e\n          {() =\u003e\n            \u003cinput name=\"lastName\"/\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n### Validators\nValidators are simply functions that return an error message.\n```js\nfunction minLength(length) {\n  return value =\u003e {\n    if (!value || value.length \u003c length) {\n      return `Must be at least ${length} characters`;\n    }\n  };\n}\n\nclass MyForm extends React.Component {\n  render() {\n    return (\n      \u003cForm\u003e\n        \u003cField validators={[minLength(3)]}\u003e\n          {state =\u003e\n            \u003cdiv\u003e\n              \u003cinput name=\"username\"/\u003e\n              \u003cdiv\u003e{state.message}\u003c/div\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\nThe validation message will be passed to your Field component via `state.message`. If a validator does not return a message, the field is considered valid.\n```js\nclass MyForm extends React.Component {\n  render() {\n    return (\n      \u003cForm\u003e\n        \u003cField\u003e\n          {state =\u003e\n            \u003cdiv className={'form-group' + (state.valid ? '' : ' has-error')}\u003e\n              \u003clabel className=\"control-label\" htmlFor=\"username\"\u003eUsername\u003c/label\u003e\n              \u003cinput name=\"username\" id=\"username\" className=\"form-control\"/\u003e\n              \u003cspan className=\"help-block\"\u003e{state.message}\u003c/span\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n};\n```\n\n### Validation\nAll fields have a `validate` function that will call each validator in the `validators` prop. By default, `validate` is only called when the form is submitted. However, this function is passed to the render prop and can be called whenever you like. Here's an example of validating a field as you type.\n```js\nclass MyForm extends React.Component {\n  render() {\n    return (\n      \u003cForm\u003e\n        \u003cField validators={[minLength(3)]}\u003e\n          {(state, validate) =\u003e\n            \u003cdiv\u003e\n              \u003cinput name=\"username\" onChange={validate}/\u003e\n              \u003cdiv\u003e{state.message}\u003c/div\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\nYou can also add field messages to your form which can be useful for things like server-side validation errors after submitting the form.\n```js\nclass MyForm extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {messages: {}};\n  }\n\n  handleSubmit() {\n    this.setState({\n      messages: {username: 'Username already exists'}\n    });\n  },\n\n  render() {\n    return (\n      \u003cForm messages={this.state.messages} onSubmit={form =\u003e this.handleSubmit(form)}\u003e\n        \u003cField\u003e\n          {state =\u003e\n            \u003cdiv\u003e\n              \u003cinput name=\"username\"/\u003e\n              \u003cdiv\u003e{state.message}\u003c/div\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n#### Field Dependant Validation\nIf you need access to another field's value for validation, you can do so by adding a `ref` to your form. To retrieve the value, call the form's `values` function.\n```js\nfunction match(compareValue, fieldName) {\n  return value =\u003e {\n    if (compareValue() !== value) {\n      return fieldName + ' does not match';\n    }\n  };\n}\n\nclass MyForm extends React.Component {\n  render() {\n    return (\n      \u003cForm ref={form =\u003e this.form = form}\u003e\n        \u003cField\u003e\n          {() =\u003e\n            \u003cinput name=\"email\" type=\"email\"/\u003e\n          }\n        \u003c/Field\u003e\n        \u003cField\n          validators={[match(() =\u003e this.form.values().email, 'Email')]}\n          exclude={true}\n        \u003e\n          {state =\u003e\n            \u003cdiv\u003e\n              \u003cinput name=\"confirm-email\" type=\"email\"/\u003e\n              \u003cdiv\u003e{state.message}\u003c/div\u003e\n            \u003c/div\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n### Nested Components\nThe `Form` component uses a `form` prop to provide field registration functions, initial values, and messages to its child components. If you want to nest a `Field` component inside another component, you will need to pass the `form` prop manually.\n```js\nclass MyComponent extends React.Component {\n  render() {\n    return (\n      \u003cField form={this.props.form}\u003e\n        {() =\u003e\n          \u003cinput name=\"username\"/\u003e\n        }\n      \u003c/Field\u003e\n    );\n  }\n}\n\nclass MyForm extends React.Component {\n  render() {\n    \u003cForm\u003e\n      \u003cMyComponent/\u003e\n    \u003c/Form\u003e\n  }\n}\n```\n\n### Transform\nThe value of any field can be transformed by passing a `transform` prop to the `Field` component. For example, a checkbox input will have the value `\"on\"` if no value is provided, but it can be changed to a boolean value if that is preferred.\n```js\nclass MyForm extends React.Component {\n  render() {\n    return (\n      \u003cForm\u003e\n        \u003cField transform={value =\u003e Boolean(value)}\u003e\n          {() =\u003e\n            \u003clabel\u003e\n              \u003cinput name=\"agree-to-terms\" type=\"checkbox\"/\u003e\n              I agree to the terms\n            \u003c/label\u003e\n          }\n        \u003c/Field\u003e\n      \u003c/Form\u003e\n    );\n  }\n}\n```\n\n### Custom Change Events\nIf you create a custom input, or use a component like [react-select](https://github.com/JedWatson/react-select), which passes a custom value to the `onChange` handler, the `Field` component will still register the value. The field's value will be set to the argument passed to `onChange`. The `Field` component passes an `onChange` prop to the child with a `name` attribute, so you do not need to set one yourself.\n\n#### React-Select\nWith the use of a couple optional props, react-select works just as you'd expect. In this example, `simpleValue` and `multi` set to `true` will produce a value of `\"one,two\"`. Using this in combination with the `transform` prop on the `Field`, a value of `[\"one\", \"two\"]` can be achieved.\n```js\nclass MyForm extends React.Component {\n  render() {\n    \u003cForm\u003e\n      \u003cField transform={value =\u003e value.split(',')}\u003e\n        {state =\u003e\n          \u003cSelect\n            name=\"numbers\"\n            value={state.value}\n            simpleValue={true}\n            multi={true}\n            options={[\n              {value: 'one', label: 'One'},\n              {value: 'two', label: 'Two'},\n            ]}\n          /\u003e\n        }\n      \u003c/Field\u003e\n    \u003c/Form\u003e\n  }\n}\n```\n\n\n\n### Form Props\n\n| Prop | Description |\n| --- | --- |\n| [`onSubmit`](#form-submission) | A callback function\u003cbr\u003e`onSubmit({valid: [bool], values: {...}, invalidFields: {...}}` |\n| [`values`](#initial-values) | An object that will provide values to the form's fields |\n| [`messages`](#validation) | An object that will provide messages to the form's fields |\n\n### Field Props\n\n| Prop | Description |\n| --- | --- |\n| `form` | An object containing the form's registration functions, initial values, and messages |\n| `validators` | An array containing the field's [validators](#validators) |\n| `exclude` | A boolean that, when set to `true`, will exclude this field from the form's values |\n| [`transform`](#transform) | A function to transform the field's value |\n\n\n","funding_links":[],"categories":["Components"],"sub_categories":["Forms"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericvaladas%2Freact-uncontrolled-form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericvaladas%2Freact-uncontrolled-form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericvaladas%2Freact-uncontrolled-form/lists"}