{"id":21075180,"url":"https://github.com/nutgaard/use-formstate","last_synced_at":"2025-12-29T09:04:12.704Z","repository":{"id":40782578,"uuid":"198159544","full_name":"nutgaard/use-formstate","owner":"nutgaard","description":"formstate library using hooks (react ^16.8)","archived":false,"fork":false,"pushed_at":"2023-01-06T04:45:13.000Z","size":908,"stargazers_count":1,"open_issues_count":17,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T03:44:28.729Z","etag":null,"topics":[],"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/nutgaard.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":"2019-07-22T06:17:37.000Z","updated_at":"2021-05-05T14:21:13.000Z","dependencies_parsed_at":"2023-02-05T08:31:17.612Z","dependency_job_id":null,"html_url":"https://github.com/nutgaard/use-formstate","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/nutgaard/use-formstate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgaard%2Fuse-formstate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgaard%2Fuse-formstate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgaard%2Fuse-formstate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgaard%2Fuse-formstate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nutgaard","download_url":"https://codeload.github.com/nutgaard/use-formstate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgaard%2Fuse-formstate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28113138,"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","status":"online","status_checked_at":"2025-12-29T02:00:07.021Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-19T19:20:03.011Z","updated_at":"2025-12-29T09:04:12.684Z","avatar_url":"https://github.com/nutgaard.png","language":"TypeScript","readme":"# UseFormstate\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![Travis](https://img.shields.io/travis/nutgaard/use-formstate.svg)](https://travis-ci.org/nutgaard/use-formstate)\n[![Dev Dependencies](https://david-dm.org/nutgaard/use-formstate/dev-status.svg)](https://david-dm.org/nutgaard/use-formstate?type=dev)\n\nA react-hook for managing form values, errors and more\n\n### Usage\n\n**Preparations**\n\nThe form-shape and how it should be validated is defined using the `useFormstate` function.\nOptimally this should be outside of your react-component.\n\n```typescript jsx\ntype FormData = { // Don't use an interface here. Read me under \"known issues\"\n  name: string;\n  city: string;\n  hobby: string;\n}\n\ninterface FormProps {\n  validate: boolean;\n}\n\nconst initialState: FormData = {\n  name: '',\n  city: '',\n  hobby: ''\n};\n\nconst validator = useFormstate\u003cFormData, FormProps\u003e({\n  name: (value) =\u003e value.length \u003e 256 ? 'Thats a might long name' : undefined,\n  city: (value, values, props) =\u003e {\n     if (props.validate) {\n       return value.length === 0 ? 'Must provide a city' : undefined\n     }\n     return undefined;\n  },\n  hobby: (value, values, props) =\u003e {\n     if (values.city.length \u003e 0 \u0026\u0026 props.validate) {\n       return value.length === 0 ? 'Hobby is required if city is provided' : undefined\n     }\n     return undefined;\n  }\n});\n```\n\nAs an alternative you may pass a function instead of an object to `useFormstate`.\nThis may useful in instances where form-elements are conditonally-validated, though the two approaches are functionally equivalent.\n```typescript jsx\nconst validator = useFormstate\u003cFormData, FormProps\u003e((values, props) =\u003e {\n  const name = values.name.length \u003e 256 ? 'Thats a might long name' : undefined;\n  const city = props.validate \u0026\u0026 values.city.length === 0 ? 'Must provide a city' : undefined;\n  const hobby = values.city.length \u003e 0 \u0026\u0026 props.validate \u0026\u0026 values.hobby.length === 0 ? 'Hobby is required if city is provided' : undefined;\n\n  return { name, city, hobby };\n});\n```\n\n**Use it**\n\n```typescript jsx\nfunction submithandler(values: FormData) {\n    return fetch('...Do your thing...');\n}\n\nfunction MyForm(props: Props) {\n  const state: Formstate\u003cFormData\u003e = validator(initialState);\n  \n  return (\n    \u003cform onSubmit={state.onSubmit(submithandler)}\u003e\n      \u003clabel htmlFor={state.fields.name.input.id}\u003eName:\u003c/label\u003e\n      \u003cinput {...state.fields.name.input} /\u003e\n      {state.field.name.error ? \u003cspan\u003estate.field.name.error\u003c/span\u003e : undefined}\n\n      \u003clabel htmlFor={state.fields.city.input.id}\u003eCity:\u003c/label\u003e\n      \u003cinput {...state.fields.city.input} /\u003e\n      {state.field.city.error ? \u003cspan\u003estate.field.city.error\u003c/span\u003e : undefined}\n\n      \u003clabel htmlFor={state.fields.hobby.input.id}Name\u003eHobby:\u003c/label\u003e\n      \u003cinput {...state.fields.hobby.input} /\u003e\n      {state.field.hobby.error ? \u003cspan\u003estate.field.hobby.error\u003c/span\u003e : undefined}\n\n      \u003cbutton type=\"submit\" disabled={state.submitting}\u003eSave\u003c/button\u003e\n    \u003c/form\u003e\n  );\n}\n```\n\n## Types\nMost notable types are `Formstate\u003cS\u003e` and `FieldState`:\n\n**Formstate**\n\nThe returntype of calling `useFormstate(validation)(initialValues, props);`\n```\nsubmitting: boolean;                // is the submithandler current running\npristine: boolean;                  // is 'values === initialValues'\nvalid: boolean;                     // is the form as a whole valid, e.g no errors\nerrors: { fieldnames: string };     \nfields: { fieldnames: FieldState }\n```\n\n**FieldState**\n\nThe type containing information for each field.\n```\npristine: boolean;                  // is 'values === initialValues'\ntouched: boolean;                   // has this element had focus\ninitialValue: boolean;              // initialValue as specified \nerror?: string;                     // this elements error if any \ninput: {\n  id: string;                       \n  name: string;\n  value: string;\n  onChange: ChangeEventHandler;\n  onBlur: FocusEventHandler;\n};                      \n```\n\n# Known issues\n### `interface` vs `type`\nIts recommended to use `type` insteadof `interface` when defining your form-shape.\nE.g\n```typescript\n// DON'T DO THIS\ninterface FormShape {\n  name: string;\n}\n\n// DO THIS\ntype FormShape = {\n  name: string;\n}\n```\nThe underlying issue can be better understood by read through this official issue; https://github.com/microsoft/TypeScript/issues/15300\n \n\n## Credits\n\nMade using the awesome [typescript library starter](https://github.com/alexjoverm/typescript-library-starter) \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnutgaard%2Fuse-formstate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnutgaard%2Fuse-formstate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnutgaard%2Fuse-formstate/lists"}