UNPKG

devextreme-react

Version:

DevExtreme React UI and Visualization Components

69 lines (67 loc) 2.53 kB
/*! * devextreme-react * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file in the root of the project for details. * * https://github.com/DevExpress/devextreme-react */ import * as React from 'react'; import { useImperativeHandle, forwardRef, useRef, useLayoutEffect, useCallback, } from 'react'; import { ComponentBase } from './component-base'; import { elementIsExtension } from './extension-component'; const Component = forwardRef((props, ref) => { const componentBaseRef = useRef(null); const extensionCreators = useRef([]); const registerExtension = useCallback((creator) => { extensionCreators.current.push(creator); }, [extensionCreators.current]); const createExtensions = useCallback(() => { extensionCreators.current.forEach((creator) => creator(componentBaseRef.current?.getElement())); }, [extensionCreators.current, componentBaseRef.current]); const renderChildren = useCallback(() => React.Children.map(props.children, (child) => { if (React.isValidElement(child) && elementIsExtension(child)) { return React.cloneElement(child, { onMounted: registerExtension }); } return child; }), [props, registerExtension]); const createWidget = useCallback((el) => { componentBaseRef.current?.createWidget(el); }, [componentBaseRef.current]); const clearExtensions = useCallback(() => { if (props.clearExtensions) { props.clearExtensions(); } extensionCreators.current = []; }, [ extensionCreators.current, props.clearExtensions, ]); useLayoutEffect(() => { createWidget(); createExtensions(); return () => { clearExtensions(); }; }, []); useImperativeHandle(ref, () => ({ getInstance() { return componentBaseRef.current?.getInstance(); }, getElement() { return componentBaseRef.current?.getElement(); }, createWidget(el) { createWidget(el); }, clearExtensions() { clearExtensions(); }, }), [componentBaseRef.current, createWidget, clearExtensions]); return (React.createElement(ComponentBase, { ref: componentBaseRef, renderChildren: renderChildren, ...props })); }); export { Component, };