Skip to content

Commit

Permalink
feat: ✨ ToolTip 组件 offset 属性支持数组和对象写法 (#625)
Browse files Browse the repository at this point in the history
Closes: #560
  • Loading branch information
RYGRIT authored Sep 29, 2024
1 parent 691a7b5 commit 5092c5a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/component/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const control = () => {
| placement | Tooltip 的出现位置 | string | top / top-start / top-end / bottom / bottom-start / bottom-end / left / left-start / left-end / right / right-start / right-end | bottom | - |
| disabled | Tooltip 是否可用 | boolean | - | false | - |
| visible-arrow | 是否显示 Tooltip 箭头 | boolean | - | true | - |
| offset | 出现位置的偏移量 | number | - | 0 | - |
| offset | 出现位置的偏移量 | number | number[] | {x:0, y:0} | - | 0 | $LOWEST_VERSION$ |
| show-close | 是否显示 Tooltip 内部的关闭按钮 | boolean | - | false | - |

## Events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCurrentInstance, ref } from 'vue'
import { getRect } from '../common/util'
import { getRect, isObj } from '../common/util'

export function usePopover() {
const { proxy } = getCurrentInstance() as any
Expand Down Expand Up @@ -77,7 +77,7 @@ export function usePopover() {
| 'right'
| 'right-start'
| 'right-end',
offset: number
offset: number | number[] | Record<'x' | 'y', number>
) {
// arrow size
const arrowSize = 9
Expand All @@ -90,8 +90,20 @@ export function usePopover() {
// 左右位(横轴)对应的距离底部的距离
const horizontalY = height.value / 2

const offsetX = (verticalX - 17 > 0 ? 0 : verticalX - 25) + offset
const offsetY = (horizontalY - 17 > 0 ? 0 : horizontalY - 25) + offset
let offsetX = 0
let offsetY = 0
if (Array.isArray(offset)) {
offsetX = (verticalX - 17 > 0 ? 0 : verticalX - 25) + offset[0]
offsetY = (horizontalY - 17 > 0 ? 0 : horizontalY - 25) + (offset[1] ? offset[1] : offset[0])
} else if (isObj(offset)) {
offsetX = (verticalX - 17 > 0 ? 0 : verticalX - 25) + offset.x
offsetY = (horizontalY - 17 > 0 ? 0 : horizontalY - 25) + offset.y
} else {
offsetX = (verticalX - 17 > 0 ? 0 : verticalX - 25) + offset
offsetY = (horizontalY - 17 > 0 ? 0 : horizontalY - 25) + offset
}
// const offsetX = (verticalX - 17 > 0 ? 0 : verticalX - 25) + offset
// const offsetY = (horizontalY - 17 > 0 ? 0 : horizontalY - 25) + offset

const placements = new Map([
// 上
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ export const tooltipProps = {
* 类型:number
* 默认值:0
*/
offset: makeNumberProp(0),
// offset: makeNumberProp(0),
offset: {
// 需要支持数字、数组、对象类型
type: [Number, Array, Object] as PropType<number | Array<number> | Record<'x' | 'y', number>>,
default: 0
},

/**
* 是否使用slot来传入content内容
Expand Down

0 comments on commit 5092c5a

Please sign in to comment.