Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 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,16 @@ export interface IStackProps
* @defaultvalue false
*/
enableScopedSelectors?: boolean;

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

/**
Expand Down