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

Commit bc2299f

Browse files
authored
refactor(article): adjust post item style (#1045)
* chore(js-ts): PostItem * refactor(DigestSentence): add image, video count concept UI * refactor(post-item): extract ActiveBadge comp * chore(js-ts): AvatarsRow * chore(js-ts): AvatarsRow, use local spec
1 parent a993d9c commit bc2299f

File tree

30 files changed

+390
-265
lines changed

30 files changed

+390
-265
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.gitignore
33
*.json
44
*.lock
5-
*.hbs
5+
*.hbs
6+
*.svg
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

src/components/AvatarsRow/MoreItem.js renamed to src/components/AvatarsRow/MoreItem.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React, { useCallback } from 'react'
1+
import React from 'react'
22

33
import { prettyNum } from '@/utils'
44
import Tooltip from '@/components/Tooltip'
55

6+
import type { TProps as TAvatarsProps } from './index'
7+
68
import {
79
Wrapper,
810
NumbersMore,
@@ -12,13 +14,19 @@ import {
1214
Focus,
1315
} from './styles/more_item'
1416

15-
const MoreItem = ({ users, size, total, onTotalSelect, showTotalNumber }) => {
16-
const handleClick = useCallback(() => {
17-
onTotalSelect({ users, total })
18-
}, [onTotalSelect, total, users])
17+
type TProps = Pick<
18+
TAvatarsProps,
19+
'size' | 'total' | 'showTotalNumber' | 'onTotalSelect'
20+
>
1921

22+
const MoreItem: React.FC<TProps> = ({
23+
size,
24+
total,
25+
onTotalSelect,
26+
showTotalNumber,
27+
}) => {
2028
return (
21-
<Wrapper onClick={handleClick}>
29+
<Wrapper size={size} onClick={() => onTotalSelect()}>
2230
<Tooltip
2331
content={
2432
showTotalNumber ? (
@@ -29,10 +37,9 @@ const MoreItem = ({ users, size, total, onTotalSelect, showTotalNumber }) => {
2937
</TotalCommentStateHint>
3038
)
3139
}
32-
duration={0}
3340
>
3441
{showTotalNumber ? (
35-
<TextMore>
42+
<TextMore size={size} total={total}>
3643
<DotText>...</DotText>
3744
</TextMore>
3845
) : (

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

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
*
55
*/
66

7-
import React, { useCallback } from 'react'
8-
import T from 'prop-types'
9-
import { compose, not, isNil, filter, reverse, slice } from 'ramda'
7+
import React from 'react'
8+
import { compose, not, isNil, filter, reverse as reverseFn, slice } from 'ramda'
109
import { trackWindowScroll } from 'react-lazy-load-image-component'
1110

11+
import type { TUser } from '@/spec'
1212
import { AVATARS_LIST_LENGTH } from '@/config'
1313
import { SIZE } from '@/constant'
14-
import { buildLog, o2s, s2o } from '@/utils'
14+
import { buildLog } from '@/utils'
1515

1616
import Tooltip from '@/components/Tooltip'
17+
import type { TAvatarSize } from './spec'
1718

1819
import MoreItem from './MoreItem'
1920

@@ -45,41 +46,46 @@ const getUniqueArray = (arr, comp) => {
4546
return unique
4647
}
4748

48-
const AvatarsRow = ({
49+
export type TProps = {
50+
users?: TUser[]
51+
size?: TAvatarSize
52+
total: number
53+
limit: number
54+
showTotalNumber?: boolean
55+
reverse?: boolean
56+
scrollPosition?: any
57+
58+
onUserSelect: (user: TUser) => void
59+
onTotalSelect: () => void
60+
}
61+
62+
const AvatarsRow: React.FC<TProps> = ({
63+
size = SIZE.SMALL,
4964
total,
50-
users,
51-
size,
52-
limit,
53-
onUserSelect,
54-
onTotalSelect,
55-
showTotalNumber,
56-
reverse: isReverse,
57-
scrollPosition,
65+
users = [],
66+
limit = AVATARS_LIST_LENGTH.POSTS,
67+
onUserSelect = log,
68+
onTotalSelect = log,
69+
showTotalNumber = false,
70+
reverse = true,
71+
// see https://github.com/Aljullu/react-lazy-load-image-component/issues/42
72+
scrollPosition = null,
5873
}) => {
59-
const handleUserSelect = useCallback(
60-
(e) => {
61-
const user = s2o(e.target.dataset.user)
62-
onUserSelect(user)
63-
},
64-
[onUserSelect],
65-
)
66-
6774
if (users.length === 0) {
6875
return <span />
6976
}
7077

7178
users = filter(validUser, getUniqueArray(users, 'id'))
72-
const sortedUsers = isReverse ? users : reverse(users)
79+
const sortedUsers = reverse ? users : reverseFn(users)
7380

7481
return (
75-
<Wrapper size={size} total={total}>
82+
<Wrapper total={total}>
7683
{total <= 1 ? (
7784
<TotalOneOffset />
7885
) : (
7986
<MoreItem
8087
size={size}
8188
total={total}
82-
users={users}
8389
showTotalNumber={showTotalNumber}
8490
onTotalSelect={onTotalSelect}
8591
/>
@@ -91,18 +97,18 @@ const AvatarsRow = ({
9197
content={user.nickname}
9298
duration={0}
9399
delay={300}
94-
contentHeight={getAvatarSize(size)}
100+
contentHeight={getAvatarSize(size, 'number') as string}
101+
noPadding
95102
>
96103
<AvatarsItem size={size} noHoverMargin={total === 1}>
97104
<AvatarsImg
98105
src={user.avatar}
99106
size={size}
100-
data-user={o2s(user)}
101-
onClick={handleUserSelect}
107+
onClick={() => onUserSelect(user)}
102108
scrollPosition={scrollPosition}
103109
fallback={
104110
<AvatarFallback
105-
size={getAvatarSize(size, 'number')}
111+
size={getAvatarSize(size, 'number') as number}
106112
user={user}
107113
/>
108114
}
@@ -114,35 +120,4 @@ const AvatarsRow = ({
114120
)
115121
}
116122

117-
AvatarsRow.propTypes = {
118-
users: T.arrayOf(
119-
T.shape({
120-
id: T.string,
121-
avatar: T.string,
122-
nickname: T.string,
123-
extra_id: T.string,
124-
}),
125-
),
126-
size: T.oneOf([SIZE.SMALL, SIZE.MEDIUM]),
127-
total: T.number.isRequired,
128-
limit: T.number,
129-
onUserSelect: T.func,
130-
onTotalSelect: T.func,
131-
showTotalNumber: T.bool,
132-
reverse: T.bool,
133-
scrollPosition: T.any,
134-
}
135-
136-
AvatarsRow.defaultProps = {
137-
size: SIZE.SMALL,
138-
users: [],
139-
limit: AVATARS_LIST_LENGTH.POSTS,
140-
onUserSelect: log,
141-
onTotalSelect: log,
142-
showTotalNumber: false,
143-
reverse: true,
144-
// see https://github.com/Aljullu/react-lazy-load-image-component/issues/42
145-
scrollPosition: null,
146-
}
147-
148123
export default trackWindowScroll(AvatarsRow)

src/components/AvatarsRow/spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { TSIZE_SM } from '@/spec'
2+
3+
export type TAvatarSize = TSIZE_SM

src/components/AvatarsRow/styles/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { theme, css } from '@/utils'
55

66
import ImgFallback from '@/components/ImgFallback'
77
import { getLiSize, getAvatarSize, getUlMarginRight } from './metric'
8+
import type { TAvatarSize } from '../spec'
89

910
export const Wrapper = styled.ul<{ total: number }>`
1011
${css.flex('align-center')};
@@ -15,7 +16,7 @@ export const Wrapper = styled.ul<{ total: number }>`
1516
margin-right: ${({ total }) => getUlMarginRight(total)};
1617
`
1718
// height: 49px;
18-
type TBaseAvatarItem = { size: string; noHoverMargin: string }
19+
type TBaseAvatarItem = { size: TAvatarSize; noHoverMargin: boolean }
1920
const BaseAvatarItem = styled.li<TBaseAvatarItem>`
2021
margin: 0px 0px 0px 0px;
2122
padding: 0px 0px 0px 0px;
@@ -45,7 +46,8 @@ export const AvatarsItem = styled(BaseAvatarItem)`
4546
export const TotalOneOffset = styled.span`
4647
margin-right: 10px;
4748
`
48-
export const AvatarsImg = styled(Img)<{ size: string }>`
49+
type TAvatarsImg = { size: string; onClick: () => void; scrollPosition: any }
50+
export const AvatarsImg = styled(Img)<TAvatarsImg>`
4951
border: 2px solid;
5052
border-color: ${theme('thread.commentsUserBorder')};
5153
border-radius: 100px 100px 100px 100px;
@@ -60,7 +62,7 @@ export const AvatarsImg = styled(Img)<{ size: string }>`
6062
6163
text-align: center;
6264
`
63-
type TAvatarsMore = { size: string; total: number }
65+
type TAvatarsMore = { size: TAvatarSize; total: number }
6466
export const AvatarsMore = styled.span<TAvatarsMore>`
6567
${css.flex('align-both')};
6668
font-size: 14px;

src/components/AvatarsRow/styles/metric.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { SIZE } from '@/constant'
2+
import type { TAvatarSize } from '../spec'
3+
24
/**
35
* NOTE: Li size should always smaller than the avatar size
46
*/
57

6-
export const getLiSize = (size: string): string => {
8+
export const getLiSize = (size: TAvatarSize): string => {
79
switch (size) {
810
case SIZE.SMALL: {
911
return '15px'

src/components/DigestSentence/index.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)