Skip to content

Commit

Permalink
fix: Fixes storybookjs#242 (an action appears in the controls tab as …
Browse files Browse the repository at this point in the history
…an invalid control type).

In the controls panel, filter out the non-control args so that e.g. actions do not incorrectly get shown in the controls tab.
  • Loading branch information
lauriharpf committed Jul 28, 2021
1 parent bf76e81 commit 12de09a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
20 changes: 10 additions & 10 deletions addons/ondevice-controls/src/ControlsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ const ControlsPanel = ({ api }: { api: API }) => {
const storyId = store.getSelection().storyId;
const [argsfromHook, updateArgs, resetArgs] = useArgs(storyId, store);
const { argTypes, parameters } = store.fromId(storyId);
const argsObject = Object.entries(argsfromHook).reduce((prev, [name, value]) => {
const isControl = Boolean(argTypes?.[name]?.control);

const isArgsStory = parameters.__isArgsStory;

const hasControls = Object.values(argTypes).some((arg: ArgType) => arg?.control);
const showWarning = !(hasControls && isArgsStory);
const argsObject = hasControls
? Object.entries(argsfromHook).reduce((prev, [name, value]) => {
return {
return isControl
? {
...prev,
[name]: { ...argTypes?.[name], name, type: argTypes[name]?.control?.type, value },
};
}, {})
: {};
}
: prev;
}, {});
const hasControls = Object.keys(argsObject).length > 0;
const isArgsStory = parameters.__isArgsStory;
const showWarning = !(hasControls && isArgsStory);

return (
<>
Expand Down
3 changes: 3 additions & 0 deletions examples/native/components/ActionExample/Actions.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const ActionButtonMeta: ComponentMeta<typeof ActionButton> = {
argTypes: {
onPress: { action: 'pressed the button' },
},
args: {
text: 'Press me!',
},
};
export default ActionButtonMeta;

Expand Down
5 changes: 3 additions & 2 deletions examples/native/components/ActionExample/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { TouchableOpacity, Text, StyleSheet } from 'react-native';

interface ActionButtonProps {
onPress: () => void;
text: string;
}

export const ActionButton = ({ onPress }: ActionButtonProps) => {
export const ActionButton = ({ onPress, text }: ActionButtonProps) => {
return (
<TouchableOpacity style={styles.container} onPress={onPress}>
<Text style={styles.text}>ActionButton</Text>
<Text style={styles.text}>{text}</Text>
</TouchableOpacity>
);
};
Expand Down

0 comments on commit 12de09a

Please sign in to comment.