Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storybook] Add stories for more components (letter P) - Part 1 #7648

Merged
merged 20 commits into from
Apr 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 81 additions & 29 deletions .storybook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import type { Args, ArgTypes, Meta, Preview, StoryObj } from '@storybook/react';
import { action } from '@storybook/addon-actions';

type StorybookConfig<T> = Meta<T> | StoryObj<T> | Preview;

Expand All @@ -27,10 +28,12 @@ export const hideStorybookControls = <Props>(
config: StorybookConfig<Props>,
propNames: Array<keyof Props>
): StorybookConfig<Props> => {
const updatedConfig = _updateArgTypes(config, propNames, {
key: 'table',
value: { disable: true },
});
const updatedConfig = _updateArgTypes(config, propNames, [
{
key: 'table',
value: { disable: true },
},
]);

return updatedConfig;
};
Expand All @@ -49,10 +52,12 @@ export const disableStorybookControls = <Props>(
config: StorybookConfig<Props>,
propNames: Array<keyof Props>
): StorybookConfig<Props> => {
const updatedConfig = _updateArgTypes(config, propNames, {
key: 'control',
value: false,
});
const updatedConfig = _updateArgTypes(config, propNames, [
{
key: 'control',
value: false,
},
]);

return updatedConfig;
};
Expand All @@ -72,11 +77,44 @@ export const moveStorybookControlsToCategory = <Props>(
propNames: Array<keyof Props>,
category = 'Additional'
): StorybookConfig<Props> => {
const updatedConfig = _updateArgTypes(config, propNames, {
key: 'table',
value: { category },
const updatedConfig = _updateArgTypes(config, propNames, [
{
key: 'table',
value: { category },
},
]);

return updatedConfig;
};

/**
* Configures passed argTypes to be setup as toggle control
* which fires a Storybook action when enabled.
* Should be used for function props only.
*
* Can be used for preview (Preview), component (Meta) or story (Story)
* context by passing the config object for either. Use after defining
* the specific config to be able to pass the config to this util.
*
* @returns the mutated config
*/
export const enableFunctionToggleControls = <Props>(
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
config: StorybookConfig<Props>,
propNames: Array<keyof Props>
) => {
const setAction = (propName: string | number) => ({
true: action(propName.toString()),
false: undefined,
});

const updatedConfig = _updateArgTypes(config, propNames, [
{ key: 'control', value: 'boolean' },
{
key: 'mapping',
value: setAction,
},
]);

return updatedConfig;
};

Expand Down Expand Up @@ -112,29 +150,43 @@ export const hidePanel = {
const _updateArgTypes = <Props>(
config: StorybookConfig<Props>,
propNames: Array<keyof Props>,
{
key,
value,
}: { key: string; value: Record<string, string | boolean> | boolean }
controls: Array<{
key: string;
value:
| Record<string, any>
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
| boolean
| string
| ((propName: any) => Record<string, any>);
}>
): StorybookConfig<Props> => {
const currentArgTypes = config.argTypes as Partial<ArgTypes<Props>>;
const newArgTypes = { ...currentArgTypes };

for (const propName of propNames) {
const currentArgTypeValue = newArgTypes?.[propName] ?? ({} as Args);
const currentControlValue = currentArgTypeValue.hasOwnProperty(key)
? currentArgTypeValue[key]
: ({} as Record<string, any>);

const newValue =
typeof value === 'object' && typeof currentArgTypeValue[key] === 'object'
? { ...currentControlValue, ...value }
: value;

newArgTypes[propName] = {
...currentArgTypeValue,
[key]: newValue,
};
for (const { key, value } of controls) {
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
const currentArgTypeValue = newArgTypes?.[propName] ?? ({} as Args);
const currentControlValue = currentArgTypeValue.hasOwnProperty(key)
? currentArgTypeValue[key]
: ({} as Record<string, any>);

let newValue = value;

if (typeof value === 'function') {
newValue = value(propName);
}

if (
typeof value === 'object' &&
typeof currentArgTypeValue[key] === 'object'
) {
newValue = { ...currentControlValue, ...value };
}

newArgTypes[propName] = {
...currentArgTypeValue,
[key]: newValue,
};
}
}

config.argTypes = newArgTypes;
Expand Down