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

Blocks: Fix Controls block not having controls #25663

Merged
merged 2 commits into from
Jan 19, 2024
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
14 changes: 12 additions & 2 deletions code/ui/blocks/src/blocks/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,24 @@ export const Controls: FC<ControlsProps> = (props) => {
const exclude = props.exclude ?? controlsParameters.exclude;
const sort = props.sort ?? controlsParameters.sort;

const [, updateArgs, resetArgs] = useArgs(story, context);
const [args, updateArgs, resetArgs] = useArgs(story, context);
const [globals] = useGlobals(story, context);

const filteredArgTypes = filterArgTypes(argTypes, include, exclude);

const hasSubcomponents = Boolean(subcomponents) && Object.keys(subcomponents).length > 0;

if (!hasSubcomponents) {
return <PureArgsTable rows={filteredArgTypes} sort={sort} />;
return (
<PureArgsTable
rows={filteredArgTypes}
sort={sort}
args={args}
globals={globals}
updateArgs={updateArgs}
resetArgs={resetArgs}
/>
);
}

const mainComponentName = getComponentName(component);
Expand All @@ -75,6 +84,7 @@ export const Controls: FC<ControlsProps> = (props) => {
<TabbedArgsTable
tabs={tabs}
sort={sort}
args={args}
globals={globals}
updateArgs={updateArgs}
resetArgs={resetArgs}
Expand Down
14 changes: 12 additions & 2 deletions code/ui/blocks/src/components/ArgsTable/TabbedArgsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ export const TabbedArgsTable: FC<TabbedArgsTableProps> = ({ tabs, ...props }) =>

return (
<TabsState>
{entries.map((entry) => {
{entries.map((entry, index) => {
const [label, table] = entry;
const id = `prop_table_div_${label}`;
const Component = 'div' as unknown as React.ElementType<
Omit<JSX.IntrinsicElements['div'], 'children'> & {
children: ({ active }: { active: boolean }) => React.ReactNode;
}
>;

/**
* The first tab is the main component, controllable if in the Controls block
* All other tabs are subcomponents, never controllable, so we filter out the props indicating controllability
* Essentially all subcomponents always behave like ArgTypes, never Controls
*/
const argsTableProps = index === 0 ? props : { sort: props.sort };

return (
<Component key={id} id={id} title={label}>
{({ active }) =>
active ? <ArgsTable key={`prop_table_${label}`} {...table} {...props} /> : null
active ? (
<ArgsTable key={`prop_table_${label}`} {...table} {...argsTableProps} />
) : null
}
</Component>
);
Expand Down
Loading