diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/button.ts b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/button.ts new file mode 100644 index 000000000000..75e101617849 --- /dev/null +++ b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/button.ts @@ -0,0 +1,10 @@ +import type { Properties as ButtonProperties } from '@js/ui/button'; +import dxButton from '@js/ui/button'; + +import { InfernoWrapper } from './widget_wrapper'; + +export class Button extends InfernoWrapper { + protected getComponentFabric(): typeof dxButton { + return dxButton; + } +} diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/template_wrapper.tsx b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/template_wrapper.tsx new file mode 100644 index 000000000000..a6684fa54a11 --- /dev/null +++ b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/template_wrapper.tsx @@ -0,0 +1,37 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ +/* eslint-disable @typescript-eslint/ban-types */ +import type { dxElementWrapper } from '@js/core/renderer'; +import $ from '@js/core/renderer'; +import { Component, createRef } from 'inferno'; + +interface TemplateType { + render: (args: { model: T; container: dxElementWrapper }) => void; +} + +// eslint-disable-next-line max-len +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types +export function TemplateWrapper(template: TemplateType) { + return class Template extends Component { + private readonly ref = createRef(); + + private renderTemplate(): void { + $(this.ref.current!).empty(); + template.render({ + container: $(this.ref.current!), + model: this.props, + }); + } + + public render(): JSX.Element { + return
; + } + + public componentDidUpdate(): void { + this.renderTemplate(); + } + + public componentDidMount(): void { + this.renderTemplate(); + } + }; +} diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/widget_wrapper.tsx b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/widget_wrapper.tsx new file mode 100644 index 000000000000..1ed801dbf5d3 --- /dev/null +++ b/packages/devextreme/js/__internal/grids/new/grid_core/inferno_wrappers/widget_wrapper.tsx @@ -0,0 +1,54 @@ +import type DOMComponent from '@js/core/dom_component'; +import type { InfernoNode, RefObject } from 'inferno'; +import { Component, createRef } from 'inferno'; + +interface WithRef { + componentRef?: RefObject; +} + +export abstract class InfernoWrapper< + TProperties, + TComponent extends DOMComponent, +> extends Component> { + protected readonly ref = createRef(); + + protected component?: TComponent; + + protected abstract getComponentFabric(): new ( + element: Element, options: TProperties + ) => TComponent; + + public render(): InfernoNode { + return
; + } + + private updateComponentRef(): void { + if (this.props.componentRef) { + // @ts-expect-error + this.props.componentRef.current = this.component; + } + } + + protected updateComponentOptions(prevProps: TProperties, props: TProperties): void { + Object.keys(props as object).forEach((key) => { + if (props[key] !== prevProps[key]) { + this.component?.option(key, props[key]); + } + }); + } + + public componentDidMount(): void { + // eslint-disable-next-line no-new, @typescript-eslint/no-non-null-assertion + this.component = new (this.getComponentFabric())(this.ref.current!, this.props); + this.updateComponentRef(); + } + + public componentDidUpdate(prevProps: TProperties): void { + this.updateComponentOptions(prevProps, this.props); + this.updateComponentRef(); + } + + public componentWillUnmount(): void { + this.component?.dispose(); + } +}