Skip to content

Commit

Permalink
Merge d5ce8d1 into f9785b2
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Frachet committed Sep 17, 2018
2 parents f9785b2 + d5ce8d1 commit c4cf6f2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rn-displayable",
"version": "1.0.0",
"version": "1.0.1",
"private": false,
"main": "src/index.js",
"homepage": "https://github.com/mfrachet/rn-displayable",
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/displayable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ describe('makeDisplayable', () => {
expect(wrapper.find('Animate').prop('Animation')).toEqual(SomeAnimation);
});
});

describe('shouldComponentUpdate', () => {
it('should have updated the component', () => {
wrapper = getWrapper();

const instance = wrapper.instance();

expect(instance.shouldComponentUpdate({ isDisplayed: true })).toBe(true);
});

it('shouldnt have updated the component', () => {
wrapper = getWrapper({ isDisplayed: true });

const instance = wrapper.instance();

expect(instance.shouldComponentUpdate({ isDisplayed: true })).toBe(false);
});
});
});
18 changes: 18 additions & 0 deletions src/__tests__/visible.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ describe('makeVisible', () => {
expect(wrapper.find('Animate').prop('Animation')).toEqual(SomeAnimation);
});
});

describe('shouldComponentUpdate', () => {
it('should have updated the component', () => {
wrapper = getWrapper();

const instance = wrapper.instance();

expect(instance.shouldComponentUpdate({ isVisible: true })).toBe(true);
});

it('shouldnt have updated the component', () => {
wrapper = getWrapper({ isVisible: true });

const instance = wrapper.instance();

expect(instance.shouldComponentUpdate({ isVisible: true })).toBe(false);
});
});
});
21 changes: 15 additions & 6 deletions src/displayable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import Animate from './animate';
import { verifyRules } from './helpers';

const makeDisplayable = (Component) => {
const Displayable = ({ rules, Animation, isDisplayed, ...props }) =>
(isDisplayed || verifyRules(rules, props) ? (
<Animate Animation={Animation}>
<Component {...props} />
</Animate>
) : null);
class Displayable extends React.Component {
shouldComponentUpdate(nextProps) {
return nextProps.isDisplayed !== this.props.isDisplayed;
}

render() {
const { rules, Animation, isDisplayed, ...props } = this.props;

return (isDisplayed || verifyRules(rules, props) ? (
<Animate Animation={Animation}>
<Component {...props} />
</Animate>
) : null);
}
}

Displayable.propTypes = {
isDisplayed: PropTypes.bool,
Expand Down
24 changes: 16 additions & 8 deletions src/visible.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import Animate from './animate';
import { verifyRules } from './helpers';

const makeVisible = (Component) => {
const Visible = ({ rules, Animation, isVisible, style, ...props }) => {
const visibilityStyle = { display: isVisible || verifyRules(rules, props) ? 'flex' : 'none' };
class Visible extends React.Component {
shouldComponentUpdate(nextProps) {
return nextProps.isVisible !== this.props.isVisible;
}

render() {
const { rules, Animation, isVisible, style, ...props } = this.props;
const visibilityStyle = { display: isVisible || verifyRules(rules, props) ? 'flex' : 'none' };

return (
<Animate Animation={Animation}>
<Component style={{ ...style, ...visibilityStyle }} {...props} />
</Animate>
);
}
}

return (
<Animate Animation={Animation}>
<Component style={{ ...style, ...visibilityStyle }} {...props} />
</Animate>
);
};
Visible.propTypes = {
isVisible: PropTypes.bool,
rules: PropTypes.arrayOf(PropTypes.func),
Expand Down

0 comments on commit c4cf6f2

Please sign in to comment.