Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Pagination): allow to override props in PaginationItem #2537

Merged
merged 3 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/addons/Pagination/PaginationItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface PaginationItemProps {
/** A pagination item can have an aria label. */
ariaLabel?: string;

/** A pagination item can be disabled. */
disabled?: boolean;

/**
* Called on click.
*
Expand Down
34 changes: 22 additions & 12 deletions src/addons/Pagination/PaginationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class PaginationItem extends Component {
/** A pagination item can have an aria label. */
ariaLabel: PropTypes.string,

/** A pagination item can be disabled. */
disabled: PropTypes.bool,

/**
* Called on click.
*
Expand Down Expand Up @@ -64,19 +67,26 @@ class PaginationItem extends Component {
if (keyboardKey.getCode(e) === keyboardKey.Enter) _.invoke(this.props, 'onClick', e, this.props)
}

handleOverrides = () => ({
onClick: this.handleClick,
onKeyDown: this.handleKeyDown,
})

render() {
const { active, ariaLabel, type, ...rest } = this.props
const disabled = type === 'ellipsisItem'

return MenuItem.create({
...rest,
active,
'aria-current': active,
'aria-label': ariaLabel,
disabled,
onClick: this.handleClick,
onKeyDown: this.handleKeyDown,
tabIndex: disabled ? -1 : 0,
const { active, ariaLabel, type } = this.props
const disabled = this.props.disabled || type === 'ellipsisItem'

return MenuItem.create(this.props, {
defaultProps: {
active,
disabled,
'aria-current': active,
'aria-label': ariaLabel,
onClick: this.handleClick,
onKeyDown: this.handleKeyDown,
tabIndex: disabled ? -1 : 0,
},
overrideProps: this.handleOverrides,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levithomason I've pushed some changes and added some new tests, ready for your review 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're awesome ❤️

})
}
}
Expand Down
42 changes: 38 additions & 4 deletions test/specs/addons/Pagination/PaginationItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,45 @@ describe('PaginationItem', () => {
common.isConformant(PaginationItem)
common.implementsCreateMethod(PaginationItem)

describe('active', () => {
it('is "undefined" by default', () => {
shallow(<PaginationItem />)
.should.have.not.prop('active')
})

it('can pass its value', () => {
shallow(<PaginationItem active />)
.should.have.prop('active', true)
})
})

describe('aria-current', () => {
it('matches the values of "active" prop by default', () => {
shallow(<PaginationItem active />)
.should.have.prop('aria-current', true)
})

it('can be overridden', () => {
shallow(<PaginationItem active aria-current={false} />)
.should.have.prop('aria-current', false)
})
})

describe('disabled', () => {
it('is false by default', () => {
it('is "false" by default', () => {
shallow(<PaginationItem />)
.should.have.prop('disabled', false)
})

it('is true when "type" is "ellipsisItem"', () => {
it('is "true" when "type" is "ellipsisItem"', () => {
shallow(<PaginationItem type='ellipsisItem' />)
.should.have.prop('disabled', true)
})

it('can be overridden', () => {
shallow(<PaginationItem disabled />)
.should.have.prop('disabled', true)
})
})

describe('onClick', () => {
Expand Down Expand Up @@ -68,14 +97,19 @@ describe('PaginationItem', () => {
})

describe('tabIndex', () => {
it('is 0 by default', () => {
it('is "0" by default', () => {
shallow(<PaginationItem />)
.should.have.prop('tabIndex', 0)
})

it('is -1 when "type" is "ellipsisItem"', () => {
it('is "-1" when "type" is "ellipsisItem"', () => {
shallow(<PaginationItem type='ellipsisItem' />)
.should.have.prop('tabIndex', -1)
})

it('can be overridden', () => {
shallow(<PaginationItem tabIndex={5} />)
.should.have.prop('tabIndex', 5)
})
})
})