File tree Expand file tree Collapse file tree 3 files changed +36
-4
lines changed
packages/feature-toggle-jsx Expand file tree Collapse file tree 3 files changed +36
-4
lines changed Original file line number Diff line number Diff line change 10
10
},
11
11
{
12
12
"path" : " ./packages/feature-toggle-jsx/lib/featuretogglejsx.umd.js" ,
13
- "maxSize" : " 840 B"
13
+ "maxSize" : " 985 B"
14
14
},
15
15
{
16
16
"path" : " ./packages/format-to-jsx/lib/formattojsx.umd.js" ,
Original file line number Diff line number Diff line change @@ -4,14 +4,15 @@ import { FeatureConfig } from '../FeaturesContext';
4
4
import useFeature from '../useFeature' ;
5
5
import { nameOf } from '../react-utils' ;
6
6
7
+
7
8
export type withFeatureHoC < TFeatureConfig extends FeatureConfig > = <
8
9
TOrigProps extends { } ,
9
10
TFeatureName extends Extract < keyof TFeatureConfig , string | number >
10
11
> (
11
12
Component : React . ComponentType < TOrigProps > ,
12
13
featureName : TFeatureName ,
13
14
isEnabled ?: ( feature : TFeatureConfig [ TFeatureName ] ) => boolean
14
- ) => React . FC < TOrigProps > ;
15
+ ) => React . FC < TOrigProps & Partial < TFeatureConfig > > ;
15
16
16
17
const withFeature = <
17
18
TOrigProps extends { } ,
@@ -23,9 +24,9 @@ const withFeature = <
23
24
isEnabled : ( feature : TFeatureConfig [ TFeatureName ] ) => boolean = ( _ ) => ! ! _
24
25
) => {
25
26
const Wrapped : React . FC < TOrigProps > = React . memo ( ( props ) => {
26
- const [ enabled ] = useFeature ( featureName , isEnabled ) ;
27
+ const [ enabled , features ] = useFeature ( featureName , isEnabled ) ;
27
28
28
- if ( enabled ) return < Component { ...props } /> ;
29
+ if ( enabled ) return < Component { ...props } { ... { [ featureName ] : features } } /> ;
29
30
30
31
return null ;
31
32
} ) ;
Original file line number Diff line number Diff line change @@ -79,4 +79,35 @@ describe('withFeature', () => {
79
79
80
80
expect ( container . textContent ) . toEqual ( 'This is feature 1' ) ;
81
81
} ) ;
82
+
83
+ it ( 'should pass config to props' , ( ) => {
84
+ const featuresConfig = {
85
+ someFeat : {
86
+ someCustomField : 10
87
+ }
88
+ } as CustomFeatureConfig ;
89
+
90
+ const App : React . FC = ( { children } ) => < div > { children } </ div > ;
91
+ type Props = { someFeat ?: { someCustomField : number } }
92
+ const Feat1 : React . FC < Props > = ( props ) => {
93
+ const { someFeat } = props ;
94
+ return ( < span > This is feature { someFeat ? someFeat . someCustomField : '1' } </ span > ) ;
95
+ } ;
96
+
97
+ const WrappedFeat1 = withFeature ( Feat1 , 'someFeat' ) ;
98
+
99
+ const UnderTest : React . FC = ( ) => (
100
+ < > < WrappedFeat1 /> </ >
101
+ ) ;
102
+
103
+ const WrappedApp = withFeaturesProvider ( App , featuresConfig ) ;
104
+
105
+ const { container } = render (
106
+ < WrappedApp >
107
+ < UnderTest />
108
+ </ WrappedApp > ,
109
+ ) ;
110
+
111
+ expect ( container . textContent ) . toEqual ( 'This is feature 10' ) ;
112
+ } ) ;
82
113
} ) ;
You can’t perform that action at this time.
0 commit comments