import React, { Component, PropTypes } from 'react'
import range from 'lodash/range'

export default class Paginator extends Component {
  static propTypes = {
    initialPage: PropTypes.number,
    numberOfPages: PropTypes.number.isRequired,
    children: PropTypes.func.isRequired
  }

  static defaultProps = { initialPage: 1 }

  state = { page: this.props.initialPage }

  getPageRange = (offset = 3) => {
    if (typeof offset !== 'number') {
      throw new Error(`offset should be a number`)
    }
    if (offset <= 0) {
      throw new Error(`offset should be equal or greater than 1`)
    }
    const start = this.state.page - offset
    const end = this.state.page + offset
    return range(
      start > 1 ? start : 1,
      (end < this.props.numberOfPages ? end : this.props.numberOfPages) + 1
    )
  }

  setPage = page => this.setState({ page })

  incrementPage = () => {
    if (this.state.page >= this.props.numberOfPages) {
      return
    }
    this.setPage(this.state.page + 1)
  }

  decrementPage = () => {
    if (this.state.page <= 1) {
      return
    }
    this.setPage(this.state.page - 1)
  }

  render() {
    return this.props.children({
      ...this.state,
      getPageRange: this.getPageRange,
      setPage: this.setPage,
      incrementPage: this.incrementPage,
      decrementPage: this.decrementPage
    })
  }
}

@@ -243,16 +243,51 @@
 uired,%0D%0A
+    onPageChange: PropTypes.func,%0D%0A
     chil
@@ -424,159 +424,415 @@
 %0D%0A  
-getPageRange = (offset = 3) =%3E %7B%0D%0A    if (typeof offset !== 'number') %7B%0D%0A      throw new Error(%60offset should be a number%60)%0D%0A    %7D%0D%0A    if (offset %3C= 0
+componentWillMount() %7B%0D%0A    if (this.props.initialPage %3C 1) %7B%0D%0A      throw new Error('initialPage should be greater than or equal to 1')%0D%0A    %7D%0D%0A    if (this.props.initialPage %3E this.props.numberOfPages) %7B%0D%0A      throw new Error(%0D%0A        %60initialPage should be less than or equal to $%7Bthis.props.numberOfPages%7D%60%0D%0A      )%0D%0A    %7D%0D%0A  %7D%0D%0A%0D%0A  getPageRange = (offset = 3) =%3E %7B%0D%0A    if (typeof offset !== 'number'
 ) %7B%0D
@@ -854,17 +854,17 @@
 w Error(
-%60
+'
 offset s
@@ -876,32 +876,17 @@
  be 
-equal or greater than 1%60
+a number'
 )%0D%0A 
@@ -1141,31 +1141,495 @@
  =%3E 
-this.setState(%7B page %7D)
+%7B%0D%0A    if (typeof page !== 'number') %7B%0D%0A      throw new Error('page should be a number')%0D%0A    %7D%0D%0A    if (page %3C 1) %7B%0D%0A      throw new Error('page should be greater than or equal to 1')%0D%0A    %7D%0D%0A    if (page %3E this.props.numberOfPages) %7B%0D%0A      throw new Error(%0D%0A        %60page should be less than or equal to $%7Bthis.props.numberOfPages%7D%60%0D%0A      )%0D%0A    %7D%0D%0A    this.setState(%7B page %7D, () =%3E %7B%0D%0A      if (this.props.onPageChange) %7B%0D%0A        this.props.onPageChange(page)%0D%0A      %7D%0D%0A    %7D)%0D%0A  %7D
 %0D%0A%0D%0A
@@ -1679,17 +1679,18 @@
 te.page 
-%3E
+==
 = this.p
@@ -1830,17 +1830,18 @@
 te.page 
-%3C
+==
 = 1) %7B%0D%0A

add onPageChange and improve type checks