-
Notifications
You must be signed in to change notification settings - Fork 11
/
form.js
38 lines (34 loc) · 997 Bytes
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React from 'react';
import { View } from 'react-native';
export default class Form extends React.Component {
constructor() {
super();
this.inputs = [];
}
renderChildren(children, recursiveIndex = 0) {
return React.Children.map(children, (child, index) => {
if(!child)
return;
if (child.props.children)
return React.cloneElement(child, {
...child.props,
children: this.renderChildren(child.props.children, index)
});
if (child.type.name !== 'TextInput') return child;
let realIndex = index + recursiveIndex
return React.cloneElement(child, {
onEnter: () =>
this.inputs[realIndex + 1] ? this.inputs[realIndex + 1].focus() : null,
inputRef: ref => (this.inputs[realIndex] = ref),
});
});
}
render() {
let { children, ...props } = this.props;
return (
<View {...props}>
{this.renderChildren(children)}
</View>
);
}
}