Skip to content

Commit

Permalink
feat(react): Closes morfeojs#125
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Nov 18, 2021
1 parent 624ec8d commit 1fa2f95
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 79 deletions.
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Box.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from './MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Box'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from './MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Card'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Forms/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Button'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Forms/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Input'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Forms/InputFeedback.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'InputFeedback'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Forms/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Label'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './Button';
export * from './Input';
export * from './Label';
export * from './Button';
export * from './InputFeedback';
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Grid/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Container'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Grid'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Grid/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Container';
export * from './Grid';
export * from './Container';
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Hr.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from './MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Hr'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from './MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'List'>, 'componentName'>;

Expand Down
54 changes: 0 additions & 54 deletions apps/web-sandbox/src/components/MorfeoComponent.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Templates/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Footer'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Templates/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Header'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Templates/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Main'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Templates/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Page'>, 'componentName'>;

Expand Down
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Templates/Section.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from '../MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Section'>, 'componentName'>;

Expand Down
4 changes: 2 additions & 2 deletions apps/web-sandbox/src/components/Templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './Footer';
export * from './Header';
export * from './Main';
export * from './Page';
export * from './Footer';
export * from './Header';
export * from './Section';
2 changes: 1 addition & 1 deletion apps/web-sandbox/src/components/Typography.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MorfeoComponent, MorfeoComponentProps } from './MorfeoComponent';
import { MorfeoComponent, MorfeoComponentProps } from '@morfeo/react';

type Props = Omit<MorfeoComponentProps<'Typography'>, 'componentName'>;

Expand Down
10 changes: 5 additions & 5 deletions apps/web-sandbox/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './Templates';
export * from './Grid';
export * from './Forms';
export * from './Hr';
export * from './Box';
export * from './Typography';
export * from './Grid';
export * from './Card';
export * from './Hr';
export * from './List';
export * from './Forms';
export * from './Templates';
export * from './Typography';
39 changes: 39 additions & 0 deletions packages/react/src/MorfeoComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ReactNode, HTMLProps, createElement } from 'react';
import { Style, Variant, Component, component } from '@morfeo/web';
import { useClassName } from './useClassName';

export type MorfeoComponentProps<T extends Component> = {
style?: Style;
variant?: Variant<T>;
children?: ReactNode;
componentName: T;
} & Omit<HTMLProps<HTMLElement>, 'style'>;

export function MorfeoComponent<T extends Component>({
style,
variant,
children,
componentName,
...props
}: MorfeoComponentProps<T>) {
const {
tag = 'div',
props: componentProps,
style: componentStyle,
} = component(componentName, variant).get();

const baseClassName = useClassName({ ...componentStyle, ...style });
const className = props.className
? `${baseClassName} ${props.className}`
: baseClassName;

return createElement(
tag,
{
...componentProps,
...props,
className,
},
children,
);
}
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from '@morfeo/web';
export * from '@morfeo/hooks';
export * from './useClassName';
export * from './MorfeoComponent';

export { useStyle, useStyles } from './hooks';
89 changes: 89 additions & 0 deletions packages/react/tests/MorfeoComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from 'react';
import { morfeo } from '@morfeo/web';
import { render } from '@testing-library/react';
import { MorfeoComponent } from '../src';

const theme = {
colors: {
primary: 'black',
secondary: 'white',
},
components: {
Box: {
style: {},
variants: {},
},
Button: {
tag: 'button',
style: {
bg: 'secondary',
},
variants: {
primary: {
style: { bg: 'primary' },
},
},
props: {
type: 'button',
},
},
},
} as any;

morfeo.setTheme('default', theme);

describe('MorfeoComponent', () => {
beforeAll(() => {
morfeo.useTheme('default');
});

test('should create a button with initial style and props based on the theme', async () => {
const { getByTestId } = render(
<MorfeoComponent componentName={'Button' as any} data-testid="button" />,
);
const button = getByTestId('button');

expect(button).toHaveStyle(
`background-color: ${morfeo.getTheme()['colors']['secondary']}`,
);
expect(button).toHaveAttribute('type', 'button');
});

test('should handle the variant', async () => {
const { getByTestId } = render(
<MorfeoComponent
componentName={'Button' as any}
variant={'primary' as any}
data-testid="button-primary"
/>,
);
const button = getByTestId('button-primary');

expect(button).toHaveStyle(
`background-color: ${morfeo.getTheme()['colors']['primary']}`,
);
});

test('should create a div if the tag is not specified', async () => {
const { getByTestId } = render(
<MorfeoComponent componentName="Box" data-testid="box" />,
);
const box = getByTestId('box');

expect(box.tagName).toBe('DIV');
});

test('should append the additional class if specified', async () => {
const { getByTestId } = render(
<MorfeoComponent
componentName="Box"
data-testid="box"
className="additional-class"
/>,
);
const box = getByTestId('box');

expect(box.classList).toHaveLength(2);
expect(box.classList).toContain('additional-class');
});
});

0 comments on commit 1fa2f95

Please sign in to comment.