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

chore(Search): replace deprecated lifecycle methods #3968

Merged
merged 2 commits into from Jun 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -72,7 +72,6 @@ export default class SearchExampleCategory extends Component {
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={8}>
Expand Down
Expand Up @@ -103,7 +103,6 @@ export default class SearchExampleCategory extends Component {
resultRenderer={resultRenderer}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={8}>
Expand Down
Expand Up @@ -47,7 +47,6 @@ export default class SearchExampleStandard extends Component {
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
Expand Down
Expand Up @@ -56,7 +56,6 @@ export default class SearchExampleStandard extends Component {
results={results}
value={value}
resultRenderer={resultRenderer}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
Expand Down
Expand Up @@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
Expand Down
Expand Up @@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
Expand Down
Expand Up @@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
Expand Down
34 changes: 14 additions & 20 deletions src/modules/Search/Search.js
Expand Up @@ -6,7 +6,7 @@ import React from 'react'
import shallowEqual from 'shallowequal'

import {
AutoControlledComponent as Component,
ModernAutoControlledComponent as Component,
customPropTypes,
eventStack,
getElementType,
Expand Down Expand Up @@ -200,25 +200,19 @@ export default class Search extends Component {
static Result = SearchResult
static Results = SearchResults

// eslint-disable-next-line camelcase
UNSAFE_componentWillMount() {
debug('componentWillMount()')
const { open, value } = this.state
static getAutoControlledStateFromProps(props, state) {
debug('getAutoControlledStateFromProps()')

this.setValue(value)
if (open) this.open()
}
// We need to store a `prevValue` to compare as in `getDerivedStateFromProps` we don't have
// prevState
if (typeof state.prevValue !== 'undefined' && shallowEqual(state.prevValue, state.value)) {
return { prevValue: state.value }
}

// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
super.UNSAFE_componentWillReceiveProps(nextProps)
debug('componentWillReceiveProps()')
debug('changed props:', objectDiff(nextProps, this.props))
const selectedIndex = props.selectFirstResult ? 0 : -1
debug('value changed, setting selectedIndex', selectedIndex)

if (!shallowEqual(nextProps.value, this.props.value)) {
debug('value changed, setting', nextProps.value)
this.setValue(nextProps.value)
}
return { prevValue: state.value, selectedIndex }
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -459,7 +453,7 @@ export default class Search extends Component {

const { selectFirstResult } = this.props

this.trySetState({ value, selectedIndex: selectFirstResult ? 0 : -1 })
this.setState({ value, selectedIndex: selectFirstResult ? 0 : -1 })
}

moveSelectionBy = (e, offset) => {
Expand Down Expand Up @@ -516,12 +510,12 @@ export default class Search extends Component {

open = () => {
debug('open()')
this.trySetState({ open: true })
this.setState({ open: true })
}

close = () => {
debug('close()')
this.trySetState({ open: false })
this.setState({ open: false })
}

// ----------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion test/specs/modules/Search/Search-test.js
Expand Up @@ -129,7 +129,7 @@ describe('Search', () => {
)
})
it('defaults to the first item with selectFirstResult', () => {
wrapperShallow(<Search results={options} minCharacters={0} selectFirstResult />)
wrapperMount(<Search results={options} minCharacters={0} selectFirstResult />)
.find('SearchResult')
.first()
.should.have.prop('active', true)
Expand Down