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

Commit fbe3ee8

Browse files
committed
Making mapProps assignable from existing mapProps if specified. Addresses #416
1 parent f6be522 commit fbe3ee8

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

docs/guides/custom-controls.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,44 @@ By default, any props on `<Control>` that are _not_ part of the `Control.propTyp
8484
/>
8585
```
8686

87+
## Model and View Values
88+
89+
One important thing to keep in mind is that there is no single `value` for a `<Control>`; rather, there are two types of values (that are equivalent, more often than not):
90+
91+
- `modelValue` - the exact value of the model in the store's state
92+
- `viewValue` - the _displayed_ value of the model in the component's state
93+
94+
For example, if a text control has `updateOn="blur"`, its `viewValue` will represent what is being typed in the input, whereas the `modelValue` will represent the persisted value, once the input is blurred.
95+
96+
Because of this, you will want to map the `viewValue` to a custom control's `value` if you wish to externally update the value:
97+
98+
```jsx
99+
const CustomInput = (props) => (
100+
<div className="custom-input">
101+
<input type="text" {...props} />
102+
</div>
103+
);
104+
105+
const CustomInputControl = (props) => (
106+
<Control
107+
component={CustomInput}
108+
mapProps={{
109+
value: (props) => props.viewValue,
110+
}}
111+
{...props}
112+
/>
113+
);
114+
```
115+
116+
Alternatively, you can just _assign_ existing mapped props to your custom control:
117+
118+
```jsx
119+
// the prop mapping of Control.text is used below:
120+
const CustomInputControl = (props) => (
121+
<Control.text component={CustomInput} />
122+
);
123+
```
124+
87125
## Advanced Custom Controls
88126

89127
Some custom controls won't have prop keys that match up exactly with the standard event handler props, such as `onChangeText` in React Native's `<TextInput>` component corresponding to `onChange`. You can specify a prop mapping in the `mapProps={{...}}` prop to specify the mapping:

src/components/control-component.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,26 +540,36 @@ Control.defaultProps = {
540540

541541
const ConnectedControl = resolveModel(connect(mapStateToProps)(Control));
542542

543+
/* eslint-disable react/prop-types */
543544
ConnectedControl.input = (props) => (
544545
<ConnectedControl
545546
component="input"
546-
mapProps={controlPropsMap.default}
547+
mapProps={{
548+
...controlPropsMap.default,
549+
...props.mapProps,
550+
}}
547551
{...props}
548552
/>
549553
);
550554

551555
ConnectedControl.text = (props) => (
552556
<ConnectedControl
553557
component="input"
554-
mapProps={controlPropsMap.text}
558+
mapProps={{
559+
...controlPropsMap.text,
560+
...props.mapProps,
561+
}}
555562
{...props}
556563
/>
557564
);
558565

559566
ConnectedControl.textarea = (props) => (
560567
<ConnectedControl
561568
component="textarea"
562-
mapProps={controlPropsMap.textarea}
569+
mapProps={{
570+
...controlPropsMap.textarea,
571+
...props.mapProps,
572+
}}
563573
{...props}
564574
/>
565575
);
@@ -568,7 +578,10 @@ ConnectedControl.radio = (props) => (
568578
<ConnectedControl
569579
component="input"
570580
type="radio"
571-
mapProps={controlPropsMap.radio}
581+
mapProps={{
582+
...controlPropsMap.radio,
583+
...props.mapProps,
584+
}}
572585
{...props}
573586
/>
574587
);
@@ -577,7 +590,10 @@ ConnectedControl.checkbox = (props) => (
577590
<ConnectedControl
578591
component="input"
579592
type="checkbox"
580-
mapProps={controlPropsMap.checkbox}
593+
mapProps={{
594+
...controlPropsMap.checkbox,
595+
...props.mapProps,
596+
}}
581597
{...props}
582598
/>
583599
);
@@ -586,15 +602,21 @@ ConnectedControl.file = (props) => (
586602
<ConnectedControl
587603
component="input"
588604
type="file"
589-
mapProps={controlPropsMap.file}
605+
mapProps={{
606+
...controlPropsMap.file,
607+
...props.mapProps,
608+
}}
590609
{...props}
591610
/>
592611
);
593612

594613
ConnectedControl.select = (props) => (
595614
<ConnectedControl
596615
component="select"
597-
mapProps={controlPropsMap.select}
616+
mapProps={{
617+
...controlPropsMap.select,
618+
...props.mapProps,
619+
}}
598620
{...props}
599621
/>
600622
);
@@ -603,7 +625,10 @@ ConnectedControl.reset = (props) => (
603625
<ConnectedControl
604626
component="button"
605627
type="reset"
606-
mapProps={controlPropsMap.reset}
628+
mapProps={{
629+
...controlPropsMap.reset,
630+
...props.mapProps,
631+
}}
607632
{...props}
608633
/>
609634
);

0 commit comments

Comments
 (0)