|
6 | 6 |
|
7 | 7 | import * as React from 'react'; |
8 | 8 | import classNames from 'classnames'; |
9 | | -import { Route } from 'react-router-dom'; |
10 | | -import type { Match, Location } from 'react-router-dom'; |
| 9 | +import { RouterContext } from '../routing/RouterContext'; |
| 10 | +import { matchPath } from '../routing/utils'; |
11 | 11 | import PlainButton from '../../../components/plain-button'; |
12 | 12 | import { isLeftClick } from '../../../utils/dom'; |
13 | 13 |
|
14 | | -type Props = { |
15 | | - activeClassName?: string, |
16 | | - children: React.Node, |
17 | | - className?: string, |
18 | | - component?: React.ComponentType<any>, |
19 | | - exact?: boolean, |
20 | | - isActive?: (match: Match, location: Location) => ?boolean, |
21 | | - isDisabled?: boolean, |
22 | | - onClick?: (event: SyntheticEvent<>) => void, |
23 | | - replace?: boolean, |
24 | | - strict?: boolean, |
25 | | - to: string | Location, |
26 | | -}; |
| 14 | +const NavButton = React.forwardRef( |
| 15 | + ( |
| 16 | + props: { |
| 17 | + activeClassName?: string, |
| 18 | + children: React.Node, |
| 19 | + className?: string, |
| 20 | + component?: React.ComponentType<any>, |
| 21 | + exact?: boolean, |
| 22 | + isActive?: (match: Object, location: Object) => ?boolean, |
| 23 | + isDisabled?: boolean, |
| 24 | + onClick?: (event: SyntheticEvent<>) => void, |
| 25 | + replace?: boolean, |
| 26 | + strict?: boolean, |
| 27 | + to: string | Object, |
| 28 | + }, |
| 29 | + ref, |
| 30 | + ) => { |
| 31 | + const { |
| 32 | + activeClassName = 'bdl-is-active', |
| 33 | + children, |
| 34 | + className = 'bdl-NavButton', |
| 35 | + component: Component = PlainButton, |
| 36 | + exact, |
| 37 | + isActive, |
| 38 | + isDisabled, |
| 39 | + onClick, |
| 40 | + replace, |
| 41 | + strict, |
| 42 | + to, |
| 43 | + ...rest |
| 44 | + } = props; |
27 | 45 |
|
28 | | -const NavButton = React.forwardRef<Props, React.Ref<any>>((props: Props, ref: React.Ref<any>) => { |
29 | | - const { |
30 | | - activeClassName = 'bdl-is-active', |
31 | | - children, |
32 | | - className = 'bdl-NavButton', |
33 | | - component: Component = PlainButton, |
34 | | - exact, |
35 | | - isActive, |
36 | | - isDisabled, |
37 | | - onClick, |
38 | | - replace, |
39 | | - strict, |
40 | | - to, |
41 | | - ...rest |
42 | | - } = props; |
43 | | - const path = typeof to === 'object' ? to.pathname : to; |
| 46 | + const path = typeof to === 'object' ? to.pathname : to; |
| 47 | + const disabledClassName = 'bdl-is-disabled'; |
44 | 48 |
|
45 | | - const disabledClassName = 'bdl-is-disabled'; |
| 49 | + return ( |
| 50 | + <RouterContext.Consumer> |
| 51 | + {({ history, location }) => { |
| 52 | + const match = matchPath(location.pathname, { path, exact, strict }); |
| 53 | + const isActiveValue = !!(isActive ? isActive(match, location) : match); |
| 54 | + const handleClick = event => { |
| 55 | + if (onClick) { |
| 56 | + onClick(event); |
| 57 | + } |
46 | 58 |
|
47 | | - return ( |
48 | | - <Route exact={exact} path={path} strict={strict}> |
49 | | - {({ history, location, match }) => { |
50 | | - const isActiveValue = !!(isActive ? isActive(match, location) : match); |
| 59 | + if (!event.defaultPrevented && isLeftClick(event)) { |
| 60 | + const method = replace ? history.replace : history.push; |
| 61 | + method(to); |
| 62 | + } |
| 63 | + }; |
51 | 64 |
|
52 | | - return ( |
53 | | - <Component |
54 | | - className={classNames(className, { |
55 | | - [activeClassName]: isActiveValue, |
56 | | - [disabledClassName]: isDisabled, |
57 | | - })} |
58 | | - isDisabled={isDisabled} |
59 | | - onClick={event => { |
60 | | - if (onClick) { |
61 | | - onClick(event); |
62 | | - } |
63 | | - |
64 | | - if (!event.defaultPrevented && isLeftClick(event)) { |
65 | | - const method = replace ? history.replace : history.push; |
66 | | - method(to); |
67 | | - } |
68 | | - }} |
69 | | - ref={ref} |
70 | | - {...rest} |
71 | | - > |
72 | | - {children} |
73 | | - </Component> |
74 | | - ); |
75 | | - }} |
76 | | - </Route> |
77 | | - ); |
78 | | -}); |
| 65 | + return ( |
| 66 | + <Component |
| 67 | + className={classNames(className, { |
| 68 | + [activeClassName]: isActiveValue, |
| 69 | + [disabledClassName]: isDisabled, |
| 70 | + })} |
| 71 | + isDisabled={isDisabled} |
| 72 | + onClick={handleClick} |
| 73 | + ref={ref} |
| 74 | + {...rest} |
| 75 | + > |
| 76 | + {children} |
| 77 | + </Component> |
| 78 | + ); |
| 79 | + }} |
| 80 | + </RouterContext.Consumer> |
| 81 | + ); |
| 82 | + }, |
| 83 | +); |
79 | 84 |
|
80 | 85 | export default NavButton; |
0 commit comments