/
AbstractNavItem.tsx
125 lines (106 loc) · 3.27 KB
/
AbstractNavItem.tsx
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { useContext } from 'react';
import useEventCallback from '@restart/hooks/useEventCallback';
import warning from 'warning';
import NavContext from './NavContext';
import SelectableContext, { makeEventKey } from './SelectableContext';
import { BsPrefixRefForwardingComponent } from './helpers';
import { EventKey } from './types';
// TODO: check this
interface AbstractNavItemProps {
active?: boolean;
as: React.ElementType;
className?: string;
disabled?: boolean;
eventKey?: EventKey;
href?: string;
role?: string;
id?: string;
tabIndex?: number;
onClick?: (e: any) => void;
onSelect?: (navKey: string, e: any) => void;
}
type AbstractNavItem = BsPrefixRefForwardingComponent<
'div',
AbstractNavItemProps
>;
const propTypes = {
id: PropTypes.string,
active: PropTypes.bool,
role: PropTypes.string,
href: PropTypes.string,
tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
eventKey: PropTypes.any,
onclick: PropTypes.func,
as: PropTypes.any,
onClick: PropTypes.func,
onSelect: PropTypes.func,
'aria-controls': PropTypes.string,
};
const defaultProps = {
disabled: false,
};
const AbstractNavItem: AbstractNavItem = React.forwardRef(
(
{
active,
className,
eventKey,
onSelect,
onClick,
as: Component,
...props
}: AbstractNavItemProps,
ref,
) => {
const navKey = makeEventKey(eventKey, props.href);
const parentOnSelect = useContext(SelectableContext);
const navContext = useContext(NavContext);
let isActive = active;
if (navContext) {
if (!props.role && navContext.role === 'tablist') props.role = 'tab';
const contextControllerId = navContext.getControllerId(navKey);
const contextControlledId = navContext.getControlledId(navKey);
warning(
!contextControllerId || !props.id,
`[react-bootstrap] The provided id '${props.id}' was overwritten by the current navContext with '${contextControllerId}'.`,
);
warning(
!contextControlledId || !props['aria-controls'],
`[react-bootstrap] The provided aria-controls value '${props['aria-controls']}' was overwritten by the current navContext with '${contextControlledId}'.`,
);
props['data-rb-event-key'] = navKey;
props.id = contextControllerId || props.id;
props['aria-controls'] = contextControlledId || props['aria-controls'];
isActive =
active == null && navKey != null
? navContext.activeKey === navKey
: active;
}
if (props.role === 'tab') {
if (props.disabled) {
props.tabIndex = -1;
props['aria-disabled'] = true;
}
props['aria-selected'] = isActive;
}
const handleOnclick = useEventCallback((e) => {
if (onClick) onClick(e);
if (navKey == null) return;
if (onSelect) onSelect(navKey, e);
if (parentOnSelect) parentOnSelect(navKey, e);
});
return (
<Component
{...props}
ref={ref}
onClick={handleOnclick}
className={classNames(className, isActive && 'active')}
/>
);
},
);
AbstractNavItem.propTypes = propTypes;
AbstractNavItem.defaultProps = defaultProps;
export default AbstractNavItem;