Skip to content

Commit 788bb05

Browse files
author
bietkul
committed
Merge branch 'develop' of https://github.com/bietkul/react-reactive-form into develop
2 parents 6e565eb + 5c243f2 commit 788bb05

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type ValidationErrors = {
44
[key: string]: any
55
};
66
export type Status = 'VALID'|'INVALID'|'DISABLED'|'PENDING';
7-
export type InputType = 'checkbox'|'radio';
7+
export type InputType = 'checkbox'|'radio'|'switch';
88
export type Handler = {
99
value: any;
1010
onChange: (e: any) => void;

src/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toObservable, isEvent, getHandler } from './utils';
1+
import { toObservable, isEvent, getHandler, isReactNative } from './utils';
22
import Subject from "./observable";
33
import Validators from './validators';
44

@@ -49,7 +49,7 @@ function getControlValue(event) {
4949
}
5050
return event.target.value;
5151
default:
52-
return event.target.value;
52+
return isReactNative() ? event.nativeEvent.text : event.target.value;
5353
}
5454
}
5555
return event;

src/utils.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,18 @@ export const propsToBeMap = {
7474
}
7575
export const controlsToBeMap = {
7676
ReactNative: {
77-
value: 'value',
78-
onChange: 'onChange',
79-
onBlur: 'onBlur',
80-
editable: 'enabled',
81-
disabled: 'disabled',
77+
switch: {
78+
value: 'value',
79+
onValueChange: 'onChange',
80+
onBlur: 'onBlur',
81+
disabled: 'disabled',
82+
},
83+
default: {
84+
value: 'value',
85+
onChange: 'onChange',
86+
onBlur: 'onBlur',
87+
editable: 'enabled',
88+
}
8289
},
8390
default: {
8491
value: 'value',
@@ -87,16 +94,21 @@ export const controlsToBeMap = {
8794
disabled: 'disabled',
8895
}
8996
}
90-
export const inputControls = isReactNative() ? controlsToBeMap.ReactNative : controlsToBeMap.default;
97+
export const getAbsoluteValue = (value) => (value === undefined || value === null) ? "" : value;
98+
99+
export const getInputControls = (inputType) => isReactNative() ?
100+
controlsToBeMap.ReactNative[inputType] || controlsToBeMap.ReactNative.default : controlsToBeMap.default;
101+
91102
export function getHandler(inputType, value, control) {
92103
const controlObject = {};
104+
const inputControls = getInputControls(inputType);
93105
Object.keys(inputControls).forEach((key) => {
94106
let controlProperty = null;
95107
if(key === 'value') {
96108
if(control.updateOn !== "change") {
97-
controlProperty = control._pendingValue || "";
109+
controlProperty = getAbsoluteValue(control._pendingValue);
98110
} else {
99-
controlProperty = control.value || "";
111+
controlProperty = getAbsoluteValue(control.value);
100112
}
101113
} else {
102114
controlProperty = control[inputControls[key]];

0 commit comments

Comments
 (0)