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

Core: Typing useArgs #17735

Merged
merged 4 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import React, { useState } from 'react';
import React, { FC, useState } from 'react';
import { useArgs } from '@storybook/client-api';

// eslint-disable-next-line react/prop-types
const ArgUpdater = ({ args, updateArgs, resetArgs }) => {
interface CustomArgs {
first?: string;
last?: string;
foo?: string;
}

type UpdateArgs = ReturnType<typeof useArgs>[1];
type ResetArgs = ReturnType<typeof useArgs>[2];

const ArgUpdater: FC<{ args: CustomArgs; updateArgs: UpdateArgs; resetArgs: ResetArgs }> = ({
args,
updateArgs,
resetArgs,
}) => {
const [argsInput, updateArgsInput] = useState(JSON.stringify(args));

return (
Expand Down Expand Up @@ -30,7 +42,7 @@ export default {
title: 'Core/Args',
decorators: [
(story) => {
const [args, updateArgs, resetArgs] = useArgs();
const [args, updateArgs, resetArgs] = useArgs<CustomArgs>();

return (
<>
Expand Down Expand Up @@ -63,14 +75,14 @@ export const DifferentSet = Template.bind({});
DifferentSet.args = {
foo: 'bar',
bar: 2,
};
} as CustomArgs;

export const TestUndefinedArgs = Template.bind({});
TestUndefinedArgs.args = {
first: 'Bob',
last: 'Miller',
foo: 'bar',
};
} as CustomArgs;
TestUndefinedArgs.argTypes = {
first: {
control: { type: 'select' },
Expand Down
8 changes: 4 additions & 4 deletions lib/addons/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,21 @@ export function useParameter<S>(parameterKey: string, defaultValue?: S): S | und
}

/* Returns current value of story args */
export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: [string]) => void] {
export function useArgs<SpecificArgs = Args>(): [SpecificArgs, (newArgs: Partial<SpecificArgs>) => void, (argNames?: (keyof SpecificArgs)[]) => void] {
const channel = addons.getChannel();
const { id: storyId, args } = useStoryContext();

const updateArgs = useCallback(
(updatedArgs: Args) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }),
(updatedArgs: Partial<SpecificArgs>) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }),
[channel, storyId]
);

const resetArgs = useCallback(
(argNames?: [string]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }),
(argNames?: (keyof SpecificArgs)[]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }),
[channel, storyId]
);

return [args, updateArgs, resetArgs];
return [args as SpecificArgs, updateArgs, resetArgs];
}

/* Returns current value of global args */
Expand Down