{"id":15365945,"url":"https://github.com/andywer/formate","last_synced_at":"2025-06-26T09:38:20.824Z","repository":{"id":66057852,"uuid":"63435562","full_name":"andywer/formate","owner":"andywer","description":"Modular form handling for React.js made easy.","archived":false,"fork":false,"pushed_at":"2016-07-18T09:06:24.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T19:23:24.567Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/andywer.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-15T16:22:20.000Z","updated_at":"2016-07-17T16:31:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"ec8ffa80-b96f-46de-9970-0ee89e4f68ad","html_url":"https://github.com/andywer/formate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andywer/formate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Fformate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Fformate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Fformate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Fformate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andywer","download_url":"https://codeload.github.com/andywer/formate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Fformate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262039570,"owners_count":23249238,"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":[],"created_at":"2024-10-01T13:16:45.844Z","updated_at":"2025-06-26T09:38:20.726Z","avatar_url":"https://github.com/andywer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# formate [Concept/Draft]\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\nModular form handling for React.js made easy. A lot of cool features loosely coupled\nvia middleware concept.\n\n\n## Usage\n\n```js\nimport React, { PropTypes } from 'react'\nimport Formate from 'formate'\n\nconst propTypes = {\n  Field: PropTypes.object.isRequired\n}\n\nfunction MyForm ({ Field, Form, formData, setFormData }) {\n  return (\n    \u003cForm\u003e\n      {/* A middleware may provide default getter/setter based on `name` prop */}\n      \u003cField component='input' name='firstName' /\u003e\n      {/* A middleware may provide shortcuts to frequently used form field components */}\n      \u003cField.input name='lastName' /\u003e\n\n      {/* A default submit button could also be provided by a middleware: \u003cForm.SubmitButton /\u003e */}\n      \u003cbutton type='submit'\u003eSubmit\u003c/button\u003e\n    \u003c/Form\u003e\n  )\n}\n\nMyForm.propTypes = propTypes\n\nexport default Formate(MyForm, [ middleware1, middleware2 ])\n\n// - or -\n// export default Formate(MyForm, middlewares)\n\n// - or (to pre-configure middlewares) -\n\n// import { createPreset } from 'formate'\n// const preset1 = createPreset(middlewares)\n// const preset2 = preset1.concat(moreMiddlewares)\n// export default preset2(MyForm, optionallyEvenMoreMiddlewares)\n```\n\nFormate just provides the basic API and middleware API. Middlewares provide all\nthe fancy functionality like validation, error handling, automatic default\ngetters/setters and so on.\n\n\n## Installation\n\n```sh\nnpm install --save formate\n# or --save-dev, depending on your frontend dependency philosophy\n```\n\n\n## How to write a middleware\n\n```js\nfunction myMiddleware (api, hook) {\n  // This hook adds a new prop to every form field: 'isEmpty' (boolean)\n  hook('formfield', ({ component, props, statics }) =\u003e {\n    const newProps = { ...props, isEmpty: isFieldEmpty(props.name) }\n    return { component, props: newProps, statics }\n    // returning just `{ props: newProps }` would work, too\n  })\n\n  // This hook adds a new prop to the form component (MyForm or similar): 'containsEmptyFields' (boolean)\n  hook('userform.props', (props) =\u003e {\n    const formNames = Object.keys(api.getFormData())\n    const containsEmptyFields = formNames.findIndex((formName) =\u003e isFieldEmpty(formName)) \u003e -1\n    return { ...props, containsEmptyFields }\n  })\n\n  function isFieldEmpty (name) {\n    return api.getFormData()[ formName ] !== ''\n  }\n}\n\n// Usage:\nexport default formate(MyForm, [ myMiddleware ])\n```\n\n\nUse the `api` object (first parameter of your middleware) to get/set additional data:\n\n- `userFormProps`: Object containing the user form's props. Use `props` hook to alter those props.\n- `getFormData`/`setFormData`: Get/set form data (The form fields' value).\n- `getStateData`/`setStateData`: Use these methods to get/set additional stateful data on the form (like errors or such).\n\nHooks you can use:\n\n- `userform.props`: Method `(props: object) =\u003e props: object`. Use it to change the user form element's props.\n- `form`: Method `({ component: class|function, props: object, statics: object }) =\u003e ({ component: class|function, props: object, statics: object })`. Use it to change form props or statics on the form class. Or return another component that decorates the form component.\n- `formfield`: Method `({ component: class|function, props: object, statics: object }) =\u003e ({ component: class|function, props: object, statics: object })`. Works like `form`, but for the form field.\n- `getFormData`, `setFormData`: Override the existing implementations by using these hooks.\n- `getStateData`/`setStateData`: Override the existing implementations by using these hooks.\n\nThe `form` and `formfield` hooks are run on `render()`. This gives you the ability to set props / decorate the components according to current form data (use `api.getFormdata()`), but make sure\nthat they are fast and pure (don't call setters).\n\n\n## Low level vs. nice by middleware\n\n```js\nimport React, { PropTypes } from 'react'\nimport Formate from 'formate'\n\nconst propTypes = {\n  Field: PropTypes.object.isRequired\n}\n\nfunction MyForm ({ Field, Form, formData, setFormData }) {\n  return (\n    \u003cForm\u003e\n      {/* Rather low-level API; `component='input'` might be used as default if no `component` prop is provided */}\n      \u003cField component='input' name='firstName' /\u003e\n      {/* A middleware may provide shortcuts to frequently used form field components */}\n      \u003cField.input name='lastName' /\u003e\n\n      {/* Manual usage; not recommended, since form field cannot be decorated by middleware */}\n      \u003cinput name='title' value={formData.title} onChange={(evt) =\u003e setFormData('title', evt.target.value)} /\u003e\n    \u003c/Form\u003e\n  )\n}\n\nMyForm.propTypes = propTypes\n\nexport default Formate(MyForm)\n```\n\n\n## Credits\n\nThe initial idea for this package grew out of enduring brain-storming with\n[jhohlfeld](https://github.com/jhohlfeld) who also wrote the initial prototype!\n\nAnother influence was [react-reformed](https://github.com/davezuko/react-reformed)\nwhich showed a better way of storing/managing form data as the initial draft by\ndecorating the whole form and storing form data in the decorating component's state.\n\n\n## License\n\nMIT. See [LICENSE](./LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandywer%2Fformate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandywer%2Fformate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandywer%2Fformate/lists"}