Skip to content

Commit

Permalink
Merge pull request #101 from femioladeji/remove-prop-types
Browse files Browse the repository at this point in the history
Remove prop-types depedency
  • Loading branch information
femioladeji authored Sep 21, 2020
2 parents 80af0bb + a32b1d3 commit 8507c74
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 135 deletions.
11 changes: 8 additions & 3 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-slideshow-image",
"version": "3.2.0",
"version": "3.3.0",
"author": "Femi Oladeji",
"description": "An image slideshow with react",
"files": [
Expand Down Expand Up @@ -81,7 +81,6 @@
},
"dependencies": {
"@tweenjs/tween.js": "^18.1.2",
"prop-types": "^15.5.10",
"resize-observer-polyfill": "^1.5.1"
}
}
62 changes: 19 additions & 43 deletions src/fade.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import TWEEN from '@tweenjs/tween.js';
import ResizeObserver from 'resize-observer-polyfill';
import { validatePropTypes, propTypes, getProps } from './props';
import {
getUnhandledProps,
showNextArrow,
Expand Down Expand Up @@ -39,6 +39,7 @@ class Fade extends Component {
this.setWidth();
this.play();
this.initResizeObserver();
validatePropTypes(this.props);
}

initResizeObserver() {
Expand All @@ -50,26 +51,27 @@ class Fade extends Component {
}

play() {
const { autoplay, children } = this.props;
const { autoplay, children, duration } = getProps(this.props);
const { index } = this.state;
if (autoplay && children.length > 1) {
clearTimeout(this.timeout);
this.timeout = setTimeout(
() => this.fadeImages(index + 1),
this.props.duration
duration
);
}
}

componentDidUpdate(props) {
if (this.props.autoplay !== props.autoplay) {
if (this.props.autoplay) {
const { autoplay, children } = getProps(this.props);
if (autoplay !== props.autoplay) {
if (autoplay) {
this.play();
} else {
clearTimeout(this.timeout);
}
}
if (this.props.children.length != props.children.length) {
if (children.length != props.children.length) {
this.applyStyle();
this.play();
}
Expand Down Expand Up @@ -113,21 +115,21 @@ class Fade extends Component {
}

pauseSlides() {
if (this.props.pauseOnHover) {
if (getProps(this.props).pauseOnHover) {
clearTimeout(this.timeout);
}
}

startSlides() {
const { pauseOnHover, autoplay } = this.props;
const { pauseOnHover, autoplay, duration } = getProps(this.props);
if (pauseOnHover && autoplay) {
this.timeout = setTimeout(() => this.goNext(), this.props.duration);
this.timeout = setTimeout(() => this.goNext(), duration);
}
}

goNext() {
const { index } = this.state;
const { children, infinite } = this.props;
const { children, infinite } = getProps(this.props);
if (!infinite && index === children.length - 1) {
return;
}
Expand All @@ -136,7 +138,7 @@ class Fade extends Component {

goBack() {
const { index } = this.state;
const { children, infinite } = this.props;
const { children, infinite } = getProps(this.props);
if (!infinite && index === 0) {
return;
}
Expand All @@ -162,9 +164,9 @@ class Fade extends Component {
}

render() {
const { indicators, children, arrows } = this.props;
const { indicators, children, arrows } = getProps(this.props);
const { index } = this.state;
const unhandledProps = getUnhandledProps(Fade.propTypes, this.props);
const unhandledProps = getUnhandledProps(propTypes, this.props);
return (
<div aria-roledescription="carousel" {...unhandledProps}>
<div
Expand All @@ -175,7 +177,7 @@ class Fade extends Component {
ref={this.reactSlideshowWrapper}
>
{arrows &&
showPreviousArrow(this.props, this.state.index, this.preFade)}
showPreviousArrow(getProps(this.props), this.state.index, this.preFade)}
<div className="react-slideshow-fade-wrapper" ref={this.wrapper}>
<div
className="react-slideshow-fade-images-wrap"
Expand All @@ -197,10 +199,10 @@ class Fade extends Component {
))}
</div>
</div>
{arrows && showNextArrow(this.props, this.state.index, this.preFade)}
{arrows && showNextArrow(getProps(this.props), this.state.index, this.preFade)}
</div>
{indicators &&
showIndicators(this.props, this.state.index, this.navigate)}
showIndicators(getProps(this.props), this.state.index, this.navigate)}
</div>
);
}
Expand All @@ -215,7 +217,7 @@ class Fade extends Component {
transitionDuration,
onChange,
easing
} = this.props;
} = getProps(this.props);
const existingTweens = this.tweenGroup.getAll();
if (!existingTweens.length) {
if (!this.divsContainer.children[newIndex]) {
Expand Down Expand Up @@ -264,30 +266,4 @@ class Fade extends Component {
}
}

Fade.defaultProps = {
duration: 5000,
transitionDuration: 1000,
defaultIndex: 0,
indicators: false,
arrows: true,
autoplay: true,
infinite: true,
pauseOnHover: true,
easing: 'linear'
};

Fade.propTypes = {
duration: PropTypes.number,
transitionDuration: PropTypes.number,
defaultIndex: PropTypes.number,
indicators: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
arrows: PropTypes.bool,
autoplay: PropTypes.bool,
infinite: PropTypes.bool,
onChange: PropTypes.func,
pauseOnHover: PropTypes.bool,
prevArrow: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
nextArrow: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
easing: PropTypes.string
};
export default Fade;
54 changes: 54 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const defaultProps = {
duration: 5000,
transitionDuration: 1000,
defaultIndex: 0,
infinite: true,
autoplay: true,
indicators: false,
arrows: true,
pauseOnHover: true,
scale: 1,
easing: 'linear'
};

export const getProps = componentProps => ({
...defaultProps,
...componentProps
});

export const propTypes = {
duration: 'number',
transitionDuration: 'number',
defaultIndex: 'number',
infinite: 'boolean',
indicators: ['boolean', 'function'],
autoplay: 'boolean',
arrows: 'boolean',
onChange: 'function',
pauseOnHover: 'boolean',
prevArrow: ['object', 'function'],
nextArrow: ['object', 'function'],
scale: 'number',
easing: 'string'
};

export const validatePropTypes = props => {
for (const key in props) {
const propValueType = typeof props[key];
if (propTypes[key]) {
if (
Array.isArray(propTypes[key]) &&
!propTypes[key].includes(propValueType)
) {
console.warn(
`${key} must be of one of type ${propTypes[key].join(', ')}`
);
} else if (
!Array.isArray(propTypes[key]) &&
propValueType !== propTypes[key]
) {
console.warn(`${key} must be of type ${propTypes[key]}`);
}
}
}
};
Loading

0 comments on commit 8507c74

Please sign in to comment.