Skip to content

Commit 8d57d1a

Browse files
committed
feat: include/exclude controls as function
1 parent 3768ca2 commit 8d57d1a

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

core/core/src/common.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { ElementType } from 'react';
22
import { deepMerge } from './deepMerge';
33
import { StoryRenderFn } from './utility';
4-
import { ComponentControls } from './controls';
4+
import { ComponentControls, ComponentControl } from './controls';
5+
import { PropType } from './components';
56

7+
export type IncludeFn = (
8+
control: { name: string; prop?: PropType } & ComponentControl,
9+
) => boolean;
610
export interface SmartControls {
711
/**
812
* whether to generate "smart" controls for a story
@@ -11,12 +15,12 @@ export interface SmartControls {
1115
/**
1216
* include props only
1317
*/
14-
include?: string[];
18+
include?: string[] | IncludeFn;
1519

1620
/**
1721
* exclude props only
1822
*/
19-
exclude?: string[];
23+
exclude?: string[] | IncludeFn;
2024
}
2125

2226
/**

core/core/src/controls-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,19 @@ export const getStoryControls = (
440440
if (Array.isArray(include) && !include.includes(key)) {
441441
return false;
442442
}
443+
if (typeof include === 'function') {
444+
const prop = component.info?.props[key];
445+
return include({ name: key, ...newControls[key], prop });
446+
}
447+
443448
if (Array.isArray(exclude) && exclude.includes(key)) {
444449
return false;
445450
}
451+
452+
if (typeof exclude === 'function') {
453+
const prop = component.info?.props[key];
454+
return !exclude({ name: key, ...newControls[key], prop });
455+
}
446456
if (usedProps && !usedProps.includes(key)) {
447457
return false;
448458
}

examples/stories/src/stories/VariantButton/VariantButton.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default {
2626
},
2727
},
2828
description: `This example demonstrates documenting a hypothetical Button component that supports variants, icons, text, and padding`,
29+
smartControls: {
30+
include: p => p.prop?.parentName === 'VariantButtonProps',
31+
},
2932
} as Document;
3033

3134
export const overview: Example<VariantButtonProps> = props => (

examples/stories/src/stories/VariantButton/VariantButton.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ export interface VariantButtonProps {
108108
/**
109109
* Button with variants
110110
*/
111-
export const VariantButton: FC<VariantButtonProps> = ({
111+
export const VariantButton: FC<VariantButtonProps &
112+
React.HTMLProps<HTMLButtonElement>> = ({
112113
text,
113114
fontSize = 18,
114115
iconSize = 'small',

0 commit comments

Comments
 (0)