Skip to content

Commit

Permalink
Fixed #591
Browse files Browse the repository at this point in the history
  • Loading branch information
Merve7 committed Sep 27, 2018
1 parent 8654d19 commit 1e405f6
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SplitButtonDemo } from './showcase/splitbutton/SplitButtonDemo';
import { CheckboxDemo } from './showcase/checkbox/CheckboxDemo';
import { ChipsDemo } from './showcase/chips/ChipsDemo';
import { DialogDemo } from './showcase/dialog/DialogDemo';
import { DeferedContentDemo } from './showcase/deferedcontent/DeferedContentDemo';
import { DropdownDemo } from './showcase/dropdown/DropdownDemo';
import { FieldsetDemo } from './showcase/fieldset/FieldsetDemo';
import { FileUploadDemo } from './showcase/fileupload/FileUploadDemo';
Expand Down Expand Up @@ -337,6 +338,7 @@ class AppMenu extends Component {
<Link to="/captcha">&#9679; Captcha</Link>
<Link to="/inplace">&#9679; Inplace</Link>
<Link to="/progressspinner">&#9679; ProgressSpinner</Link>
<Link to="/deferedcontent">&#9679; DeferedContent</Link>
</div>
</div>
</div>
Expand Down Expand Up @@ -559,6 +561,7 @@ class App extends Component {
<Route path="/keyfilter" component={KeyFilterDemo}/>
<Route path="/dataview" component={DataViewDemo}/>
<Route path="/inplace" component={InplaceDemo}/>
<Route path="/deferedcontent" component={DeferedContentDemo}/>

<div className="content-section layout-footer clearfix">
<span>Released under the MIT License, Copyright © 2018 <a href="http://www.primetek.com.tr" target="_blank" rel="noopener noreferrer">PrimeTek</a></span>
Expand Down
65 changes: 65 additions & 0 deletions src/components/deferedcontent/DeferedContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, {Component} from "react";
import PropTypes from "prop-types";

export class DeferedContent extends Component {

static defaultProps = {
onload: null
}

static propsTypes = {
onLoad: PropTypes.func
}

constructor() {
super();
this.state = {
loaded: false
};
}

componentDidMount() {
if(this.shouldLoad() && !this.state.loaded) {
this.load();
}

this.documentScrollListener = () => {
if(this.shouldLoad() && !this.state.loaded) {
this.load();
}
};
window.addEventListener('scroll', this.documentScrollListener);
}


shouldLoad() {
let rect = this.container.getBoundingClientRect();
let docElement = document.documentElement;
let scrollTop = (window.pageYOffset||document.documentElement.scrollTop);
let winHeight = docElement.clientHeight;

return (winHeight >= rect.top);
}

load(event) {
this.setState({loaded:true})
if (this.props.onLoad) {
this.props.onLoad(event);
}
}

componentWillUnmount() {
if(this.documentScrollListener) {
window.removeEventListener('scroll', this.documentScrollListener)
}
}

render() {
return (
<div ref={(el) => this.container = el}>
{this.state.loaded ? this.props.children : null}
</div>
);
}

}
232 changes: 232 additions & 0 deletions src/showcase/deferedcontent/DeferedContentDemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import React, {Component} from 'react';
import {DeferedContent} from '../../components/deferedcontent/DeferedContent';
import {TabView,TabPanel} from '../../components/tabview/TabView';
import {CodeHighlight} from '../codehighlight/CodeHighlight';
import {CarService} from "../service/CarService";
import {DataTable} from "../../components/datatable/DataTable";
import {Column} from "../../components/column/Column";
import {Growl} from "../../components/growl/Growl";

export class DeferedContentDemo extends Component {

constructor() {
super();
this.state = {
cars: []
};
this.carservice = new CarService();
this.initImage = this.initImage.bind(this);
this.initData = this.initData.bind(this);
}

componentDidMount() {
this.carservice.getCarsSmall().then(data => this.setState({cars: data}));
}

initImage() {
this.growl.show({severity: 'success', summary: 'Image Initialized', detail: 'Scroll down to see datatable'});
}

initData() {
this.growl.show({severity: 'success', summary: 'Data Initialized', detail: 'Render Completed'});
}

render() {
return (
<div>
<div className="content-section introduction">
<div className="feature-intro">
<h1>DeferedContent</h1>
<p>DeferedContent postpones the loading the content that is initially not in viewport until it becomes visible on scroll. Scroll down to load the DataTable which initiates a query that is not executed on initial page load to speed up load performance.</p>
</div>
</div>

<div className="content-section implementation">
<Growl ref={(el) => this.growl = el} />
<div style={{height:'800px'}}>
Image is not loaded, scroll down to initialize it.
</div>
<DeferedContent onLoad={this.initImage}>
<img src="showcase/resources/demo/images/galleria/galleria1.jpg" alt="prime"/>
</DeferedContent>


<div style={{height:'500px'}}>
</div>
<DeferedContent onLoad={this.initData}>
<DataTable value={this.state.cars}>
<Column field="vin" header="Vin" />
<Column field="year" header="Year" />
<Column field="brand" header="Brand" />
<Column field="color" header="Color" />
</DataTable>
</DeferedContent>
</div>

<DeferedContentDoc/>

</div>
)
}
}

export class DeferedContentDoc extends Component {

shouldComponentUpdate(){
return false;
}

render() {
return (
<div className="content-section documentation">
<TabView>
<TabPanel header="Documentation">
<h3>Import</h3>
<CodeHighlight className="language-javascript">
{`
import {DeferedContent} from 'primereact/deferedcontent';
`}
</CodeHighlight>

<h3>Getting Started</h3>
<p>Defer is applied to a container element where content needs to be placed inside an child component.</p>
<CodeHighlight className="language-jsx">
{`
<DeferedContent>
<DataTable value={this.state.cars}>
<Column field="vin" header="Vin" />
<Column field="year" header="Year" />
<Column field="brand" header="Brand" />
<Column field="color" header="Color" />
</DataTable>
</DeferedContent>
`}
</CodeHighlight>

<h3>Callback</h3>
<p>onLoad callback is useful to initialize the content when it becomes visible on scroll such as loading data.</p>
<CodeHighlight className="language-jsx">
{`
<DeferedContent onLoad={this.initData}>
<DataTable value={this.state.cars}>
<Column field="vin" header="Vin" />
<Column field="year" header="Year" />
<Column field="brand" header="Brand" />
<Column field="color" header="Color" />
</DataTable>
</DeferedContent>
`}
</CodeHighlight>

<h3>Properties</h3>
<div className="doc-tablewrapper">
Component has no attributes.
</div>

<h3>Events</h3>
<div className="doc-tablewrapper">
<table className="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Parameters</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>onLoad</td>
<td>event: Event object</td>
<td>Callback to invoke when deferred content is loaded.</td>
</tr>
</tbody>
</table>
</div>

<h3>Styling</h3>
<div className="doc-tablewrapper">
Componentdoes not apply any styling to host.
</div>

<h3>Dependencies</h3>
<p>None.</p>

</TabPanel>

<TabPanel header="Source">
<a href="https://github.com/primefaces/primereact/tree/master/src/showcase/deferedcontent" className="btn-viewsource" target="_blank" rel="noopener noreferrer">
<span>View on GitHub</span>
</a>
<CodeHighlight className="language-javascript">
{`
export class DeferedContentDemo extends Component {
constructor() {
super();
this.state = {
cars: []
};
this.carservice = new CarService();
this.initImage = this.initImage.bind(this);
this.initData = this.initData.bind(this);
}
componentDidMount() {
this.carservice.getCarsSmall().then(data => this.setState({cars: data}));
}
initImage() {
this.growl.show({severity: 'success', summary: 'Image Initialized', detail: 'Scroll down to see datatable'});
}
initData() {
this.growl.show({severity: 'success', summary: 'Data Initialized', detail: 'Render Completed'});
}
render() {
return (
<div>
<div className="content-section introduction">
<div className="feature-intro">
<h1>DeferedContent</h1>
<p></p>
</div>
</div>
<div className="content-section implementation">
<Growl ref={(el) => this.growl = el} />
<div style={{height:'800px'}}>
Image is not loaded, scroll down to initialize it.
</div>
<DeferedContent onLoad={this.initImage}>
<img src="showcase/resources/demo/images/galleria/galleria1.jpg" alt="prime"/>
</DeferedContent>
<div style={{height:'500px'}}>
</div>
<DeferedContent onLoad={this.initData}>
<DataTable value={this.state.cars}>
<Column field="vin" header="Vin" />
<Column field="year" header="Year" />
<Column field="brand" header="Brand" />
<Column field="color" header="Color" />
</DataTable>
</DeferedContent>
</div>
</div>
)
}
}
`}
</CodeHighlight>
</TabPanel>
</TabView>
</div>
);
}
}

0 comments on commit 1e405f6

Please sign in to comment.