Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 4fb1fc2

Browse files
authored
fix(AutoControlledComponent): fix behaviour when undefined it passed (#499)
* fix(AutoControlledComponent): fix behaviour when undefined it passed * docs(CHANGELOG): add entry
1 parent 28c8307 commit 4fb1fc2

File tree

3 files changed

+7
-25
lines changed

3 files changed

+7
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased]
1919

20+
### Fixes
21+
- Fix the behaviour of `AutoControlledComponent` when `undefined` is passed as a prop value @layershifter ([#499](https://github.com/stardust-ui/react/pull/499))
22+
2023
<!--------------------------------[ v0.12.0 ]------------------------------- -->
2124
## [v0.12.0](https://github.com/stardust-ui/react/tree/v0.12.0) (2018-11-19)
2225
[Compare changes](https://github.com/stardust-ui/react/compare/v0.11.0...v0.12.0)

src/lib/AutoControlledComponent.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,10 @@ export default class AutoControlledComponent<P = {}, S = {}> extends UIComponent
171171

172172
// Solve the next state for autoControlledProps
173173
const newState = autoControlledProps.reduce((acc, prop) => {
174-
const isNextUndefined = _.isUndefined(nextProps[prop])
175-
const propWasRemoved = !_.isUndefined(this.props[prop]) && isNextUndefined
174+
const isNextDefined = !_.isUndefined(nextProps[prop])
176175

177176
// if next is defined then use its value
178-
if (!isNextUndefined) acc[prop] = nextProps[prop]
179-
// reinitialize state for props just removed / set undefined
180-
else if (propWasRemoved) acc[prop] = getAutoControlledStateValue(prop, nextProps)
177+
if (isNextDefined) acc[prop] = nextProps[prop]
181178

182179
return acc
183180
}, {})

test/specs/lib/AutoControlledComponent-test.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ describe('extending AutoControlledComponent', () => {
320320
expect(wrapper.state()).not.toHaveProperty(randomDefaultProp, randomValue)
321321
})
322322

323-
test('does not return state to default props when setting props undefined', () => {
323+
test('keeps current state value when setting props undefined', () => {
324324
consoleUtil.disableOnce()
325325

326326
const autoControlledProps = ['foo']
@@ -333,25 +333,7 @@ describe('extending AutoControlledComponent', () => {
333333
expect(wrapper.state()).toHaveProperty('foo', 'initial')
334334

335335
wrapper.setProps({ foo: undefined })
336-
337-
expect(wrapper.state()).toHaveProperty('foo', undefined)
338-
})
339-
340-
test('sets state to undefined for props passed as undefined by the parent', () => {
341-
consoleUtil.disableOnce()
342-
const props = makeProps()
343-
const autoControlledProps = _.keys(props)
344-
345-
const randomProp = _.sample(autoControlledProps)
346-
347-
TestClass = createTestClass({ autoControlledProps, state: {} })
348-
const wrapper = shallow(<TestClass {...props} />)
349-
350-
expect(wrapper.state()).toHaveProperty(randomProp)
351-
352-
wrapper.setProps({ [randomProp]: undefined })
353-
354-
expect(wrapper.state()).toHaveProperty(randomProp, undefined)
336+
expect(wrapper.state()).toHaveProperty('foo', 'initial')
355337
})
356338

357339
test('does not set state for props passed as null by the parent', () => {

0 commit comments

Comments
 (0)