Skip to content

Commit 388cc3d

Browse files
committed
simplified extend
1 parent 092b1e6 commit 388cc3d

File tree

2 files changed

+61
-51
lines changed

2 files changed

+61
-51
lines changed

src/Fieldset.jsx

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,67 @@ 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']) {
13+
function extendChild(child, parent) {
14+
const { index } = parent.props;
15+
if (typeof index === 'undefined') {
1716
return child;
1817
}
1918

19+
const { addIndex, remove, up, down, onClick } = child.props;
2020
const newProps = {};
21-
let extendedCount = 0;
21+
let changed = false;
2222

23-
forOwn(props, (fn, key) => {
24-
if (typeof fn !== 'function' || fn._extended) {
25-
return;
26-
}
2723

28-
extendedCount++;
24+
if (addIndex) {
25+
newProps.addIndex = null;
26+
changed = true;
27+
28+
forOwn(child.props, (fn, key) => {
29+
if (typeof fn === 'function') {
30+
newProps[key] = (...args) => fn(index, ...args);
31+
}
32+
});
33+
}
34+
35+
const onClickBefore = newProps.onClick || onClick;
36+
37+
if (remove) {
38+
newProps.remove = null;
39+
changed = true;
2940

30-
const newFn = (...args) => fn(index, ...args);
31-
newFn._extended = true;
41+
newProps.onClick = () => {
42+
parent.remove(index);
3243

33-
newProps[key] = newFn;
34-
});
44+
if (onClickBefore) {
45+
onClickBefore();
46+
}
47+
};
48+
} else if (up) {
49+
newProps.up = null;
50+
changed = true;
51+
52+
newProps.onClick = () => {
53+
parent.up(index);
3554

36-
if (extendedCount) {
37-
newProps['data-extended'] = true;
38-
const newChild = cloneElement(child, newProps);
39-
return newChild;
55+
if (onClickBefore) {
56+
onClickBefore();
57+
}
58+
};
59+
} else if (down) {
60+
newProps.down = null;
61+
changed = true;
62+
63+
newProps.onClick = () => {
64+
parent.down(index);
65+
66+
if (onClickBefore) {
67+
onClickBefore();
68+
}
69+
};
70+
}
71+
72+
if (changed) {
73+
return cloneElement(child, newProps);
4074
}
4175

4276
return child;
@@ -50,7 +84,6 @@ export default class Fieldset extends Element {
5084
onChange: PropTypes.func,
5185
map: PropTypes.bool.isRequired,
5286
index: PropTypes.number,
53-
addIndex: PropTypes.bool,
5487
};
5588

5689
static defaultProps = {
@@ -69,8 +102,7 @@ export default class Fieldset extends Element {
69102
|| props.className !== nextProps.className
70103
|| props.value !== nextProps.value
71104
|| props.map !== nextProps.map
72-
|| props.index !== nextProps.index
73-
|| props.addIndex !== nextProps.addIndex);
105+
|| props.index !== nextProps.index);
74106
}
75107

76108
remove(index) {
@@ -212,20 +244,18 @@ export default class Fieldset extends Element {
212244
_registerChildren(children, topLevel) {
213245
this.smartUpdate = true;
214246

215-
const { value, map, index, addIndex } = this.props;
247+
const { value, map, index } = this.props;
216248

217249
if (topLevel && map && isArray(value)) {
218250
return value.map((val, index) => {
219251
return this._registerChildren((
220-
<Fieldset name={index} key={index} index={index} addIndex={addIndex}>
252+
<Fieldset name={index} key={index} index={index}>
221253
{children}
222254
</Fieldset>
223255
));
224256
});
225257
}
226258

227-
const hasIndex = typeof index !== 'undefined';
228-
229259
return traverse(children, (child) => {
230260
if (!isFunction(child.type) || !child.type.isElement) {
231261
return void 0;
@@ -247,32 +277,12 @@ export default class Fieldset extends Element {
247277
onChange: (value, component) => this.setValue(name, value, component),
248278
});
249279
}, (child) => {
250-
const { replace } = this.getFormProps();
251-
252-
if (hasIndex) {
253-
if (child.props.remove) {
254-
return cloneElement(child, {
255-
remove: null,
256-
onClick: () => this.remove(index),
257-
});
258-
} else if (child.props.up) {
259-
return cloneElement(child, {
260-
up: null,
261-
onClick: () => this.up(index),
262-
});
263-
} else if (child.props.down) {
264-
return cloneElement(child, {
265-
down: null,
266-
onClick: () => this.down(index),
267-
});
268-
} else if (addIndex || child.props.addIndex) {
269-
const updatedChild = extendCallbacks(child, index);
270-
if (updatedChild !== child) {
271-
return updatedChild;
272-
}
273-
}
280+
const updatedChild = extendChild(child, this);
281+
if (updatedChild !== child) {
282+
return updatedChild;
274283
}
275284

285+
const { replace } = this.getFormProps();
276286
if (!replace) {
277287
return void 0;
278288
}

tests/input.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ describe('Fieldset', () => {
561561

562562
const node = renderJSX(
563563
<Form value={value}>
564-
<Fieldset name="data" addIndex>
564+
<Fieldset name="data">
565565
<Input />
566-
<button onClick={onClick} />
566+
<button onClick={onClick} addIndex/>
567567
</Fieldset>
568568
</Form>
569569
);

0 commit comments

Comments
 (0)