Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e63b136

Browse files
authored
refactor(cool-guide): terms adjust && ts convert (#1048)
* chore(cool-guide): adjust items * chore(navi-catalog): js -> ts * chore(navi-catalog): js -> ts && re-org
1 parent d7b4d1e commit e63b136

File tree

16 files changed

+1078
-1027
lines changed

16 files changed

+1078
-1027
lines changed

src/components/NaviCatalog/Dashboard.js renamed to src/components/NaviCatalog/Dashboard.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { last } from 'ramda'
1111
import { ICON } from '@/config'
1212
import { buildLog } from '@/utils'
1313

14+
import type { TMenuItem } from './spec'
15+
1416
// import { SpaceGrow } from '@/components/Common'
1517
import {
1618
Wrapper,
@@ -45,11 +47,16 @@ const getLevels = (paths) => {
4547
}
4648
}
4749

50+
type TProps = {
51+
viewPath: TMenuItem[]
52+
goCatalog: (id?: string) => void
53+
}
54+
4855
/**
4956
* 显示成成层叠结构的样式,有子目录的可以回上一层,没有子目录的则作为只读显示,
5057
* 类似抽屉的样子
5158
*/
52-
const Dashboard = ({ viewPath, goCatalog }) => {
59+
const Dashboard: React.FC<TProps> = ({ viewPath, goCatalog }) => {
5360
const [headLevels, lastLevel] = getLevels(viewPath)
5461
/* 判断是不是第二层 */
5562
const is2ecLevel = viewPath.length === 1

src/components/NaviCatalog/Header.js renamed to src/components/NaviCatalog/Header.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { nilOrEmpty } from '@/utils'
55

66
import Tooltip from '@/components/Tooltip'
77

8+
import type { TMenuItem } from './spec'
9+
810
import {
911
Wrapper,
1012
Title,
@@ -16,7 +18,19 @@ import {
1618
HelpHint,
1719
} from './styles/header'
1820

19-
const Header = ({
21+
type TProps = {
22+
title: string
23+
activeCatalogId: string
24+
viewPath: TMenuItem[]
25+
testid?: string
26+
27+
goHome: () => void
28+
onReset: () => void
29+
onLocate: () => void
30+
}
31+
32+
const Header: React.FC<TProps> = ({
33+
testid = 'navi-catalog-header',
2034
title,
2135
activeCatalogId,
2236
goHome,
@@ -29,7 +43,7 @@ const Header = ({
2943
const showLocate = activeCatalogId !== ''
3044

3145
return (
32-
<Wrapper>
46+
<Wrapper testid={testid}>
3347
{title ? <Title active={showReset}>{title}</Title> : <div />}
3448

3549
<OperatorsWrapper>
@@ -38,6 +52,7 @@ const Header = ({
3852
content={<HelpHint>返回主目录</HelpHint>}
3953
placement="bottom"
4054
delay={1000}
55+
noPadding
4156
>
4257
<HomeIcon src={`${ICON}/shape/home.svg`} />
4358
</Tooltip>
@@ -48,6 +63,7 @@ const Header = ({
4863
content={<HelpHint>定位当前选中</HelpHint>}
4964
placement="bottom"
5065
delay={1000}
66+
noPadding
5167
>
5268
<LocateIcon src={`${ICON}/shape/locate-solid.svg`} />
5369
</Tooltip>
@@ -58,6 +74,7 @@ const Header = ({
5874
content={<HelpHint>重置筛选条件</HelpHint>}
5975
placement="bottom"
6076
delay={1000}
77+
noPadding
6178
>
6279
<ResetIcon src={`${ICON}/shape/reset.svg`} />
6380
</Tooltip>

src/components/NaviCatalog/List/index.js renamed to src/components/NaviCatalog/List/index.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
import React from 'react'
8-
import T from 'prop-types'
98
import { map, prop, includes } from 'ramda'
109

1110
import { buildLog } from '@/utils'
1211
import { SpaceGrow, Space } from '@/components/Common'
1312

13+
import type { TMenuItem } from '../spec'
1414
import { ROOT_MENU, CHILD_MENU } from '../constant'
1515

1616
import {
@@ -42,7 +42,22 @@ const renderRightIcon = (item, active, showItemTotal) => {
4242
)
4343
}
4444

45-
const List = ({
45+
type TProps = {
46+
menuMode: 'root' | 'child'
47+
catalogItems: TMenuItem[]
48+
activeCatalogId: string
49+
activePath: TMenuItem[]
50+
withDivider: boolean
51+
showMoreItem: boolean
52+
showItemTotal: boolean
53+
54+
testid?: string
55+
56+
onSelect: (id: string, type: string) => void
57+
onShowMore: () => void
58+
}
59+
60+
const List: React.FC<TProps> = ({
4661
menuMode,
4762
catalogItems,
4863
onSelect,
@@ -52,11 +67,12 @@ const List = ({
5267
showMoreItem,
5368
showItemTotal,
5469
onShowMore,
70+
testid = 'navi-catalog-list',
5571
}) => {
5672
const activePathIdList = [...map(prop('id'), activePath), activeCatalogId]
5773

5874
return (
59-
<Wrapper isRootMenu={menuMode === ROOT_MENU}>
75+
<Wrapper isRootMenu={menuMode === ROOT_MENU} testid={testid}>
6076
{catalogItems.map((item) => {
6177
const active = includes(item.id, activePathIdList)
6278

@@ -66,7 +82,7 @@ const List = ({
6682
isRootMenu={menuMode === ROOT_MENU}
6783
active={active}
6884
withDivider={withDivider}
69-
onClick={() => onSelect(item)}
85+
onClick={() => onSelect(item.id, item.displayType)}
7086
>
7187
{item.fixedIcon && <FixedIcon src={item.fixedIcon} />}
7288
{item.title}
@@ -88,21 +104,4 @@ const List = ({
88104
)
89105
}
90106

91-
List.propTypes = {
92-
menuMode: T.oneOf([ROOT_MENU, CHILD_MENU]).isRequired,
93-
// TODO:
94-
catalogItems: T.arrayOf(T.object).isRequired,
95-
activePath: T.arrayOf(T.object).isRequired,
96-
onSelect: T.func.isRequired,
97-
activeCatalogId: T.string.isRequired,
98-
withDivider: T.bool.isRequired,
99-
showMoreItem: T.bool.isRequired,
100-
showItemTotal: T.bool.isRequired,
101-
onShowMore: T.oneOfType([T.func, T.instanceOf(null)]),
102-
}
103-
104-
List.defaultProps = {
105-
onShowMore: null,
106-
}
107-
108107
export default React.memo(List)

src/components/NaviCatalog/PinNumber.js renamed to src/components/NaviCatalog/PinNumber.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { Wrapper, Num, PinIcon, TooltipPopContent } from './styles/pin_number'
88

99
// TODO: onPin / undoPin
1010

11-
const PinNumber = ({ num, pinNumberHoverType }) => {
11+
type TProps = {
12+
num: number
13+
pinNumberHoverType: 'pin' | 'unpin'
14+
}
15+
16+
const PinNumber: React.FC<TProps> = ({ num, pinNumberHoverType }) => {
1217
return (
1318
<Wrapper>
1419
<Num>{num}</Num>
@@ -19,7 +24,7 @@ const PinNumber = ({ num, pinNumberHoverType }) => {
1924
placement="right"
2025
delay={1000}
2126
showArrow={false}
22-
noDefaultPadding
27+
noPadding
2328
>
2429
<PinIcon src={`${ICON_CMD}/navi/pin.svg`} />
2530
</Tooltip>
@@ -29,7 +34,7 @@ const PinNumber = ({ num, pinNumberHoverType }) => {
2934
placement="right"
3035
delay={1000}
3136
showArrow={false}
32-
noDefaultPadding
37+
noPadding
3338
>
3439
<PinIcon src={`${ICON_CMD}/navi/unpin.svg`} />
3540
</Tooltip>
@@ -39,13 +44,4 @@ const PinNumber = ({ num, pinNumberHoverType }) => {
3944
)
4045
}
4146

42-
PinNumber.propTypes = {
43-
num: T.number.isRequired,
44-
pinNumberHoverType: T.oneOf(['pin', 'unpin']).isRequired,
45-
// onPin: T.func.isRequired,
46-
// undoPin: T.func.isRequired,
47-
}
48-
49-
PinNumber.defaultProps = {}
50-
5147
export default React.memo(PinNumber)

src/components/NaviCatalog/helper.js renamed to src/components/NaviCatalog/helper.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import { map, find, propEq, last } from 'ramda'
33
import { URL_QUERY } from '@/constant'
44
import { nilOrEmpty } from '@/utils'
55

6+
import type { TMenuItem } from './spec'
7+
68
// 根据 path 路径得到当前目录项
7-
export const getCurrentMenuItem = (path, items) => {
9+
export const getCurrentMenuItem = (
10+
path: TMenuItem[],
11+
items: TMenuItem[],
12+
): TMenuItem => {
813
if (nilOrEmpty(path) || nilOrEmpty(items)) return
914

10-
const item = find(propEq('id', path[0].id), items)
15+
const item = find(propEq('id', path[0].id), items) as TMenuItem
1116
if (item.id === last(path).id) return item
1217

1318
return getCurrentMenuItem(path.slice(1), item?.childMenu)
@@ -20,7 +25,10 @@ export const getCurrentMenuItem = (path, items) => {
2025
* @param {string} pathString
2126
* @returns {array of catalog}
2227
*/
23-
export const findPath = (items, pathString = 'aa-bb-cc') => {
28+
export const findPath = (
29+
items: TMenuItem[],
30+
pathString = 'aa-bb-cc',
31+
): TMenuItem[] => {
2432
// pathList => parentId-childId-subChildId-xxx
2533
const idPaths = pathString.split('-')
2634

@@ -30,7 +38,7 @@ export const findPath = (items, pathString = 'aa-bb-cc') => {
3038
const pathId = idPaths[index]
3139

3240
const catalog = previousCatalog
33-
const item = find(propEq('id', pathId), catalog)
41+
const item = find(propEq('id', pathId), catalog) as TMenuItem
3442
if (!item) return path
3543

3644
if (item.childMenu) {
@@ -42,7 +50,9 @@ export const findPath = (items, pathString = 'aa-bb-cc') => {
4250
return path
4351
}
4452

45-
export const covertPathToURLQuery = (path) => {
53+
export const covertPathToURLQuery = (
54+
path: TMenuItem[],
55+
): Record<string, unknown> => {
4656
const idPathString = map((catalog) => catalog.id, path).join('-')
4757
if (nilOrEmpty(idPathString)) return { [URL_QUERY.NAVI_CATALOG_PATH]: '' }
4858

src/components/NaviCatalog/index.js renamed to src/components/NaviCatalog/index.tsx

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import React, { useState, useCallback, useEffect } from 'react'
8-
import T from 'prop-types'
98
import { find, findIndex, propEq, last } from 'ramda'
109

1110
import { URL_QUERY } from '@/constant'
@@ -17,31 +16,45 @@ import {
1716
findDeepMatch,
1817
} from '@/utils'
1918

19+
import type { TMenuItem, TMenuMode } from './spec'
2020
import { ROOT_MENU, CHILD_MENU } from './constant'
2121
import Header from './Header'
2222
import Dashboard from './Dashboard'
2323
import List from './List'
2424

25-
import menuItemsData from './menuData'
2625
import { Wrapper } from './styles'
2726

2827
import { getCurrentMenuItem, findPath, covertPathToURLQuery } from './helper'
2928

3029
/* eslint-disable-next-line */
3130
const log = buildLog('c:NaviCatalog:index')
3231

33-
const NaviCatalog = ({
34-
title,
32+
type TProps = {
33+
title?: string
34+
withDivider?: boolean
35+
// initActiveMenuId?: string
36+
showMoreItem?: boolean
37+
// 是否显示每个目录项的条目总数
38+
showItemTotal?: boolean
39+
testid?: string
40+
items: TMenuItem[]
41+
42+
onSelect: (id: string, type: string) => void
43+
onShowMore?: () => void
44+
}
45+
46+
const NaviCatalog: React.FC<TProps> = ({
47+
testid = 'navi-menu',
48+
title = '',
3549
items,
36-
onSelect,
37-
withDivider,
38-
initActiveMenuId,
39-
showMoreItem,
40-
showItemTotal,
41-
onShowMore,
42-
testid,
50+
onSelect = log,
51+
withDivider = false,
52+
// initActiveMenuId = '',
53+
showMoreItem = false,
54+
showItemTotal = false,
55+
onShowMore = null,
4356
}) => {
44-
const [menuMode, setMenuMode] = useState(ROOT_MENU)
57+
const [menuMode, setMenuMode] = useState<TMenuMode>(ROOT_MENU)
4558
// 当前选中的目录 id, 不包括在其链路上的 id
4659
const [activeCatalogId, setActiveCatalogId] = useState('')
4760
// 当前展示的 path list, 可能是选中的,也可能是仅浏览,未必选中
@@ -178,7 +191,6 @@ const NaviCatalog = ({
178191
withDivider={withDivider}
179192
activeCatalogId={activeCatalogId}
180193
activePath={activePath}
181-
initActiveMenuId={initActiveMenuId}
182194
showMoreItem={showMoreItem}
183195
showItemTotal={showItemTotal}
184196
onShowMore={onShowMore}
@@ -187,53 +199,4 @@ const NaviCatalog = ({
187199
)
188200
}
189201

190-
NaviCatalog.propTypes = {
191-
title: T.string,
192-
onSelect: T.func,
193-
withDivider: T.bool,
194-
initActiveMenuId: T.string,
195-
items: T.arrayOf(
196-
T.shape({
197-
id: T.string,
198-
title: T.string,
199-
icon: T.string,
200-
displayType: T.string,
201-
fixedIcon: T.oneOfType([T.string, T.node]),
202-
pinNumber: T.number,
203-
childMenu: T.arrayOf(
204-
T.shape({
205-
id: T.string,
206-
title: T.string,
207-
icon: T.string,
208-
displayType: T.string,
209-
childMenu: T.arrayOf(
210-
T.shape({
211-
id: T.string,
212-
title: T.string,
213-
displayType: T.string,
214-
}),
215-
),
216-
}),
217-
),
218-
}),
219-
),
220-
showMoreItem: T.bool,
221-
// 是否显示每个目录项的条目总数
222-
showItemTotal: T.bool,
223-
onShowMore: T.oneOfType([T.func, T.instanceOf(null)]),
224-
testid: T.string,
225-
}
226-
227-
NaviCatalog.defaultProps = {
228-
items: menuItemsData,
229-
onSelect: log,
230-
withDivider: false,
231-
initActiveMenuId: '',
232-
showMoreItem: false,
233-
showItemTotal: false,
234-
onShowMore: null,
235-
testid: 'navi-menu',
236-
title: '',
237-
}
238-
239202
export default React.memo(NaviCatalog)

0 commit comments

Comments
 (0)