forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0db5848
commit 0957101
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/plugins/discover/public/application/view_components/canvas/discover_actions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState, forwardRef, ReactNode, useImperativeHandle } from 'react'; | ||
|
||
interface DiscoverActionsProps { | ||
initialActions?: ReactNode[]; | ||
} | ||
|
||
interface RegisteredAction { | ||
id: string; | ||
action: ReactNode; | ||
} | ||
|
||
// Use forwardRef to allow the parent to pass a ref | ||
const DiscoverActions = forwardRef< | ||
{ registerAction: (action: ReactNode) => string }, | ||
DiscoverActionsProps | ||
>(({ initialActions = [] }, ref) => { | ||
const [registeredActions, setRegisteredActions] = useState<RegisteredAction[]>( | ||
initialActions.map((action, index) => ({ id: `action-${index}`, action })) | ||
); | ||
|
||
const registerAction = (action: ReactNode): string => { | ||
const id = `action-${registeredActions.length}`; | ||
setRegisteredActions([...registeredActions, { id, action }]); | ||
return id; | ||
}; | ||
|
||
// Expose the registerAction function to the parent component | ||
useImperativeHandle(ref, () => ({ | ||
registerAction, | ||
})); | ||
|
||
return ( | ||
<> | ||
{registeredActions.map(({ id, action }) => ( | ||
<div key={id}>{React.cloneElement(action as React.ReactElement, { key: id })}</div> | ||
))} | ||
</> | ||
); | ||
}); | ||
|
||
export { DiscoverActions }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters