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(Dropdown): remove deprecated lifecycle methods [WIP] #3956

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 0 additions & 199 deletions src/lib/AutoControlledComponent.js

This file was deleted.

42 changes: 41 additions & 1 deletion src/lib/ModernAutoControlledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,47 @@
*/
import _ from 'lodash'
import { Component } from 'react'
import { getAutoControlledStateValue, getDefaultPropName } from './AutoControlledComponent'

export 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
*/
export 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 Component {
constructor(...args) {
Expand Down
1 change: 0 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import makeDebugger from './makeDebugger'

export AutoControlledComponent from './AutoControlledComponent'
export ModernAutoControlledComponent from './ModernAutoControlledComponent'
export { getChildMapping, mergeChildMappings } from './childMapping'
export * as childrenUtils from './childrenUtils'
Expand Down
Loading