Skip to content

Commit

Permalink
feat: refactor blocks container
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 29, 2020
1 parent cdd994e commit be9847a
Show file tree
Hide file tree
Showing 37 changed files with 274 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { FC } from 'react';
import {
BlockContainer,
BlockContainerProps,
} from '@component-controls/components';
import { ComponentsContainer, ComponentsContainerProps } from '../../context';

export type ComponentsBlockContainerProps = ComponentsContainerProps &
Omit<BlockContainerProps, 'id'>;

export const ComponentsBlockContainer: FC<ComponentsBlockContainerProps> = ({
title,
collapsible,
of,
children,
}) => {
return (
<BlockContainer title={title} collapsible={collapsible}>
<ComponentsContainer of={of}>
{(component, props) => children(component, props)}
</ComponentsContainer>
</BlockContainer>
);
};
1 change: 1 addition & 0 deletions ui/blocks/src/BlockContainer/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ComponentsBlockContainer';
2 changes: 2 additions & 0 deletions ui/blocks/src/BlockContainer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './components';
export * from './story';
46 changes: 46 additions & 0 deletions ui/blocks/src/BlockContainer/story/StoryBlockContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { FC } from 'react';
import {
BlockContainer,
BlockContainerProps,
} from '@component-controls/components';
import {
useStoryContext,
StoryInputProps,
StoryContextProps,
} from '../../context';
import { CURRENT_STORY, getStoryTitle } from '../../utils';

export type StoryBlockContainerProps = {
children: (
context: StoryContextProps,
props: any,
) => React.ReactElement | null;
} & StoryInputProps &
Omit<BlockContainerProps, 'id'>;

export const StoryBlockContainer: FC<StoryBlockContainerProps> = ({
id,
name,
collapsible,
title: userTitle,
children,
...rest
}) => {
const context = useStoryContext({
id,
name,
});
const { component, kind, story } = context;
const title =
userTitle == CURRENT_STORY ? getStoryTitle(kind, component) : userTitle;
// console.log(userTitle, title);
return (
<BlockContainer
title={title}
collapsible={collapsible}
id={userTitle == CURRENT_STORY && story ? story.id : undefined}
>
{children(rest, context)}
</BlockContainer>
);
};
1 change: 1 addition & 0 deletions ui/blocks/src/BlockContainer/story/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './StoryBlockContainer';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BlockComponentSource } from './BlockComponentSource';
import { MockContext } from '../../test/MockContext';

export default {
title: 'Blocks/Core/ComponentSource/block',
title: 'Blocks/ComponentSource/block',
component: BlockComponentSource,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ComponentSource } from './ComponentSource';
import { MockContext } from '../../test/MockContext';

export default {
title: 'Blocks/Core/ComponentSource/plain',
title: 'Blocks/ComponentSource/plain',
component: ComponentSource,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MockContext } from '../../test/MockContext';
import { BlockControlsTable } from './BlockControlsTable';

export default {
title: 'Blocks/Core/ControlsTable/block',
title: 'Blocks/ControlsTable/block',
component: BlockControlsTable,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ControlsTable } from './ControlsTable';
import { MockContext } from '../../test/MockContext';

export default {
title: 'Blocks/Core/ControlsTable/plain',
title: 'Blocks/ControlsTable/plain',
component: ControlsTable,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/Description/Description.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { Description } from './Description';
import { MockContext } from '../test/MockContext';
export default {
title: 'Blocks/Core/Description',
title: 'Blocks/Description',
component: Description,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/PropsTable/block/BlockPropsTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BlockPropsTable } from './BlockPropsTable';
import { MockContext } from '../../test/MockContext';

export default {
title: 'Blocks/Core/PropsTable/block',
title: 'Blocks/PropsTable/block',
component: BlockPropsTable,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/PropsTable/plain/PropsTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StoryContextConsumer } from '../../context/story/StoryContext';
import { MockContext } from '../../test/MockContext';

export default {
title: 'Blocks/Core/PropsTable/plain',
title: 'Blocks/PropsTable/plain',
component: PropsTable,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/Stories/plain/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useBlockContext,
} from '../../context';
import { Playground, PlaygroundProps } from '../../Playground';
import { Story } from '../../Story/plain';
import { Story } from '../../Story';

export type StoriesProps = StoryInputProps & PlaygroundProps;

Expand Down
30 changes: 30 additions & 0 deletions ui/blocks/src/Story/Story.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Story } from './Story';
import { MockContext } from '../test/MockContext';
export default {
title: 'Blocks/Story',
component: Story,
};

export const overview = () => (
<MockContext storyId="controls">
<Story id="." />
</MockContext>
);

export const title = () => (
<MockContext storyId="controls">
<Story title="." id="." />
</MockContext>
);

export const customTitle = () => (
<MockContext storyId="controls">
<Story title="My Story Title" id="." />
</MockContext>
);
export const notCollapsible = () => (
<MockContext storyId="controls">
<Story title="." collapsible={false} />
</MockContext>
);
36 changes: 36 additions & 0 deletions ui/blocks/src/Story/Story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { createElement, FC } from 'react';
import { getControlValues } from '@component-controls/core';
import {
StoryBlockContainer,
StoryBlockContainerProps,
} from '../BlockContainer/story';

export type StoryProps = Omit<StoryBlockContainerProps, 'children'>;

export const Story: FC<StoryProps> = (props: StoryProps) => (
<StoryBlockContainer {...props}>
{(rest, context) => {
const { story } = context;
// console.log(story);
if (story && story.renderFn) {
let children;
try {
const values = story.controls ? getControlValues(story.controls) : {};
children = createElement(
'div',
null,
story.renderFn(values, { context }),
);
return (
<div id={story.id} {...rest}>
{children}
</div>
);
} catch (e) {
console.error(e);
}
}
return null;
}}
</StoryBlockContainer>
);
13 changes: 0 additions & 13 deletions ui/blocks/src/Story/block/BlockStory.stories.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions ui/blocks/src/Story/block/BlockStory.tsx

This file was deleted.

1 change: 0 additions & 1 deletion ui/blocks/src/Story/block/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions ui/blocks/src/Story/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './plain';
export * from './block';
export * from './Story';
13 changes: 0 additions & 13 deletions ui/blocks/src/Story/plain/Story.stories.tsx

This file was deleted.

33 changes: 0 additions & 33 deletions ui/blocks/src/Story/plain/Story.tsx

This file was deleted.

1 change: 0 additions & 1 deletion ui/blocks/src/Story/plain/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MockContext } from '../../test/MockContext';
import { BlockStorySource } from './BlockStorySource';

export default {
title: 'Blocks/Core/StorySource/block',
title: 'Blocks/StorySource/block',
component: BlockStorySource,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/StorySource/plain/StorySource.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import shadesOfPurple from 'prism-react-renderer/themes/shadesOfPurple';
import { StorySource } from './StorySource';
import { MockContext } from '../../test/MockContext';
export default {
title: 'Blocks/Core/StorySource/plain',
title: 'Blocks/StorySource/plain',
component: StorySource,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/Subtitle/Subtitle.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Subtitle } from '.';
import { MockContext } from '../test/MockContext';

export default {
title: 'Blocks/Core/Subtitle',
title: 'Blocks/Subtitle',
component: Subtitle,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/Title/Title.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Title } from './';
import { MockContext } from '../test/MockContext';

export default {
title: 'Blocks/Core/Title',
title: 'Blocks/Title',
component: Title,
};

Expand Down
9 changes: 2 additions & 7 deletions ui/blocks/src/Title/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TitleProps as TitlePropsBase,
} from '@component-controls/components';
import { useStoryContext, StoryInputProps } from '../context';
import { getStoryTitle } from '../utils';

export type TitleProps = StoryInputProps & TitlePropsBase;

Expand All @@ -15,12 +16,6 @@ export const Title: FC<TitleProps> = ({ id, name, ...rest }) => {
id,
name,
});
let title;
if (component && component.info && component.info.displayName) {
title = component.info.displayName;
} else if (kind) {
const titleParts = kind.title.split('/');
title = titleParts[titleParts.length - 1];
}
const title = getStoryTitle(kind, component);
return title ? <TitleBlock {...rest}>{title}</TitleBlock> : null;
};
2 changes: 1 addition & 1 deletion ui/blocks/src/context/block/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface BlockContextInputProps {
*/
mockStore?: StoriesStore;
}
export const CURRENT_STORY = '.';

export interface BlockContextProps {
/**
* current story id
Expand Down
4 changes: 2 additions & 2 deletions ui/blocks/src/context/components/ComponentsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
StoryComponent,
} from '@component-controls/specification';

import { BlockContext, CURRENT_STORY } from '../block';
import { getComponentName } from './utils';
import { BlockContext } from '../block';
import { getComponentName, CURRENT_STORY } from '../../utils';

export interface ComponentInputProps {
/**
Expand Down
7 changes: 0 additions & 7 deletions ui/blocks/src/context/components/utils.ts

This file was deleted.

Loading

0 comments on commit be9847a

Please sign in to comment.