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

Commit c555b2b

Browse files
authored
refactor(js-ts): tooltip convert (#1043)
1 parent 32c2916 commit c555b2b

File tree

8 files changed

+84
-85
lines changed

8 files changed

+84
-85
lines changed

src/components/Cards/JobCard.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ const JobCard: React.FC<TProps> = ({
9292
</PublisherInfo>
9393
</Publisher>
9494
<TechKeywords>
95-
{/* @ts-ignore */}
9695
<Tooltip
9796
content={<CommunityCard item={fakeCommunity} />}
9897
placement="top"

src/components/FiltersMenu/Header.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const Header: React.FC<TProps> = ({ title, showReset, onReset }) => {
2323
<Wrapper>
2424
<Title active={showReset}>{title}</Title>
2525
<OperatorsWrapper>
26-
{/* @ts-ignore */}
2726
<Tooltip
2827
content={<HelpHint>重置筛选条件</HelpHint>}
2928
placement="bottom"

src/components/PagedContents/CommunityRecommends.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const CommunityRecommends: React.FC<TProps> = ({ items = tmpItems }) => {
6464
<ListWrapper>
6565
{items.map((item) => (
6666
<Community key={item.id}>
67-
{/* @ts-ignore */}
6867
<Tooltip content={<CommunityCard item={item} />} placement="top">
6968
<CommunityTitle>{item.title}</CommunityTitle>
7069
</Tooltip>

src/components/Tooltip/ConfirmFooter.js renamed to src/components/Tooltip/ConfirmFooter.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ import { FOOTER_BEHAVIOR } from './constant'
77

88
import { Wrapper, ButtonsWrapper, CancelButton } from './styles/confirm_footer'
99

10-
const ConfirmFooter = ({ onConfirm, onCancel, footerBehavior }) => {
10+
type TProps = {
11+
footerBehavior?: 'default' | 'confirm' | 'delete-confirm' | 'add'
12+
13+
onConfirm?: () => void
14+
onCancel?: () => void
15+
}
16+
17+
const ConfirmFooter: React.FC<TProps> = ({
18+
onConfirm,
19+
onCancel,
20+
footerBehavior,
21+
}) => {
1122
let content = null
1223

1324
switch (footerBehavior) {
1425
case FOOTER_BEHAVIOR.DELETE_CONFIRM: {
1526
content = (
1627
<ButtonsWrapper>
17-
<Button size="small" type="red" onClick={onConfirm}>
28+
<Button size="small" type="red" onClick={() => onConfirm?.()}>
1829
确认删除
1930
</Button>
2031
<Space right={10} />
@@ -27,11 +38,11 @@ const ConfirmFooter = ({ onConfirm, onCancel, footerBehavior }) => {
2738
case FOOTER_BEHAVIOR.ADD: {
2839
content = (
2940
<ButtonsWrapper>
30-
<Button size="small" type="primary" onClick={onConfirm}>
41+
<Button size="small" type="primary" onClick={() => onConfirm?.()}>
3142
添加
3243
</Button>
3344
<Space right={10} />
34-
<CancelButton onClick={onCancel}>取消</CancelButton>
45+
<CancelButton onClick={() => onCancel?.()}>取消</CancelButton>
3546
</ButtonsWrapper>
3647
)
3748
break
@@ -44,7 +55,7 @@ const ConfirmFooter = ({ onConfirm, onCancel, footerBehavior }) => {
4455
确定
4556
</Button>
4657
<Space right={10} />
47-
<CancelButton onClick={onCancel}>取消</CancelButton>
58+
<CancelButton onClick={() => onCancel?.()}>取消</CancelButton>
4859
</ButtonsWrapper>
4960
)
5061
break

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

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
*/
66

77
import React, { useState, useRef } from 'react'
8-
import T from 'prop-types'
9-
import { values } from 'ramda'
108

9+
import type { TTooltipPlacement, TTooltipAnimation } from '@/spec'
1110
import { css, buildLog, isDescendant } from '@/utils'
1211
import { useOutsideClick } from '@/hooks'
1312

@@ -26,19 +25,44 @@ import {
2625
/* eslint-disable-next-line */
2726
const log = buildLog('c:Tooltip:index')
2827

29-
const Tooltip = ({
28+
type TProps = {
29+
children: React.ReactNode
30+
content: string | React.ReactNode
31+
animation?: TTooltipAnimation
32+
placement?: TTooltipPlacement
33+
// more options see: https://atomiks.github.io/tippyjs/all-options/
34+
delay?: number
35+
duration?: number
36+
trigger?: 'mouseenter focus' | 'click'
37+
hideOnClick?: boolean
38+
noPadding?: boolean
39+
showArrow?: boolean
40+
footerBehavior?: 'default' | 'confirm' | 'delete-confirm' | 'add'
41+
// currently only for AvatarsRow, it will collapse the height
42+
// for same reason, figure out later
43+
contentHeight?: string
44+
45+
onShow?: () => void
46+
onHide?: () => void
47+
onConfirm?: () => void
48+
}
49+
50+
const Tooltip: React.FC<TProps> = ({
3051
children,
31-
noPadding,
52+
animation = 'fade',
53+
noPadding = false,
3254
onHide,
3355
onShow,
34-
placement,
56+
placement = 'top',
57+
delay = 0,
58+
duration = 0,
3559
content,
36-
hideOnClick,
37-
showArrow,
38-
footerBehavior,
60+
hideOnClick = true,
61+
showArrow = true,
62+
footerBehavior = 'default',
63+
trigger = 'mouseenter focus',
3964
onConfirm,
4065
contentHeight,
41-
...restProps
4266
}) => {
4367
const [instance, setInstance] = useState(null)
4468
const [active, setActive] = useState(false)
@@ -110,7 +134,10 @@ const Tooltip = ({
110134
}}
111135
zIndex={css.zIndex.popover}
112136
interactive
113-
{...restProps}
137+
delay={delay}
138+
duration={duration}
139+
animation={animation}
140+
trigger={trigger}
114141
>
115142
{ContentComp}
116143
</StyledTippy>
@@ -120,7 +147,7 @@ const Tooltip = ({
120147
content={PopoverContent}
121148
placement={placement}
122149
hideOnClick={hideOnClick}
123-
animation="scale"
150+
animation={animation}
124151
onHide={(instance) => {
125152
setInstance(instance)
126153
setActive(false)
@@ -133,76 +160,13 @@ const Tooltip = ({
133160
}}
134161
zIndex={css.zIndex.popover}
135162
interactive
136-
{...restProps}
163+
delay={delay}
164+
duration={duration}
165+
trigger={trigger}
137166
>
138167
{ContentComp}
139168
</NoPaddingStyledTippy>
140169
)
141170
}
142171

143-
Tooltip.propTypes = {
144-
children: T.node.isRequired,
145-
content: T.oneOfType([T.string, T.node]).isRequired,
146-
// options
147-
animation: T.oneOf([
148-
'shift-away',
149-
'shift-toward',
150-
'fade',
151-
'scale',
152-
'perspective',
153-
]),
154-
arrow: T.bool,
155-
delay: T.number,
156-
duration: T.number,
157-
placement: T.oneOf([
158-
'top',
159-
'top-start',
160-
'top-end',
161-
'bottom',
162-
'bottom-start',
163-
'bottom-end',
164-
'left',
165-
'left-start',
166-
'left-end',
167-
'right',
168-
'right-start',
169-
'right-end',
170-
]),
171-
// hooks
172-
trigger: T.oneOf(['mouseenter focus', 'click']),
173-
hideOnClick: T.oneOf([true, false]),
174-
maxWidth: T.oneOf([350, 'none']),
175-
// more options see: https://atomiks.github.io/tippyjs/all-options/
176-
onShow: T.oneOfType([T.func, T.instanceOf(null)]),
177-
onHide: T.oneOfType([T.func, T.instanceOf(null)]),
178-
noPadding: T.bool,
179-
showArrow: T.bool,
180-
footerBehavior: T.oneOf(values(FOOTER_BEHAVIOR)),
181-
onConfirm: T.oneOfType([T.func, T.instanceOf(null)]),
182-
183-
// currently only for AvatarsRow, it will collapse the height
184-
// for same reason, figure out later
185-
contentHeight: T.string,
186-
}
187-
188-
Tooltip.defaultProps = {
189-
animation: 'fade',
190-
arrow: true,
191-
delay: 0,
192-
duration: 0,
193-
hideOnClick: true,
194-
placement: 'top',
195-
// hooks
196-
trigger: 'mouseenter focus',
197-
onShow: null,
198-
onHide: null,
199-
noPadding: false,
200-
maxWidth: 'none',
201-
showArrow: true,
202-
footerBehavior: 'default',
203-
onConfirm: null,
204-
205-
contentHeight: 'auto',
206-
}
207-
208172
export default React.memo(Tooltip)

src/components/Tooltip/styles/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export const StyledTippy = styled(Tippy)`
1414
1515
border-radius: 5px;
1616
padding: 10px;
17+
18+
.tippy-arrow {
19+
display: none;
20+
}
1721
`
1822
export const NoPaddingStyledTippy = styled(StyledTippy)`
1923
padding: 0;

src/spec/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export type {
2727
TPlatform,
2828
Nullable,
2929
TScrollDirection,
30+
TTooltipAnimation,
31+
TTooltipPlacement,
3032
} from './utils'
3133

3234
export type { TGQLError } from './graphql'

src/spec/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,24 @@ export type TPlatform = {
4747
}
4848

4949
export type TScrollDirection = 'up' | 'down'
50+
51+
export type TTooltipAnimation =
52+
| 'shift-away'
53+
| 'shift-toward'
54+
| 'fade'
55+
| 'scale'
56+
| 'perspective'
57+
58+
export type TTooltipPlacement =
59+
| 'top'
60+
| 'top-start'
61+
| 'top-end'
62+
| 'bottom'
63+
| 'bottom-start'
64+
| 'bottom-end'
65+
| 'left'
66+
| 'left-start'
67+
| 'left-end'
68+
| 'right'
69+
| 'right-start'
70+
| 'right-end'

0 commit comments

Comments
 (0)