Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit c76beb1

Browse files
committed
First draft Android support for React-Redux-Form.
1 parent 9d9cef8 commit c76beb1

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

src/android.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* eslint-disable react/prop-types */
2+
import React from 'react';
3+
import {
4+
Picker,
5+
Switch,
6+
TextInput,
7+
Slider,
8+
Text,
9+
View,
10+
} from 'react-native';
11+
12+
import {
13+
modelReducer,
14+
formReducer,
15+
modeled,
16+
actions,
17+
combineForms,
18+
initialFieldState,
19+
actionTypes,
20+
Control,
21+
Form,
22+
Fieldset,
23+
Errors,
24+
batched,
25+
form,
26+
getField,
27+
track,
28+
} from './index';
29+
import omit from './utils/omit';
30+
31+
function getTextValue(value) {
32+
if (typeof value === 'string' || typeof value === 'number') {
33+
return `${value}`;
34+
}
35+
36+
return '';
37+
}
38+
39+
const noop = () => undefined;
40+
41+
Control.Picker = (props) => (
42+
<Control
43+
component={Picker}
44+
mapProps={{
45+
onResponderGrant: ({ onFocus }) => onFocus,
46+
onResponderRelease: ({ onBlur }) => onBlur,
47+
selectedValue: ({ modelValue }) => modelValue,
48+
onValueChange: ({ onChange }) => onChange,
49+
onChange: noop,
50+
...props.mapProps,
51+
}}
52+
{...omit(props, 'mapProps')}
53+
/>
54+
);
55+
56+
Control.Switch = (props) => (
57+
<Control
58+
component={Switch}
59+
mapProps={{
60+
onResponderGrant: ({ onFocus }) => onFocus,
61+
onResponderRelease: ({ onBlur }) => onBlur,
62+
value: ({ modelValue }) => ! ! modelValue,
63+
onValueChange: ({ onChange }) => onChange,
64+
onChange: noop,
65+
...props.mapProps,
66+
}}
67+
{...omit(props, 'mapProps')}
68+
/>
69+
);
70+
71+
Control.TextInput = (props) => (
72+
<Control
73+
component={TextInput}
74+
mapProps={{
75+
onResponderGrant: ({ onFocus }) => onFocus,
76+
value: (_props) => ((! _props.defaultValue && ! _props.hasOwnProperty('value'))
77+
? getTextValue(_props.viewValue)
78+
: _props.value),
79+
onChangeText: ({ onChange }) => onChange,
80+
onChange: noop,
81+
onBlur: ({ onBlur, viewValue }) => () => onBlur(viewValue),
82+
onFocus: ({ onFocus }) => onFocus,
83+
...props.mapProps,
84+
}}
85+
{...omit(props, 'mapProps')}
86+
/>
87+
);
88+
89+
Control.Slider = (props) => (
90+
<Control
91+
component={Slider}
92+
mapProps={{
93+
value: ({ modelValue }) => modelValue,
94+
onResponderGrant: ({ onFocus }) => onFocus,
95+
onSlidingComplete: ({ onBlur }) => onBlur,
96+
onValueChange: ({ onChange }) => onChange,
97+
onChange: noop,
98+
...props.mapProps,
99+
}}
100+
{...omit(props, 'mapProps')}
101+
/>
102+
);
103+
104+
const NativeForm = (props) => <Form component={View} {...omit(props, 'mapProps')} />;
105+
const NativeFieldset = (props) => <Fieldset component={View} {...omit(props, 'mapProps')} />;
106+
const NativeErrors = (props) => (
107+
<Errors
108+
wrapper={View}
109+
component={Text}
110+
{...props}
111+
/>
112+
);
113+
114+
export {
115+
// Reducers
116+
formReducer,
117+
modelReducer,
118+
combineForms,
119+
120+
// Constants
121+
initialFieldState,
122+
actions,
123+
actionTypes,
124+
125+
// Components
126+
Control,
127+
NativeForm as Form,
128+
NativeErrors as Errors,
129+
NativeFieldset as Fieldset,
130+
131+
// Enhancers
132+
modeled,
133+
batched,
134+
135+
// Selectors
136+
form,
137+
138+
// Utilities
139+
getField,
140+
track,
141+
};

0 commit comments

Comments
 (0)