Pagination 调整, 去掉 pageNum changePageNum 属性

This commit is contained in:
liaoxuezhi 2019-05-13 13:08:24 +08:00
parent 0645874cac
commit c28aad6bc2
2 changed files with 30 additions and 34 deletions

View File

@ -1015,9 +1015,7 @@ export default class CRUD extends React.Component<CRUDProps, any> {
items: lastPage, items: lastPage,
hasNext: store.hasNext, hasNext: store.hasNext,
mode: store.mode, mode: store.mode,
onPageChange: this.handleChangePage, onPageChange: this.handleChangePage
pageNum: store.pageNum,
changePageNum: store.changePageNum,
} }
)} )}
</div> </div>

View File

@ -1,31 +1,23 @@
import * as React from 'react'; import * as React from 'react';
import {Renderer, RendererProps} from '../factory'; import {Renderer, RendererProps} from '../factory';
import { autobind } from '../utils/helper';
export interface PaginationProps extends RendererProps { export interface PaginationProps extends RendererProps {
activePage?: number;
items?: number;
maxButtons?: number;
hasNext?: boolean;
mode?: string;
onPageChange: (page: number, perPage?: number) => void;
pageNum?: number;
changePageNum: (value: number) => void;
showPageInput: boolean;
}
export interface DefaultProps {
activePage: number; activePage: number;
items: number; items: number;
maxButtons: number; maxButtons: number;
mode: string;
hasNext: boolean; hasNext: boolean;
mode: string;
onPageChange: (page: number, perPage?: number) => void;
showPageInput: boolean; showPageInput: boolean;
} }
type PropsWithDefault = PaginationProps & DefaultProps; export interface PaginationState {
pageNum: string;
};
export default class Pagination extends React.PureComponent<PaginationProps, any> { export default class Pagination extends React.Component<PaginationProps, PaginationState> {
static defaultProps: DefaultProps = { static defaultProps = {
activePage: 1, activePage: 1,
items: 1, items: 1,
maxButtons: 5, maxButtons: 5,
@ -34,13 +26,20 @@ export default class Pagination extends React.PureComponent<PaginationProps, any
showPageInput: true, showPageInput: true,
}; };
constructor(props: PaginationProps) { state = {
super(props); pageNum: String(this.props.activePage) || ''
this.handlePageChange = this.handlePageChange.bind(this); };
componentWillReceiveProps(nextProps:PaginationProps) {
if (this.props.activePage !== nextProps.activePage) {
this.setState({
pageNum: String(nextProps.activePage) || ''
})
}
} }
renderSimple() { renderSimple() {
const {activePage, hasNext, onPageChange, classnames: cx} = this.props as PropsWithDefault; const {activePage, hasNext, onPageChange, classnames: cx} = this.props;
return ( return (
<ul className={cx('Pagination', 'Pagination--sm')}> <ul className={cx('Pagination', 'Pagination--sm')}>
@ -68,22 +67,21 @@ export default class Pagination extends React.PureComponent<PaginationProps, any
); );
} }
@autobind
handlePageChange(e: React.ChangeEvent<any>) { handlePageChange(e: React.ChangeEvent<any>) {
const {changePageNum, items} = this.props; const {items} = this.props;
let value = e.currentTarget.value; let value = e.currentTarget.value;
if (((typeof value === 'number' || /^\d+$/.test(value)) && value > 0) || value === '') { if (/^\d+$/.test(value) && parseInt(value, 10) > items) {
if (value !== '') { value = String(items);
value = parseInt(value, 10);
value = (value > (items as number) ? items : value) as number;
}
changePageNum(value);
} }
this.setState({pageNum: value});
} }
renderNormal() { renderNormal() {
let {activePage, items, maxButtons, onPageChange, pageNum, classnames: cx, showPageInput} = this let {activePage, items, maxButtons, onPageChange, classnames: cx, showPageInput} = this.props;
.props as PropsWithDefault; const pageNum = this.state.pageNum;
let pageButtons: any = []; let pageButtons: any = [];
let startPage: number; let startPage: number;
@ -195,7 +193,7 @@ export default class Pagination extends React.PureComponent<PaginationProps, any
return ( return (
<div> <div>
<ul className={cx('Pagination', 'Pagination--sm')} onSelect={(value: number) => onPageChange(value)}> <ul className={cx('Pagination', 'Pagination--sm')}>
{pageButtons} {pageButtons}
</ul> </ul>
@ -214,7 +212,7 @@ export default class Pagination extends React.PureComponent<PaginationProps, any
/> />
<span> <span>
<button <button
onClick={() => onPageChange(pageNum as number)} onClick={() => onPageChange(parseInt(pageNum, 10))}
type="submit" type="submit"
className={cx('Button', 'Button--default')} className={cx('Button', 'Button--default')}
> >