diff --git a/src/components/DashKit/DashKit.tsx b/src/components/DashKit/DashKit.tsx index b34e0e8..4b856dc 100644 --- a/src/components/DashKit/DashKit.tsx +++ b/src/components/DashKit/DashKit.tsx @@ -33,6 +33,7 @@ interface DashKitDefaultProps { settings: SettingsProps; context: ContextProps; noOverlay: boolean; + hideWidgetOverlayControl: (args: {id: string; controlId: string}) => void; } export interface DashKitProps extends DashKitGeneralProps, Partial {} @@ -45,6 +46,7 @@ export class DashKit extends React.PureComponent { static defaultProps: DashKitDefaultProps = { onItemEdit: noop, onChange: noop, + hideWidgetOverlayControl: noop, defaultGlobalParams: {}, globalParams: {}, itemsStateAndParams: {}, @@ -125,4 +127,13 @@ export class DashKit extends React.PureComponent { getItemsMeta() { return this.metaRef.current?.getItemsMeta(); } + + /** + * public method for hiding overlayControl item by id + * for ex. can be triggered after data render + * @param args + */ + hideWidgetOverlayControl(args: {id: string; controlId: string}) { + return this.metaRef.current?.hideWidgetOverlayControl(args); + } } diff --git a/src/components/DashKit/__stories__/DashKitShowcase.tsx b/src/components/DashKit/__stories__/DashKitShowcase.tsx index 20793da..b645ebb 100644 --- a/src/components/DashKit/__stories__/DashKitShowcase.tsx +++ b/src/components/DashKit/__stories__/DashKitShowcase.tsx @@ -170,6 +170,17 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> { ? 'Disable action panel' : 'Enable action panel'} + {this.state.lastAction} @@ -366,6 +377,12 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> { this.setState({enableActionPanel: !this.state.enableActionPanel}); } + private hideWidgetOverlayControl(args: {id: string; controlId: string}) { + if (this.dashKitRef.current) { + this.dashKitRef.current.hideWidgetOverlayControl(args); + } + } + private getActionPanelItems() { return [ { diff --git a/src/components/GridLayout/GridLayout.js b/src/components/GridLayout/GridLayout.js index 6881326..d526b0e 100644 --- a/src/components/GridLayout/GridLayout.js +++ b/src/components/GridLayout/GridLayout.js @@ -69,6 +69,12 @@ export default class GridLayout extends React.PureComponent { return getItemsMeta(this.pluginsRefs); }; + hideWidgetOverlayControl = (args) => { + const {hideWidgetOverlayControl} = this.context; + const plugin = this.pluginsRefs.find((item) => item.props.id); + return hideWidgetOverlayControl({...args, plugin}); + }; + reloadItems() { const { editMode, diff --git a/src/hocs/withContext.js b/src/hocs/withContext.js index f82a392..7a01194 100644 --- a/src/hocs/withContext.js +++ b/src/hocs/withContext.js @@ -14,6 +14,10 @@ function useMemoStateContext(props) { const originalLayouts = React.useRef({}); const adjustedLayouts = React.useRef({}); + const [filteredOverlayControls, setFilteredOverlayControls] = React.useState( + props.overlayControls || [], + ); + // TODO: need move originalLayouts, adjustedLayouts to state const [layoutUpdateCounter, forceUpdateLayout] = React.useState(0); @@ -156,6 +160,19 @@ function useMemoStateContext(props) { .filter(Boolean); }, []); + const hideWidgetOverlayControl = React.useCallback( + (args) => { + const newOverlayControls = props.overlayControls.allWidgetsControls.filter( + (item) => item.id !== args.controlId, + ); + setFilteredOverlayControls({ + ...props.overlayControls, + allWidgetsControls: newOverlayControls, + }); + }, + [props.overlayControls], + ); + const resultLayout = React.useMemo(() => { return props.layout.map((item) => { if (item.i in adjustedLayouts.current) { @@ -193,6 +210,8 @@ function useMemoStateContext(props) { editItem: props.onItemEdit, layoutChange: onLayoutChange, getItemsMeta, + hideWidgetOverlayControl, + filteredOverlayControls, reloadItems, memorizeOriginalLayout, revertToOriginalLayout, @@ -215,6 +234,8 @@ function useMemoStateContext(props) { props.onItemEdit, onLayoutChange, getItemsMeta, + hideWidgetOverlayControl, + filteredOverlayControls, reloadItems, memorizeOriginalLayout, revertToOriginalLayout, @@ -227,10 +248,11 @@ function useMemoStateContext(props) { export function withContext(Component) { const WithContext = (props) => { const contextValue = useMemoStateContext(props); + const overlayControls = contextValue.filteredOverlayControls; return ( - + ); };