Skip to content

Commit

Permalink
Refactor #2310
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Sep 27, 2021
1 parent 79f554d commit a7fe140
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
29 changes: 24 additions & 5 deletions src/components/paginator/JumpToPageInput.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { InputNumber } from '../inputnumber/InputNumber';
import { ObjectUtils } from '../utils/Utils';

export class JumpToPageInput extends Component {

static defaultProps = {
page: null,
rows: null,
pageCount: null,
disabled: false,
template: null,
onChange: null
}

static propTypes = {
page: PropTypes.number,
rows: PropTypes.number,
pageCount: PropTypes.number,
disabled: PropTypes.bool,
template: PropTypes.any,
onChange: PropTypes.func
}

constructor(props) {
super(props)

this.inputChange = this.inputChange.bind(this);
this.onChange = this.onChange.bind(this);
}

inputChange(event) {
onChange(event) {
if (this.props.onChange) {
this.props.onChange(this.props.rows * (event.value - 1), this.props.rows);
}
}

render() {
return (
<InputNumber value={this.props.page} onChange={this.inputChange} className="p-paginator-page-input" disabled={this.props.disabled} />
)
const value = this.props.pageCount > 0 ? this.props.page + 1 : 0;
const element = <InputNumber value={value} onChange={this.onChange} className="p-paginator-page-input" disabled={this.props.disabled} />;

if (this.props.template) {
const defaultOptions = {
value,
onChange: this.onChange,
disabled: this.props.disabled,
className: 'p-paginator-page-input',
element,
props: this.props
};

return ObjectUtils.getJSXElement(this.props.template, defaultOptions);
}

return element;
}

}
8 changes: 5 additions & 3 deletions src/components/paginator/Paginator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ interface PaginatorCurrentPageReportOptions {
}

interface PaginatorJumpToPageInputOptions {
page: number;
rows: number
disabled: boolean;
value: number;
onChange(first:number, rows: number): void;
disabled: boolean;
className: string;
element: JSX.Element;
props: PaginatorProps;
}

interface PaginatorTemplateOptions {
Expand Down
6 changes: 1 addition & 5 deletions src/components/paginator/Paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ export class Paginator extends Component {
return Math.floor(this.props.first / this.props.rows);
}

currentPage() {
return this.getPageCount() > 0 ? this.getPage() + 1 : 0;
}

empty() {
let pageCount = this.getPageCount()
return pageCount === 0;
Expand Down Expand Up @@ -204,7 +200,7 @@ export class Paginator extends Component {
rows={this.props.rows} totalRecords={this.props.totalRecords} template={template} />;
break;
case 'JumpToPageInput':
element = <JumpToPageInput key={key} rows={this.props.rows} page={this.currentPage()} onChange={this.changePage} disabled={this.empty()} />;
element = <JumpToPageInput key={key} rows={this.props.rows} page={this.getPage()} pageCount={this.getPageCount()} onChange={this.changePage} disabled={this.empty()} template={template} />;
break;

default:
Expand Down

0 comments on commit a7fe140

Please sign in to comment.