This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be6eeda
commit 3c43293
Showing
9 changed files
with
261 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: Tooltip 文字提示 | ||
date: 2019-06-06 | ||
--- | ||
文字提示气泡框。 | ||
|
||
## 使用方式 | ||
鼠标移入显示提示,移出消失,气泡浮层不承载复杂文本和操作。 | ||
|
||
用来替换默认 `title` 提示,提供解释文案。 | ||
|
||
## 代码演示 | ||
### 文本提示 | ||
给文本提供说明文案。 | ||
``` | ||
<Tooltip label={<span>鼠标移入会展示提示。</span>}> | ||
你好👋! | ||
</Tooltip> | ||
``` | ||
### 提示方向 | ||
- 可以指定提示展示的方向; | ||
- 如果不指定,默认是朝上 `top` 展示; | ||
- 不指定方向,根据 tooltip 在浏览器中的位置,在初次渲染时会调整展示方向,避免提示信息超出窗口,同时可能方便用户查看提示信息。 | ||
``` | ||
<div className="flex-between"> | ||
<Tooltip label={<Button>右 right</Button>} placement="right"> | ||
你好👋! | ||
</Tooltip> | ||
<Tooltip label={<Button>上 top</Button>} placement="top"> | ||
你好👋! | ||
</Tooltip> | ||
<Tooltip label={<Button>底 bottom</Button>} placement="bottom"> | ||
你好👋! | ||
</Tooltip> | ||
<Tooltip label={<Button>左 left</Button>} placement="left"> | ||
你好👋! | ||
</Tooltip> | ||
</div> | ||
``` | ||
|
||
### 提示颜色 | ||
默认提示背景是黑色,文案是白色。可以使用 `contrast` 调换。白色背景、黑色文案。 | ||
``` | ||
<Tooltip contrast label={<Button>移入试试!</Button>}> | ||
你好👋! | ||
</Tooltip> | ||
``` | ||
### 图标提示 | ||
部分场景布局紧凑,无法完全展示信息内容,这时可以配合图标给出提示文案。默认是`提示 info`图标。 | ||
``` | ||
<div> | ||
<Tooltip>提示!</Tooltip> | ||
<span style={{ paddingLeft: '40px' }} /> | ||
<Tooltip icon='help'>帮助!</Tooltip> | ||
</div> | ||
``` | ||
|
||
## API | ||
```jsx previewOnly | ||
<PropTable of="tooltip" /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
export interface BadgeProps { | ||
count?: number | string; | ||
showZero?: boolean; | ||
dot?: boolean; | ||
size?: string; | ||
status?: string; | ||
overflowCount?: number; | ||
text?: string; | ||
title?: string; | ||
count?: number | string | ||
showZero?: boolean | ||
dot?: boolean | ||
size?: string | ||
status?: string | ||
overflowCount?: number | ||
text?: string | ||
title?: string | ||
} | ||
|
||
export interface IconProps { | ||
type: string, | ||
className?: string, | ||
type: string | ||
className?: string | ||
} | ||
|
||
export interface TooltipProps { | ||
iconClass?: string | ||
icon?: string | ||
iconAlign?: string | ||
onClick?: any | ||
label?: React.ReactNode, | ||
contrast?: boolean | ||
style?: string | ||
placement?: string | ||
children: React.ReactNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import * as React from 'react'; | ||
import * as PropTypes from 'prop-types'; | ||
import cn from 'classnames'; | ||
import { Overlay, Tooltip as BaseTooltip } from 'react-bootstrap'; | ||
import { TooltipProps } from '../../interface'; | ||
import './style.scss'; | ||
|
||
const Tooltip: React.FC<TooltipProps> = props => { | ||
const { | ||
iconClass, | ||
icon, | ||
iconAlign, | ||
onClick, | ||
label, | ||
contrast, | ||
style, | ||
children, | ||
placement: defaultPlacement, | ||
...extra | ||
} = props; | ||
|
||
const wrapper = React.useRef(null); | ||
const [ placement, setPlacement ] = React.useState('top'); | ||
// 类似 componentDidMount。只会在 render 后执行一次 | ||
React.useEffect(() => { | ||
const elem: any = wrapper.current; | ||
let placement = 'top'; | ||
const docElem = document.documentElement; | ||
const box = elem.getBoundingClientRect(); | ||
const elemOffsetLeft = box.left + docElem.scrollLeft; | ||
const elemOffsetTop = box.top + docElem.scrollTop; | ||
const pageWidth = document.documentElement.clientWidth; | ||
const pageHeight = document.documentElement.clientHeight; | ||
if (elemOffsetLeft > pageWidth * 0.8) { | ||
placement = 'left'; | ||
} | ||
if (elemOffsetLeft < pageWidth * 0.2) { | ||
placement = 'right'; | ||
} | ||
if (elemOffsetTop < pageHeight * 0.2) { | ||
placement = 'bottom'; | ||
} | ||
setPlacement(placement); | ||
}, []); | ||
const [ show, setShow ] = React.useState(false); | ||
const handleShow = () => setShow(true); | ||
const handleHide = () => setShow(false); | ||
|
||
const placeholder = label ? ( | ||
label | ||
) : icon ? ( | ||
<span | ||
className={`Tooltip__icon icon icon-${icon} ${iconClass}`} | ||
onClick={onClick} | ||
style={{ | ||
verticalAlign: iconAlign, | ||
}} | ||
/> | ||
) : ( | ||
undefined | ||
); | ||
return ( | ||
<div | ||
ref={wrapper} | ||
className="Tooltip" | ||
onMouseEnter={handleShow} | ||
onMouseLeave={handleHide} | ||
> | ||
{placeholder} | ||
<Overlay | ||
{...extra} | ||
placement={defaultPlacement || placement} | ||
target={wrapper.current || undefined} | ||
show={show} | ||
> | ||
<BaseTooltip | ||
id="tooltip" | ||
style={Object.assign({}, { maxWidth: 280 }, style)} | ||
className={cn({ contrast })} | ||
onMouseEnter={handleShow} | ||
onMouseLeave={handleHide} | ||
> | ||
{children} | ||
</BaseTooltip> | ||
</Overlay> | ||
</div> | ||
); | ||
} | ||
|
||
Tooltip.propTypes = { | ||
/** | ||
* 提示框的内容,子节点; | ||
**/ | ||
children: PropTypes.node.isRequired, | ||
/** | ||
* 选定的元素 | ||
**/ | ||
label: PropTypes.element, | ||
/** | ||
* 当选定的特定元素是一个图标的时候传入的参数,具体详见 icomoon; | ||
**/ | ||
icon: PropTypes.string, | ||
/** | ||
* 设置图标的垂直对齐方式,具体参见 vertical-align 的可选值; | ||
**/ | ||
iconAlign: PropTypes.string, | ||
/** | ||
* 提示框的位置,可选'top','right','bottom','left'。 | ||
* 若不传入这一属性,会根据 OverlayTrigger 的位置,自适应选取提示框的位置; | ||
**/ | ||
placement: PropTypes.string, | ||
/** | ||
* 提示框的颜色; | ||
**/ | ||
contrast: PropTypes.bool, | ||
/** | ||
* 给图标传入的其他 class; | ||
**/ | ||
iconClass: PropTypes.string, | ||
}; | ||
|
||
Tooltip.defaultProps = { | ||
icon: 'info', | ||
iconAlign: 'text-bottom', | ||
contrast: false, | ||
}; | ||
|
||
export default Tooltip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@import '../../style/variables.scss'; | ||
|
||
.Tooltip__icon { | ||
font-size: 14px; | ||
color: var(--light-steel-blue-normal); | ||
} | ||
.Tooltip { | ||
display: inline-block; | ||
} | ||
.tooltip { | ||
&.contrast { | ||
&.top > .tooltip-arrow { | ||
border-top-color: white; | ||
} | ||
&.right > .tooltip-arrow { | ||
border-right-color: white; | ||
} | ||
&.bottom > .tooltip-arrow { | ||
border-bottom-color: white; | ||
} | ||
&.left > .tooltip-arrow { | ||
border-left-color: white; | ||
} | ||
.tooltip-inner { | ||
color: black; | ||
background-color: white; | ||
box-shadow: 0 0 20px rgba(0,0,0,.3); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import Badge from './Badge'; | ||
import Icon from './Icon'; | ||
import Tooltip from './Tooltip'; | ||
|
||
export { | ||
Badge, | ||
Icon, | ||
Tooltip, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters