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

Commit d4c7292

Browse files
authored
fix(Flex): handle cases of falsy values provided as children (#890)
* add null checks * address flex gap issue * improve null detection logic * update changelog * upd changelog * fix changelog * upd * improve expressiveness of filtering expression
1 parent 272d18e commit d4c7292

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

CHANGELOG.md

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

1818
## [Unreleased]
1919

20+
### Features
21+
- Export `arrow-up`,`arrow-down` and `chat` SVG icon @VyshnaviDasari ([#873](https://github.com/stardust-ui/react/pull/873))
22+
23+
### Fixes
24+
- Properly handle falsy values provided as `Flex` and `Flex.Item` children @kuzhelov ([#890](https://github.com/stardust-ui/react/pull/890))
25+
2026
<!--------------------------------[ v0.21.0 ]------------------------------- -->
2127
## [v0.21.0](https://github.com/stardust-ui/react/tree/v0.21.0) (2019-02-12)
2228
[Compare changes](https://github.com/stardust-ui/react/compare/v0.20.0...v0.21.0)
@@ -83,7 +89,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
8389
- Add sample screener tests with steps for `Dropdown` @silviuavram ([#797](https://github.com/stardust-ui/react/pull/797))
8490
- Add shorthand support for `triggerButton` in `Dropdown` @silviuavram ([#815](https://github.com/stardust-ui/react/pull/815))
8591
- Add toggle functionality in the `Popoup` even if the `trigger` is not button @kolaps33 ([#758](https://github.com/stardust-ui/react/pull/758))
86-
- Export `arrow-up`,`arrow-down` and `chat` SVG icon @VyshnaviDasari ([#873](https://github.com/stardust-ui/react/pull/873))
8792

8893
### Fixes
8994
- Handle `onClick` and `onFocus` on ListItems correctly @layershifter ([#779](https://github.com/stardust-ui/react/pull/779))

packages/react/src/components/Flex/Flex.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,27 @@ class Flex extends UIComponent<ReactProps<FlexProps>> {
9090
renderChildren = (gapClasses: string) => {
9191
const { column, gap, children } = this.props
9292

93-
return React.Children.map(children, (child: React.ReactElement<any>, index) => {
94-
const childElement =
95-
child.type && ((child.type as any) as typeof FlexItem).__isFlexItem
96-
? React.cloneElement(child, {
97-
flexDirection: column ? 'column' : 'row',
98-
})
99-
: child
100-
101-
const renderGap = index !== 0
93+
let isFirstElement = true
94+
return React.Children.map(children, (child: any) => {
95+
const isFlexItemElement: boolean = _.get(child, 'type.__isFlexItem')
96+
const maybeChildElement = isFlexItemElement
97+
? React.cloneElement(child, {
98+
flexDirection: column ? 'column' : 'row',
99+
})
100+
: child
101+
102+
const renderGap = !isFirstElement
103+
if (maybeChildElement) {
104+
isFirstElement = false
105+
}
106+
102107
return (
103-
<>
104-
{renderGap && gap && <Flex.Gap className={cx(`${Flex.className}__gap`, gapClasses)} />}
105-
{childElement}
106-
</>
108+
maybeChildElement && (
109+
<>
110+
{renderGap && gap && <Flex.Gap className={cx(`${Flex.className}__gap`, gapClasses)} />}
111+
{maybeChildElement}
112+
</>
113+
)
107114
)
108115
})
109116
}

packages/react/src/components/Flex/FlexItem.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import * as PropTypes from 'prop-types'
33
import cx from 'classnames'
4+
import * as _ from 'lodash'
45
import { UIComponent, commonPropTypes } from '../../lib'
56
import { mergeStyles } from '../../lib/mergeThemes'
67
import { Extendable } from '../../types'
@@ -78,7 +79,9 @@ class FlexItem extends UIComponent<Extendable<FlexItemProps>> {
7879
})
7980
}
8081

81-
return applyStyles(React.Children.only(children), styles, classes)
82+
return _.isNil(children)
83+
? children
84+
: applyStyles(React.Children.only(children), styles, classes)
8285
}
8386
}
8487

0 commit comments

Comments
 (0)