-
Notifications
You must be signed in to change notification settings - Fork 25
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
e98c0df
commit 45d741e
Showing
5 changed files
with
128 additions
and
63 deletions.
There are no files selected for viewing
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
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,98 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { Flex, Stack, StackItem, ToggleGroup, ToggleGroupItem, Truncate } from '@patternfly/react-core'; | ||
Check failure on line 2 in packages/module/src/Compare/Compare.tsx GitHub Actions / call-build-lint-test-workflow / lint
Check failure on line 2 in packages/module/src/Compare/Compare.tsx GitHub Actions / call-build-lint-test-workflow / lint
Check failure on line 2 in packages/module/src/Compare/Compare.tsx GitHub Actions / call-build-lint-test-workflow / lint
|
||
|
||
interface CompareProps { | ||
/** First of two children to render */ | ||
firstChild: React.ReactNode; | ||
/** Second of two children to render */ | ||
secondChild: React.ReactNode; | ||
/** Display name for first child, used in mobile toggle */ | ||
firstChildDisplayName: string; | ||
/** Display name for second child, used in mobile toggle */ | ||
secondChildDisplayName: string; | ||
/** Aria label for mobile toggle group */ | ||
toggleGroupAriaLabel?: string; | ||
/** Callback for when mobile toggle is used */ | ||
onToggleClick?: (event: MouseEvent | React.MouseEvent<any, MouseEvent> | React.KeyboardEvent<Element>) => void; | ||
} | ||
|
||
export const Compare = ({ | ||
firstChild, | ||
secondChild, | ||
firstChildDisplayName, | ||
secondChildDisplayName, | ||
onToggleClick, | ||
toggleGroupAriaLabel = 'Select which chatbot to display' | ||
}: PropsWithChildren<CompareProps>) => { | ||
const [isSelected, setIsSelected] = React.useState('toggle-group-chatbot-1'); | ||
const [showFirstChatbot, setShowFirstChatbot] = React.useState(true); | ||
const [showSecondChatbot, setShowSecondChatbot] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
// we want to show the first if we switch to the mobile toggle view | ||
// and reset/switch back to normal otherwise | ||
const updateChatbotVisibility = () => { | ||
if (window.innerWidth >= 901) { | ||
setShowFirstChatbot(true); | ||
setShowSecondChatbot(true); | ||
} else { | ||
setShowFirstChatbot(true); | ||
setShowSecondChatbot(false); | ||
setIsSelected('toggle-group-chatbot-1'); | ||
} | ||
}; | ||
window.addEventListener('resize', updateChatbotVisibility); | ||
|
||
return () => { | ||
window.removeEventListener('resize', updateChatbotVisibility); | ||
}; | ||
}, []); | ||
|
||
// this only happens on mobile | ||
const handleChildToggleClick = ( | ||
event: MouseEvent | React.MouseEvent<any, MouseEvent> | React.KeyboardEvent<Element> | ||
) => { | ||
const id = event.currentTarget.id; | ||
setIsSelected(id); | ||
setShowSecondChatbot(!showSecondChatbot); | ||
setShowFirstChatbot(!showFirstChatbot); | ||
onToggleClick && onToggleClick(event); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="pf-chatbot__compare-mobile-controls"> | ||
<ToggleGroup aria-label={toggleGroupAriaLabel}> | ||
<ToggleGroupItem | ||
className="pf-chatbot__compare-toggle" | ||
text={firstChildDisplayName} | ||
buttonId="toggle-group-chatbot-1" | ||
isSelected={isSelected === 'toggle-group-chatbot-1'} | ||
onChange={handleChildToggleClick} | ||
/> | ||
<ToggleGroupItem | ||
className="pf-chatbot__compare-toggle" | ||
text={secondChildDisplayName} | ||
buttonId="toggle-group-chatbot-2" | ||
isSelected={isSelected === 'toggle-group-chatbot-2'} | ||
onChange={handleChildToggleClick} | ||
/> | ||
</ToggleGroup> | ||
</div> | ||
<div className="pf-chatbot__compare"> | ||
<div | ||
className={`pf-chatbot__compare-item ${!showFirstChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`} | ||
> | ||
{firstChild} | ||
</div> | ||
<div | ||
className={`pf-chatbot__compare-item ${!showSecondChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`} | ||
> | ||
{secondChild} | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Compare; |
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,2 @@ | ||
export { default } from './Compare'; | ||
export * from './Compare'; |
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