Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(T34716): add an update method in the tooltip directory #542

Merged
merged 8 commits into from
Sep 22, 2023
47 changes: 37 additions & 10 deletions src/components/DpTooltip/utils/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { v4 as uuid } from 'uuid'
let handleShowTooltip = null
let handleHideTooltip = null
let handleTimeoutForDestroy = null
let tooltips = []

const deleteTooltip = (tooltipEl) => {
if (tooltipEl) {
tooltips.filter(el => el.id !== tooltipEl.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what for this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored it f410255

tooltipEl.remove()
}
}
Expand All @@ -32,22 +34,24 @@ const getZIndex = (element) => {
}

const hideTooltip = (tooltipEl) => {
tooltipEl.classList.add('z-below-zero')
tooltipEl.classList.add('opacity-0')
if (tooltipEl) {
tooltipEl.classList.add('z-below-zero')
tooltipEl.classList.add('opacity-0')
}

handleTimeoutForDestroy = setTimeout(() => deleteTooltip(tooltipEl), 300)
}

const createTooltip = (id, el, value, container, classes) => {
const createTooltip = (id, container, classes) => {
const tooltip = tooltips.find(el => el.id === id)
// this has to be in sync with the Template in DpTooltip
const tooltipHtml =
`<div class="tooltip absolute ${classes} z-below-zero" role="tooltip" id="${id}">` +
`<div class="tooltip absolute ${classes} z-below-zero" role="tooltip" id="${tooltip.id}">` +
`<div class="tooltip__arrow" data-tooltip-arrow></div>` +
`<div class="tooltip__inner">${value}</div>` +
`<div class="tooltip__inner">${tooltip.value}</div>` +
`</div>`

const range = document.createRange()
el.setAttribute('aria-describedby', id)

const content = range.createContextualFragment(tooltipHtml)

Expand All @@ -59,11 +63,14 @@ const initTooltip = (el, value, options) => {

const id = `tooltip-${uuid()}`
const zIndex = getZIndex(el)
const tooltipData = { id: id, value: value }

tooltips.push(tooltipData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do You use an array and not an Object?

Suggested change
tooltips.push(tooltipData)
tooltips[id] = tooltipData

with that you can avoid the loop in line 145

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored it here f410255. Use an object instead of an array

el.setAttribute('aria-describedby', id)

handleShowTooltip = () => showTooltip(
id,
el,
value,
options,
zIndex
)
Expand All @@ -75,9 +82,9 @@ const initTooltip = (el, value, options) => {
el.addEventListener('blur', handleHideTooltip)
}

const showTooltip = async (id, wrapperEl, value, { place = 'top', container = 'body', classes = '' }, zIndex) => {
const showTooltip = async (id, wrapperEl, { place = 'top', container = 'body', classes = '' }, zIndex) => {
if (!document.getElementById(wrapperEl.getAttribute('aria-describedby'))) {
createTooltip(id, wrapperEl, value, container, classes)
createTooltip(id, container, classes)
} else {
clearTimeout(handleTimeoutForDestroy)
}
Expand Down Expand Up @@ -129,4 +136,24 @@ const showTooltip = async (id, wrapperEl, value, { place = 'top', container = 'b
tooltipEl.classList.remove('opacity-0')
}

export { destroyTooltip, initTooltip }
const updateTooltip = (elWrapper, value, options) => {
if (!value) return

const wrapperId = elWrapper.getAttribute('aria-describedby')
const zIndex = getZIndex(elWrapper)

tooltips.forEach(el => {
if (wrapperId === el.id) {
el.value = value
}
})

const tooltipEl = document.getElementById(wrapperId)

if (tooltipEl) {
deleteTooltip(tooltipEl)
showTooltip(wrapperId, elWrapper, options, zIndex)
}
}

export { destroyTooltip, initTooltip, updateTooltip }
22 changes: 21 additions & 1 deletion src/directives/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VPopover as Popover } from 'v-tooltip'
import { destroyTooltip, initTooltip } from '../../components/DpTooltip/utils/tooltip'
import { destroyTooltip, initTooltip, updateTooltip } from '../../components/DpTooltip/utils/tooltip'

/**
* @deprecated Use DpTooltip instead
Expand Down Expand Up @@ -60,6 +60,26 @@ const Tooltip = {

initTooltip(element, content, options)
},

update: function (element, binding) {
let content = binding.value
let options = { place: 'top' }

if (binding.value && typeof binding.value === 'object') {
content = binding.value.content

if (binding.value.container) {
options = { ...options, container: binding.value.container }
}

if (binding.value.classes) {
options = { ...options, classes: binding.value.classes }
}
}

updateTooltip(element, content, options)
},

unbind: function (element) {
destroyTooltip(element)
}
Expand Down