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

Commit 54444f0

Browse files
committed
Exposing isToggle and updating docs. Fixes #725
1 parent 1cc67a1 commit 54444f0

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

docs/api/Control.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- [`disabled`](#disabled)
1818
- [`getRef`](#prop-getRef)
1919
- [`persist`](#prop-persist)
20+
- [`getValue`](#prop-getValue)
21+
- [`isToggle`](#prop-isToggle)
2022

2123
## `<Control>`
2224

@@ -372,3 +374,26 @@ _(Boolean)_: Signifies that the field state (validation, etc.) should not persis
372374
persist
373375
/>
374376
```
377+
378+
## `getValue={(event, props) => ...}`
379+
_(Function)_: Determines the value given the `event` (from `onChange`) and optionally the control component's `props`.
380+
381+
By default, the `getValue` function returns the value by hecking if the `event` is a DOM event.
382+
- If so, it returns `event.target.value`
383+
- If not, it returns the `event`.
384+
385+
For `<Control.checkbox />`, the default `getValue` function is:
386+
- the original `value={...}` passed into the control, for multi-checkboxes (e.g., `model="user.hobbies[]"`)
387+
- the inverse boolean value of the `modelValue`.
388+
389+
```jsx
390+
<Control.text
391+
model="user.name"
392+
getValue={(event) => event.target.value}
393+
/>
394+
```
395+
396+
## `isToggle={false}`
397+
_(Boolean)_: Signifies that the control is a toggle (e.g., a checkbox or a radio). If `true`, then some optimizations are made.
398+
399+
Default: `true` for `<Control.radio>` and `<Control.checkbox>`, `false` for all other controls.

src/components/control-component.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const propTypes = {
100100
debounce: PropTypes.number,
101101
persist: PropTypes.bool,
102102
getValue: PropTypes.func,
103+
isToggle: PropTypes.bool,
103104
};
104105

105106
const defaultStrategy = {
@@ -333,7 +334,7 @@ function createControlClass(s = defaultStrategy) {
333334
}
334335

335336
setViewValue(viewValue) {
336-
if (!this.isToggle()) {
337+
if (!this.props.isToggle) {
337338
this.setState({ viewValue: this.parse(viewValue) });
338339
}
339340
}
@@ -342,12 +343,6 @@ function createControlClass(s = defaultStrategy) {
342343
return this.props.getValue(event, this.props);
343344
}
344345

345-
isToggle() {
346-
const { component, controlProps } = this.props;
347-
348-
return component === 'input' && ~['radio', 'checkbox'].indexOf(controlProps.type);
349-
}
350-
351346
handleIntents() {
352347
const {
353348
model,
@@ -371,7 +366,7 @@ function createControlClass(s = defaultStrategy) {
371366

372367
if ((focused && this.node.focus)
373368
&& (
374-
!this.isToggle()
369+
!this.props.isToggle
375370
|| typeof intent.value === 'undefined'
376371
|| intent.value === controlProps.value
377372
)) {
@@ -517,7 +512,7 @@ function createControlClass(s = defaultStrategy) {
517512
: event;
518513
}
519514

520-
if (this.isToggle()) {
515+
if (this.props.isToggle) {
521516
return compose(
522517
dispatchBatchActions,
523518
persistEventWithCallback(controlEventHandler || identity)
@@ -626,6 +621,7 @@ function createControlClass(s = defaultStrategy) {
626621
withField: true,
627622
persist: false,
628623
getValue: _getValue,
624+
isToggle: false,
629625
};
630626

631627
function mapStateToProps(state, props) {
@@ -702,6 +698,7 @@ function createControlClass(s = defaultStrategy) {
702698
<ConnectedControl
703699
component="input"
704700
type="radio"
701+
isToggle
705702
mapProps={{
706703
...controlPropsMap.radio,
707704
...props.mapProps,
@@ -714,6 +711,7 @@ function createControlClass(s = defaultStrategy) {
714711
<ConnectedControl
715712
component="input"
716713
type="checkbox"
714+
isToggle
717715
mapProps={{
718716
...controlPropsMap.checkbox,
719717
...props.mapProps,

src/components/field-component.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ function createFieldClass(customControlPropsMap = {}, s = defaultStrategy) {
140140
checkbox: {
141141
changeAction: s.actions.checkWithValue,
142142
getValue: getCheckboxValue,
143+
isToggle: true,
144+
},
145+
radio: {
146+
isToggle: true,
143147
},
144148
};
145149

0 commit comments

Comments
 (0)