-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
PaginationItem.js
81 lines (68 loc) · 1.83 KB
/
PaginationItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import keyboardKey from 'keyboard-key'
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import { createShorthandFactory } from '../../lib'
import MenuItem from '../../collections/Menu/MenuItem'
/**
* An item of a pagination.
*/
const PaginationItem = React.forwardRef(function (props, ref) {
const { active, type } = props
const disabled = props.disabled || type === 'ellipsisItem'
const handleClick = (e) => {
_.invoke(props, 'onClick', e, props)
}
const handleKeyDown = (e) => {
_.invoke(props, 'onKeyDown', e, props)
if (keyboardKey.getCode(e) === keyboardKey.Enter) {
_.invoke(props, 'onClick', e, props)
}
}
return MenuItem.create(props, {
defaultProps: {
active,
'aria-current': active,
'aria-disabled': disabled,
disabled,
tabIndex: disabled ? -1 : 0,
},
overrideProps: () => ({
onClick: handleClick,
onKeyDown: handleKeyDown,
ref,
}),
})
})
PaginationItem.displayName = 'PaginationItem'
PaginationItem.propTypes = {
/** A pagination item can be active. */
active: PropTypes.bool,
/** A pagination item can be disabled. */
disabled: PropTypes.bool,
/**
* Called on click.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onClick: PropTypes.func,
/**
* Called on key down.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onKeyDown: PropTypes.func,
/** A pagination should have a type. */
type: PropTypes.oneOf([
'ellipsisItem',
'firstItem',
'prevItem',
'pageItem',
'nextItem',
'lastItem',
]),
}
PaginationItem.create = createShorthandFactory(PaginationItem, (content) => ({ content }))
export default PaginationItem