Skip to content

Commit

Permalink
feat(Stack): Add deprecated flag to render null when receiving a fals…
Browse files Browse the repository at this point in the history
…y value (#28978)

* feat(Stack): Add deprecated flag to render null when receiving a falsy value.

* change file

* types change
  • Loading branch information
sopranopillow authored Aug 24, 2023
1 parent 0e33d8e commit 846a454
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat(Stack): Add deprecated flag to render null when receiving a falsy value.",
"packageName": "@fluentui/react",
"email": "esteban.230@hotmail.com",
"dependentChangeType": "patch"
}
2 changes: 2 additions & 0 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8789,6 +8789,8 @@ export interface IStackItemTokens {
export interface IStackProps extends ISlottableProps<IStackSlots>, IStyleableComponentProps<IStackProps, IStackTokens, IStackStyles>, React_2.HTMLAttributes<HTMLElement> {
as?: React_2.ElementType<React_2.HTMLAttributes<HTMLElement>>;
disableShrink?: boolean;
// @deprecated
doNotRenderFalsyValues?: boolean;
enableScopedSelectors?: boolean;
// @deprecated
gap?: number | string;
Expand Down
26 changes: 21 additions & 5 deletions packages/react/src/components/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import type { IStackComponent, IStackProps, IStackSlots } from './Stack.types';
import type { IStackItemProps } from './StackItem/StackItem.types';

const StackView: IStackComponent['view'] = props => {
const { as: RootType = 'div', disableShrink = false, enableScopedSelectors = false, wrap, ...rest } = props;
const {
as: RootType = 'div',
disableShrink = false,
// eslint-disable-next-line deprecation/deprecation
doNotRenderFalsyValues = false,
enableScopedSelectors = false,
wrap,
...rest
} = props;

warnDeprecations('Stack', props, {
gap: 'tokens.childrenGap',
Expand All @@ -18,7 +26,11 @@ const StackView: IStackComponent['view'] = props => {
padding: 'tokens.padding',
});

const stackChildren = _processStackChildren(props.children, { disableShrink, enableScopedSelectors });
const stackChildren = _processStackChildren(props.children, {
disableShrink,
enableScopedSelectors,
doNotRenderFalsyValues,
});

const nativeProps = getNativeProps<React.HTMLAttributes<HTMLDivElement>>(rest, htmlElementProperties);

Expand All @@ -40,18 +52,22 @@ const StackView: IStackComponent['view'] = props => {

function _processStackChildren(
children: React.ReactNode,
{ disableShrink, enableScopedSelectors }: { disableShrink: boolean; enableScopedSelectors: boolean },
{
disableShrink,
enableScopedSelectors,
doNotRenderFalsyValues,
}: { disableShrink: boolean; enableScopedSelectors: boolean; doNotRenderFalsyValues: boolean },
): (React.ReactChild | React.ReactFragment | React.ReactPortal)[] {
let childrenArray = React.Children.toArray(children);

childrenArray = React.Children.map(childrenArray, child => {
if (!child || !React.isValidElement(child)) {
return child;
return doNotRenderFalsyValues ? null : child;
}

if (child.type === React.Fragment) {
return child.props.children
? _processStackChildren(child.props.children, { disableShrink, enableScopedSelectors })
? _processStackChildren(child.props.children, { disableShrink, enableScopedSelectors, doNotRenderFalsyValues })
: null;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/components/Stack/Stack.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ export interface IStackProps
* @defaultvalue false
*/
enableScopedSelectors?: boolean;

/**
* When receiving a falsy value, render null instead.
*
* @deprecated Default behavior now allows rendering falsy values so cases like
* this one can happen:
* ```tsx
* <Stack>0 1 2 3 4</Stack>
* ```
*/
doNotRenderFalsyValues?: boolean;
}

/**
Expand Down

0 comments on commit 846a454

Please sign in to comment.