├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── dist ├── preact-bind-group.es.js ├── preact-bind-group.js ├── react-bind-group.es.js ├── react-bind-group.js ├── react-form-group.es.js └── react-form-group.js ├── package-lock.json ├── package.json ├── preact.dev.js ├── preact.js ├── react.dev.js ├── react.js ├── rollup.common.js ├── rollup.preact.js └── rollup.react.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "env", 5 | { 6 | "modules": false, 7 | "loose": true 8 | } 9 | ] 10 | ], 11 | "plugins": [ 12 | "transform-class-properties", 13 | "transform-es2015-computed-properties", 14 | "transform-object-rest-spread" 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 k1r0s 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## preact-bind-group 2 | 3 | [![downloads](https://img.shields.io/npm/dm/preact-bind-group.svg)](https://www.npmjs.com/package/preact-bind-group) 4 | [![version](https://img.shields.io/npm/v/preact-bind-group.svg)](https://www.npmjs.com/package/preact-bind-group/) 5 | [![dependencies](https://david-dm.org/k1r0s/preact-bind-group/status.svg)](https://david-dm.org/k1r0s/preact-bind-group/status.svg) 6 | [![dev-dependencies](https://david-dm.org/k1r0s/preact-bind-group/dev-status.svg)](https://www.npmjs.com/package/preact-bind-group) 7 | 8 | 9 | An event wrapper for preact and react to centralize and simplify events management and state binding. 10 | 11 | [![Edit preact-bind-group example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/6v861168y3) 12 | 13 | breaking changes since version 2.* 14 | 15 | - React is now supported. Just import from "preact-bind-group/react" 16 | - `BindGroup` component has been renamed to `FormGroup` 17 | 18 | ### Why 19 | 20 | React/Preact forms are a bit cryptic because it leads developer to deal with too many language primitives to tie state with fields, care about events and duplicate this pattern for each field. 21 | 22 | ### How 23 | 24 | preact-bind-group exposes a `` component that looks for children that contain `data-bind` attribute which should be assigned to any element or component that emits an `onChange` event. 25 | 26 | 27 | ### Get started 28 | 29 | Install it: 30 | 31 | ```bash 32 | npm install preact-bind-group 33 | ``` 34 | 35 | Import it it in your components and use it: 36 | 37 | ```jsx 38 | import { render } from "preact"; 39 | import { FormGroup } from "preact-bind-group"; 40 | 41 | const App = () => ( 42 |
43 |

FormGroup demo

44 | console.log(change)}> 45 | 48 |
49 | 52 |
53 |
54 | ); 55 | ``` 56 | 57 | ### Watch callback 58 | 59 | The `watch` callback receives an `{key: value}` object containing the changed property as its parameter. 60 | 61 | ``` 62 | {name: "asdas"} 63 | ``` 64 | 65 | Then you can update your state easily: 66 | 67 | ```jsx 68 | this.setState({ ...change })}> 69 | ``` 70 | 71 | If the input element is of type _checkbox_ or _radio_, then it'll receive the checked html property as its value. 72 | 73 | For convenience, you'll get a second argument with the field key. The callback signature is _({ [key: string]: any }, key: string) => void_. 74 | 75 | ### Custom events 76 | 77 | You can change the event that `FormGroup` should listen to: 78 | 79 | ```javascript 80 | 81 | ``` 82 | 83 | > Note: keep in mind that `onInput` in Preact === `onChange` in React, and `onChange` in Preact === `onBlur` on React. 84 | 85 | #### Preload form with data 86 | 87 | You should use `preload` attr to fill form fields with default data 88 | 89 | ```javascript 90 |
91 |

FormGroup demo

92 | console.log(change)} preload={userModel}> 93 |
94 | 95 |
96 |
97 | 98 |
99 |
100 | 101 | 102 | 103 |
104 |
105 |