From d18b79d6c707e50a44d06b4fedd9bbf2db9881c0 Mon Sep 17 00:00:00 2001 From: Anamika T S Date: Thu, 9 May 2024 13:39:41 +0530 Subject: [PATCH 1/2] refactor(useWebTerminal): add typescript types --- .../WebTerminal/hooks/{index.js => index.tsx} | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) rename packages/ibm-products/src/components/WebTerminal/hooks/{index.js => index.tsx} (68%) diff --git a/packages/ibm-products/src/components/WebTerminal/hooks/index.js b/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx similarity index 68% rename from packages/ibm-products/src/components/WebTerminal/hooks/index.js rename to packages/ibm-products/src/components/WebTerminal/hooks/index.tsx index cae8a7a15e..ac07e3e15f 100644 --- a/packages/ibm-products/src/components/WebTerminal/hooks/index.js +++ b/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx @@ -5,15 +5,34 @@ // LICENSE file in the root directory of this source tree. // -import React, { useState, useCallback, createContext } from 'react'; +import React, { useState, useCallback, createContext, ReactNode } from 'react'; import { useContext } from 'react'; import PropTypes from 'prop-types'; import { pkg } from '../../../settings'; -export const WebTerminalContext = createContext(); +export interface WebTerminalContextType { + open: boolean; + openWebTerminal: () => void; + closeWebTerminal: () => void; + toggleWebTerminal: () => void; +} + +export const WebTerminalContext = createContext( + null +); const componentName = 'WebTerminalProvider'; -export let WebTerminalProvider = ({ children }) => { + +interface WebTerminalProviderProps { + /** + * Provide your own terminal component as children to show up in the web terminal + */ + children: ReactNode | ReactNode[]; +} + +export let WebTerminalProvider: React.FC = ({ + children, +}) => { const [open, setOpen] = useState(false); const openWebTerminal = useCallback(() => setOpen(true), []); From ce36c40085ba87c182c33cbc3e15f67c46f54024 Mon Sep 17 00:00:00 2001 From: Anamika T S Date: Thu, 9 May 2024 19:27:38 +0530 Subject: [PATCH 2/2] refactor(useWebTerminal): resolve error in WebTerminal.tsx --- .../src/components/WebTerminal/WebTerminal.tsx | 4 ++-- .../src/components/WebTerminal/hooks/index.tsx | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/ibm-products/src/components/WebTerminal/WebTerminal.tsx b/packages/ibm-products/src/components/WebTerminal/WebTerminal.tsx index 987ba5431d..4ccc42b1fd 100644 --- a/packages/ibm-products/src/components/WebTerminal/WebTerminal.tsx +++ b/packages/ibm-products/src/components/WebTerminal/WebTerminal.tsx @@ -131,7 +131,7 @@ export let WebTerminal = React.forwardRef( */ useEffect(() => { if (isInitiallyOpen) { - openWebTerminal(); + openWebTerminal?.(); } }, []); // eslint-disable-line @@ -152,7 +152,7 @@ export let WebTerminal = React.forwardRef( if (prefersReducedMotion) { setRender(false); } - closeWebTerminal(); + closeWebTerminal?.(); }; return shouldRender ? ( diff --git a/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx b/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx index ac07e3e15f..cf4b6333e4 100644 --- a/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx +++ b/packages/ibm-products/src/components/WebTerminal/hooks/index.tsx @@ -11,15 +11,13 @@ import PropTypes from 'prop-types'; import { pkg } from '../../../settings'; export interface WebTerminalContextType { - open: boolean; - openWebTerminal: () => void; - closeWebTerminal: () => void; - toggleWebTerminal: () => void; + open?: boolean; + openWebTerminal?: () => void; + closeWebTerminal?: () => void; + toggleWebTerminal?: () => void; } -export const WebTerminalContext = createContext( - null -); +export const WebTerminalContext = createContext({}); const componentName = 'WebTerminalProvider';