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

Use export * in component indexes and subcomponent reexports #2625

Merged
merged 6 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
### Code quality

- Converted `/tests/build.test.js` to TypeScript ([#2617](https://github.com/Shopify/polaris-react/pull/2617))
- Use `export *` to rexport component content in component indexs and subcomponent listings ([#2625](https://github.com/Shopify/polaris-react/pull/2625))

### Deprecations
2 changes: 1 addition & 1 deletion src/components/AccountConnection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {AccountConnection, AccountConnectionProps} from './AccountConnection';
export * from './AccountConnection';
2 changes: 1 addition & 1 deletion src/components/ActionList/components/Item/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Item, ItemProps} from './Item';
export * from './Item';
2 changes: 1 addition & 1 deletion src/components/ActionList/components/Section/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Section, SectionProps} from './Section';
export * from './Section';
4 changes: 2 additions & 2 deletions src/components/ActionList/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {Item, ItemProps} from './Item';
export * from './Item';

export {Section, SectionProps} from './Section';
export * from './Section';
2 changes: 1 addition & 1 deletion src/components/ActionList/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {ActionList, ActionListProps} from './ActionList';
export * from './ActionList';
2 changes: 1 addition & 1 deletion src/components/ActionMenu/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function hasGroupsWithActions(groups: ActionMenuProps['groups'] = []) {
: groups.some((group) => group.actions.length > 0);
}

export function convertGroupToSection({
function convertGroupToSection({
title,
actions,
}: MenuGroupDescriptor): ActionListSection {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ActionMenu/components/MenuAction/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {MenuAction, MenuActionProps} from './MenuAction';
export * from './MenuAction';
2 changes: 1 addition & 1 deletion src/components/ActionMenu/components/MenuGroup/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {MenuGroup, MenuGroupProps} from './MenuGroup';
export * from './MenuGroup';
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {RollupActions, RollupActionsProps} from './RollupActions';
export * from './RollupActions';
6 changes: 3 additions & 3 deletions src/components/ActionMenu/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {MenuAction, MenuActionProps} from './MenuAction';
export * from './MenuAction';

export {MenuGroup, MenuGroupProps} from './MenuGroup';
export * from './MenuGroup';

export {RollupActions, RollupActionsProps} from './RollupActions';
export * from './RollupActions';
2 changes: 1 addition & 1 deletion src/components/ActionMenu/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {ActionMenu, ActionMenuProps, hasGroupsWithActions} from './ActionMenu';
export * from './ActionMenu';
12 changes: 4 additions & 8 deletions src/components/ActionMenu/tests/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {mountWithAppProvider, trigger} from 'test-utilities/legacy';

import {MenuGroupDescriptor, ActionListItemDescriptor} from '../../../types';
import {MenuAction, MenuGroup, RollupActions} from '../components';
import {
ActionMenu,
ActionMenuProps,
convertGroupToSection,
} from '../ActionMenu';
import {ActionMenu, ActionMenuProps} from '../ActionMenu';

describe('<ActionMenu />', () => {
const mockProps: ActionMenuProps = {
Expand Down Expand Up @@ -72,9 +68,9 @@ describe('<ActionMenu />', () => {
});

it('renders as <RollupActions /> `sections` when `rollup` is `true`', () => {
const convertedSections = mockGroups.map((group) =>
convertGroupToSection(group),
);
const convertedSections = mockGroups.map((group) => {
return {title: group.title, items: group.actions};
});
const wrapper = mountWithAppProvider(
<ActionMenu {...mockProps} groups={mockGroups} rollup />,
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/AfterInitialMount/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {AfterInitialMount} from './AfterInitialMount';
export * from './AfterInitialMount';
2 changes: 1 addition & 1 deletion src/components/AppProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {AppProvider, AppProviderProps} from './AppProvider';
export * from './AppProvider';
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface State {
popoverWasActive: boolean;
}

export interface ComboBoxProps {
interface ComboBoxProps {
/** A unique identifier for the ComboBox */
id?: string;
/** Collection of options to be listed */
Expand Down
2 changes: 1 addition & 1 deletion src/components/Autocomplete/components/ComboBox/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {ComboBox} from './ComboBox';
export * from './ComboBox';
2 changes: 1 addition & 1 deletion src/components/Autocomplete/components/TextField/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {TextField} from './TextField';
export * from './TextField';
4 changes: 2 additions & 2 deletions src/components/Autocomplete/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {ComboBox} from './ComboBox';
export * from './ComboBox';

export {TextField} from './TextField';
export * from './TextField';
2 changes: 1 addition & 1 deletion src/components/Autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Autocomplete, AutocompleteProps} from './Autocomplete';
export * from './Autocomplete';
4 changes: 2 additions & 2 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {Image} from '../Image';
import styles from './Avatar.scss';
import * as avatars from './images';

export type Size = 'small' | 'medium' | 'large';
type Size = 'small' | 'medium' | 'large';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sort of thing crops up a lot. Using this particular case as an example, Size was exported here but was never reexported by src/Avatar/index.ts and was never used in any other file, so there is no value in exporting it at all.


enum Status {
Pending = 'PENDING',
Loaded = 'LOADED',
Errored = 'ERRORED',
}

export const STYLE_CLASSES = ['one', 'two', 'three', 'four', 'five'];
const STYLE_CLASSES = ['one', 'two', 'three', 'four', 'five'];
const AVATAR_IMAGES = Object.keys(avatars).map(
// import/namespace does not allow computed values by default
// eslint-disable-next-line import/namespace
Expand Down
2 changes: 1 addition & 1 deletion src/components/Avatar/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Avatar, AvatarProps} from './Avatar';
export * from './Avatar';
5 changes: 0 additions & 5 deletions src/components/Avatar/tests/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react';
import {mountWithAppProvider} from 'test-utilities/legacy';
import {mountWithApp} from 'test-utilities';
import {Avatar, Image} from 'components';
import {STYLE_CLASSES} from '../Avatar';

describe('<Avatar />', () => {
describe('intials', () => {
Expand Down Expand Up @@ -116,10 +115,6 @@ describe('<Avatar />', () => {
});

describe('styleClass', () => {
it('defaults to five styles', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing an internal implementation detail, I didn't want to expose STYLE_CLASSES so it was easiest to remove this test instead of moving files around to avoid exposing STYLE_CLASSES

expect(STYLE_CLASSES).toHaveLength(5);
});

it('renders a sixth style when unstableGlobalTheming is false', () => {
const avatar = mountWithApp(<Avatar name="e" />, {
features: {unstableGlobalTheming: false},
Expand Down
2 changes: 1 addition & 1 deletion src/components/Backdrop/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Backdrop, BackdropProps} from './Backdrop';
export * from './Backdrop';
6 changes: 3 additions & 3 deletions src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {useI18n} from '../../utilities/i18n';
import {VisuallyHidden} from '../VisuallyHidden';
import styles from './Badge.scss';

export type Status = 'success' | 'info' | 'attention' | 'warning' | 'new';
export type Progress = 'incomplete' | 'partiallyComplete' | 'complete';
export type Size = 'small' | 'medium';
type Status = 'success' | 'info' | 'attention' | 'warning' | 'new';
type Progress = 'incomplete' | 'partiallyComplete' | 'complete';
type Size = 'small' | 'medium';

export interface BadgeProps {
/** The content to display inside the badge. */
Expand Down
9 changes: 1 addition & 8 deletions src/components/Badge/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export {
Badge,
Progress,
BadgeProps,
Status,
PROGRESS_LABELS,
STATUS_LABELS,
} from './Badge';
export * from './Badge';
20 changes: 11 additions & 9 deletions src/components/Badge/tests/Badge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
// eslint-disable-next-line no-restricted-imports
import {mountWithAppProvider} from 'test-utilities/legacy';
import {VisuallyHidden} from 'components';
import {Badge, Status, Progress, PROGRESS_LABELS, STATUS_LABELS} from '..';
import {Badge, PROGRESS_LABELS, STATUS_LABELS} from '..';

describe('<Badge />', () => {
it('renders its children', () => {
Expand All @@ -12,20 +12,22 @@ describe('<Badge />', () => {
});

it('accepts a status prop and renders a visually hidden label', () => {
Object.keys(STATUS_LABELS).forEach((key: Status) => {
Object.keys(STATUS_LABELS).forEach((key: keyof typeof STATUS_LABELS) => {
const badge = mountWithAppProvider(<Badge status={STATUS_LABELS[key]} />);
expect(badge.find(VisuallyHidden).exists()).toBe(true);
badge.unmount();
});
});

it('accepts a progress prop and renders a visually hidden label', () => {
Object.keys(PROGRESS_LABELS).forEach((key: Progress) => {
const badge = mountWithAppProvider(
<Badge progress={PROGRESS_LABELS[key]} />,
);
expect(badge.find(VisuallyHidden).exists()).toBe(true);
badge.unmount();
});
Object.keys(PROGRESS_LABELS).forEach(
(key: keyof typeof PROGRESS_LABELS) => {
const badge = mountWithAppProvider(
<Badge progress={PROGRESS_LABELS[key]} />,
);
expect(badge.find(VisuallyHidden).exists()).toBe(true);
badge.unmount();
},
);
});
});
2 changes: 1 addition & 1 deletion src/components/Banner/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Banner, BannerProps, BannerStatus} from './Banner';
export * from './Banner';
2 changes: 1 addition & 1 deletion src/components/Breadcrumbs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Breadcrumbs, BreadcrumbsProps} from './Breadcrumbs';
export * from './Breadcrumbs';
8 changes: 4 additions & 4 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {IconProps} from '../../types';
import {Spinner} from '../Spinner';
import styles from './Button.scss';

export type Size = 'slim' | 'medium' | 'large';
type Size = 'slim' | 'medium' | 'large';

export type TextAlign = 'left' | 'right' | 'center';
type TextAlign = 'left' | 'right' | 'center';

export type IconSource = IconProps['source'];
type IconSource = IconProps['source'];

export interface ButtonProps {
/** The content to display inside the button */
Expand Down Expand Up @@ -274,7 +274,7 @@ export function Button({
);
}

export function IconWrapper({children}: any) {
function IconWrapper({children}: any) {
return <span className={styles.Icon}>{children}</span>;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/Button/tests/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {PlusMinor} from '@shopify/polaris-icons';
import {mountWithAppProvider, trigger} from 'test-utilities/legacy';
import {mountWithApp} from 'test-utilities';
import {UnstyledLink, Icon, Spinner} from 'components';
import {Button, IconWrapper} from '../Button';
import {Button} from '../Button';

describe('<Button />', () => {
describe('url', () => {
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('<Button />', () => {

it('does not render the markup for the icon if none is provided', () => {
const button = mountWithAppProvider(<Button />);
expect(button.find(IconWrapper).exists()).toBe(false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IconWrapper shouldn't be exposed outside of Button.

expect(button.find('svg').exists()).toBe(false);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/ButtonGroup/components/Item/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Item, ItemProps} from './Item';
export * from './Item';
2 changes: 1 addition & 1 deletion src/components/ButtonGroup/components/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Item, ItemProps} from './Item';
export * from './Item';
2 changes: 1 addition & 1 deletion src/components/ButtonGroup/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {ButtonGroup, ButtonGroupProps} from './ButtonGroup';
export * from './ButtonGroup';
2 changes: 1 addition & 1 deletion src/components/CalloutCard/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {CalloutCard, CalloutCardProps} from './CalloutCard';
export * from './CalloutCard';
2 changes: 1 addition & 1 deletion src/components/Caption/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Caption, CaptionProps} from './Caption';
export * from './Caption';
2 changes: 1 addition & 1 deletion src/components/Card/components/Header/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Header, HeaderProps} from './Header';
export * from './Header';
2 changes: 1 addition & 1 deletion src/components/Card/components/Section/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Section, SectionProps} from './Section';
export * from './Section';
2 changes: 1 addition & 1 deletion src/components/Card/components/Subsection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Subsection, SubsectionProps} from './Subsection';
export * from './Subsection';
6 changes: 3 additions & 3 deletions src/components/Card/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {Header, HeaderProps} from './Header';
export * from './Header';

export {Section, SectionProps} from './Section';
export * from './Section';

export {Subsection, SubsectionProps} from './Subsection';
export * from './Subsection';
2 changes: 1 addition & 1 deletion src/components/Card/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Card, CardProps} from './Card';
export * from './Card';
4 changes: 1 addition & 3 deletions src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {Error, Key, CheckboxHandles} from '../../types';

import styles from './Checkbox.scss';

export interface BaseProps {
export interface CheckboxProps {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crops up in a few files - a BaseProps interface and the component's props that extend from that without adding anything news - so remove BaseProps.

/** Indicates the ID of the element that describes the checkbox*/
ariaDescribedBy?: string;
/** Label for the checkbox */
Expand Down Expand Up @@ -40,8 +40,6 @@ export interface BaseProps {
onBlur?(): void;
}

export interface CheckboxProps extends BaseProps {}

export const Checkbox = React.forwardRef<CheckboxHandles, CheckboxProps>(
function Checkbox(
{
Expand Down
2 changes: 1 addition & 1 deletion src/components/Checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Checkbox, CheckboxProps} from './Checkbox';
export * from './Checkbox';
2 changes: 1 addition & 1 deletion src/components/Choice/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Choice, ChoiceProps, helpTextID} from './Choice';
export * from './Choice';
4 changes: 1 addition & 3 deletions src/components/ChoiceList/ChoiceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Error} from '../../types';

import styles from './ChoiceList.scss';

export interface ChoiceDescriptor {
interface Choice {
/** Value of the choice */
value: string;
/** Label for the choice */
Expand All @@ -25,8 +25,6 @@ export interface ChoiceDescriptor {
renderChildren?(isSelected: boolean): React.ReactNode | false;
}

export type Choice = ChoiceDescriptor;

export interface ChoiceListProps {
/** Label for list of choices */
title: React.ReactNode;
Expand Down
4 changes: 2 additions & 2 deletions src/components/ChoiceList/tests/ChoiceList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {mountWithApp} from 'test-utilities';
// eslint-disable-next-line no-restricted-imports
import {mountWithAppProvider, ReactWrapper} from 'test-utilities/legacy';
import {RadioButton, Checkbox, InlineError, errorTextID} from 'components';
import {ChoiceList, ChoiceDescriptor} from '../ChoiceList';
import {ChoiceList, ChoiceListProps} from '../ChoiceList';

describe('<ChoiceList />', () => {
let choices: ChoiceDescriptor[];
let choices: ChoiceListProps['choices'];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where possible we should look at the values that live on the Props interface instead of exposing the interfaces the Props use


beforeEach(() => {
choices = [
Expand Down
12 changes: 6 additions & 6 deletions src/components/Collapsible/Collapsible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface CollapsibleProps {
children?: React.ReactNode;
}

export type AnimationState =
type AnimationState =
| 'idle'
| 'measuring'
| 'closingStart'
Expand All @@ -34,7 +34,7 @@ interface State {

const ParentCollapsibleExpandingContext = createContext(false);

class Collapsible extends React.Component<CollapsibleProps, State> {
class CollapsibleInner extends React.Component<CollapsibleProps, State> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill off cases where we export a default value - now things that use AppProvider are exported with a named component, which means that the component index file doesn't need to rename the default export to a named export

static contextType = ParentCollapsibleExpandingContext;

static getDerivedStateFromProps(
Expand Down Expand Up @@ -165,7 +165,7 @@ function collapsibleHeight(
return `${height || 0}px`;
}

// Use named export once we work out why not casting this breaks web
// eslint-disable-next-line import/no-default-export
export default Collapsible as ComponentClass<CollapsibleProps> &
typeof Collapsible;
export const Collapsible = CollapsibleInner as ComponentClass<
CollapsibleProps
> &
typeof CollapsibleInner;
4 changes: 1 addition & 3 deletions src/components/Collapsible/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Collapsible, {CollapsibleProps} from './Collapsible';

export {Collapsible, CollapsibleProps};
export * from './Collapsible';
2 changes: 1 addition & 1 deletion src/components/Collapsible/tests/Collapsible.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
// eslint-disable-next-line no-restricted-imports
import {mountWithAppProvider} from 'test-utilities/legacy';
import Collapsible from '../Collapsible';
import {Collapsible} from '../Collapsible';

describe('<Collapsible />', () => {
const ariaHiddenSelector = '[aria-hidden=true]';
Expand Down
Loading