Skip to content

Commit

Permalink
chore(TransitionablePortal): remove usage of UNSAFE_* methods (#3966)
Browse files Browse the repository at this point in the history
* chore(TransitionablePortal): remove usage of UNSAFE_* methods

* make behavior acc

* fix remaining issue

* remove only
  • Loading branch information
layershifter committed Jun 23, 2020
1 parent 9513785 commit ad47a6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
31 changes: 18 additions & 13 deletions src/addons/TransitionablePortal/TransitionablePortal.js
Expand Up @@ -64,23 +64,28 @@ export default class TransitionablePortal extends Component {
},
}

constructor(props) {
super(props)

this.state = {
portalOpen: props.open,
}
}
state = {}

// ----------------------------------------
// Lifecycle
// ----------------------------------------

// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps({ open }) {
debug('componentWillReceiveProps()', { open })
static getDerivedStateFromProps(props, state) {
// This is definitely a hack :(
//
// It's coupled with handlePortalClose() for force set the state of `portalOpen` omitting
// props.open. It's related to implementation of the component itself as `onClose()` will be
// called after a transition will end.
// https://github.com/Semantic-Org/Semantic-UI-React/issues/2382
if (state.portalOpen === -1) {
return { portalOpen: false }
}

this.setState({ portalOpen: open })
if (_.isUndefined(props.open)) {
return null
}

return { portalOpen: props.open }
}

// ----------------------------------------
Expand All @@ -90,7 +95,7 @@ export default class TransitionablePortal extends Component {
handlePortalClose = () => {
debug('handlePortalClose()')

this.setState({ portalOpen: false })
this.setState({ portalOpen: -1 })
}

handlePortalOpen = () => {
Expand Down Expand Up @@ -129,7 +134,7 @@ export default class TransitionablePortal extends Component {

render() {
debug('render()', this.state)

// console.log('render', this.state)
const { children, transition } = this.props
const { portalOpen, transitionVisible } = this.state

Expand Down
23 changes: 12 additions & 11 deletions test/specs/addons/TransitionablePortal/TransitionablePortal-test.js
Expand Up @@ -43,7 +43,7 @@ describe('TransitionablePortal', () => {
})
})

describe('componentWillReceiveProps', () => {
describe('getDerivedStateFromProps', () => {
it('passes `open` prop to `portalOpen` when defined', () => {
wrapperMount(<TransitionablePortal {...requiredProps} />)

Expand All @@ -64,13 +64,13 @@ describe('TransitionablePortal', () => {
describe('onClose', () => {
it('is called with (null, data) when Portal closes', (done) => {
const onClose = sandbox.spy()
const trigger = <button />

wrapperMount(
<TransitionablePortal
{...requiredProps}
onClose={onClose}
transition={quickTransition}
trigger={trigger}
trigger={<button />}
/>,
)

Expand All @@ -84,9 +84,12 @@ describe('TransitionablePortal', () => {
})

it('changes `portalOpen` to false', () => {
const trigger = <button />
wrapperMount(
<TransitionablePortal {...requiredProps} transition={quickTransition} trigger={trigger} />,
<TransitionablePortal
{...requiredProps}
transition={quickTransition}
trigger={<button />}
/>,
)

wrapper.find('button').simulate('click')
Expand Down Expand Up @@ -125,19 +128,16 @@ describe('TransitionablePortal', () => {
describe('onOpen', () => {
it('is called with (null, data) when Portal opens', () => {
const onOpen = sandbox.spy()
const trigger = <button />

wrapperMount(<TransitionablePortal {...requiredProps} onOpen={onOpen} trigger={trigger} />)
.find('button')
.simulate('click')
wrapperMount(<TransitionablePortal {...requiredProps} onOpen={onOpen} trigger={<button />} />)
wrapper.find('button').simulate('click')

onOpen.should.have.been.calledOnce()
onOpen.should.have.been.calledWithMatch(null, { portalOpen: true })
})

it('changes `portalOpen` to true', () => {
const trigger = <button />
wrapperMount(<TransitionablePortal {...requiredProps} trigger={trigger} />)
wrapperMount(<TransitionablePortal {...requiredProps} trigger={<button />} />)

wrapper.find('button').simulate('click')
wrapper.should.have.state('portalOpen', true)
Expand All @@ -148,6 +148,7 @@ describe('TransitionablePortal', () => {
it('does not block update of state on Portal close', () => {
wrapperMount(<TransitionablePortal {...requiredProps} open />)
wrapper.should.have.state('portalOpen', true)
wrapper.update()

domEvent.click(document.body)
wrapper.should.have.state('portalOpen', false)
Expand Down

0 comments on commit ad47a6b

Please sign in to comment.