{"id":13450409,"url":"https://github.com/ckedwards/react-form-stateful","last_synced_at":"2025-03-23T16:31:22.894Z","repository":{"id":143905315,"uuid":"163460233","full_name":"ckedwards/react-form-stateful","owner":"ckedwards","description":"React form using hooks","archived":true,"fork":false,"pushed_at":"2019-08-12T15:09:43.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T04:45:24.130Z","etag":null,"topics":["form","hooks","react","react-hooks"],"latest_commit_sha":null,"homepage":"","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/ckedwards.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-12-29T00:21:48.000Z","updated_at":"2023-01-28T17:34:12.000Z","dependencies_parsed_at":"2024-01-07T18:06:17.712Z","dependency_job_id":"852644d4-c9e4-4592-8bdf-35cc92ee03dd","html_url":"https://github.com/ckedwards/react-form-stateful","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckedwards%2Freact-form-stateful","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckedwards%2Freact-form-stateful/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckedwards%2Freact-form-stateful/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckedwards%2Freact-form-stateful/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ckedwards","download_url":"https://codeload.github.com/ckedwards/react-form-stateful/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245130754,"owners_count":20565705,"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":["form","hooks","react","react-hooks"],"created_at":"2024-07-31T07:00:34.368Z","updated_at":"2025-03-23T16:31:22.602Z","avatar_url":"https://github.com/ckedwards.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"**NOTE:** This was an expeiriment exporing hooks while hooks was still in aplha. I don't use this in any projects and I have no intention of maintianing this project.\n\n# react-form-stateful\n\nreact-form-stateful a full featured, extensible form component for react using react hooks\n\n\u003e **NOTE:** this project requires the use of an alpha version of react to use.\n\n## Getting Started\n\n```\nnpm install --save react-form-stateful\n```\n\n## Examples\n\n### Basic Usage\n\n```tsx\nimport { FC } from 'react';\nimport { StatefulForm, SFInput, SFSelect, SFTextArea } from 'react-form-stateful';\nimport * as yup from 'yup';\n\nconst ValidationSchemeForm: FC = () =\u003e {\n  return (\n    \u003cStatefulForm\n      validationSchema={yup.object().shape({\n        email: yup\n          .string()\n          .required('Email required')\n          .email('Invalid email address'),\n        desc: yup.string().max(256, 'Please keep your description short!'),\n        complaint: yup\n          .string()\n          .required('Complaint required')\n          .max(10000, 'Max complaint size: 10,000 characters.'),\n      })}\n    \u003e\n      \u003cdiv\u003eFeedback form:\u003c/div\u003e\n      \u003clabel\u003e\n        Email:\n        \u003cSFInput name=\"email\" /\u003e\n      \u003c/label\u003e\n      \u003clabel\u003e\n        Short Description:\n        \u003cSFInput name=\"desc\" /\u003e\n      \u003c/label\u003e\n      \u003clabel\u003e\n        Reason for complaint:\n        \u003cSFSelect\n          name=\"reason\"\n          defaultEntry={'Please select a reason'}\n          values={['Bug', 'Typo', 'Feature Request', 'Other']}\n        /\u003e\n      \u003c/label\u003e\n      \u003clabel\u003e\n        Complaint:\n        \u003cSFTextArea name=\"complaint\" /\u003e\n      \u003c/label\u003e\n    \u003c/StatefulForm\u003e\n  );\n};\n```\n\n### Other examples\n\nOther examples can be seen in the [examples folder](./examples). `ValidationScheme.tsx` is a simple form and the other two are more complex.\n\n### Customizing Forms\n\nContext is used to expose the state. This allows for helper hooks to be written. Several already exist, but more could easily be written. (Feel free to make a PR).\n\n## Components\n\nOne of the components from [components](./src/controls.tsx):\n\n```tsx\ntype SFControlProps\u003cT = string\u003e = {\n  name: string;\n  className?: string;\n  errorClassName?: string;\n  initialValue?: T;\n  defaultValue?: T;\n};\n\nexport const SFInput: FC\u003cSFControlProps\u003e = props =\u003e {\n  const { touch, value, setValue, error } = useSFControl(props.name, props.initialValue, props.defaultValue);\n  return (\n    \u003cFragment\u003e\n      \u003cinput className={props.className} onBlur={touch} value={value || ''} onChange={e =\u003e setValue(e.target.value)} /\u003e\n      \u003cdiv className={props.errorClassName}\u003e{error}\u003c/div\u003e\n    \u003c/Fragment\u003e\n  );\n};\n```\n\nExample Usage\n\n```tsx\nimport { FC, createElement } from 'react';\nimport { StatefulForm, SFInput } from 'react-form-stateful';\n\nconst Form: FC = () =\u003e {\n  return (\n    \u003cStatefulForm\u003e\n      \u003cSFInput name=\"item\" /\u003e\n    \u003c/StatefulForm\u003e\n  );\n};\n```\n\nAs you can see component that matches your application's look and feel, but basic components do exist for your convenience.\n\n## Extending\n\nWhile the internal reducer is not exposed, the dispatch and actions are exposed, which allows for extension through side effects.\nAn example of this can be seen in examples/pages/Pages.tsx.\n\n## NO_DEFAULT and ASYNC_VALIDATION\n\nThere are two special constants that help with extending the functionality of react-form-stateful.\n\n#### NO_DEFAULT\n\n`NO_DEFAULT` prevents resets from affecting this value. Useful for hidden from values that are used to control validation.\nThis us used in the advanced example [examples/pages/Pages](examples/pages/Pages.tsx):\n\n```tsx\nconst valueState = useSFValue\u003cnumber[]\u003e(\n  '@@pages',\n  [0],\n  NO_DEFAULT, // Don't get reset\n  value =\u003e (props.pages.length !== value.length ? 'more pages exist' : null)\n);\n```\n\n#### ASYNC_VALIDATION\n\n`ASYNC_VALIDATION` Is used to to defer the validation to some external process. This is useful when you want to defer the validation to a separate process. This could also be done with Promises, but there may be cases where `ASYNC_VALIDATION` is more convenient.\n\n```tsx\nconst { error, setValue, value, touch, touched } = useSFControl\u003cstring\u003e(props.name, '', '', () =\u003e ASYNC_VALIDATION);\n```\n\nHere when a validation is triggered, the error state is set to `{ async:true }`. The form is not submitable until this is resolved. One way to resolve this is to use the `useSFError` hook and set the error state for the component.\n\n```tsx\nconst [, setError] = useSFError(props.name);\nsetError('Invalid username');\n```\n\n# Prior art\n\n- [Formik](https://github.com/jaredpalmer/formik) (Obviously)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckedwards%2Freact-form-stateful","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fckedwards%2Freact-form-stateful","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckedwards%2Freact-form-stateful/lists"}