Skip to content

Commit

Permalink
feat: add smart controls
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 11, 2020
1 parent d5ac9c8 commit 23a5284
Show file tree
Hide file tree
Showing 13 changed files with 1,326 additions and 1,772 deletions.
11 changes: 11 additions & 0 deletions core/specification/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ export interface StoryComponent {
*/
info?: ComponentInfo;
}
/**
* given a component, return its name
* @param component a string component name, or a component class, with a name or displayName static property
*/
export const getComponentName = (component: any): string | undefined => {
return component
? typeof component === 'string'
? component
: component.name || component.displayName
: undefined;
};
1 change: 1 addition & 0 deletions core/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"license": "MIT",
"dependencies": {
"@component-controls/core": "^0.6.0",
"@component-controls/loader": "^0.6.0",
"@component-controls/specification": "^0.6.0",
"@storybook/csf": "^0.0.1",
Expand Down
8 changes: 0 additions & 8 deletions core/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ class Store implements StoryStore {
this.observers = [];
this.channel.onmessage = ({ storyId, moduleId }: MessageType) => {
if (storyId && moduleId) {
console.log(
'ON MESSAGE',
this.moduleId,
moduleId,
storyId,
this.moduleId !== moduleId,
);
if (this.moduleId !== moduleId) {
this.readData(storyId);
this.notifyObservers(storyId);
Expand All @@ -63,7 +56,6 @@ class Store implements StoryStore {
};

setStore = (store?: StoriesStore) => {
console.log('SET STORE');
this.loadedStore = store;
this.notifyObservers();
};
Expand Down
13 changes: 13 additions & 0 deletions core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StoriesStore, Story } from '@component-controls/specification';
const deepMerge = require('deepmerge');
import { toId, storyNameFromExport } from '@storybook/csf';
import getInjectedStore from '@component-controls/loader/story-store-data';
import { addSmartControls } from './smart-controls';

let storyStore: StoriesStore | undefined = undefined;

Expand Down Expand Up @@ -74,6 +75,18 @@ export const loadStoryStore = (): StoriesStore | undefined => {
...rest
} = kind;
Object.assign(story, deepMerge(rest, story));
const smartControls = addSmartControls(
story,
kind,
store.components,
);
if (smartControls) {
story.controls = deepMerge(
smartControls,
story.controls || {},
);
}

if (kind.title && story.name) {
const id = toId(kind.title, storyNameFromExport(story.name));
if (!kind.stories) {
Expand Down
58 changes: 58 additions & 0 deletions core/store/src/serialization/smart-controls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
ComponentControls,
Story,
StoriesKind,
StoryComponent,
getComponentName,
} from '@component-controls/specification';
import { controlsFromProps } from '@component-controls/core';

export const addSmartControls = (
story: Story,
kind: StoriesKind,
components: { [key: string]: StoryComponent },
): ComponentControls | null => {
if (!story.arguments || story.arguments.length < 1) {
//story has no arguments
return null;
}
const params = story.parameters || {};
const { addonControls = {} } = params;
const { smart: smartControls = true } = addonControls;
if (!smartControls) {
return null;
}
const storyComponent = story.component || params.component;
if (!storyComponent) {
return null;
}
const componentName = getComponentName(storyComponent);
if (componentName) {
const component = components[kind.components[componentName]];
if (component.info) {
const newControls = controlsFromProps(component.info.props);
const { include, exclude } = smartControls;
const usedProps: string[] | undefined = Array.isArray(
story.arguments[0].value,
)
? story.arguments[0].value.map(v => v.name as string)
: undefined;
const controls = Object.keys(newControls)
.filter(key => {
if (Array.isArray(include) && !include.includes(key)) {
return false;
}
if (Array.isArray(exclude) && exclude.includes(key)) {
return false;
}
if (usedProps && !usedProps.includes(key)) {
return false;
}
return true;
})
.reduce((acc, key) => ({ ...acc, [key]: newControls[key] }), {});
return controls;
}
}
return null;
};
4 changes: 2 additions & 2 deletions integrations/storybook/src/page/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Subtitle,
Story,
Playground,
// Stories,
Stories,
Description,
ComponentSource,
PropsTable,
Expand All @@ -23,7 +23,7 @@ export const DocsPage: FC = () => {
</Playground>
<ControlsTable id="." />
<PropsTable of="." />
{/* <Stories dark={true} /> */}
<Stories dark={true} />
</>
);
};
5 changes: 4 additions & 1 deletion integrations/storybook/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = {
managerEntries: (entry: any[] = [], options: any = {}) => {
const result = [...entry];
result.push(require.resolve('./register'));
result.push(require.resolve('./register-panel'));
const { addonPanel = true } = options;
if (addonPanel) {
result.push(require.resolve('./register-panel'));
}
return result;
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/helper-call-delegate": "^7.8.7",
"@babel/preset-env": "^7.9.0",
"@babel/preset-typescript": "^7.9.0",
"@commitlint/cli": "^8.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
BlockContainer,
BlockContainerProps,
} from '@component-controls/components';
import { CURRENT_STORY, getComponentName } from '../../utils';
import { getComponentName } from '@component-controls/specification';
import { CURRENT_STORY } from '../../utils';
import {
ComponentsContainer,
ComponentsContainerProps,
Expand Down
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 @@ -9,9 +9,9 @@ import {
SetControlValueFn,
ClickControlFn,
ComponentControlButton,
getComponentName,
} from '@component-controls/specification';
import { mergeControlValues } from '@component-controls/core';
import { getComponentName } from '../../utils';

export interface BlockContextInputProps {
/**
Expand Down
8 changes: 6 additions & 2 deletions ui/blocks/src/context/components/ComponentsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from 'react';
import { Story, StoriesKind } from '@component-controls/specification';
import {
Story,
StoriesKind,
getComponentName,
} from '@component-controls/specification';

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

export interface ComponentInputProps {
/**
Expand Down
8 changes: 0 additions & 8 deletions ui/blocks/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ import { StoriesKind, StoryComponent } from '@component-controls/specification';

export const CURRENT_STORY = '.';

export const getComponentName = (component: any): string | undefined => {
return component
? typeof component === 'string'
? component
: component.name || component.displayName
: undefined;
};

export const getStoryTitle = (
kind: StoriesKind | undefined,
component: StoryComponent | undefined,
Expand Down
Loading

0 comments on commit 23a5284

Please sign in to comment.