Skip to content

Commit

Permalink
fix: infer component props in makeDecoratable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Apr 5, 2024
1 parent dfaed51 commit eff32b4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
5 changes: 1 addition & 4 deletions packages/app-admin/src/base/ui/FileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ export const FileManager = ({ children, render, onChange, ...rest }: FileManager
// @ts-expect-error
<FileManagerRenderer
onClose={() => setShow(false)}
onChange={
/* TODO: figure out how to create a conditional type based on the value of `rest.multiple` */
onChangeRef.current
}
onChange={onChangeRef.current}
{...rest}
/>,
containerRef.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,40 @@ export type DynamicZoneContainerProps = {
className?: string;
};

export const DynamicZoneContainer = makeDecoratable<
React.FunctionComponent<DynamicZoneContainerProps>
>("DynamicZoneContainer", props => {
const {
field,
bind: {
validation: { isValid, message }
},
title = field.label,
description = field.helpText,
className,
children
} = props;
export const DynamicZoneContainer = makeDecoratable(
"DynamicZoneContainer",
(props: DynamicZoneContainerProps) => {
const {
field,
bind: {
validation: { isValid, message }
},
title = field.label,
description = field.helpText,
className,
children
} = props;

const defaultClassName = field.multipleValues ? noBottomPadding : undefined;
const defaultClassName = field.multipleValues ? noBottomPadding : undefined;

return (
<>
<Accordion>
<AccordionItem
title={title}
description={description}
className={className || defaultClassName}
>
{children}
</AccordionItem>
</Accordion>
{isValid === false && <FormElementMessage error={true}>{message}</FormElementMessage>}
</>
);
});
return (
<>
<Accordion>
<AccordionItem
title={title}
description={description}
className={className || defaultClassName}
>
{children}
</AccordionItem>
</Accordion>
{isValid === false && (
<FormElementMessage error={true}>{message}</FormElementMessage>
)}
</>
);
}
);

const DynamicZoneContent = ({ field, getBind, contentModel }: CmsModelFieldRendererProps) => {
const templates = field.settings?.templates || [];
Expand Down
14 changes: 5 additions & 9 deletions packages/react-composition/src/makeDecoratable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import React, { createContext, useContext, useMemo } from "react";
import { useComponent } from "./Context";
import {
CanReturnNull,
DecoratableComponent,
DecoratableHook,
GenericComponent,
GenericHook
} from "~/types";
import { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "~/types";
import { withDecoratorFactory, withHookDecoratorFactory } from "~/decorators";

const ComposableContext = createContext<string[]>([]);
Expand Down Expand Up @@ -75,11 +69,13 @@ export function createVoidComponent<T>() {
};
}

export function makeDecoratable<T extends GenericHook>(hook: T): DecoratableHook<T>;
export function makeDecoratable<T extends GenericHook>(
hook: T
): ReturnType<typeof makeDecoratableHook<T>>;
export function makeDecoratable<T extends GenericComponent>(
name: string,
Component: T
): DecoratableComponent<CanReturnNull<T>>;
): ReturnType<typeof makeDecoratableComponent<T>>;
export function makeDecoratable(hookOrName: any, Component?: any) {
if (Component) {
return makeDecoratableComponent(hookOrName, Component);
Expand Down

0 comments on commit eff32b4

Please sign in to comment.