FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor(Tabs): move TabButton to new file by tlabaj · Pull Request #3033 · patternfly/patternfly-react · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .snap  (3) .tsx  (5) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
    • Tab.test.tsx
    • Tab.tsx
    • TabButton.test.tsx
    • TabButton.tsx
    • Tabs.tsx
      • Tab.test.tsx.snap
      • TabButton.test.tsx.snap
      • Tabs.test.tsx.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { Tab } from './Tab';

test('should render tab', () => {
const view = shallow(
<Tab eventKey={0} title="Tab item 1">
Tab 1 section
</Tab>
);
expect(view).toMatchSnapshot();
});

test('should render active tab', () => {
test('should not render anything', () => {
const view = shallow(
<Tab eventKey={0} title="Tab item 1">
Tab 1 section
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,10 @@ export interface TabProps extends Omit<React.HTMLProps<HTMLAnchorElement | HTMLB
tabContentRef?: React.RefObject<any>;
}

const Tab0: React.FunctionComponent<TabProps> = ({
children,
eventKey,
export const Tab: React.FunctionComponent<TabProps> = ({
className = '',
tabContentId,
tabContentRef,
title,
...props
}: TabProps) => {
// destructuring to prevent console warnings for applying eventKey, and tabContentId to a DOM element and remove title from the DOM element
const Component = (props.href ? 'a' : 'button') as any;
return (
<Component {...props} className={className} ref={tabContentRef}>
{children}
</Component>
null
);
};

interface ForwardedRefProps extends TabProps {
forwardRef?: React.Ref<any>;
}

const withForwardedRef = (Component: any) => {
class TabContainer extends React.Component<ForwardedRefProps> {
render() {
const { forwardRef, ...rest } = this.props;
return <Component ref={forwardRef} {...rest} />;
}
}
return React.forwardRef((props: any, tabContentRef) => <TabContainer {...props} forwardRef={tabContentRef} />);
};

export const Tab = withForwardedRef(Tab0);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { shallow } from 'enzyme';
import { TabButton } from './TabButton';

test('should render tab button', () => {
const view = shallow(
<TabButton eventKey={0} >
Tab button
</TabButton>
);
expect(view).toMatchSnapshot();
});
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';

export interface TabButtonProps extends React.HTMLProps<HTMLAnchorElement | HTMLButtonElement> {
/** content rendered inside the Tab content area. */
children?: React.ReactNode;
/** additional classes added to the Tab */
className?: string;
/** URL associated with the Tab. A Tab with an href will render as an <a> instead of a <button>. A Tab inside a <Tabs variant="nav"> should have an href. */
href?: string;
/** child reference for case in which a TabContent section is defined outside of a Tabs component */
tabContentRef?: React.RefObject<any>;
}

const TabButtonWithRef: React.FunctionComponent<TabButtonProps> = ({
children,
className = '',
tabContentRef,
...props
}: TabButtonProps) => {
const Component = (props.href ? 'a' : 'button') as any;
return (
<Component {...props} className={className} ref={tabContentRef}>
{children}
</Component>
);
}

interface ForwardedRefProps extends TabButtonProps {
forwardRef?: React.Ref<any>;
}

const withForwardedRef = (Component: any) => {
class TabContainer extends React.Component<ForwardedRefProps> {
render() {
const { forwardRef, ...rest } = this.props;
return <Component ref={forwardRef} {...rest} />;
}
}
return React.forwardRef((props: any, tabContentRef) => <TabContainer {...props} forwardRef={tabContentRef} />);
};

export const TabButton = withForwardedRef(TabButtonWithRef);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AngleLeftIcon, AngleRightIcon } from '@patternfly/react-icons';
import { getUniqueId, isElementInView, sideElementIsOutOfView } from '../../helpers/util';
import { SIDE } from '../../helpers/constants';
import { Tab } from './Tab';
import { TabButton } from './TabButton';
import { TabContent } from './TabContent';
import { InjectedOuiaProps, withOuiaContext } from '../withOuia';

Expand Down Expand Up @@ -250,21 +251,18 @@ class Tabs extends React.Component<TabsProps & InjectedOuiaProps, TabsState> {
key={index}
className={css(styles.tabsItem, eventKey === activeKey && styles.modifiers.current, className)}
>
<Tab
<TabButton
className={css(styles.tabsButton)}
onClick={(event: any) => this.handleTabClick(event, eventKey, tabContentRef, mountOnEnter)}
id={`pf-tab-${eventKey}-${childId || uniqueId}`}
aria-controls={
tabContentId ? `${tabContentId}` : `pf-tab-section-${eventKey}-${childId || uniqueId}`
}
tabContentId={tabContentId}
tabContentRef={tabContentRef}
eventKey={eventKey}
title={title}
{...rest}
>
{title}
</Tab>
</TabButton>
</li>
);
})}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render active tab 1`] = `
<TabContainer
eventKey={0}
forwardRef={null}
title="Tab item 1"
>
Tab 1 section
</TabContainer>
`;

exports[`should render tab 1`] = `
<TabContainer
eventKey={0}
forwardRef={null}
title="Tab item 1"
>
Tab 1 section
</TabContainer>
`;
exports[`should not render anything 1`] = `""`;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render tab button 1`] = `
<TabContainer
eventKey={0}
forwardRef={null}
>
Tab button
</TabContainer>
`;
Loading

Back | FazBrowse Home | New Git URL