Skip to content

Commit

Permalink
v2.4.1
Browse files Browse the repository at this point in the history
- Allow multiple children
  • Loading branch information
Federico Zivolo committed May 1, 2017
1 parent daea3c0 commit 73c7254
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
1 change: 0 additions & 1 deletion docs/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function MyComponent(_ref) {
function App() {
return React.createElement(ReactResizeAware.default, {
component: MyComponent,
useBoundingClientRect: true,
style: {position: 'relative'},
onResize: function onResize(sizes) {
return console.log(sizes);
Expand Down
7 changes: 4 additions & 3 deletions docs/react-resize-aware.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-resize-aware",
"version": "2.4.0",
"version": "2.4.1",
"description": "A resize aware component used to detect sizes changes on your components",
"main": "dist/index.js",
"scripts": {
Expand Down
42 changes: 42 additions & 0 deletions src/__snapshots__/index.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,48 @@ exports[`allows to define an \`onResize\` property 1`] = `
</ResizeAware>
`;

exports[`allows to use its child as target 1`] = `
<ResizeAware
component="div"
style={
Object {
"position": "relative",
}
}
>
<div
style={
Object {
"position": "relative",
}
}
>
<object
onLoad={[Function]}
style={
Object {
"display": "block",
"height": "100%",
"left": 0,
"overflow": "hidden",
"pointerEvents": "none",
"position": "absolute",
"top": 0,
"width": "100%",
"zIndex": -1,
}
}
type="text/html"
/>
<DummyComponent>
<div>
x
</div>
</DummyComponent>
</div>
</ResizeAware>
`;

exports[`allows to use its children as target 1`] = `
<ResizeAware
component="div"
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright 2016, Federico Zivolo
//

import {createElement, Component, cloneElement} from 'react';
import {createElement, Component, Children, cloneElement} from 'react';

const style = {
display: 'block',
Expand Down Expand Up @@ -62,14 +62,13 @@ export default class ResizeAware extends Component {
const {width, height} = this.state;

const hasCustomComponent = typeof component !== 'string';
const passSizeProps = !onlyEvent && hasCustomComponent;

return createElement(
component,
{
[hasCustomComponent ? 'getRef' : 'ref']: el => (this.container = el),
width,
height,
width: hasCustomComponent ? width : undefined,
height: hasCustomComponent ? height : undefined,
...props,
},
createElement('object', {
Expand All @@ -78,7 +77,9 @@ export default class ResizeAware extends Component {
ref: el => (this.resizeElement = el),
onLoad: this.handleObjectLoad,
}),
!!children && cloneElement(children, passSizeProps ? this.state : null)
Children.map(children, child =>
cloneElement(child, !onlyEvent ? this.state : null)
)
);
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function DummyComponent2({getRef, ...props}) {
return <div ref={getRef} {...props} />;
}

it('allows to use its children as target', () => {
it('allows to use its child as target', () => {
const wrapper = mount(
<ResizeAware style={{position: 'relative'}}>
<DummyComponent />
Expand All @@ -21,6 +21,19 @@ it('allows to use its children as target', () => {
expect(toJson(wrapper)).toMatchSnapshot();
});

it('allows to use itself between a component markup', () => {
const wrapper = mount(
<div>
<ResizeAware style={{position: 'relative'}}>
<div />
<div />
</ResizeAware>
</div>
);

expect(toJson(wrapper)).toMatchSnapshot();
});

it('allows to define an `onResize` property', () => {
const handleResize = jest.fn();
const wrapper = mount(
Expand Down

0 comments on commit 73c7254

Please sign in to comment.