Skip to content

Commit

Permalink
v2.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Zivolo committed Aug 24, 2017
1 parent 4fe1c68 commit 24d9a73
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 24 deletions.
22 changes: 18 additions & 4 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.5.0",
"version": "2.6.0",
"description": "A resize aware component used to detect sizes changes on your components",
"main": "dist/index.js",
"scripts": {
Expand Down
50 changes: 50 additions & 0 deletions src/__snapshots__/index.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,56 @@ exports[`allows to define an \`onResize\` property 1`] = `
</ResizeAware>
`;

exports[`allows to define custom width and height names 1`] = `
<ResizeAware
component="div"
heightPropName="customHeight"
style={
Object {
"position": "relative",
}
}
widthPropName="customWidth"
>
<div
style={
Object {
"position": "relative",
}
}
>
<object
aria-hidden={true}
onLoad={[Function]}
style={
Object {
"display": "block",
"height": "100%",
"left": 0,
"overflow": "hidden",
"pointerEvents": "none",
"position": "absolute",
"top": 0,
"width": "100%",
"zIndex": -1,
}
}
tabIndex={-1}
type="text/html"
/>
<Test
customHeight={10}
customWidth={10}
>
<div
customHeight={10}
customWidth={10}
/>
</Test>
</div>
</ResizeAware>
`;

exports[`allows to use its child as target 1`] = `
<ResizeAware
component="div"
Expand Down
37 changes: 29 additions & 8 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, Children, cloneElement} from 'react';
import { createElement, Component, Children, cloneElement } from 'react';

const style = {
display: 'block',
Expand All @@ -21,7 +21,14 @@ const style = {
};

export default class ResizeAware extends Component {
static defaultProps = {component: 'div'};
static defaultProps = {
component: 'div',
// don't define here the defaults to avoid to break
// the render result of previous versions
// TODO: use defaultProps to define default values
widthPropName: undefined,
heightPropName: undefined,
};
state = {};

// Init the resizeElement
Expand Down Expand Up @@ -58,17 +65,28 @@ export default class ResizeAware extends Component {
};

render() {
const {children, onResize, onlyEvent, component, ...props} = this.props;
const {width, height} = this.state;
const {
children,
onResize,
onlyEvent,
component,
widthPropName,
heightPropName,
...props
} = this.props;
const { width, height } = this.state;

const hasCustomComponent = typeof component !== 'string';

const widthProp = [widthPropName || 'width'];
const heightProp = [heightPropName || 'height'];

return createElement(
component,
{
[hasCustomComponent ? 'getRef' : 'ref']: el => (this.container = el),
width: hasCustomComponent ? width : undefined,
height: hasCustomComponent ? height : undefined,
[widthProp]: hasCustomComponent ? width : undefined,
[heightProp]: hasCustomComponent ? height : undefined,
...props,
},
createElement('object', {
Expand All @@ -80,12 +98,15 @@ export default class ResizeAware extends Component {
tabIndex: -1,
}),
Children.map(children, child =>
cloneElement(child, !onlyEvent ? {width, height} : null)
cloneElement(
child,
!onlyEvent ? { [widthProp]: width, [heightProp]: height } : null
)
)
);
}
}

export function makeResizeAware(component) {
return props => createElement(ResizeAware, {component, ...props});
return props => createElement(ResizeAware, { component, ...props });
}
41 changes: 30 additions & 11 deletions src/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import React from 'react';
import {shallow, mount} from 'enzyme';
import { shallow, mount } from 'enzyme';
import toJson from 'enzyme-to-json';
import ResizeAware from './index.js';

function DummyComponent({width, height}) {
return <div>{width}x{height}</div>;
function DummyComponent({ width, height }) {
return (
<div>
{width}x{height}
</div>
);
}

function DummyComponent2({getRef, ...props}) {
function DummyComponent2({ getRef, ...props }) {
return <div ref={getRef} {...props} />;
}

it('allows to use its child as target', () => {
const wrapper = mount(
<ResizeAware style={{position: 'relative'}}>
<ResizeAware style={{ position: 'relative' }}>
<DummyComponent />
</ResizeAware>
);
Expand All @@ -24,7 +28,7 @@ it('allows to use its child as target', () => {
it('allows to use itself between a component markup', () => {
const wrapper = mount(
<div>
<ResizeAware style={{position: 'relative'}}>
<ResizeAware style={{ position: 'relative' }}>
<div />
<div />
</ResizeAware>
Expand All @@ -37,25 +41,40 @@ it('allows to use itself between a component markup', () => {
it('allows to define an `onResize` property', () => {
const handleResize = jest.fn();
const wrapper = mount(
<ResizeAware style={{position: 'relative'}} onResize={handleResize} />
<ResizeAware style={{ position: 'relative' }} onResize={handleResize} />
);

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

it('allows to define a custom component', () => {
const wrapper = shallow(
<ResizeAware style={{position: 'relative'}} component={DummyComponent} />
<ResizeAware style={{ position: 'relative' }} component={DummyComponent} />
);

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

it('applies aria attributes to <object> to avoid screenreader issues', () => {
const object = mount(
<ResizeAware />
).find('object');
const object = mount(<ResizeAware />).find('object');

expect(object.prop('aria-hidden')).toBe(true);
expect(object.prop('tabIndex')).toBe(-1);
});

it('allows to define custom width and height names', () => {
const Test = props => <div {...props} />;
const wrapper = mount(
<ResizeAware
style={{ position: 'relative' }}
widthPropName="customWidth"
heightPropName="customHeight"
>
<Test />
</ResizeAware>
);

wrapper.setState({ width: 10, height: 10 });

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

0 comments on commit 24d9a73

Please sign in to comment.