A data-binding library for react
var react = require("react");
var {Context,Binding} = require("react-bind-context");
var TextField = require("./my_textfield_component");
class App extends react.Component{
constructor(props,context){
super(props,context);
this.data = {firstname:"Max",lastname:"Muster"}
}
render(){
return <Context target=this.data>
<div className="my-form">
<Binding name="firstname">
<TextField>
</Binding>
<Binding name="lastname">
<TextField>
</Binding>
</div>
</Context>
}
}
So, basically, you render a Context
component containing one or more Binding
components containing one component capable of handling the binding.
To Context
you pass the data-object using the target
prop. The Binding
components need the name
prop, which property on the data-object holds the
value they should manage.
A component is capable of handling the binding when it takes a value
prop and
a onChange
prop. The value
prop holds the value of the property of the
data-object. The onChange
prop is a function that must be called with the new
value as first argument as soon as the value was changed in the component.
This means, that a normal input
does not support the binding, since it calls
onChange with an event-object as first parameter. In order to make it work,
you need to wrap it to support the API.
target
(object): The object holding the data you want to bind your UI to.name
(string): You can also nestContext
components. On nested ones, you can set aname
instead oftarget
. It then takes its target from the parent context.validator
(Validator) (optional): A validator to validate the data everytime data was changed.
name
(string): The name of the property of the context's data-object you want to bind to
In order to make a Component bindable, it has to support the following API:
value
(any): The value this component must render and make editableonChange(value)
(any): The function this component must call when the value was changed, with the new value as first argumenterrors
(array of Error objects): Ifvalidator
was specified onContext
then you get an array of errors on this field. It's zero-length if there aren't any.
A validator is any function that takes an object as first parameter and returns an array of zero or more Errors.
Errors are returned by a Validator's validate
function. The are read-only objects.
They have the following properties:
- fields (string[]): An array of property names this error applies to
- message (string): A string describing the error