Skip to content

Commit

Permalink
Remove legacy context (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored May 30, 2021
1 parent 90d606b commit 4c258a2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 85 deletions.
18 changes: 11 additions & 7 deletions docs/src/pages/demos/DemoAnimateHeight.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import SwipeableViews from 'react-swipeable-views';
import SwipeableViews, { SwipeableViewsContext } from 'react-swipeable-views';

const styles = {
slide: {
Expand Down Expand Up @@ -35,12 +34,15 @@ for (let i = 0; i < 30; i += 1) {
}

class Slide4 extends PureComponent {
static contextType = SwipeableViewsContext;

state = {
large: false,
};

componentDidUpdate() {
this.context.swipeableViews.slideUpdateHeight();
const { slideUpdateHeight } = this.context;
slideUpdateHeight();
}

handleClick = () => {
Expand All @@ -52,6 +54,12 @@ class Slide4 extends PureComponent {
render() {
return (
<div style={Object.assign({}, styles.slide, styles.slide4)}>
<SwipeableViewsContext.Consumer>
{value => {
this.context = value;
return null;
}}
</SwipeableViewsContext.Consumer>
{list.slice(0, this.state.large ? 8 : 3)}
<button onClick={this.handleClick} type="button" style={styles.button}>
{this.state.large ? 'Collaspe' : 'Expand'}
Expand All @@ -61,10 +69,6 @@ class Slide4 extends PureComponent {
}
}

Slide4.contextTypes = {
swipeableViews: PropTypes.object.isRequired,
};

function DemoAnimateHeight() {
return (
<SwipeableViews animateHeight enableMouseEvents>
Expand Down
126 changes: 63 additions & 63 deletions packages/react-swipeable-views/src/SwipeableViews.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import PropTypes from 'prop-types';
import warning from 'warning';
import {
Expand Down Expand Up @@ -197,6 +197,12 @@ export function findNativeHandler(params) {
});
}

export const SwipeableViewsContext = React.createContext();

if (process.env.NODE_ENV !== 'production') {
SwipeableViewsContext.displayName = 'SwipeableViewsContext';
}

class SwipeableViews extends React.Component {
rootNode = null;

Expand Down Expand Up @@ -251,16 +257,6 @@ class SwipeableViews extends React.Component {
this.setIndexCurrent(props.index);
}

getChildContext() {
return {
swipeableViews: {
slideUpdateHeight: () => {
this.updateHeight();
},
},
};
}

componentDidMount() {
// Subscribe to transition end events.
this.transitionListener = addEventListener(this.containerNode, 'transitionend', event => {
Expand Down Expand Up @@ -327,6 +323,14 @@ class SwipeableViews extends React.Component {
clearTimeout(this.firstRenderTimeout);
}

getSwipeableViewsContext() {
return {
slideUpdateHeight: () => {
this.updateHeight();
},
};
}

setIndexCurrent(indexCurrent) {
if (!this.props.animateTransitions && this.indexCurrent !== indexCurrent) {
this.handleTransitionEnd();
Expand Down Expand Up @@ -785,56 +789,58 @@ So animateHeight is most likely having no effect at all.`,
}

return (
<div
ref={this.setRootNode}
style={Object.assign({}, axisProperties.root[axis], style)}
{...other}
{...touchEvents}
{...mouseEvents}
onScroll={this.handleScroll}
>
<SwipeableViewsContext.Provider value={this.getSwipeableViewsContext()}>
<div
ref={this.setContainerNode}
style={Object.assign({}, containerStyle, styles.container, containerStyleProp)}
className="react-swipeable-view-container"
ref={this.setRootNode}
style={Object.assign({}, axisProperties.root[axis], style)}
{...other}
{...touchEvents}
{...mouseEvents}
onScroll={this.handleScroll}
>
{React.Children.map(children, (child, indexChild) => {
if (renderOnlyActive && indexChild !== indexLatest) {
return null;
}

warning(
React.isValidElement(child),
`react-swipeable-view: one of the children provided is invalid: ${child}.
We are expecting a valid React Element`,
);

let ref;
let hidden = true;

if (indexChild === indexLatest) {
hidden = false;

if (animateHeight) {
ref = this.setActiveSlide;
slideStyle.overflowY = 'hidden';
<div
ref={this.setContainerNode}
style={Object.assign({}, containerStyle, styles.container, containerStyleProp)}
className="react-swipeable-view-container"
>
{React.Children.map(children, (child, indexChild) => {
if (renderOnlyActive && indexChild !== indexLatest) {
return null;
}
}

return (
<div
ref={ref}
style={slideStyle}
className={slideClassName}
aria-hidden={hidden}
data-swipeable="true"
>
{child}
</div>
);
})}

warning(
React.isValidElement(child),
`react-swipeable-view: one of the children provided is invalid: ${child}.
We are expecting a valid React Element`,
);

let ref;
let hidden = true;

if (indexChild === indexLatest) {
hidden = false;

if (animateHeight) {
ref = this.setActiveSlide;
slideStyle.overflowY = 'hidden';
}
}

return (
<div
ref={ref}
style={slideStyle}
className={slideClassName}
aria-hidden={hidden}
data-swipeable="true"
>
{child}
</div>
);
})}
</div>
</div>
</div>
</SwipeableViewsContext.Provider>
);
}
}
Expand Down Expand Up @@ -1018,10 +1024,4 @@ SwipeableViews.defaultProps = {
resistance: false,
};

SwipeableViews.childContextTypes = {
swipeableViews: PropTypes.shape({
slideUpdateHeight: PropTypes.func,
}),
};

export default SwipeableViews;
41 changes: 29 additions & 12 deletions packages/react-swipeable-views/src/SwipeableViews.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,15 @@ describe('SwipeableViews', () => {
</SwipeableViews>,
{ disableLifecycleMethods: true },
);

assert.include(wrapper.childAt(0).props().style, {
transition: 'all 0s ease 0s',
});
assert.include(
wrapper
.childAt(0)
.childAt(0)
.props().style,
{
transition: 'all 0s ease 0s',
},
);
});
});

Expand Down Expand Up @@ -605,10 +610,16 @@ describe('SwipeableViews', () => {
);

assert.strictEqual(wrapper.text(), 'slide n°2');
assert.shallowDeepEqual(wrapper.childAt(0).props().style, {
transition: 'all 0s ease 0s',
transform: undefined,
});
assert.shallowDeepEqual(
wrapper
.childAt(0)
.childAt(0)
.props().style,
{
transition: 'all 0s ease 0s',
transform: undefined,
},
);
});

it('should render all the children during the second render', () => {
Expand Down Expand Up @@ -653,10 +664,16 @@ describe('SwipeableViews', () => {
);

assert.strictEqual(wrapper.text(), 'slide n°1slide n°2slide n°3slide n°4slide n°5');
assert.shallowDeepEqual(wrapper.childAt(0).props().style, {
transition: 'all 0s ease 0s',
transform: 'translate(-100%, 0)',
});
assert.shallowDeepEqual(
wrapper
.childAt(0)
.childAt(0)
.props().style,
{
transition: 'all 0s ease 0s',
transform: 'translate(-100%, 0)',
},
);
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions packages/react-swipeable-views/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import SwipeableViews from './SwipeableViews';

export default SwipeableViews;
export { default, SwipeableViewsContext } from './SwipeableViews';

1 comment on commit 4c258a2

@vishwajeetv
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ykzts thanks a ton! This has been bugging me for a long, long time!
Kudos to you! :-)

Please sign in to comment.