Skip to content

Commit

Permalink
fix(Stack): filter children that should not be rendered (rsuite#2732)
Browse files Browse the repository at this point in the history
* fix(Stack): remove empty child

* test(Stack): add empty child case

* fix(Stack): fix test case

* fix(Stack): fix children count

* test(Stack): change test assertion
  • Loading branch information
MarvelSQ authored and myNameIsDu committed Oct 12, 2022
1 parent 9812c95 commit 86f6969
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const Stack = React.forwardRef((props: StackProps, ref: React.Ref<HTMLDivElement
const classes = merge(className, withClassPrefix());
const isSupportGridGap = isSupportFlexGap();

const count = React.Children.count(children);
const gridGap = Array.isArray(spacing) ? spacing : [spacing, 0];
const itemStyles: React.CSSProperties = {
[rtl ? 'marginLeft' : 'marginRight']: gridGap[0],
Expand All @@ -73,12 +72,20 @@ const Stack = React.forwardRef((props: StackProps, ref: React.Ref<HTMLDivElement
...style
};

/*
* toArray remove undefined, null and boolean
*/
const filterChildren = React.Children.toArray(children);

const count = filterChildren.length;

return (
<Component {...rest} ref={ref} className={classes} style={styles}>
{React.Children.map(children as React.ReactElement[], (child, index) => {
{React.Children.map(filterChildren as React.ReactElement[], (child, index) => {
const childNode =
child.type !== StackItem ? (
<StackItem
key={index}
className={prefix('item')}
style={!isSupportGridGap ? itemStyles : undefined}
>
Expand Down
14 changes: 14 additions & 0 deletions src/Stack/test/StackSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ describe('Stack', () => {
assert.equal(getByTestId('test').children.length, 3);
assert.equal(getByTestId('test').children[1].textContent, '|');
});

it('Should not render empty child', () => {
const { getByTestId } = render(
<Stack data-testid="test">
{0}
{null}
{false}
{''}
{[1, 2]}
</Stack>
);

expect(getByTestId('test').children).to.length(4);
});
});

0 comments on commit 86f6969

Please sign in to comment.