-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: componet context handle subcomponents
- Loading branch information
1 parent
7f9d5db
commit c6a36bc
Showing
25 changed files
with
378 additions
and
294 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
ui/blocks/src/PropsTable/BlockPropsTable/BlockPropsTable.stories.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
ui/blocks/src/PropsTable/block/BlockPropsTable.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
Oops, something went wrong.