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

refactor(FlexBox): Replace withStyles HOC with forwardRef implementation #78

Merged
merged 2 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions packages/main/src/components/FlexBox/FlexBox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as React from 'react';
import { FlexBox } from '../../lib/FlexBox';
import { FlexBoxJustifyContent } from '../../lib/FlexBoxJustifyContent';

declare const expect: any;

describe('FlexBox', () => {
test('JustifyContent: End', () => {
const wrapper = mountThemedComponent(
Expand Down Expand Up @@ -34,4 +36,13 @@ describe('FlexBox', () => {
const node = wrapper.getDOMNode();
expect(window.getComputedStyle(node).display).toEqual('inline-flex');
});

test('with Custom Class Names and Style', () => {
const wrapper = mountThemedComponent(
<FlexBox className="testClass" style={{ backgroundColor: '#000' }}>
<span>Test 1</span>
</FlexBox>
);
expect(wrapper.render()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FlexBox with Custom Class Names and Style 1`] = `
<div
class="FlexBox--flexBox- FlexBox--flexBoxDirectionRow- FlexBox--justifyContentStart- FlexBox--alignItemsStretch- FlexBox--flexWrapNoWrap- testClass"
style="background-color: rgb(0, 0, 0);"
>
<span>
Test 1
</span>
</div>
`;
27 changes: 27 additions & 0 deletions packages/main/src/components/FlexBox/demo.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { boolean, select, text } from '@storybook/addon-knobs/';
import { storiesOf } from '@storybook/react';
import React from 'react';
import { FlexBox } from '../../lib/FlexBox';
import { FlexBoxAlignItems } from '../../lib/FlexBoxAlignItems';
import { FlexBoxDirection } from '../../lib/FlexBoxDirection';
import { FlexBoxJustifyContent } from '../../lib/FlexBoxJustifyContent';
import { FlexBoxWrap } from '../../lib/FlexBoxWrap';
import { Label } from '../../lib/Label';

storiesOf('Components | FlexBox', module).add('Default', () => (
<FlexBox
justifyContent={select('justifyContent', FlexBoxJustifyContent, FlexBoxJustifyContent.Start)}
alignItems={select('alignItems', FlexBoxAlignItems, FlexBoxAlignItems.Stretch)}
direction={select('direction', FlexBoxDirection, FlexBoxDirection.Row)}
wrap={select('wrap', FlexBoxWrap, FlexBoxWrap.NoWrap)}
height={text('height', 'auto')}
width={text('width', '500px')}
displayInline={boolean('displayInline', false)}
>
<Label>Item 1</Label>
<Label>Item 2</Label>
<Label>Item 3</Label>
<Label>Item 4</Label>
<Label>Item 5</Label>
</FlexBox>
));
159 changes: 68 additions & 91 deletions packages/main/src/components/FlexBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
import { StyleClassHelper, withStyles } from '@ui5/webcomponents-react-base';
import React, { Component, CSSProperties, ReactNode, ReactNodeArray } from 'react';
import { ClassProps } from '../../interfaces/ClassProps';
import { StyleClassHelper } from '@ui5/webcomponents-react-base';
import React, { CSSProperties, FC, forwardRef, ReactNode, ReactNodeArray, Ref } from 'react';
import { createUseStyles } from 'react-jss';
import { CommonProps } from '../../interfaces/CommonProps';
import { styles } from './Flexbox.jss';
import { FlexBoxJustifyContent } from '../../lib/FlexBoxJustifyContent';
import { FlexBoxAlignItems } from '../../lib/FlexBoxAlignItems';
import { FlexBoxDirection } from '../../lib/FlexBoxDirection';
import { FlexBoxWrap } from '../../lib/FlexBoxWrap';

export enum FlexBoxJustifyContent {
Start = 'Start',
Center = 'Center',
End = 'End',
SpaceAround = 'SpaceAround',
SpaceBetween = 'SpaceBetween'
}

export enum FlexBoxAlignItems {
Start = 'Start',
Center = 'Center',
End = 'End',
Baseline = 'Baseline',
Stretch = 'Stretch'
}

export enum FlexBoxDirection {
Column = 'Column',
ColumnReverse = 'ColumnReverse',
Row = 'Row',
RowReverse = 'RowReverse'
}

export enum FlexBoxWrap {
NoWrap = 'NoWrap',
Wrap = 'Wrap',
WrapReverse = 'WrapReverse'
}
const useStyles = createUseStyles(styles, { name: 'FlexBox' });

export interface FlexBoxPropTypes extends CommonProps {
alignItems?: FlexBoxAlignItems;
Expand All @@ -45,69 +22,69 @@ export interface FlexBoxPropTypes extends CommonProps {
children: ReactNode | ReactNodeArray;
}

@withStyles(styles)
export class FlexBox extends Component<FlexBoxPropTypes> {
static defaultProps = {
alignItems: FlexBoxAlignItems.Stretch,
direction: FlexBoxDirection.Row,
displayInline: false,
height: '',
justifyContent: FlexBoxJustifyContent.Start,
width: '',
wrap: FlexBoxWrap.NoWrap
};

render() {
const {
children,
classes,
justifyContent,
direction,
alignItems,
height,
width,
displayInline,
wrap,
style,
className,
tooltip,
innerRef,
slot
} = this.props as FlexBoxPropTypes & ClassProps;
const flexBoxClasses = StyleClassHelper.of(classes.flexBox);
// direction
flexBoxClasses.put(classes[`flexBoxDirection${direction}`]);
// justify content
flexBoxClasses.put(classes[`justifyContent${justifyContent}`]);
// align items
flexBoxClasses.put(classes[`alignItems${alignItems}`]);
// wrapping
flexBoxClasses.put(classes[`flexWrap${wrap}`]);
const FlexBox: FC<FlexBoxPropTypes> = forwardRef((props: FlexBoxPropTypes, ref: Ref<HTMLDivElement>) => {
const {
children,
justifyContent,
direction,
alignItems,
height,
width,
displayInline,
wrap,
style,
className,
tooltip,
slot
} = props;

if (displayInline) {
flexBoxClasses.put(classes.flexBoxDisplayInline);
}
const classes = useStyles();
const flexBoxClasses = StyleClassHelper.of(classes.flexBox);
// direction
flexBoxClasses.put(classes[`flexBoxDirection${direction}`]);
// justify content
flexBoxClasses.put(classes[`justifyContent${justifyContent}`]);
// align items
flexBoxClasses.put(classes[`alignItems${alignItems}`]);
// wrapping
flexBoxClasses.put(classes[`flexWrap${wrap}`]);

const inlineStyle = {} as CSSProperties;
if (height) {
inlineStyle.height = height;
}
if (width) {
inlineStyle.width = width;
}
if (displayInline) {
flexBoxClasses.put(classes.flexBoxDisplayInline);
}

if (className) {
flexBoxClasses.put(className);
}
const inlineStyle = {} as CSSProperties;
MarcusNotheis marked this conversation as resolved.
Show resolved Hide resolved
if (height) {
inlineStyle.height = height;
}
if (width) {
inlineStyle.width = width;
}

if (style) {
Object.assign(inlineStyle, style);
}
if (className) {
flexBoxClasses.put(className);
}

return (
<div ref={innerRef} className={flexBoxClasses.valueOf()} style={inlineStyle} title={tooltip} slot={slot}>
{children}
</div>
);
if (style) {
Object.assign(inlineStyle, style);
}
}

return (
<div ref={ref} className={flexBoxClasses.valueOf()} style={inlineStyle} title={tooltip} slot={slot}>
{children}
</div>
);
});

FlexBox.defaultProps = {
alignItems: FlexBoxAlignItems.Stretch,
direction: FlexBoxDirection.Row,
displayInline: false,
height: '',
justifyContent: FlexBoxJustifyContent.Start,
width: '',
wrap: FlexBoxWrap.NoWrap
};
FlexBox.displayName = 'FlexBox';

export { FlexBox };
7 changes: 7 additions & 0 deletions packages/main/src/enums/FlexBoxAlignItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum FlexBoxAlignItems {
Start = 'Start',
Center = 'Center',
End = 'End',
Baseline = 'Baseline',
Stretch = 'Stretch'
}
6 changes: 6 additions & 0 deletions packages/main/src/enums/FlexBoxDirection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum FlexBoxDirection {
Column = 'Column',
ColumnReverse = 'ColumnReverse',
Row = 'Row',
RowReverse = 'RowReverse'
}
7 changes: 7 additions & 0 deletions packages/main/src/enums/FlexBoxJustifyContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum FlexBoxJustifyContent {
Start = 'Start',
Center = 'Center',
End = 'End',
SpaceAround = 'SpaceAround',
SpaceBetween = 'SpaceBetween'
}
5 changes: 5 additions & 0 deletions packages/main/src/enums/FlexBoxWrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum FlexBoxWrap {
NoWrap = 'NoWrap',
Wrap = 'Wrap',
WrapReverse = 'WrapReverse'
}
2 changes: 1 addition & 1 deletion packages/main/src/lib/FlexBoxAlignItems.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { FlexBoxAlignItems } from '../components/FlexBox';
import { FlexBoxAlignItems } from '../enums/FlexBoxAlignItems';

export { FlexBoxAlignItems };
2 changes: 1 addition & 1 deletion packages/main/src/lib/FlexBoxDirection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { FlexBoxDirection } from '../components/FlexBox';
import { FlexBoxDirection } from '../enums/FlexBoxDirection';

export { FlexBoxDirection };
2 changes: 1 addition & 1 deletion packages/main/src/lib/FlexBoxJustifyContent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { FlexBoxJustifyContent } from '../components/FlexBox';
import { FlexBoxJustifyContent } from '../enums/FlexBoxJustifyContent';

export { FlexBoxJustifyContent };
2 changes: 1 addition & 1 deletion packages/main/src/lib/FlexBoxWrap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { FlexBoxWrap } from '../components/FlexBox';
import { FlexBoxWrap } from '../enums/FlexBoxWrap';

export { FlexBoxWrap };