Skip to content

Commit

Permalink
feat: componet context handle subcomponents
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 17, 2020
1 parent 7f9d5db commit c6a36bc
Show file tree
Hide file tree
Showing 25 changed files with 378 additions and 294 deletions.
52 changes: 0 additions & 52 deletions ui/blocks/src/BlocksContext/ComponentsContext.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions ui/blocks/src/BlocksContext/index.ts

This file was deleted.

91 changes: 48 additions & 43 deletions ui/blocks/src/ComponentSource/ComponentSource.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,68 @@
import React, { FC } from 'react';
import { ActionItem } from '@component-controls/components';
import {
useComponentsContext,
ComponentInputProps,
} from '../BlocksContext/ComponentsContext';
import { ComponentsContainer } from '../context/components/ComponentsContainer';
import { ComponentInputProps } from '../context/components/ComponentsContext';
import {
ThemeContext,
Source as SourceBlock,
SourceProps,
} from '@component-controls/components';
import { repositoryActions } from '../utils/repositoryActions';

export type ComponentSourceProps = ComponentInputProps & SourceProps;
export type ComponentSourceProps = ComponentInputProps &
Omit<SourceProps, 'children'>;

export const ComponentSource: FC<ComponentSourceProps> = ({
of,
actions,
...rest
}) => {
const { components } = useComponentsContext({
of,
});
let source;
const component = components[0];
const { from, importedName, name: componentName, repository } =
component || {};
const importFrom = repository && repository.name ? repository.name : from;
if (importFrom) {
source =
importedName !== 'default' && importedName !== 'namespace'
? `import { ${componentName} } from '${importFrom}';`
: `import ${componentName} from '${importFrom}';`;
}
return (
<ComponentsContainer of={of}>
{component => {
let source;
const { from, importedName, name: componentName, repository } =
component || {};
const importFrom =
repository && repository.name ? repository.name : from;
if (importFrom) {
source =
importedName !== 'default' && importedName !== 'namespace'
? `import { ${componentName} } from '${importFrom}';`
: `import ${componentName} from '${importFrom}';`;
}

if (!source) {
return null;
}
const { dark } = React.useContext(ThemeContext);
const [showFileSource, setShowFileSource] = React.useState<boolean>(false);
if (!source) {
return null;
}
const { dark } = React.useContext(ThemeContext);
const [showFileSource, setShowFileSource] = React.useState<boolean>(
false,
);

const onShowFileSource = () => setShowFileSource(!showFileSource);
const allActions: ActionItem[] = [];
const onShowFileSource = () => setShowFileSource(!showFileSource);
const allActions: ActionItem[] = [];

if (component && component.source) {
allActions.push({
title: showFileSource ? 'import' : 'component',
onClick: onShowFileSource,
});
}
const repositoryItems = component && repositoryActions(component?.repository);
if (repositoryItems) {
allActions.push.apply(allActions, repositoryItems);
}
if (actions) {
allActions.push.apply(allActions, actions);
}
return (
<SourceBlock dark={dark} {...rest} actions={allActions}>
{showFileSource ? component?.source : source}
</SourceBlock>
if (component && component.source) {
allActions.push({
title: showFileSource ? 'import' : 'component',
onClick: onShowFileSource,
});
}
const repositoryItems =
component && repositoryActions(component?.repository);
if (repositoryItems) {
allActions.push.apply(allActions, repositoryItems);
}
if (actions) {
allActions.push.apply(allActions, actions);
}
return (
<SourceBlock dark={dark} {...rest} actions={allActions}>
{showFileSource ? component?.source : source}
</SourceBlock>
);
}}
</ComponentsContainer>
);
};
21 changes: 12 additions & 9 deletions ui/blocks/src/Description/Description.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FC } from 'react';
import { Markdown, MarkdownProps } from '@component-controls/components';
import { useComponentsContext, ComponentInputProps } from '../BlocksContext';
import { ComponentsContainer } from '../context/components/ComponentsContainer';
import { ComponentInputProps } from '../context/components/ComponentsContext';

export type DescriptionProps = Omit<MarkdownProps, 'children'> &
ComponentInputProps;
Expand All @@ -9,11 +10,13 @@ export type DescriptionProps = Omit<MarkdownProps, 'children'> &
* Description component with context
* 'of' property can specify which component's description to use
*/
export const Description: FC<DescriptionProps> = ({ of, ...rest }) => {
const { components } = useComponentsContext({ of });
const component = components.length > 0 && components[0];
if (!component || !component.info || !component.info.description) {
return null;
}
return <Markdown {...rest}>{component.info.description}</Markdown>;
};
export const Description: FC<DescriptionProps> = ({ of, ...rest }) => (
<ComponentsContainer of={of}>
{component => {
if (!component || !component.info || !component.info.description) {
return null;
}
return <Markdown {...rest}>{component.info.description}</Markdown>;
}}
</ComponentsContainer>
);

This file was deleted.

168 changes: 0 additions & 168 deletions ui/blocks/src/PropsTable/PropsTable/PropsTable.tsx

This file was deleted.

14 changes: 14 additions & 0 deletions ui/blocks/src/PropsTable/block/BlockPropsTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { BlockPropsTable } from './BlockPropsTable';
import { MockContext } from '../../test/MockContext';

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

export const simple = () => (
<MockContext>
<BlockPropsTable title="BlockPropsTable table" />
</MockContext>
);
Loading

0 comments on commit c6a36bc

Please sign in to comment.