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

[Stack] Converted to a functional component #2534

Merged
merged 2 commits into from
Jan 8, 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 @@ -25,6 +25,7 @@
### Code quality

- Converted `FormLayout` into a functional component ([#2539](https://github.com/Shopify/polaris-react/pull/2539))
- Converted `Stack` into a functional component ([#2534](https://github.com/Shopify/polaris-react/pull/2534))
- Converted `BulkActionButton` into a functional component ([#2542](https://github.com/Shopify/polaris-react/pull/2542))
- Converted `Focus` into a functional component ([#2540](https://github.com/Shopify/polaris-react/pull/2540))
- Converted `Tooltip` into a functional component ([#2543](https://github.com/Shopify/polaris-react/pull/2543))
Expand Down
60 changes: 29 additions & 31 deletions src/components/Stack/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {memo, NamedExoticComponent} from 'react';
import {classNames, variationName} from '../../utilities/css';
import {elementChildren, wrapWithComponent} from '../../utilities/components';

Expand Down Expand Up @@ -32,33 +32,31 @@ export interface StackProps {
distribution?: Distribution;
}

export class Stack extends React.PureComponent<StackProps, never> {
static Item = Item;

render() {
const {
children,
vertical,
spacing,
distribution,
alignment,
wrap,
} = this.props;

const className = classNames(
styles.Stack,
vertical && styles.vertical,
spacing && styles[variationName('spacing', spacing)],
distribution && styles[variationName('distribution', distribution)],
alignment && styles[variationName('alignment', alignment)],
wrap === false && styles.noWrap,
);

const itemMarkup = elementChildren(children).map((child, index) => {
const props = {key: index};
return wrapWithComponent(child, Item, props);
});

return <div className={className}>{itemMarkup}</div>;
}
}
export const Stack = memo(function Stack({
AndrewMusgrave marked this conversation as resolved.
Show resolved Hide resolved
children,
vertical,
spacing,
distribution,
alignment,
wrap,
}: StackProps) {
const className = classNames(
styles.Stack,
vertical && styles.vertical,
spacing && styles[variationName('spacing', spacing)],
distribution && styles[variationName('distribution', distribution)],
alignment && styles[variationName('alignment', alignment)],
wrap === false && styles.noWrap,
);

const itemMarkup = elementChildren(children).map((child, index) => {
const props = {key: index};
return wrapWithComponent(child, Item, props);
});

return <div className={className}>{itemMarkup}</div>;
}) as NamedExoticComponent<StackProps> & {
Copy link
Member

Choose a reason for hiding this comment

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

In other cases where we need this recasting it's been done at the top of the constant declaration rather than using an as, see Autocomplete for any example. Can we keep to that style for consistency?

export const Stack: React.NamedExoticComponent<StackProps> & {
  Item: typeof Item;
} = function memo(function Stack({
//...

Copy link
Member Author

Choose a reason for hiding this comment

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

Typing is always preferred overcasting, however, unfortunately, we can't in this situation.

Type 'NamedExoticComponent<StackProps>' is not assignable to type 'NamedExoticComponent<StackProps> & { Item: ({ children, fill }: ItemProps) => Element; }'.
  Property 'Item' is missing in type 'NamedExoticComponent<StackProps>' but required in type '{ Item: ({ children, fill }: ItemProps) => Element; }'

Copy link
Member

Choose a reason for hiding this comment

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

well that's a little annoying :(

/me waves fist at React.memo

Item: typeof Item;
};

Stack.Item = Item;
6 changes: 3 additions & 3 deletions tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ describe('build', () => {
});

it('preserves classes to facilitate class-level tree shaking', () => {
// `Stack` is a foundation class, so is unlikely to disappear from the build.
// `Collapsible` deeply ties into the react class based life-cycles methods, so is likely to be one of the last components converted to a function.
expect(
fs.readFileSync('esnext/components/Stack/Stack.js', 'utf8'),
).toMatch('class Stack');
fs.readFileSync('esnext/components/Collapsible/Collapsible.js', 'utf8'),
).toMatch('class Collapsible');
});

it('preserves jsx to give consumers control over Babel transforms', () => {
Expand Down