Ensure NaN doesn't sneak in there.

This commit is contained in:
Timmy Crawford 2018-07-31 09:38:24 -07:00
parent d373599f8a
commit f93421c750
1 changed files with 12 additions and 12 deletions

View File

@ -8,30 +8,22 @@ import { Component } from '@wordpress/element';
import { IconButton, SelectControl } from '@wordpress/components';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { noop, uniqueId } from 'lodash';
import { isFinite, noop, uniqueId } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
function keyListener( methodToCall, event ) {
switch ( event.key ) {
case 'Enter':
this[ methodToCall ]( event );
break;
}
}
class Pagination extends Component {
constructor( props ) {
super( props );
this.previousPage = this.previousPage.bind( this );
this.nextPage = this.nextPage.bind( this );
this.selectPageListener = keyListener.bind( this, 'selectPage' );
this.onPageValueChange = this.onPageValueChange.bind( this );
this.perPageChange = this.perPageChange.bind( this );
this.selectInputValue = this.selectInputValue.bind( this );
}
previousPage( event ) {
@ -63,7 +55,15 @@ class Pagination extends Component {
onPageValueChange( event ) {
const { onPageChange } = this.props;
onPageChange( parseInt( event.target.value ) );
const newPage = parseInt( event.target.value, 10 );
if ( isFinite( newPage ) ) {
onPageChange( newPage );
}
}
selectInputValue( event ) {
event.target.select();
}
renderPageArrows() {
@ -129,8 +129,8 @@ class Pagination extends Component {
className={ inputClass }
aria-invalid={ isError }
type="number"
onClick={ this.selectInputValue }
onChange={ this.onPageValueChange }
onKeyDown={ this.selectPageListener }
value={ page }
min={ 1 }
max={ this.pageCount }