Skip to content

Commit

Permalink
fix: update control values in state
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 27, 2020
1 parent d0b9dde commit e5b9218
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 0 additions & 6 deletions examples/storybook-5/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import { addDecorator, addParameters } from '@storybook/react';
import { Title as SBTitle, Description as SBDescription, Story as SBStory, Stories as SBStories, Props } from '@storybook/addon-docs/blocks';
import { DependenciesTable } from 'storybook-addon-deps/blocks';
import { ControlsTable, ThemeProvider, Title, Subtitle, Story, Stories, Description, StorySource, Playground, ComponentSource, PropsTable, BlockContextProvider } from '@component-controls/storybook';

Expand All @@ -15,24 +14,19 @@ addDecorator((story, ctx ) => {
export const DocsPage = () => {
return (
<BlockContextProvider>
<SBTitle />
<Title />
<Subtitle />
<SBDescription />
<Description />
<ComponentSource id="." title='Component source' />
<Story id="." />
<SBStory id="." />
<StorySource id="." title='Story source'/>
<ControlsTable id="." />
<PropsTable of="." />
<Props />
<Playground>
<Story id="." />
</Playground>
<DependenciesTable titleDependencies='Dependencies' titleDependents='Dependents' />
<Stories />
<SBStories />
</BlockContextProvider>
);
};
Expand Down
6 changes: 2 additions & 4 deletions ui/blocks/src/ComponentSource/plain/ComponentSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const ComponentSource: FC<ComponentSourceProps> = ({
actions,
...rest
}) => {
const [showFileSource, setShowFileSource] = React.useState<boolean>(false);
const { dark } = React.useContext(ThemeContext);
return (
<ComponentsContainer of={of}>
{component => {
Expand All @@ -40,10 +42,6 @@ export const ComponentSource: FC<ComponentSourceProps> = ({
if (!source) {
return null;
}
const { dark } = React.useContext(ThemeContext);
const [showFileSource, setShowFileSource] = React.useState<boolean>(
false,
);

const onShowFileSource = () => setShowFileSource(!showFileSource);
const allActions: ActionItem[] = [];
Expand Down
24 changes: 19 additions & 5 deletions ui/blocks/src/context/block/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,36 @@ export const BlockContextProvider: React.FC<BlockContextInputProps> = ({
children,
currentId,
}) => {
const store = storyFn();
const [store, setStore] = React.useState<StoriesStore>({
components: {},
stories: {},
kinds: {},
});
React.useEffect(() => {
setStore(storyFn());
}, []);

const setControlValue: SetControlValueFn = (
storyId: string,
propName: string | undefined,
propValue: any,
) => {
const controls = store && store[storyId] && store[storyId].controls;
const controls =
store && store.stories[storyId] && store.stories[storyId].controls;
if (controls) {
const newValues = mergeControlValues(controls, propName, propValue);
Object.keys(controls).forEach(key => {
controls[key] = newValues[key];
setStore({
...store,
stories: {
...store.stories,
[storyId]: { ...store.stories[storyId], controls: newValues },
},
});
}
};
const clickControl: ClickControlFn = (storyId: string, propName: string) => {
const controls = store && store[storyId] && store[storyId].controls;
const controls =
store && store.stories[storyId] && store.stories[storyId].controls;
if (controls && controls[propName]) {
const control: ComponentControlButton = controls[
propName
Expand Down

0 comments on commit e5b9218

Please sign in to comment.