|
24 | 24 | * hoisted and exposed by the HOC. |
25 | 25 | */ |
26 | 26 | import _ from 'lodash' |
27 | | -import { Component } from 'react' |
28 | | -import { getAutoControlledStateValue, getDefaultPropName } from './AutoControlledComponent' |
| 27 | +import React from 'react' |
29 | 28 |
|
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 { |
31 | 71 | constructor(...args) { |
32 | 72 | super(...args) |
33 | 73 |
|
@@ -147,10 +187,14 @@ export default class ModernAutoControlledComponent extends Component { |
147 | 187 | // Due to the inheritance of the AutoControlledComponent we should call its |
148 | 188 | // getAutoControlledStateFromProps() and merge it with the existing state |
149 | 189 | 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 | + ) |
154 | 198 |
|
155 | 199 | // We should follow the idea of getDerivedStateFromProps() and return only modified state |
156 | 200 | return { ...newStateFromProps, ...computedState } |
|
0 commit comments