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

Commit 6ecee08

Browse files
authored
refactor(tags-bar): simu real-world group tag && fix some edge-case (#1006)
1 parent 1f4999e commit 6ecee08

File tree

6 files changed

+71
-43
lines changed

6 files changed

+71
-43
lines changed

src/containers/unit/TagsBar/DesktopView/Folder.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useRef, useEffect } from 'react'
22

33
import { findIndex } from 'ramda'
44

@@ -20,11 +20,13 @@ import {
2020
} from '../styles/desktop_view/folder'
2121

2222
const MAX_DISPLAY_COUNT = 5
23-
const TOGGLE_SUB_TOGGLE_THROLD = 5 // TODO: only for test, should be 15
23+
const TOGGLE_SUB_TOGGLE_THROLD = 15
2424

2525
const Folder = ({ title, groupTags, allTags, activeTag, onSelect }) => {
26-
// 如果标签总数都没有超过 15 个,那么就不用显示 '展示更多'了,直接全部显示完事儿
27-
const needSubToggle = allTags?.length > TOGGLE_SUB_TOGGLE_THROLD || false
26+
// 决定是否显示 '展示更多' 的时候参考标签总数
27+
const needSubToggle =
28+
allTags?.length > TOGGLE_SUB_TOGGLE_THROLD &&
29+
groupTags.length > MAX_DISPLAY_COUNT
2830

2931
const initDisplayCount = needSubToggle ? MAX_DISPLAY_COUNT : groupTags.length
3032

@@ -36,9 +38,27 @@ const Folder = ({ title, groupTags, allTags, activeTag, onSelect }) => {
3638
const isActiveTagInFolder =
3739
findIndex((item) => item.id === activeTag.id, groupTags) >= 0
3840

41+
const subToggleRef = useRef(null)
42+
// 当选中的 Tag 被折叠在展示更多里面时,将其展开
43+
useEffect(() => {
44+
if (subToggleRef && isActiveTagInFolder) {
45+
setCurDisplayCount(groupTags.length)
46+
}
47+
}, [subToggleRef, isActiveTagInFolder, groupTags])
48+
3949
return (
4050
<Wrapper>
41-
<Header onClick={() => toggleFolder(!isFolderOpen)}>
51+
<Header
52+
onClick={() => {
53+
toggleFolder(!isFolderOpen)
54+
55+
// 当关闭 Folder 的时候,如果当前 Folder 没有被激活的 Tag, 那么就回到折叠状态
56+
// 如果有,那么保持原来的状态
57+
if (isFolderOpen && !isActiveTagInFolder) {
58+
setCurDisplayCount(MAX_DISPLAY_COUNT)
59+
}
60+
}}
61+
>
4262
<ArrowIcon
4363
isOpen={isFolderOpen}
4464
src={`${ICON}/shape/arrow-simple.svg`}
@@ -63,6 +83,7 @@ const Folder = ({ title, groupTags, allTags, activeTag, onSelect }) => {
6383
</TagsWrapper>
6484
{needSubToggle && (
6585
<SubToggle
86+
ref={subToggleRef}
6687
onClick={() => {
6788
setCurDisplayCount(
6889
curDisplayCount === MAX_DISPLAY_COUNT

src/containers/unit/TagsBar/DesktopView/index.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
import React from 'react'
88
import T from 'prop-types'
9+
import { keys } from 'ramda'
910

1011
import { THREAD, TOPIC } from '@/constant'
11-
import { buildLog, pluggedIn } from '@/utils'
12+
import { buildLog, pluggedIn, groupByKey } from '@/utils'
1213

1314
import GobackTag from './GobackTag'
1415
import Folder from './Folder'
15-
// import TagItem from './TagItem'
1616

17-
import { Wrapper /* TagsWrapper */ } from '../styles/desktop_view'
17+
import { Wrapper } from '../styles/desktop_view'
1818

1919
import { useInit } from '../logic'
2020

@@ -30,45 +30,34 @@ const TagsBarContainer = ({
3030
}) => {
3131
useInit(store, thread, topic, active)
3232
const { tagsData, activeTagData } = store
33-
// const sortedTags = sortByColor(tagsData)
33+
// const tagsByGroup = groupByKey(tagsData, 'group')
34+
const tagsByGroup = groupByKey(
35+
tagsData.map((tag) => {
36+
if (tag.id < 4) {
37+
tag.group = '这是第一组'
38+
} else {
39+
tag.group = '这是第二组' // '__default__'
40+
}
41+
return tag
42+
}),
43+
'group',
44+
)
45+
// console.log('tagsByGroup: ', tagsByGroup)
46+
const groupsKeys = keys(tagsByGroup)
3447

3548
return (
3649
<Wrapper>
3750
{activeTagData.title && <GobackTag onSelect={onSelect} />}
38-
39-
<Folder
40-
title="默认分组"
41-
groupTags={tagsData}
42-
allTags={tagsData}
43-
activeTag={activeTagData}
44-
onSelect={onSelect}
45-
/>
46-
47-
{/* <Folder title="实践 Demo">
48-
<TagsWrapper>
49-
{sortedTags.slice(0, 5).map((tag) => (
50-
<TagItem
51-
key={tag.id}
52-
tag={tag}
53-
active={activeTagData.title === tag.title}
54-
activeId={activeTagData.id}
55-
onSelect={onSelect}
56-
/>
57-
))}
58-
</TagsWrapper>
59-
</Folder> */}
60-
61-
{/* <TagsWrapper>
62-
{sortedTags.map((tag) => (
63-
<TagItem
64-
key={tag.id}
65-
tag={tag}
66-
active={activeTagData.title === tag.title}
67-
activeId={activeTagData.id}
51+
{groupsKeys.map((groupKey) => (
52+
<Folder
53+
key={groupKey}
54+
title={groupKey}
55+
groupTags={tagsByGroup[groupKey]}
56+
allTags={tagsData}
57+
activeTag={activeTagData}
6858
onSelect={onSelect}
6959
/>
70-
))} </TagsWrapper>
71-
*/}
60+
))}
7261
</Wrapper>
7362
)
7463
}

src/containers/unit/TagsBar/styles/desktop_view/folder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const Title = styled.div`
2929
margin-left: 4px;
3030
font-size: 14px;
3131
margin-right: 8px;
32-
${css.cutFrom('120px')};
32+
${css.cutFrom('85px')};
3333
3434
${Header}:hover & {
3535
opacity: 0.65;

src/containers/unit/TagsBar/styles/desktop_view/tag_item.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const TagTitle = styled.div`
4848
letter-spacing: 2px;
4949
font-weight: ${({ active }) => (active ? 'bold' : 'normal')};
5050
opacity: ${({ active }) => (active ? 1 : 0.9)};
51-
${css.cutFrom('120px')};
51+
${({ isInline }) => (!isInline ? css.cutFrom('120px') : css.cutFrom('50px'))};
5252
5353
&:hover {
5454
cursor: pointer;

utils/helper.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,20 @@ export const findDeepMatch = (data, key, value) => {
273273

274274
return result
275275
}
276+
277+
/**
278+
* groupByKey
279+
* see @link: https://stackoverflow.com/a/47385953/4050784
280+
*
281+
* @param {Array} - array
282+
* @param {String} - key
283+
* @returns {Object}
284+
*/
285+
export const groupByKey = (array, key) => {
286+
return array.reduce((hash, obj) => {
287+
if (obj[key] === undefined) return hash
288+
return Object.assign(hash, {
289+
[obj[key]]: (hash[obj[key]] || []).concat(obj),
290+
})
291+
}, {})
292+
}

utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export {
3131
isCypressRunning,
3232
multiClick,
3333
findDeepMatch,
34+
groupByKey,
3435
} from './helper'
3536

3637
export { errorForHuman, ssrAmbulance } from './errors'

0 commit comments

Comments
 (0)