-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from ctxhou/async
Support async panel
- Loading branch information
Showing
8 changed files
with
436 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, {Component} from 'react'; | ||
import {Tabs, TabList, Tab, PanelList, AsyncPanel, Panel} from '../../src'; | ||
import {Facebook} from 'react-content-loader' | ||
const MyFacebookLoader = () => <Facebook /> | ||
|
||
function fakeCbFetch(cb) { | ||
setTimeout(() => { | ||
cb(null, { | ||
content: 'hi' | ||
}); | ||
}, 1500); | ||
} | ||
|
||
function fakePromiseFetch() { | ||
return new Promise((res) => { | ||
setTimeout(() => { | ||
res({ | ||
content: 'promise fetched' | ||
}); | ||
}, 1500); | ||
}) | ||
} | ||
|
||
export default class Basic extends Component { | ||
render() { | ||
return ( | ||
<Tabs customStyle={this.props.customStyle}> | ||
<TabList> | ||
<Tab>Normal panel</Tab> | ||
<Tab>Callback fetch panel</Tab> | ||
<Tab>Promise fetch panel</Tab> | ||
<Tab>fetch without cache</Tab> | ||
</TabList> | ||
<PanelList> | ||
<Panel> | ||
Normal tab. | ||
You can mix normal panel with async panel | ||
</Panel> | ||
<AsyncPanel loadContent={fakeCbFetch} | ||
render={data => (<div>{JSON.stringify(data)}</div>)} | ||
renderLoading={() => (<MyFacebookLoader/>)} | ||
/> | ||
<AsyncPanel loadContent={fakePromiseFetch} | ||
render={data => (<div>{JSON.stringify(data)}</div>)} | ||
renderLoading={() => (<MyFacebookLoader />)} | ||
/> | ||
<AsyncPanel loadContent={fakePromiseFetch} | ||
render={data => (<div>{JSON.stringify(data)}</div>)} | ||
renderLoading={() => (<MyFacebookLoader />)} | ||
cache={false} | ||
/> | ||
</PanelList> | ||
</Tabs> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import Panel from './Panel'; | ||
|
||
type Props = { | ||
loadContent: (cb: Function) => void, | ||
render: (data: any) => void, | ||
renderLoading: () => void, | ||
CustomPanelStyle: () => void, | ||
active: boolean, | ||
index: number, | ||
cache: boolean | ||
}; | ||
|
||
type State = { | ||
isLoading: boolean, | ||
data: any | ||
}; | ||
|
||
export default class AsyncPanelComponent extends React.PureComponent<Props, State> { | ||
static defaultProps = { | ||
cache: true | ||
}; | ||
|
||
cacheData: any; | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
(this: any).loadPanel = this.loadPanel.bind(this); | ||
(this: any).cacheData = undefined; | ||
this.state = { | ||
isLoading: false, | ||
data: undefined | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
if (this.props.active) | ||
this.loadPanel(); | ||
} | ||
|
||
componentWillReceiveProps(nextProps: Props) { | ||
if (nextProps.active) | ||
this.loadPanel(); | ||
} | ||
|
||
loadPanel() { | ||
const {loadContent, cache} = this.props; | ||
if (cache && this.cacheData) { | ||
this.setState({ | ||
isLoading: false, | ||
data: this.cacheData | ||
}); | ||
return; | ||
} | ||
const callback = (err, data) => { | ||
if (err) { | ||
console.log('React-Tabtab async panel error:', err); | ||
} | ||
if (cache) { | ||
this.cacheData = data; | ||
} | ||
this.setState({ | ||
isLoading: false, | ||
data | ||
}); | ||
} | ||
const promise = loadContent(callback); | ||
if (promise) { | ||
promise.then( | ||
(data) => callback(null, data), | ||
(err) => callback(err) | ||
); | ||
} | ||
if (!this.state.isLoading) { | ||
this.setState({isLoading: true}); | ||
} | ||
} | ||
|
||
render() { | ||
const {render, renderLoading, CustomPanelStyle, active, index} = this.props; | ||
const {isLoading, data} = this.state; | ||
let content; | ||
if (isLoading) { | ||
content = renderLoading(); | ||
} else { | ||
content = render(data); | ||
} | ||
return ( | ||
<Panel {...{CustomPanelStyle, active, index}}> | ||
{content} | ||
</Panel> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.