Skip to content

Commit bd92e01

Browse files
authored
chore(Search): replace deprecated lifecycle methods (#3968)
* chore(Search): replace deprecated lifecycle methods * add a comment
1 parent 4c0ecc7 commit bd92e01

File tree

9 files changed

+15
-28
lines changed

9 files changed

+15
-28
lines changed

docs/src/examples/modules/Search/Types/SearchExampleCategory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export default class SearchExampleCategory extends Component {
7272
})}
7373
results={results}
7474
value={value}
75-
{...this.props}
7675
/>
7776
</Grid.Column>
7877
<Grid.Column width={8}>

docs/src/examples/modules/Search/Types/SearchExampleCategoryCustom.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ export default class SearchExampleCategory extends Component {
103103
resultRenderer={resultRenderer}
104104
results={results}
105105
value={value}
106-
{...this.props}
107106
/>
108107
</Grid.Column>
109108
<Grid.Column width={8}>

docs/src/examples/modules/Search/Types/SearchExampleStandard.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export default class SearchExampleStandard extends Component {
4747
})}
4848
results={results}
4949
value={value}
50-
{...this.props}
5150
/>
5251
</Grid.Column>
5352
<Grid.Column width={10}>

docs/src/examples/modules/Search/Types/SearchExampleStandardCustom.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export default class SearchExampleStandard extends Component {
5656
results={results}
5757
value={value}
5858
resultRenderer={resultRenderer}
59-
{...this.props}
6059
/>
6160
</Grid.Column>
6261
<Grid.Column width={10}>

docs/src/examples/modules/Search/Variations/SearchExampleAligned.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
4848
})}
4949
results={results}
5050
value={value}
51-
{...this.props}
5251
/>
5352
</Grid.Column>
5453
<Grid.Column width={10}>

docs/src/examples/modules/Search/Variations/SearchExampleFluid.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
4848
})}
4949
results={results}
5050
value={value}
51-
{...this.props}
5251
/>
5352
</Grid.Column>
5453
<Grid.Column width={10}>

docs/src/examples/modules/Search/Variations/SearchExampleInput.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default class SearchExampleStandard extends Component {
4848
})}
4949
results={results}
5050
value={value}
51-
{...this.props}
5251
/>
5352
</Grid.Column>
5453
<Grid.Column width={10}>

src/modules/Search/Search.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from 'react'
66
import shallowEqual from 'shallowequal'
77

88
import {
9-
AutoControlledComponent as Component,
9+
ModernAutoControlledComponent as Component,
1010
customPropTypes,
1111
eventStack,
1212
getElementType,
@@ -200,25 +200,19 @@ export default class Search extends Component {
200200
static Result = SearchResult
201201
static Results = SearchResults
202202

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

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

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

218-
if (!shallowEqual(nextProps.value, this.props.value)) {
219-
debug('value changed, setting', nextProps.value)
220-
this.setValue(nextProps.value)
221-
}
215+
return { prevValue: state.value, selectedIndex }
222216
}
223217

224218
shouldComponentUpdate(nextProps, nextState) {
@@ -459,7 +453,7 @@ export default class Search extends Component {
459453

460454
const { selectFirstResult } = this.props
461455

462-
this.trySetState({ value, selectedIndex: selectFirstResult ? 0 : -1 })
456+
this.setState({ value, selectedIndex: selectFirstResult ? 0 : -1 })
463457
}
464458

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

517511
open = () => {
518512
debug('open()')
519-
this.trySetState({ open: true })
513+
this.setState({ open: true })
520514
}
521515

522516
close = () => {
523517
debug('close()')
524-
this.trySetState({ open: false })
518+
this.setState({ open: false })
525519
}
526520

527521
// ----------------------------------------

test/specs/modules/Search/Search-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('Search', () => {
129129
)
130130
})
131131
it('defaults to the first item with selectFirstResult', () => {
132-
wrapperShallow(<Search results={options} minCharacters={0} selectFirstResult />)
132+
wrapperMount(<Search results={options} minCharacters={0} selectFirstResult />)
133133
.find('SearchResult')
134134
.first()
135135
.should.have.prop('active', true)

0 commit comments

Comments
 (0)