forked from acdlite/recompose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request acdlite#677 from evenchange4/feature/render-props-hoc
feat: add fromRenderProps [BREAKING CHANGES]
- Loading branch information
Showing
10 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { fromRenderProps, compose, toRenderProps, defaultProps } from '../' | ||
|
||
test('fromRenderProps passes additional props to base component', () => { | ||
const RenderPropsComponent = ({ children }) => children({ i18n: 'zh-TW' }) | ||
const EnhancedComponent = fromRenderProps( | ||
RenderPropsComponent, | ||
({ i18n }) => ({ | ||
i18n, | ||
}) | ||
)('div') | ||
expect(EnhancedComponent.displayName).toBe('fromRenderProps(div)') | ||
|
||
const div = mount(<EnhancedComponent />) | ||
expect(div.html()).toBe(`<div i18n="zh-TW"></div>`) | ||
}) | ||
|
||
test('fromRenderProps passes additional props to base component with custom renderPropName', () => { | ||
const RenderPropsComponent = ({ render }) => render({ i18n: 'zh-TW' }) | ||
const EnhancedComponent = fromRenderProps( | ||
RenderPropsComponent, | ||
({ i18n }) => ({ | ||
i18n, | ||
}), | ||
'render' | ||
)('div') | ||
expect(EnhancedComponent.displayName).toBe('fromRenderProps(div)') | ||
|
||
const div = mount(<EnhancedComponent />) | ||
expect(div.html()).toBe(`<div i18n="zh-TW"></div>`) | ||
}) | ||
|
||
test('fromRenderProps passes additional props to base component with 2 RenderPropsComponents', () => { | ||
const RenderPropsComponent1 = ({ children }) => children({ theme: 'dark' }) | ||
const RenderPropsComponent2 = ({ render }) => render({ i18n: 'zh-TW' }) | ||
const EnhancedComponent = compose( | ||
fromRenderProps( | ||
RenderPropsComponent1, | ||
({ theme }) => ({ theme }), | ||
'children' | ||
), | ||
fromRenderProps( | ||
RenderPropsComponent2, | ||
({ i18n }) => ({ locale: i18n }), | ||
'render' | ||
) | ||
)('div') | ||
expect(EnhancedComponent.displayName).toBe( | ||
'fromRenderProps(fromRenderProps(div))' | ||
) | ||
|
||
const div = mount(<EnhancedComponent />) | ||
expect(div.html()).toBe(`<div theme="dark" locale="zh-TW"></div>`) | ||
}) | ||
|
||
test('fromRenderProps meet toRenderProps', () => { | ||
const RenderPropsComponent = toRenderProps( | ||
defaultProps({ foo1: 'bar1', foo2: 'bar2' }) | ||
) | ||
|
||
const EnhancedComponent = fromRenderProps( | ||
RenderPropsComponent, | ||
({ foo1 }) => ({ | ||
foo: foo1, | ||
}) | ||
)('div') | ||
expect(EnhancedComponent.displayName).toBe('fromRenderProps(div)') | ||
|
||
const div = mount(<EnhancedComponent />) | ||
expect(div.html()).toBe(`<div foo="bar1"></div>`) | ||
}) |
6 changes: 3 additions & 3 deletions
6
...compose/__tests__/withRenderProps-test.js → ...recompose/__tests__/toRenderProps-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/packages/recompose/__tests__/types/test_fromRenderProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* @flow */ | ||
import React from 'react' | ||
import { compose, fromRenderProps } from '../..' | ||
|
||
import type { HOC } from '../..' | ||
|
||
const RenderPropsComponent1 = ({ children }) => children({ theme: 'dark' }) | ||
const RenderPropsComponent2 = ({ render }) => render({ i18n: 'zh-TW' }) | ||
|
||
type EnhancedCompProps = {||} | ||
|
||
const Comp = ({ i18n, theme }) => | ||
<div> | ||
{i18n} | ||
{theme} | ||
{ | ||
// $ExpectError | ||
(i18n: number) | ||
} | ||
{ | ||
// $ExpectError | ||
(theme: number) | ||
} | ||
</div> | ||
|
||
const enhancer: HOC<*, EnhancedCompProps> = compose( | ||
fromRenderProps(RenderPropsComponent1, props => ({ | ||
theme: props.theme, | ||
// $ExpectError property not found | ||
err: props.iMNotExists, | ||
})), | ||
fromRenderProps( | ||
RenderPropsComponent2, | ||
props => ({ | ||
i18n: props.i18n, | ||
// $ExpectError property not found | ||
err: props.iMNotExists, | ||
}), | ||
'render' | ||
) | ||
) | ||
|
||
const EnhancedComponent = enhancer(Comp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react' | ||
import setDisplayName from './setDisplayName' | ||
import wrapDisplayName from './wrapDisplayName' | ||
|
||
const fromRenderProps = ( | ||
RenderPropsComponent, | ||
propsMapper, | ||
renderPropName = 'children' | ||
) => BaseComponent => { | ||
const baseFactory = React.createFactory(BaseComponent) | ||
const renderPropsFactory = React.createFactory(RenderPropsComponent) | ||
|
||
const FromRenderProps = ownerProps => | ||
renderPropsFactory({ | ||
[renderPropName]: props => | ||
baseFactory({ ...ownerProps, ...propsMapper(props) }), | ||
}) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
return setDisplayName(wrapDisplayName(BaseComponent, 'fromRenderProps'))( | ||
FromRenderProps | ||
) | ||
} | ||
|
||
return FromRenderProps | ||
} | ||
|
||
export default fromRenderProps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/packages/recompose/withRenderProps.js → src/packages/recompose/toRenderProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export default function withRenderProps(hoc) { | ||
export default function toRenderProps(hoc) { | ||
const RenderPropsComponent = props => props.children(props) | ||
return hoc(RenderPropsComponent) | ||
} |