@@ -6,7 +6,7 @@ import React from 'react'
66import shallowEqual from 'shallowequal'
77
88import {
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 // ----------------------------------------
0 commit comments