Skip to content

Commit

Permalink
chore(Dropdown): remove deprecated lifecycle methods (#3986)
Browse files Browse the repository at this point in the history
* chore(Dropdown): remove deprecated lifecycle methods

* fix console.error
  • Loading branch information
layershifter committed Jul 15, 2020
1 parent d7ef80a commit be8def5
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 742 deletions.
199 changes: 0 additions & 199 deletions src/lib/AutoControlledComponent.js

This file was deleted.

58 changes: 51 additions & 7 deletions src/lib/ModernAutoControlledComponent.js
Expand Up @@ -24,10 +24,50 @@
* hoisted and exposed by the HOC.
*/
import _ from 'lodash'
import { Component } from 'react'
import { getAutoControlledStateValue, getDefaultPropName } from './AutoControlledComponent'
import React from 'react'

export default class ModernAutoControlledComponent extends Component {
const getDefaultPropName = (prop) => `default${prop[0].toUpperCase() + prop.slice(1)}`

/**
* Return the auto controlled state value for a give prop. The initial value is chosen in this order:
* - regular props
* - then, default props
* - then, initial state
* - then, `checked` defaults to false
* - then, `value` defaults to '' or [] if props.multiple
* - else, undefined
*
* @param {string} propName A prop name
* @param {object} [props] A props object
* @param {object} [state] A state object
* @param {boolean} [includeDefaults=false] Whether or not to heed the default props or initial state
*/
const getAutoControlledStateValue = (propName, props, state, includeDefaults = false) => {
// regular props
const propValue = props[propName]
if (propValue !== undefined) return propValue

if (includeDefaults) {
// defaultProps
const defaultProp = props[getDefaultPropName(propName)]
if (defaultProp !== undefined) return defaultProp

// initial state - state may be null or undefined
if (state) {
const initialState = state[propName]
if (initialState !== undefined) return initialState
}
}

// React doesn't allow changing from uncontrolled to controlled components,
// default checked/value if they were not present.
if (propName === 'checked') return false
if (propName === 'value') return props.multiple ? [] : ''

// otherwise, undefined
}

export default class ModernAutoControlledComponent extends React.Component {
constructor(...args) {
super(...args)

Expand Down Expand Up @@ -147,10 +187,14 @@ export default class ModernAutoControlledComponent extends Component {
// Due to the inheritance of the AutoControlledComponent we should call its
// getAutoControlledStateFromProps() and merge it with the existing state
if (getAutoControlledStateFromProps) {
const computedState = getAutoControlledStateFromProps(props, {
...state,
...newStateFromProps,
})
const computedState = getAutoControlledStateFromProps(
props,
{
...state,
...newStateFromProps,
},
state,
)

// We should follow the idea of getDerivedStateFromProps() and return only modified state
return { ...newStateFromProps, ...computedState }
Expand Down
1 change: 0 additions & 1 deletion src/lib/index.js
@@ -1,6 +1,5 @@
import makeDebugger from './makeDebugger'

export AutoControlledComponent from './AutoControlledComponent'
export ModernAutoControlledComponent from './ModernAutoControlledComponent'
export * as childrenUtils from './childrenUtils'

Expand Down

0 comments on commit be8def5

Please sign in to comment.