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(Tooltip): ensure controlled active prop takes presence #1547

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ exports[`Dialog component snapshot should match component snapshot 1`] = `
class={null}
className={null}
fixed_position={false}
group="main"
group={null}
hide_delay={500}
id="dialog_id-tooltip"
no_animation={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ exports[`Drawer component snapshot should match component snapshot 1`] = `
class={null}
className={null}
fixed_position={false}
group="main"
group={null}
hide_delay={500}
id="drawer_id-tooltip"
no_animation={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ exports[`Modal component have to match snapshot 1`] = `
class={null}
className={null}
fixed_position={false}
group="main"
group={null}
hide_delay={500}
id="modal_id-tooltip"
no_animation={false}
Expand Down
4 changes: 2 additions & 2 deletions packages/dnb-eufemia/src/components/tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class Tooltip extends React.PureComponent {

static defaultProps = {
id: null,
group: 'main',
group: null,
size: 'basis',
active: null,
position: 'top',
Expand Down Expand Up @@ -176,7 +176,7 @@ export default class Tooltip extends React.PureComponent {
...this.props,
...inherited,
internal_id: this._id,
group: this.props.id || group,
group: this.props.id || group || 'main-' + this._id,
}
if (newProps.active === null) {
delete newProps.active
Expand Down
25 changes: 15 additions & 10 deletions packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class TooltipPortal extends React.PureComponent {
static defaultProps = {
internal_id: null,
active: false,
group: 'main',
group: null,
hide_delay: 500,
}

Expand All @@ -46,7 +46,7 @@ export default class TooltipPortal extends React.PureComponent {

tooltipPortal[group].count++

this.setState({ isMounted: true, active }, () => {
this.setState({ isMounted: true, isActive: active }, () => {
if (!this.isMainGorup()) {
this.renderPortal()
}
Expand All @@ -64,16 +64,22 @@ export default class TooltipPortal extends React.PureComponent {
componentDidUpdate(prevProps) {
const { group, active, hide_delay } = this.props

if (this.props.children !== prevProps.children) {
this.renderPortal()
}

if (tooltipPortal[group] && active !== prevProps.active) {
clearTimeout(tooltipPortal[group].timeout)

if (active && !prevProps.active) {
this.setState({ active: true }, () => {
this.setState({ isActive: true }, () => {
if (!this.isMainGorup()) {
this.renderPortal()
}
})
} else if (!active && prevProps.active) {
this.timeout = tooltipPortal[group].timeout = setTimeout(() => {
this.setState({ active: false }, () => {
tooltipPortal[group].timeout = setTimeout(() => {
this.setState({ isActive: false }, () => {
if (!this.isMainGorup()) {
this.renderPortal()
}
Expand All @@ -85,18 +91,17 @@ export default class TooltipPortal extends React.PureComponent {

isMainGorup() {
const { group } = this.props
return group === 'main'
return group.includes('main')
}

componentWillUnmount() {
const { group } = this.props

clearTimeout(this.timeout)

if (tooltipPortal[group]) {
tooltipPortal[group].count--

if (!this.isMainGorup()) {
clearTimeout(tooltipPortal[group].timeout)
ReactDOM.unmountComponentAtNode(tooltipPortal[group].node)
}

Expand Down Expand Up @@ -167,7 +172,7 @@ export default class TooltipPortal extends React.PureComponent {
<TooltipContainer
targetElement={targetElement}
{...this.props}
active={this.state.active}
active={this.state.isActive}
/>,
tooltipPortal[group].node
)
Expand All @@ -176,7 +181,7 @@ export default class TooltipPortal extends React.PureComponent {
<TooltipContainer
targetElement={targetElement}
{...this.props}
active={this.state.active}
active={this.state.isActive}
/>,
tooltipPortal[group].node
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,55 @@ describe('Tooltip', () => {
const Comp = mount(<Component active />)
expect(await axeComponent(Comp)).toHaveNoViolations()
})

it('should show when active prop is true', async () => {
const Component = () => {
const [active, setActive] = React.useState(false)

return (
<Tooltip
active={active}
show_delay={1}
hide_delay={1}
target_element={
<button
onMouseEnter={() => {
setActive(true)
}}
onMouseLeave={() => {
setActive(false)
}}
>
Text
</button>
}
>
Tooltip
</Tooltip>
)
}

const Comp = mount(<Component />)

const mainElem = document.body.querySelector('.dnb-tooltip')

Comp.find('button').simulate('mouseenter')

expect(mainElem.classList.contains('dnb-tooltip--active')).toBe(true)

Comp.find('button').simulate('mouseleave')
Comp.find('button').simulate('mouseenter')

await wait(2)

expect(mainElem.classList.contains('dnb-tooltip--active')).toBe(true)

Comp.find('button').simulate('mouseleave')

await wait(2)

expect(mainElem.classList.contains('dnb-tooltip--hide')).toBe(true)
})
})

describe('Anchor with tooltip', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ exports[`Tooltip with target_element have to match default tooltip snapshot 1`]
class={null}
className={null}
fixed_position={false}
group="main"
group={null}
hide_delay={0}
id="tooltip"
no_animation={false}
Expand Down