Skip to content

Commit

Permalink
refactor(Carousel): Update Carousel from legacy context
Browse files Browse the repository at this point in the history
  • Loading branch information
bestguy authored and phwebi committed Oct 27, 2021
1 parent 51ae792 commit 1c6db58
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Carousel.js
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import CarouselItem from './CarouselItem';
import { CarouselContext } from './CarouselContext';
import { mapToCssModules } from './utils';

const SWIPE_THRESHOLD = 40;
Expand All @@ -24,7 +25,7 @@ class Carousel extends React.Component {
};
}

getChildContext() {
getContextValue() {
return { direction: this.state.direction };
}

Expand Down Expand Up @@ -188,7 +189,9 @@ class Carousel extends React.Component {
if (slidesOnly) {
return (
<div className={outerClasses} onMouseEnter={this.hoverStart} onMouseLeave={this.hoverEnd}>
{this.renderItems(children, innerClasses)}
<CarouselContext.Provider value={this.getContextValue()}>
{this.renderItems(children, innerClasses)}
</CarouselContext.Provider>
</div>
);
}
Expand All @@ -201,9 +204,11 @@ class Carousel extends React.Component {

return (
<div className={outerClasses} onMouseEnter={this.hoverStart} onMouseLeave={this.hoverEnd}>
{this.renderItems(carouselItems, innerClasses)}
{controlLeft}
{controlRight}
<CarouselContext.Provider value={this.getContextValue()}>
{this.renderItems(carouselItems, innerClasses)}
{controlLeft}
{controlRight}
</CarouselContext.Provider>
</div>
);
}
Expand All @@ -223,10 +228,12 @@ class Carousel extends React.Component {
return (
<div className={outerClasses} onMouseEnter={this.hoverStart} onMouseLeave={this.hoverEnd}
onTouchStart={this.handleTouchStart} onTouchEnd={this.handleTouchEnd}>
{wrappedIndicators}
{this.renderItems(carouselItems, innerClasses)}
{controlLeft}
{controlRight}
<CarouselContext.Provider value={this.getContextValue()}>
{wrappedIndicators}
{this.renderItems(carouselItems, innerClasses)}
{controlLeft}
{controlRight}
</CarouselContext.Provider>
</div>
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/CarouselContext.js
@@ -0,0 +1,9 @@
import React from 'react';

/**
* CarouselContext
* {
* direction: PropTypes.oneOf(['start', 'end']).isRequired,
* }
*/
export const CarouselContext = React.createContext({});

0 comments on commit 1c6db58

Please sign in to comment.