Skip to content

Commit 50101ba

Browse files
author
Vlad Balin
committed
updated readme
1 parent 576d425 commit 50101ba

File tree

1 file changed

+8
-242
lines changed

1 file changed

+8
-242
lines changed

README.md

Lines changed: 8 additions & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ If linked value is plain object or array, it's possible to generate
125125
and link to its element will be updated, it will lead to proper
126126
update of stateful component.
127127

128-
- Take link to array or object member
128+
- Take link to array or object member: `link.at( key )`
129129
```javascript
130130
const firstElementLink = arrayLink.at( 0 );
131131
```
@@ -134,266 +134,32 @@ If linked value is plain object or array, it's possible to generate
134134
const nameLink = objectLink.at( 'name' );
135135
```
136136

137-
- Map and filter through array or object
137+
- Map and filter through array or object: `link.map( iterator )`
138138
```javascript
139139
var list = stringArrayLink.map( ( itemLink, index ) => {
140140
if( itemLink.value ){
141141
return (
142142
<div key={ index }>
143-
<input valueLink={ itemLink } />
143+
<Input valueLink={ itemLink } />
144144
</div>
145145
);
146146
}
147147
});
148148
```
149149

150-
## Boolean links
150+
## Offhand boolean links
151151

152-
- Link to the presence of value in array
152+
- Link to the presence of value in array: `arrayLink.contains( value )`
153153
```javascript
154154
const optionXLink = arrayLink.contains( 'optionX' );
155155
```
156156

157-
- Link to value equality
157+
- Link to value equality: `link.equals( value )`
158158
```javascript
159159
const optionXLink = stringLink.equals( 'optionX' );
160160
```
161161

162-
- toggle boolean link
163-
```javascript
164-
<button onClick={ () => link.toggle() } />
165-
```
166-
or
167-
```javascript
168-
<button onClick={ link.update( x => !x ) } />
169-
```
170-
or
171-
```javascript
172-
<button onClick={ () => link.set( !link.value ) } />
173-
```
174-
175162
# Data binding examples
176163

177-
Here are the set of examples for typical data binding use cases. Each section contains custom databound component, state definitions, and usage examples.
178-
179-
It's generally advised to keep stateful components at the top level.
180-
181-
## Checkboxes
182-
183-
Standard `<input/>` will work. Custom Checkbox component might be implemented like this:
184-
185-
```javascript
186-
const Checkbox = ({ className = 'checkbox', checkedLink }) => (
187-
<div className={ className + ( checkedLink.value ? ' selected' : '' ) }
188-
onClick = { checkedLink.update( x => !x ) }
189-
/>
190-
);
191-
```
192-
193-
Examples will assume working with custom Checkbox.
194-
195-
### Binding to boolean attributes
196-
197-
```javascript
198-
const CheckboxGroup = ({ modelLink }) => (
199-
<div>
200-
<div>
201-
<Checkbox checkedLink={ modelLink.at( 'option1' ) } />
202-
Option 1
203-
</div>
204-
<div>
205-
<Checkbox checkedLink={ modelLink.at( 'option2' ) } />
206-
Option 2
207-
</div>
208-
</div>
209-
);
210-
```
211-
212-
Usage:
213-
214-
```javascript
215-
getInitialState(){
216-
return {
217-
model : {
218-
option1 : true,
219-
option2 : false
220-
}
221-
}
222-
}
223-
224-
render(){
225-
return <CheckboxGroup modelLink={ Link.state( this, 'model' ) } />
226-
}
227-
```
228-
229-
### Binding to array of selected options
230-
231-
```javascript
232-
const CheckboxGroup = ({ modelLink }) => {
233-
const link = modelLink.at( 'options' );
234-
235-
return (
236-
<div>
237-
<div>
238-
<Checkbox checkedLink={ link.contains( 'option1' ) } />
239-
Option 1
240-
</div>
241-
<div>
242-
<Checkbox checkedLink={ link.contains( 'option2' ) } />
243-
Option 2
244-
</div>
245-
</div>
246-
);
247-
};
248-
```
249-
250-
Usage:
251-
252-
```javascript
253-
getInitialState(){
254-
return {
255-
model : {
256-
options : [ 'option1' ]
257-
}
258-
}
259-
}
260-
261-
render(){
262-
return <CheckboxGroup modelLink={ Link.state( this, 'model' ) } />
263-
}
264-
```
265-
266-
## Radio Groups
267-
268-
For the radio groups you will need custom Radio component. It's very similar to custom Checkbox one,
269-
with one difference in click handler:
270-
271-
```javascript
272-
const Radio = ({ className = 'radio', checkedLink }) => (
273-
<div className={ className + ( checkedLink.value ? ' selected' : '' ) }
274-
onClick = { checkedLink.update( () => true ) }
275-
/>
276-
);
277-
```
278-
279-
In this example, we bind radio to string values. It's not required for them to be strings.
280-
281-
```javascript
282-
const RadioGroup = ({ modelLink }) => {
283-
const link = modelLink.at( 'option' );
284-
285-
return (
286-
<div>
287-
<div>
288-
<Radio checkedLink={ link.equals( 'option1' ) } />
289-
Option 1
290-
</div>
291-
<div>
292-
<Radio checkedLink={ link.equals( 'option2' ) } />
293-
Option 2
294-
</div>
295-
</div>
296-
);
297-
};
298-
```
299-
300-
Usage:
301-
302-
```javascript
303-
getInitialState(){
304-
return {
305-
model : {
306-
options : 'option1'
307-
}
308-
}
309-
}
310-
311-
render(){
312-
return <RadioGroup modelLink={ Link.state( this, 'model' ) } />
313-
}
314-
```
315-
316-
## Input fields
317-
318-
Standard `<input>` will work. You may implement custom input controls to handle complex scenarios
319-
with validation and appearance.
320-
321-
```javascript
322-
const Input = ({ valueLink, ...props }) => (
323-
<div className={ `my-nice-input ${ valueLink.validationError ? 'is-invalid' : '' }` }
324-
<input {...props} value={ valueLink.value } onChange={ e => valueLink.set( e.target.value ) }/>
325-
</div>
326-
);
327-
```
328-
329-
### Binding to object's attributes
330-
331-
```javascript
332-
const InputGroup = ({ modelLink }) => (
333-
<div>
334-
<label>
335-
Number:
336-
<Input type='number' valueLink={ modelLink.at( 'number' ).check( x => x > 0 ) } />
337-
</label>
338-
<label>
339-
String:
340-
<Input valueLink={ modelLink.at( 'string' ) } />
341-
</label>
342-
</div>
343-
);
344-
};
345-
```
346-
347-
Usage:
348-
349-
```javascript
350-
getInitialState(){
351-
return {
352-
model : {
353-
number : 0,
354-
string : ''
355-
}
356-
}
357-
}
358-
359-
render(){
360-
return <InputGroup modelLink={ Link.state( this, 'model' ) } />
361-
}
362-
```
363-
364-
### Binding to an array of strings
365-
366-
The same technique may be used to bind to an array or hash of strings. First, take a link to this
367-
attribute. Next, use `link.map` method to iterate through elements links created for you.
368-
369-
`link.map` will internally execute `link.at( key )` method to create a link to the plain object or array element.
370-
These methods may be used manually to create binding for the structures of any particular depth and complexity.
371-
372-
```javascript
373-
const InputGroup = ({ model /* instanceof MyModel */ }) => (
374-
<div>
375-
{ model.getLink( 'strings' ).map( strLink => (
376-
<div>
377-
<input valueLink={ strLink } />
378-
</div>
379-
)) }
380-
</div>
381-
);
382-
};
383-
```
384-
385-
Usage:
386-
387-
```javascript
388-
getInitialState(){
389-
return {
390-
model : {
391-
strings : [ 'first', 'second' ]
392-
}
393-
}
394-
}
395-
396-
render(){
397-
return <InputGroup modelLink={ Link.state( this, 'model' ) } />
398-
}
399-
```
164+
Here are the set of working [examples](/index.html) for typical data binding use cases.
165+
Sources are [here](/example/main.jsx).

0 commit comments

Comments
 (0)