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

Commit 1cc67a1

Browse files
committed
Refactoring Control to use getValue and getCheckboxValue for #725
1 parent 5094ade commit 1cc67a1

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

src/components/control-component.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import omit from '../utils/omit';
1212
import actionTypes from '../action-types';
1313
import debounce from '../utils/debounce';
1414

15-
import _getValue from '../utils/get-value';
15+
import _getValue, { getCheckboxValue } from '../utils/get-value';
1616
import getValidity from '../utils/get-validity';
1717
import invertValidity from '../utils/invert-validity';
1818
import getFieldFromState from '../utils/get-field-from-state';
@@ -33,21 +33,6 @@ const findDOMNode = !isNative
3333

3434
const disallowedProps = ['changeAction', 'getFieldFromState', 'store'];
3535

36-
function getToggleValue(props) {
37-
const { modelValue, controlProps } = props;
38-
39-
switch (controlProps.type) {
40-
case 'checkbox':
41-
return typeof controlProps.value !== 'undefined'
42-
? controlProps.value
43-
: !modelValue; // simple checkbox
44-
45-
case 'radio':
46-
default:
47-
return controlProps.value;
48-
}
49-
}
50-
5136
function mergeOrSetErrors(model, errors, options) {
5237
return actions.setErrors(model, errors, {
5338
merge: isPlainObject(errors),
@@ -223,20 +208,12 @@ function createControlClass(s = defaultStrategy) {
223208
}
224209

225210
getChangeAction(event) {
226-
const {
227-
model,
228-
modelValue,
229-
changeAction,
230-
getValue,
231-
} = this.props;
232-
const value = this.isToggle()
233-
? getToggleValue(this.props)
234-
: event;
235-
236-
return changeAction(model, getValue(value), {
237-
currentValue: modelValue,
238-
external: false,
239-
});
211+
return this.props.changeAction(
212+
this.props.model,
213+
this.getValue(event), {
214+
currentValue: this.props.modelValue,
215+
external: false,
216+
});
240217
}
241218

242219
getValidateAction(value, eventName) {
@@ -361,6 +338,10 @@ function createControlClass(s = defaultStrategy) {
361338
}
362339
}
363340

341+
getValue(event) {
342+
return this.props.getValue(event, this.props);
343+
}
344+
364345
isToggle() {
365346
const { component, controlProps } = this.props;
366347

@@ -426,11 +407,9 @@ function createControlClass(s = defaultStrategy) {
426407
}
427408

428409
handleChange(event) {
429-
const { getValue } = this.props;
430-
431410
if (event && event.persist) event.persist();
432411

433-
this.setViewValue(getValue(event));
412+
this.setViewValue(this.getValue(event));
434413
this.handleUpdate(event);
435414
}
436415

@@ -504,7 +483,6 @@ function createControlClass(s = defaultStrategy) {
504483
ignore,
505484
withField,
506485
fieldValue,
507-
getValue,
508486
} = this.props;
509487

510488
const eventAction = {
@@ -557,7 +535,7 @@ function createControlClass(s = defaultStrategy) {
557535
},
558536
dispatchBatchActions,
559537
parser,
560-
getValue,
538+
(e) => this.getValue(e),
561539
persistEventWithCallback(controlEventHandler || identity)
562540
)(event, withField ? fieldValue : undefined);
563541
};
@@ -740,6 +718,7 @@ function createControlClass(s = defaultStrategy) {
740718
...controlPropsMap.checkbox,
741719
...props.mapProps,
742720
}}
721+
getValue={getCheckboxValue}
743722
changeAction={props.changeAction || s.actions.checkWithValue}
744723
{...omit(props, 'mapProps')}
745724
/>

src/components/field-component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import shallowCompareWithoutChildren from '../utils/shallow-compare-without-chil
1616
import getModel from '../utils/get-model';
1717
import getFieldFromState from '../utils/get-field-from-state';
1818
import resolveModel from '../utils/resolve-model';
19+
import { getCheckboxValue } from '../utils/get-value';
1920
import initialFieldState from '../constants/initial-field-state';
2021

2122
const fieldPropTypes = {
@@ -138,6 +139,7 @@ function createFieldClass(customControlPropsMap = {}, s = defaultStrategy) {
138139
const defaultControlPropsMap = {
139140
checkbox: {
140141
changeAction: s.actions.checkWithValue,
142+
getValue: getCheckboxValue,
141143
},
142144
};
143145

src/utils/get-value.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import isMulti from './is-multi';
2+
13
function isEvent(event) {
24
return !!(event && event.stopPropagation && event.preventDefault);
35
}
@@ -28,3 +30,13 @@ function getEventValue(event) {
2830
export default function getValue(value) {
2931
return isEvent(value) ? getEventValue(value) : value;
3032
}
33+
34+
export function getCheckboxValue(_, props) {
35+
const { controlProps } = props;
36+
37+
if (isMulti(props.model)) {
38+
return controlProps.value;
39+
}
40+
41+
return !props.modelValue;
42+
}

0 commit comments

Comments
 (0)