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(Tabs): enforce restricted type for children #8217

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/react-core/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ export interface HorizontalOverflowObject {
toggleAriaLabel?: string;
}

type TabElement = React.ReactElement<TabProps, React.JSXElementConstructor<TabProps>>;
type TabsChild = TabElement | boolean | null | undefined;

export interface TabsProps extends Omit<React.HTMLProps<HTMLElement | HTMLDivElement>, 'onSelect'>, OUIAProps {
/** Content rendered inside the tabs component. Must be React.ReactElement<TabProps>[] */
children: React.ReactNode;
/** Content rendered inside the tabs component. Only `Tab` components or expressions resulting in a falsy value are allowed here. */
children: TabsChild | TabsChild[];
/** Additional classes added to the tabs */
className?: string;
/** Tabs background color variant */
Expand Down Expand Up @@ -205,8 +208,8 @@ export class Tabs extends React.Component<TabsProps, TabsState> {
// process any tab content sections outside of the component
if (tabContentRef) {
React.Children.toArray(this.props.children)
.map(child => child as React.ReactElement<TabProps>)
.filter(child => child.props && child.props.tabContentRef && child.props.tabContentRef.current)
.filter((child): child is TabElement => React.isValidElement(child))
.filter(({ props }) => props.tabContentRef && props.tabContentRef.current)
.forEach(child => (child.props.tabContentRef.current.hidden = true));
// most recently selected tabContent
if (tabContentRef.current) {
Expand Down Expand Up @@ -407,9 +410,9 @@ export class Tabs extends React.Component<TabsProps, TabsState> {
uncontrolledIsExpandedLocal,
overflowingTabCount
} = this.state;
const filteredChildren = (React.Children.toArray(children) as React.ReactElement<TabProps>[])
.filter(Boolean)
.filter(child => !child.props.isHidden);
const filteredChildren = React.Children.toArray(children)
.filter((child): child is TabElement => React.isValidElement(child))
.filter(({ props }) => !props.isHidden);

const filteredChildrenWithoutOverflow = filteredChildren.slice(0, filteredChildren.length - overflowingTabCount);
const filteredChildrenOverflowing = filteredChildren.slice(filteredChildren.length - overflowingTabCount);
Expand Down