Skip to content

Commit

Permalink
fix(SidebarE): Use explicit refs with Transition
Browse files Browse the repository at this point in the history
Stops a deprecation warning for `findDOMNode` usage being throwin in React strict mode.
  • Loading branch information
m7kvqbe1 committed May 17, 2021
1 parent dff5cf0 commit 6aa14b2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/react-component-library/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { format } from 'util'
const originalConsoleError = global.console.error

global.console.error = (...args) => {
const reactFailures = [/Failed prop type/, /Warning: /]
const reactFailures = [/Failed prop type/, /Warning: /, /StrictMode/]

if (reactFailures.some((p) => p.test(args[0]))) {
originalConsoleError(...args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ const notifications = (
describe('SidebarE', () => {
let wrapper: RenderResult

describe('when rendered in React strict mode', () => {
it('should not throw deprecation warning about findDOMNode', () => {
expect(() => {
wrapper = render(
<React.StrictMode>
<SidebarE />
</React.StrictMode>
)

fireEvent.mouseOver(wrapper.getByTestId('sidebar'))
}).not.toThrowError(/findDOMNode is deprecated in StrictMode/)
})
})

describe('when composed with minimal props', () => {
beforeEach(() => {
wrapper = render(<SidebarE />)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useRef } from 'react'
import { Transition } from 'react-transition-group'

import { SidebarHandle } from './SidebarHandle'
Expand Down Expand Up @@ -45,6 +45,8 @@ export const SidebarE: React.FC<SidebarEProps> = ({
notifications,
...rest
}) => {
const nodeRef = useRef(null)

return (
<SidebarProvider>
<SidebarContext.Consumer>
Expand All @@ -56,9 +58,17 @@ export const SidebarE: React.FC<SidebarEProps> = ({
onMouseLeave={(_) => setHasMouseOver(false)}
{...rest}
>
<Transition in={hasMouseOver} timeout={0} unmountOnExit>
<Transition
nodeRef={nodeRef}
in={hasMouseOver}
timeout={0}
unmountOnExit
>
{(state) => (
<SidebarHandle style={{ ...TRANSITION_STYLES[state] }} />
<SidebarHandle
ref={nodeRef}
style={{ ...TRANSITION_STYLES[state] }}
/>
)}
</Transition>
{title && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import React, { useContext } from 'react'
import React, { useContext, forwardRef } from 'react'
import { IconChevronLeft, IconChevronRight } from '@royalnavy/icon-library'

import { ComponentWithClass } from '../../../common/ComponentWithClass'
import { SidebarContext } from './context'
import { StyledHandle } from './partials/StyledHandle'

interface SidebarHandleProps extends ComponentWithClass {
style?: React.CSSProperties
style: React.CSSProperties
}

export const SidebarHandle: React.FC<SidebarHandleProps> = (props) => {
const { isOpen, setIsOpen } = useContext(SidebarContext)
export const SidebarHandle = forwardRef(
(props: SidebarHandleProps, ref?: React.Ref<HTMLButtonElement>) => {
const { isOpen, setIsOpen } = useContext(SidebarContext)

function handleClick(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault()
setIsOpen(!isOpen)
}
function handleClick(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault()
setIsOpen(!isOpen)
}

return (
<StyledHandle
onClick={handleClick}
aria-label={`${isOpen ? 'Collapse' : 'Expand'} sidebar`}
data-testid="sidebar-handle"
{...props}
>
{isOpen ? <IconChevronLeft /> : <IconChevronRight />}
</StyledHandle>
)
}
return (
<StyledHandle
ref={ref}
onClick={handleClick}
aria-label={`${isOpen ? 'Collapse' : 'Expand'} sidebar`}
data-testid="sidebar-handle"
{...props}
>
{isOpen ? <IconChevronLeft /> : <IconChevronRight />}
</StyledHandle>
)
}
)

SidebarHandle.displayName = 'SidebarHandle'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext } from 'react'
import React, { useState, useContext, useRef } from 'react'
import { Transition } from 'react-transition-group'

import { SidebarSubNav } from './SidebarSubNav'
Expand Down Expand Up @@ -34,15 +34,22 @@ export const SidebarNavItemE: React.FC<SidebarNavItemEProps> = ({
const [hasMouseOver, setHasMouseOver] = useState(false)
const { isOpen } = useContext(SidebarContext)
const linkElement = link as React.ReactElement
const nodeRef = useRef(null)

const item = React.cloneElement(linkElement, {
...link.props,
children: (
<>
{icon && <StyledNavItemIcon isOpen={isOpen}>{icon}</StyledNavItemIcon>}
<Transition in={isOpen} timeout={TRANSITION_TIMEOUT} unmountOnExit>
<Transition
nodeRef={nodeRef}
in={isOpen}
timeout={TRANSITION_TIMEOUT}
unmountOnExit
>
{(state) => (
<StyledNavItemText
ref={nodeRef}
style={{ ...TRANSITION_STYLES[state] }}
isOpen={isOpen}
data-testid="sidebar-nav-item-text"
Expand Down

0 comments on commit 6aa14b2

Please sign in to comment.