All Projects → k1r0s → preact-bind-group

k1r0s / preact-bind-group

Licence: MIT License
Preact Component to Group Form fields onChange Events to a single Callback

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to preact-bind-group

preact-route-async
Easy asynchronous loading for your router components. For 440B.
Stars: ✭ 36 (+44%)
Mutual labels:  preact, preact-components
preact-perf-profiler
A HOC to enable measuring rendering performance for Preact components
Stars: ✭ 18 (-28%)
Mutual labels:  preact, preact-components
preact-custom-scrollbars
⇅ Preact scrollbars component
Stars: ✭ 20 (-20%)
Mutual labels:  preact, preact-components
preact-component-console
A console emulator for preact.
Stars: ✭ 29 (+16%)
Mutual labels:  preact, preact-components
preact-views
📺 Named views for Preact, with easy-as-pie linking between them.
Stars: ✭ 39 (+56%)
Mutual labels:  preact, preact-components
preact-token-input
🔖 A text field that tokenizes input, for things like tags.
Stars: ✭ 57 (+128%)
Mutual labels:  preact, preact-components
preact-mdc
material design components for preact using material-components-web sass styles (for live demo click the link below)
Stars: ✭ 23 (-8%)
Mutual labels:  preact, preact-components
preact-motion
A fork of React-Motion to be used with Preact
Stars: ✭ 28 (+12%)
Mutual labels:  preact, preact-components
fly-kit-preact
A starter kit for building offline / SPA / PWA apps with Preact
Stars: ✭ 28 (+12%)
Mutual labels:  preact
preact-server-renderer
Server side rendering of preact components
Stars: ✭ 66 (+164%)
Mutual labels:  preact
dl-model
Dragalia Lost Model Viewer
Stars: ✭ 30 (+20%)
Mutual labels:  preact
preact-urql
Preact bindings for urql
Stars: ✭ 28 (+12%)
Mutual labels:  preact
theodorusclarence.com
💠 Personal website and blog made using Next.js, TypeScript, Tailwind CSS, MDX Bundler, FaunaDB, and Preact.
Stars: ✭ 205 (+720%)
Mutual labels:  preact
weather-sucks
Weather App with Estonian Mood
Stars: ✭ 23 (-8%)
Mutual labels:  preact
csgo-rcon-nodejs
A web panel to control a CS:GO server
Stars: ✭ 46 (+84%)
Mutual labels:  preact
preact-delegate
Preact delegate DOM events
Stars: ✭ 17 (-32%)
Mutual labels:  preact
jasonformat.com
My blog
Stars: ✭ 18 (-28%)
Mutual labels:  preact
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+236%)
Mutual labels:  preact
desvg
🌅 Converts SVG files into components 🌞
Stars: ✭ 79 (+216%)
Mutual labels:  preact
preact-polka-ssr
Preact SSR using Polka
Stars: ✭ 27 (+8%)
Mutual labels:  preact

preact-bind-group

downloads version dependencies dev-dependencies

An event wrapper for preact and react to centralize and simplify events management and state binding.

Edit preact-bind-group example

breaking changes since version 2.*

  • React is now supported. Just import from "preact-bind-group/react"
  • BindGroup component has been renamed to FormGroup

Why

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.

How

preact-bind-group exposes a <FormGroup> component that looks for children that contain data-bind attribute which should be assigned to any element or component that emits an onChange event.

Get started

Install it:

npm install preact-bind-group

Import it it in your components and use it:

import { render } from "preact";
import { FormGroup } from "preact-bind-group";

const App = () => (
  <div>
    <h3>FormGroup demo</h3>
    <FormGroup watch={change => console.log(change)}>
      <label>
        Name: <input data-bind="name" />
      </label>
      <br />
      <label>
        Age: <input data-bind="age" />
      </label>
    </FormGroup>
  </div>
);

Watch callback

The watch callback receives an {key: value} object containing the changed property as its parameter.

 {name: "asdas"}

Then you can update your state easily:

  <FormGroup watch={change => this.setState({ ...change })}>

If the input element is of type checkbox or radio, then it'll receive the checked html property as its value.

For convenience, you'll get a second argument with the field key. The callback signature is ({ [key: string]: any }, key: string) => void.

Custom events

You can change the event that FormGroup should listen to:

  <input data-bind="name" data-event="onInput"/>

Note: keep in mind that onInput in Preact === onChange in React, and onChange in Preact === onBlur on React.

Preload form with data

You should use preload attr to fill form fields with default data

<div>
  <h3>FormGroup demo</h3>
  <FormGroup watch={change => console.log(change)} preload={userModel}>
    <div>
      <input data-bind="name"/>
    </div>
    <div>
      <input data-bind="likesPizza" type="checkbox"/>
    </div>
    <div>
      <input data-bind="belovedFood" type="radio" value="potato"/>
      <input data-bind="belovedFood" type="radio" value="banana"/>
      <input data-bind="belovedFood" type="radio" value="peanuts"/>
    </div>
    <div>
      <textarea data-bind="comments"/>
    </div>
  </FormGroup>
</div>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].