Skip to content

Commit be8def5

Browse files
authored
chore(Dropdown): remove deprecated lifecycle methods (#3986)
* chore(Dropdown): remove deprecated lifecycle methods * fix console.error
1 parent d7ef80a commit be8def5

File tree

8 files changed

+343
-742
lines changed

8 files changed

+343
-742
lines changed

src/lib/AutoControlledComponent.js

Lines changed: 0 additions & 199 deletions
This file was deleted.

src/lib/ModernAutoControlledComponent.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,50 @@
2424
* hoisted and exposed by the HOC.
2525
*/
2626
import _ from 'lodash'
27-
import { Component } from 'react'
28-
import { getAutoControlledStateValue, getDefaultPropName } from './AutoControlledComponent'
27+
import React from 'react'
2928

30-
export default class ModernAutoControlledComponent extends Component {
29+
const getDefaultPropName = (prop) => `default${prop[0].toUpperCase() + prop.slice(1)}`
30+
31+
/**
32+
* Return the auto controlled state value for a give prop. The initial value is chosen in this order:
33+
* - regular props
34+
* - then, default props
35+
* - then, initial state
36+
* - then, `checked` defaults to false
37+
* - then, `value` defaults to '' or [] if props.multiple
38+
* - else, undefined
39+
*
40+
* @param {string} propName A prop name
41+
* @param {object} [props] A props object
42+
* @param {object} [state] A state object
43+
* @param {boolean} [includeDefaults=false] Whether or not to heed the default props or initial state
44+
*/
45+
const getAutoControlledStateValue = (propName, props, state, includeDefaults = false) => {
46+
// regular props
47+
const propValue = props[propName]
48+
if (propValue !== undefined) return propValue
49+
50+
if (includeDefaults) {
51+
// defaultProps
52+
const defaultProp = props[getDefaultPropName(propName)]
53+
if (defaultProp !== undefined) return defaultProp
54+
55+
// initial state - state may be null or undefined
56+
if (state) {
57+
const initialState = state[propName]
58+
if (initialState !== undefined) return initialState
59+
}
60+
}
61+
62+
// React doesn't allow changing from uncontrolled to controlled components,
63+
// default checked/value if they were not present.
64+
if (propName === 'checked') return false
65+
if (propName === 'value') return props.multiple ? [] : ''
66+
67+
// otherwise, undefined
68+
}
69+
70+
export default class ModernAutoControlledComponent extends React.Component {
3171
constructor(...args) {
3272
super(...args)
3373

@@ -147,10 +187,14 @@ export default class ModernAutoControlledComponent extends Component {
147187
// Due to the inheritance of the AutoControlledComponent we should call its
148188
// getAutoControlledStateFromProps() and merge it with the existing state
149189
if (getAutoControlledStateFromProps) {
150-
const computedState = getAutoControlledStateFromProps(props, {
151-
...state,
152-
...newStateFromProps,
153-
})
190+
const computedState = getAutoControlledStateFromProps(
191+
props,
192+
{
193+
...state,
194+
...newStateFromProps,
195+
},
196+
state,
197+
)
154198

155199
// We should follow the idea of getDerivedStateFromProps() and return only modified state
156200
return { ...newStateFromProps, ...computedState }

src/lib/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import makeDebugger from './makeDebugger'
22

3-
export AutoControlledComponent from './AutoControlledComponent'
43
export ModernAutoControlledComponent from './ModernAutoControlledComponent'
54
export * as childrenUtils from './childrenUtils'
65

0 commit comments

Comments
 (0)