Skip to content

Commit 3cf4faf

Browse files
committed
优化组件结构、优化下拉菜单组件re render的问题
1 parent 75ab6ff commit 3cf4faf

File tree

7 files changed

+53
-68
lines changed

7 files changed

+53
-68
lines changed

src/assets/index.scss

+4
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ body,
9898
background: transparent;
9999
color: #000;
100100
}
101+
102+
.ant-input-textarea-show-count::after {
103+
color: #e5e5e5;
104+
}

src/components/my-header/index.tsx

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
import React, { ChangeEvent, memo, PropsWithChildren, useCallback, useState } from 'react'
1+
import React, { ChangeEvent, memo, PropsWithChildren, useState } from 'react'
22

33
import MySvg from '../svg/index'
44
import styles from './style.module.scss'
5-
import { Input, message, Dropdown } from 'antd'
6-
import {SettingOutlined} from '@ant-design/icons'
7-
import {settingMenu} from 'src/mock'
8-
import MyMenu from 'src/components/my-menu'
9-
import { SettingMenuItem} from 'src/utils'
5+
import { Input, message, Dropdown, Menu } from 'antd'
6+
import { SettingOutlined, CheckOutlined } from '@ant-design/icons'
7+
import { settingMenu } from 'src/mock'
8+
import { SettingMenuItem } from 'src/utils'
109

1110
interface MyHeaderProps {
1211
onInputValue?: (val: string) => void
1312
onMenuChange?: (val: any) => void
1413
}
1514

16-
export default memo(function index({ onInputValue, onMenuChange }: PropsWithChildren<MyHeaderProps>) {
17-
console.log('header')
15+
const handleItemContent = (item: SettingMenuItem) => {
16+
if (item.type === 'check') {
17+
const activedClass = item.value === '1' ? styles['actived'] : ''
18+
return (
19+
<>
20+
<CheckOutlined className={activedClass} />
21+
<span className={activedClass}>{item.content}</span>
22+
</>
23+
)
24+
}
25+
return item.content
26+
}
27+
28+
export default memo(function index({
29+
onInputValue,
30+
onMenuChange,
31+
}: PropsWithChildren<MyHeaderProps>) {
1832
const [menuData, setMenuData] = useState<SettingMenuItem[]>(settingMenu)
1933

2034
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
@@ -25,23 +39,30 @@ export default memo(function index({ onInputValue, onMenuChange }: PropsWithChil
2539
onInputValue && onInputValue(value)
2640
}
2741

28-
const onChangeKey = (key: string) => {
29-
const newData = menuData.map(item => {
42+
const onChangeKey = ({ key }: any) => {
43+
const newData = menuData.map((item) => {
3044
if (item.id === parseInt(key)) {
3145
if (key === '120') {
3246
const changeValue = item.value === '0' ? '1' : '0'
47+
changeValue === '1' && message.success('已启用markdown语法')
3348
onMenuChange && onMenuChange(changeValue)
34-
return {...item, value: changeValue}
49+
return { ...item, value: changeValue }
3550
}
3651
}
3752
return item
3853
})
3954
setMenuData(newData)
4055
}
4156

42-
const showMenu = useCallback(() => {
43-
return <MyMenu menuData={menuData} onChangeKey={onChangeKey}/>
44-
}, [menuData])
57+
const menu = (
58+
<Menu onClick={onChangeKey} className={styles['menu-container']}>
59+
{menuData.map((item) => (
60+
<Menu.Item key={item.id} className={styles['menu-item']}>
61+
{handleItemContent(item)}
62+
</Menu.Item>
63+
))}
64+
</Menu>
65+
)
4566

4667
return (
4768
<div className={styles['header-container']}>
@@ -56,7 +77,7 @@ export default memo(function index({ onInputValue, onMenuChange }: PropsWithChil
5677
</div>
5778
</div>
5879
<div className={`${styles['header-right']} all-center`}>
59-
<Dropdown overlay={showMenu}>
80+
<Dropdown overlay={menu} trigger={['click']}>
6081
<SettingOutlined className={styles['setting']} />
6182
</Dropdown>
6283
</div>

src/components/my-header/style.module.scss

+12-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $navHeight: 80px;
4040
border-bottom: 1px dashed #d9d9d9;
4141
color: #d9d9d9;
4242
font-size: 18px;
43-
&:focus{
43+
&:focus {
4444
font-weight: 700;
4545
box-shadow: none;
4646
outline: none;
@@ -55,15 +55,24 @@ $navHeight: 80px;
5555
}
5656
}
5757
}
58-
.header-right{
58+
.header-right {
5959
font-size: 22px;
6060
width: 50px;
6161
text-align: center;
6262
.setting {
6363
cursor: pointer;
6464
&:hover {
65-
opacity: .2;
65+
opacity: 0.2;
6666
}
6767
}
6868
}
6969
}
70+
.menu-container {
71+
.menu-item {
72+
user-select: none;
73+
}
74+
75+
.actived {
76+
color: green;
77+
}
78+
}

src/components/my-menu/index.tsx

-39
This file was deleted.

src/components/my-menu/style.module.scss

-5
This file was deleted.

src/layout/index.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ export default memo(function index() {
1717
}
1818
return (
1919
<div className="top-bot-layout">
20-
<MyHeader onInputValue={getInputValue} onMenuChange={getMenuChangeData}/>
20+
<MyHeader onInputValue={getInputValue} onMenuChange={getMenuChangeData} />
2121
{/* 以后这里填 renderRoutes */}
2222
<ResumName.Provider value={resumName}>
2323
<ShowMarkdown.Provider value={useMarkdown}>
2424
<div className="main-container">
2525
<MyHome />
2626
</div>
2727
</ShowMarkdown.Provider>
28-
2928
</ResumName.Provider>
30-
3129
</div>
3230
)
3331
})
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
.self-skill {
22
:global {
3-
.ant-input-textarea-show-count::after {
4-
color: #e5e5e5;
5-
}
63
}
74
}

0 commit comments

Comments
 (0)