-
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 f4eb2ae
Showing
6 changed files
with
159 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,31 @@ | ||
import React from 'react'; | ||
import { act, fireEvent, render, screen } from '@testing-library/react'; | ||
Check failure on line 2 in packages/module/src/Compare/Compare.test.tsx GitHub Actions / call-build-lint-test-workflow / lint
|
||
import '@testing-library/jest-dom'; | ||
import Compare from './Compare'; | ||
|
||
const firstChild = ( | ||
<div> | ||
<h1>Child 1</h1> | ||
</div> | ||
); | ||
|
||
const secondChild = ( | ||
<div> | ||
<h1>Child 2</h1> | ||
</div> | ||
); | ||
|
||
describe('Compare', () => { | ||
it('should render compare correctly', () => { | ||
render( | ||
<Compare | ||
firstChildDisplayName="Child 1" | ||
secondChildDisplayName="Child 2" | ||
firstChild={firstChild} | ||
secondChild={secondChild} | ||
/> | ||
); | ||
expect(screen.getByRole('heading', { name: /Child 1/i })).toBeTruthy(); | ||
expect(screen.getByRole('heading', { name: /Child 2/i })).toBeTruthy(); | ||
}); | ||
}); |
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 { ToggleGroup, ToggleGroupItem } from '@patternfly/react-core'; | ||
|
||
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