Skip to content

Commit 44a993e

Browse files
committed
changes hoc signature
Adopt a more conventional way of statically configuring the HOC and avoid mixing ElementPortal props with the transported component.
1 parent 424a743 commit 44a993e

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

src/withElementPortal.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ function getDisplayName(Component) {
55
return Component.displayName || Component.name || 'Component';
66
}
77

8-
const withElementPortal = (Child) => {
9-
const Portal = ({ selector, resetStyle, mapNodeToProps, ...childProps }) => {
10-
const ChildWrapper = (props) => <Child {...childProps} {...props} />;
11-
return <ElementPortal
12-
selector={selector}
13-
component={ChildWrapper}
14-
mapNodeToProps={mapNodeToProps}
15-
resetStyle={resetStyle}
16-
/>;
17-
};
8+
const withElementPortal = (portalProps) => (component) => {
9+
const WithElementPortal = (props) => (
10+
<ElementPortal
11+
{...portalProps}
12+
component={(mappedProps) => (
13+
React.createElement(component, {...props, ...mappedProps})
14+
)}
15+
/>
16+
);
1817

19-
Portal.displayName = `WithElementPortal(${getDisplayName(Child)})`;
20-
21-
return Portal;
18+
WithElementPortal.displayName = `WithElementPortal(${getDisplayName(component)})`;
19+
return WithElementPortal;
2220
};
2321

2422
export default withElementPortal;

test/ElementPortal.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ test('can be used as higher-order component', t => {
186186
</div>
187187
`;
188188
const Greeting = () => (<div>Hello</div>);
189-
const GreetingWithPortal = withElementPortal(Greeting);
189+
const GreetingWithPortal = withElementPortal({
190+
selector: `#${headerId}`
191+
})(Greeting);
190192

191193
render(
192194
<div>
193-
<GreetingWithPortal selector={`#${headerId}`} />
195+
<GreetingWithPortal />
194196
</div>,
195197
document.getElementById(appId)
196198
);
@@ -215,13 +217,13 @@ test('can be composed with other HOC\'s', t => {
215217
const MyComponent = (props) => <h1>Hello, {props.name}!</h1>;
216218

217219
const MyComposedComponent = compose(
218-
withElementPortal,
220+
withElementPortal({ selector: `#${headerId}` }),
219221
connect((state) => ({ name: state.name }))
220222
)(MyComponent);
221223

222224
render(
223225
<Provider store={store}>
224-
<MyComposedComponent selector={`#${headerId}`} />
226+
<MyComposedComponent />
225227
</Provider>,
226228
document.getElementById(appId)
227229
);
@@ -238,16 +240,18 @@ test('map dom node to props when used as HOC', t => {
238240
<div id="${headerId}" data-new="true">Joe</div>
239241
<div id="${appId}">Mary</div>
240242
`;
241-
const mapNodeToProps = (domNode) => ({
242-
name: domNode.textContent,
243-
isNew: !!domNode.getAttribute('data-new')
244-
});
245243
const Greeting = ({ name, isNew }) => (<div>Hello {isNew && 'and welcome '}{name}</div>);
246-
const GreetingWithPortal = withElementPortal(Greeting);
244+
const GreetingWithPortal = withElementPortal({
245+
selector: `#${headerId}`,
246+
mapNodeToProps: (domNode) => ({
247+
name: domNode.textContent,
248+
isNew: !!domNode.getAttribute('data-new')
249+
})
250+
})(Greeting);
247251

248252
render(
249253
<div>
250-
<GreetingWithPortal selector={`#${headerId}`} mapNodeToProps={mapNodeToProps} />
254+
<GreetingWithPortal />
251255
</div>,
252256
document.getElementById(appId)
253257
);
@@ -266,11 +270,13 @@ test('passes props through to the inner component when used as a HOC', t => {
266270
</div>
267271
`;
268272
const Greeting = ({name}) => (<div>Hello {name}</div>);
269-
const GreetingWithPortal = withElementPortal(Greeting);
273+
const GreetingWithPortal = withElementPortal({
274+
selector: `#${headerId}`
275+
})(Greeting);
270276

271277
render(
272278
<div>
273-
<GreetingWithPortal selector={`#${headerId}`} name="Joe" />
279+
<GreetingWithPortal name="Joe" />
274280
</div>,
275281
document.getElementById(appId)
276282
);

0 commit comments

Comments
 (0)