Skip to content

Commit 68240df

Browse files
Shuwen Qiananthonykoerber
authored andcommitted
Repeater docs
1 parent 9258e98 commit 68240df

4 files changed

Lines changed: 149 additions & 1 deletion

File tree

docs/articles/manifest.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
},
1313
{
1414
"key":"migration_guide"
15+
},
16+
{
17+
"key":"repeater"
1518
}
16-
]
19+
]

docs/articles/repeater.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
### Using mm-repeater
2+
`mm-repeater` allows the duplication of a set of fields contained within a template tag.
3+
4+
```html
5+
<mm-repeater id="myRepeater">
6+
<template preserve-content>
7+
<mm-input name="first_name" placeholder="First Name"></mm-input>
8+
<mm-input name="last_name" placeholder="Last Name"></mm-input>
9+
<mm-input name="address" placeholder="Address"></mm-input>
10+
<mm-input name="city" placeholder="City"></mm-input>
11+
<mm-dropdown name="state" placeholder="State/Province">
12+
...
13+
</mm-dropdown>
14+
<mm-input name="zip_code" placeholder="ZIP/Postal Code"></mm-input>
15+
</template>
16+
</mm-repeater>
17+
```
18+
19+
#### Validation
20+
Like `mm-form`, `mm-repeater` takes a `config` object, which can take `validation` as a `string` or a custom validation method taking the arguments `value, row:Object, domref:HTMLElement`, and an `errorMessage`. If a custom validation method is used, `this.errorMessage` can be set dynamically.
21+
22+
```javascript
23+
var myRepeater = document.getElementById('myRepeater'),
24+
Validator = StrandLib.Validator;
25+
myRepeater.config = {
26+
'first_name': {
27+
validation: 'alpha'
28+
},
29+
'last_name': {
30+
validation: 'alpha'
31+
},
32+
'address': {
33+
validation: function(value) {
34+
var a = value.split(" "),
35+
street_num = parseInt(a[0]),
36+
street_name = a[1];
37+
return street_num != NaN;
38+
},
39+
},
40+
'city': {
41+
validation: 'alpha'
42+
},
43+
'zip_code': {
44+
validation: function(zip) {
45+
var z = zip.replace('-','');
46+
return parseInt(z) != NaN && (z.length == 5 || z.length == 9);
47+
},
48+
errorMessage: 'Not a valid ZIP'
49+
}
50+
}
51+
```
52+
53+
#### Getting data from `mm-repeater`
54+
User data from repeated form fields are accessible through the `value` property on the `mm-repeater` element. `value` is a getter/setter interface for the `data` property—this ensures Polymer's data binding updates properly. Each object in the `value` array corresponds to a single repeater row, with key-value pairs corresponding to the name-value pairs of the form elements.
55+
56+
Data can be preloaded into the repeater by setting the `value`. This is useful in views where the end user wishes to edit some pre-existing data.
57+
```javascript
58+
var myRepeater = document.getElementById('myRepeater');
59+
myRepeater.value = [
60+
{
61+
first_name: "Jerry",
62+
last_name: "Seinfeld",
63+
address: "2880 Broadway",
64+
city: "New York",
65+
state: "NY",
66+
zip_code: "10025"
67+
},
68+
{
69+
first_name: "George",
70+
last_name: "Costanza",
71+
address: "2880 Broadway",
72+
city: "New York",
73+
state: "NY",
74+
zip_code: "10025"
75+
}
76+
];
77+
```
78+
By default, `mm-repeater` initializes the `value` property with an array containing a single, empty `Object`. This results in a single, blank instance of the template being rendered when the form loads.

src/mm-repeater/doc.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name":"mm-repeater",
3+
"description":"A component for duplicating a set of form fields from a template",
4+
"experimental" : [
5+
{
6+
"message" : "This component contains experimental features. The configuration and API are subject to change. Please use at your own risk."
7+
}
8+
],
9+
"attributes": [
10+
{
11+
"name":"value",
12+
"type":"Array",
13+
"description":"An array containing key:value maps of the mm-repeater data, one per row"
14+
},
15+
{
16+
"name":"config",
17+
"type":"Object",
18+
"description":"An configuration object used to provide validation rules and error messaging"
19+
},
20+
{
21+
"name":"addRowLabel",
22+
"type":"String",
23+
"description":"Custom text for adding a row",
24+
"default":"Add Row"
25+
},
26+
{
27+
"name":"added",
28+
"type":"Array",
29+
"readOnly":"true",
30+
"description":"A read-only array of key:value maps of rows that have been added by the user"
31+
},
32+
{
33+
"name":"changed",
34+
"type":"Array",
35+
"readOnly":"true",
36+
"description":"A read-only array of key:value maps of rows that have been modified by the user"
37+
},
38+
{
39+
"name":"removed",
40+
"type":"Array",
41+
"readOnly":"true",
42+
"description":"A read-only array of key:value maps of rows that have been removed by the user"
43+
}
44+
],
45+
"methods": [
46+
{
47+
"name":"validate",
48+
"description":"Validate the data in the repeater according to the rules provided in config. Returns a Boolean indicating whether all of the data is valid"
49+
}
50+
]
51+
}

src/mm-repeater/example.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<mm-repeater id="repeater">
2+
<template preserve-content>
3+
<mm-input name="name"></mm-input>
4+
<mm-dropdown name="firstOption">
5+
<mm-list-item>Test Item 1</mm-list-item>
6+
<mm-list-item>Test Item 2</mm-list-item>
7+
<mm-list-item>Test Item 3</mm-list-item>
8+
<mm-list-item>Test Item 4</mm-list-item>
9+
</mm-dropdown>
10+
<mm-group name="secondOption">
11+
<mm-radio><label>Test 1</label></mm-radio>
12+
<mm-radio><label>Test 2</label></mm-radio>
13+
<mm-radio><label>Test 3</label></mm-radio>
14+
</mm-group>
15+
</template>
16+
</mm-repeater>

0 commit comments

Comments
 (0)