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

feat(panel): smoother accessing of widgetRenderState #4558

Merged
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
10 changes: 6 additions & 4 deletions src/components/Panel/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useState, useEffect, useRef } from 'preact/hooks';
import cx from 'classnames';
import Template from '../Template/Template';
import { PanelCSSClasses, PanelTemplates } from '../../widgets/panel/panel';
import { RenderOptions } from '../../types';
import { RenderOptions, WidgetFactory } from '../../types';

type PanelComponentCSSClasses = Required<
Omit<
Expand All @@ -17,17 +17,19 @@ type PanelComponentCSSClasses = Required<
>
>;

type PanelProps = {
type PanelProps<TWidget extends WidgetFactory<any, any, any>> = {
hidden: boolean;
collapsible: boolean;
isCollapsed: boolean;
data: RenderOptions;
cssClasses: PanelComponentCSSClasses;
templates: Required<PanelTemplates>;
templates: Required<PanelTemplates<TWidget>>;
bodyElement: HTMLElement;
};

function Panel(props: PanelProps) {
function Panel<TWidget extends WidgetFactory<any, any, any>>(
props: PanelProps<TWidget>
) {
const [isCollapsed, setIsCollapsed] = useState<boolean>(props.isCollapsed);
const [isControlled, setIsControlled] = useState<boolean>(false);
const bodyRef = useRef<HTMLElement | null>(null);
Expand Down
37 changes: 19 additions & 18 deletions src/widgets/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ export type PanelCSSClasses = {
footer?: string | string[];
};

export type PanelTemplates = {
export type PanelTemplates<TWidget extends WidgetFactory<any, any, any>> = {
/**
* Template to use for the header.
*/
header?: Template<RenderOptions>;
header?: Template<PanelRenderOptions<TWidget>>;

/**
* Template to use for the footer.
*/
footer?: Template<RenderOptions>;
footer?: Template<PanelRenderOptions<TWidget>>;

/**
* Template to use for collapse button.
Expand All @@ -78,11 +78,11 @@ export type PanelTemplates = {

export type PanelRenderOptions<
TWidget extends WidgetFactory<any, any, any>
> = RenderOptions & {
widgetRenderState: ReturnType<
Exclude<ReturnType<TWidget>['getWidgetRenderState'], undefined>
>;
};
> = RenderOptions & ReturnType<TWidget>['getWidgetRenderState'] extends (
renderOptions: any
) => infer TRenderState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know about infer. looks nice 👍

? TRenderState
: Record<string, any>;

export type PanelWidgetOptions<TWidget extends WidgetFactory<any, any, any>> = {
/**
Expand All @@ -100,7 +100,7 @@ export type PanelWidgetOptions<TWidget extends WidgetFactory<any, any, any>> = {
/**
* The templates to use for the widget.
*/
templates?: PanelTemplates;
templates?: PanelTemplates<TWidget>;

/**
* The CSS classes to override.
Expand All @@ -111,14 +111,14 @@ export type PanelWidgetOptions<TWidget extends WidgetFactory<any, any, any>> = {
const withUsage = createDocumentationMessageGenerator({ name: 'panel' });
const suit = component('Panel');

const renderer = ({
const renderer = <TWidget extends WidgetFactory<any, any, any>>({
containerNode,
bodyContainerNode,
cssClasses,
templates,
}) => ({ options, hidden, collapsible, collapsed }) => {
render(
<Panel
<Panel<TWidget>
cssClasses={cssClasses}
hidden={hidden}
collapsible={collapsible}
Expand All @@ -132,12 +132,12 @@ const renderer = ({
};

export type PanelWidget = <TWidget extends WidgetFactory<any, any, any>>(
params?: PanelWidgetOptions<TWidget>
widgetParams?: PanelWidgetOptions<TWidget>
) => <
TWidgetOptions extends { container: HTMLElement | string; [key: string]: any }
TWidgetParams extends { container: HTMLElement | string; [key: string]: any }
>(
widgetFactory: (widgetOptions: TWidgetOptions) => Widget
) => (widgetOptions: TWidgetOptions) => Widget;
widgetFactory: TWidget
) => (widgetOptions: TWidgetParams) => Widget;

/**
* The panel widget wraps other widgets in a consistent panel design.
Expand Down Expand Up @@ -222,7 +222,7 @@ const panel: PanelWidget = widgetParams => {
</svg>`,
};

const renderPanel = renderer({
const renderPanel = renderer<typeof widgetFactory>({
containerNode: getContainerNode(container),
bodyContainerNode,
cssClasses,
Expand Down Expand Up @@ -259,9 +259,10 @@ const panel: PanelWidget = widgetParams => {
const [renderOptions] = args;

const options = {
...(widget.getWidgetRenderState
? widget.getWidgetRenderState(renderOptions)
: {}),
...renderOptions,
widgetRenderState: (widget.getWidgetRenderState?.(renderOptions) ||
{}) as any,
};

renderPanel({
Expand Down
8 changes: 5 additions & 3 deletions stories/panel.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ storiesOf('Basics/Panel', module)
withHits(
({ search, container }) => {
const breadcrumbInPanel = panel<typeof breadcrumb>({
collapsed({ widgetRenderState }) {
return widgetRenderState.canRefine;
collapsed({ canRefine }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This usage is much better and eventually what we want to enable in next major version so if there can't be collision with the IS instance (which is also passed and spread) that's great!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn't realize there cannot be collision. This looks so much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I can tell there's no collision here, so this doesn't even need a major :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it makes sense there is no collision :)

return canRefine === false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your example used to be wrong

},
templates: {
header: 'Collapsible panel',
header({ canRefine }) {
return `Breadcrumb that can${canRefine ? '' : "'t "} refine`;
},
footer:
'The panel collapses if it cannot refine. Click "Home". This panel will collapse and you will not see this footer anymore.',
},
Expand Down