Commit 59755fa9 authored by ChrisH's avatar ChrisH Committed by GitHub

feat: Added children type declaration for React 18 (#36)

* Update PullToRefresh.tsx

- React 18 requires children props type to be explicitly declared

* updated react version
parent d4f43f1d
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -32,8 +32,8 @@ ...@@ -32,8 +32,8 @@
"awesome-typescript-loader": "^5.2.1", "awesome-typescript-loader": "^5.2.1",
"jest": "^23.6.0", "jest": "^23.6.0",
"path": "^0.12.7", "path": "^0.12.7",
"react": "^16.5.2", "react": "^18.0.0",
"react-dom": "^16.5.2", "react-dom": "^18.0.0",
"react-hot-loader": "^4.3.11", "react-hot-loader": "^4.3.11",
"react-test-renderer": "^16.5.2", "react-test-renderer": "^16.5.2",
"rimraf": "^2.6.2", "rimraf": "^2.6.2",
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
"webpack-dev-server": "^3.1.9" "webpack-dev-server": "^3.1.9"
}, },
"peerDependencies": { "peerDependencies": {
"react": "^16.5.2", "react": "^18.0.0",
"react-dom": "^16.5.2" "react-dom": "^18.0.0"
} }
} }
...@@ -11,6 +11,7 @@ export interface PullToRefreshProps { ...@@ -11,6 +11,7 @@ export interface PullToRefreshProps {
backgroundColor?: string; backgroundColor?: string;
containerStyle?: React.CSSProperties; containerStyle?: React.CSSProperties;
startInvisible?: boolean; startInvisible?: boolean;
children?: React.ReactNode;
} }
export interface PullToRefreshState { export interface PullToRefreshState {
...@@ -19,7 +20,10 @@ export interface PullToRefreshState { ...@@ -19,7 +20,10 @@ export interface PullToRefreshState {
onRefreshing: boolean; onRefreshing: boolean;
} }
export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRefreshState> { export class PullToRefresh extends React.Component<
PullToRefreshProps,
PullToRefreshState
> {
private container: any; private container: any;
private containerRef(container) { private containerRef(container) {
...@@ -30,9 +34,13 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -30,9 +34,13 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
private pullDownRef(pullDown) { private pullDownRef(pullDown) {
this.pullDown = pullDown; this.pullDown = pullDown;
const maxPullDownDistance = this.pullDown && this.pullDown.firstChild && this.pullDown.firstChild["getBoundingClientRect"] const maxPullDownDistance =
? this.pullDown.firstChild["getBoundingClientRect"]().height : 0; this.pullDown &&
this.setState({maxPullDownDistance}); this.pullDown.firstChild &&
this.pullDown.firstChild["getBoundingClientRect"]
? this.pullDown.firstChild["getBoundingClientRect"]().height
: 0;
this.setState({ maxPullDownDistance });
} }
private dragging = false; private dragging = false;
...@@ -81,7 +89,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -81,7 +89,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
} }
private onTouchStart(e) { private onTouchStart(e) {
const {triggerHeight = 40} = this.props; const { triggerHeight = 40 } = this.props;
this.startY = e["pageY"] || e.touches[0].pageY; this.startY = e["pageY"] || e.touches[0].pageY;
this.currentY = this.startY; this.currentY = this.startY;
...@@ -103,7 +111,10 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -103,7 +111,10 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
return; return;
} }
} else { } else {
const top = this.container.getBoundingClientRect().top || this.container.getBoundingClientRect().y || 0; const top =
this.container.getBoundingClientRect().top ||
this.container.getBoundingClientRect().y ||
0;
if (this.startY - top > triggerHeight) { if (this.startY - top > triggerHeight) {
return; return;
} }
...@@ -128,7 +139,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -128,7 +139,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
e.preventDefault(); e.preventDefault();
} }
if ((this.currentY - this.startY) >= this.props.pullDownThreshold) { if (this.currentY - this.startY >= this.props.pullDownThreshold) {
this.setState({ this.setState({
pullToRefreshThresholdBreached: true, pullToRefreshThresholdBreached: true,
}); });
...@@ -139,7 +150,9 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -139,7 +150,9 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
} }
this.container.style.overflow = "visible"; this.container.style.overflow = "visible";
this.container.style.transform = `translate(0px, ${this.currentY - this.startY}px)`; this.container.style.transform = `translate(0px, ${
this.currentY - this.startY
}px)`;
this.pullDown.style.visibility = "visible"; this.pullDown.style.visibility = "visible";
} }
...@@ -149,16 +162,20 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -149,16 +162,20 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
this.currentY = 0; this.currentY = 0;
if (!this.state.pullToRefreshThresholdBreached) { if (!this.state.pullToRefreshThresholdBreached) {
this.pullDown.style.visibility = this.props.startInvisible ? "hidden" : "visible"; this.pullDown.style.visibility = this.props.startInvisible
? "hidden"
: "visible";
this.initContainer(); this.initContainer();
return; return;
} }
this.container.style.overflow = "visible"; this.container.style.overflow = "visible";
this.container.style.transform = `translate(0px, ${this.props.pullDownThreshold}px)`; this.container.style.transform = `translate(0px, ${this.props.pullDownThreshold}px)`;
this.setState({ this.setState(
{
onRefreshing: true, onRefreshing: true,
}, () => { },
() => {
this.props.onRefresh().then(() => { this.props.onRefresh().then(() => {
this.initContainer(); this.initContainer();
setTimeout(() => { setTimeout(() => {
...@@ -168,7 +185,8 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -168,7 +185,8 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
}); });
}, 200); }, 200);
}); });
}); },
);
} }
private initContainer() { private initContainer() {
...@@ -181,9 +199,14 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -181,9 +199,14 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
} }
private renderPullDownContent() { private renderPullDownContent() {
const {releaseContent, pullDownContent, refreshContent, startInvisible} = this.props; const { releaseContent, pullDownContent, refreshContent, startInvisible } =
const {onRefreshing, pullToRefreshThresholdBreached} = this.state; this.props;
const content = onRefreshing ? refreshContent : pullToRefreshThresholdBreached ? releaseContent : pullDownContent; const { onRefreshing, pullToRefreshThresholdBreached } = this.state;
const content = onRefreshing
? refreshContent
: pullToRefreshThresholdBreached
? releaseContent
: pullDownContent;
const contentStyle: React.CSSProperties = { const contentStyle: React.CSSProperties = {
position: "absolute", position: "absolute",
overflow: "hidden", overflow: "hidden",
...@@ -200,7 +223,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -200,7 +223,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
} }
public render() { public render() {
const {backgroundColor} = this.props; const { backgroundColor } = this.props;
const containerStyle: React.CSSProperties = { const containerStyle: React.CSSProperties = {
height: "auto", height: "auto",
overflow: "hidden", overflow: "hidden",
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment