Skip to content

Commit 8c9e600

Browse files
committed
Release 0.11.7
1 parent 52e3e73 commit 8c9e600

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-form-controlled",
3-
"version": "0.11.6",
3+
"version": "0.11.7",
44
"description": "React controlled form components. The main idea is to make forms as simple as possible.",
55
"author": {
66
"name": "Zlatko Fedor",

src/Fieldset.jsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,44 @@ import isArray from 'lodash/lang/isArray';
44
import isFunction from 'lodash/lang/isFunction';
55
import set from 'lodash/object/set';
66
import get from 'lodash/object/get';
7+
import forOwn from 'lodash/object/forOwn';
78
import traverse from './utils/traverse';
8-
99
import Input from './Input';
1010
import Select from './Select';
1111
import Textarea from './Textarea';
1212

13+
function extendCallbacks(child, index) {
14+
const props = child.props;
15+
16+
if (typeof index === 'undefined' || props['data-extended']) {
17+
return child;
18+
}
19+
20+
const newProps = {};
21+
let extendedCount = 0;
22+
23+
forOwn(props, (fn, key) => {
24+
if (typeof fn !== 'function' || fn._extended) {
25+
return;
26+
}
27+
28+
extendedCount++;
29+
30+
const newFn = (...args) => fn(...args, index);
31+
newFn._extended = true;
32+
33+
newProps[key] = newFn;
34+
});
35+
36+
if (extendedCount) {
37+
newProps['data-extended'] = true;
38+
const newChild = cloneElement(child, newProps);
39+
return newChild;
40+
}
41+
42+
return child;
43+
}
44+
1345
export default class Fieldset extends Element {
1446
static isElement = true;
1547

@@ -18,10 +50,12 @@ export default class Fieldset extends Element {
1850
onChange: PropTypes.func,
1951
map: PropTypes.bool.isRequired,
2052
index: PropTypes.number,
53+
extend: PropTypes.bool.isRequired,
2154
};
2255

2356
static defaultProps = {
2457
map: true,
58+
extend: false,
2559
};
2660

2761
getValue(name) {
@@ -60,18 +94,20 @@ export default class Fieldset extends Element {
6094
}
6195

6296
_registerChildren(children, topLevel) {
63-
const { value, map } = this.props;
97+
const { value, map, index, extend } = this.props;
6498

6599
if (topLevel && map && isArray(value)) {
66100
return value.map((currentValue, index) => {
67101
return this._registerChildren((
68-
<Fieldset name={index} key={index} index={index}>
102+
<Fieldset name={index} key={index} index={index} extend={extend}>
69103
{children}
70104
</Fieldset>
71105
));
72106
});
73107
}
74108

109+
const hasIndex = typeof index !== 'undefined';
110+
75111
return traverse(children, (child) => {
76112
if (!isFunction(child.type) || !child.type.isElement) {
77113
return void 0;
@@ -90,7 +126,14 @@ export default class Fieldset extends Element {
90126
onChange: (value, component) => this.setValue(child.props.name, value, component),
91127
});
92128
}, (child) => {
93-
const { replace } = this.getFormProps();
129+
const { replace, extend: formExtend } = this.getFormProps();
130+
if (hasIndex && extend && formExtend) {
131+
const updatedChild = extendCallbacks(child, index);
132+
if (updatedChild !== child) {
133+
return updatedChild;
134+
}
135+
}
136+
94137
if (!replace) {
95138
return void 0;
96139
}

src/Form.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class Form extends Fieldset {
2020
onError: PropTypes.func,
2121
ajvOptions: PropTypes.object.isRequired,
2222
replace: PropTypes.bool.isRequired,
23+
extend: PropTypes.bool.isRequired,
2324
};
2425

2526
static defaultProps = {
@@ -30,6 +31,7 @@ export default class Form extends Fieldset {
3031
onChange: () => {},
3132
onSubmit: () => {},
3233
replace: true,
34+
extend: true,
3335
};
3436

3537
constructor(props, context) {

tests/input.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,27 @@ describe('Fieldset', () => {
548548
const ele = findDOMNode(node).querySelector('input');
549549
ele.value.should.equal('123');
550550
});
551+
552+
it('should be able to use onClick with index', (done) => {
553+
const value = {
554+
data: [123, 222],
555+
};
556+
557+
function onClick(evn, id, index) {
558+
index.should.equal(0);
559+
done();
560+
}
561+
562+
const node = renderJSX(
563+
<Form value={value}>
564+
<Fieldset name="data" extend={true}>
565+
<Input />
566+
<button onClick={onClick} />
567+
</Fieldset>
568+
</Form>
569+
);
570+
571+
const ele = findDOMNode(node).querySelector('button');
572+
TestUtils.Simulate.click(ele);
573+
});
551574
});

0 commit comments

Comments
 (0)