Skip to content

Commit 162a0e7

Browse files
authored
fix: selectedKeys on Header menu should inject from pathname (#79)
1 parent c4ff4b9 commit 162a0e7

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

src/components/organisms/Header/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ type Props = {
2828
title: string
2929
onTitleClick: (event: React.MouseEvent<HTMLAnchorElement>) => void
3030
onMenuClick: ComponentProps<typeof Menu>['onClick']
31+
tabKey: string
3132
}
3233

3334
export const Header: React.FC<Props> = ({
3435
onTitleClick,
3536
onMenuClick,
3637
title,
38+
tabKey,
3739
}) => {
3840
return (
3941
<StyledHeader>
@@ -44,12 +46,12 @@ export const Header: React.FC<Props> = ({
4446
</HeaderTitle>
4547
<StyledMenu
4648
mode="horizontal"
47-
defaultSelectedKeys={['days']}
49+
selectedKeys={[tabKey]}
4850
onClick={onMenuClick}
4951
>
50-
<Menu.Item key="days">日々</Menu.Item>
51-
<Menu.Item key="works">写真</Menu.Item>
52-
<Menu.Item key="about">私について</Menu.Item>
52+
<Menu.Item key="/days">日々</Menu.Item>
53+
<Menu.Item key="/works">写真</Menu.Item>
54+
<Menu.Item key="/about">私について</Menu.Item>
5355
</StyledMenu>
5456
</StyledHeader>
5557
)

src/components/pages/About/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const About: React.FC<Props> = ({
5050
<Col span={16}>
5151
<ProfileTypography>
5252
{profileDescriptions.map(desc => (
53-
<Paragraph key="">{desc}</Paragraph>
53+
<Paragraph key={desc}>{desc}</Paragraph>
5454
))}
5555
</ProfileTypography>
5656
</Col>

src/components/templates/Layout/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ type Props = ComponentProps<typeof Header> & ComponentProps<typeof Footer>
2525
export const Layout: React.FC<Props> = ({
2626
children,
2727
title,
28+
tabKey,
2829
onTitleClick,
2930
onMenuClick,
3031
onTwitterClick,
3132
}) => {
3233
return (
3334
<AntLayout style={{ minHeight: '100vh' }}>
3435
<Header
36+
tabKey={tabKey}
3537
title={title}
3638
onTitleClick={onTitleClick}
3739
onMenuClick={onMenuClick}

src/containers/Layout/hooks/useLayout.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentProps, useCallback } from 'react'
2-
import { useHistory } from 'react-router'
2+
import { useHistory, useLocation } from 'react-router'
33

44
import { Layout } from 'components/templates/Layout'
55
import SiteConfig from 'data/site.json'
@@ -9,10 +9,11 @@ const TWITTER_URL = 'https://twitter.com/'
99

1010
export const useLayout = (): ComponentProps<typeof Layout> => {
1111
const history = useHistory()
12-
12+
const { pathname } = useLocation()
13+
const currentKey = pathname === '/' ? '/days' : pathname
1314
const onMenuClick = useCallback(
1415
({ key }) => {
15-
history.push(`/${key}`)
16+
history.push(key)
1617
},
1718
[history]
1819
)
@@ -28,6 +29,7 @@ export const useLayout = (): ComponentProps<typeof Layout> => {
2829
}, [])
2930

3031
return {
32+
tabKey: currentKey,
3133
title: SiteConfig.blogTitle,
3234
onMenuClick,
3335
onTitleClick,

src/containers/Layout/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ import { useLayout } from './hooks/useLayout'
55
import { Layout } from 'components/templates/Layout'
66

77
export const LayoutContainer: React.FC = ({ children }) => {
8-
const { title, onTitleClick, onMenuClick, onTwitterClick } = useLayout()
8+
const {
9+
title,
10+
onTitleClick,
11+
onMenuClick,
12+
onTwitterClick,
13+
tabKey,
14+
} = useLayout()
915
return (
1016
<Layout
1117
title={title}
18+
tabKey={tabKey}
1219
onTitleClick={onTitleClick}
1320
onMenuClick={onMenuClick}
1421
onTwitterClick={onTwitterClick}

0 commit comments

Comments
 (0)