-
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.
Output SQL console results in react-table (#161)
- Loading branch information
Showing
20 changed files
with
707 additions
and
344 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,85 @@ | ||
import { render, screen } from 'test/testUtils'; | ||
import CrateTabsShad, { CrateTabsShadProps } from './CrateTabsShad'; | ||
|
||
const defaultProps: CrateTabsShadProps = { | ||
items: [ | ||
{ | ||
key: 'tab1', | ||
label: 'Tab 1', | ||
content: <div data-testid="tab_1_content">tab 1</div>, | ||
}, | ||
{ | ||
key: 'tab2', | ||
label: 'Tab 2', | ||
content: <div data-testid="tab_2_content">tab 2</div>, | ||
}, | ||
{ | ||
key: 'tab3', | ||
label: 'Tab 3', | ||
content: <div data-testid="tab_3_content">tab 3</div>, | ||
}, | ||
], | ||
}; | ||
|
||
const setup = (props: Partial<CrateTabsShadProps> = {}) => { | ||
const combinedProps = { ...defaultProps, ...props }; | ||
|
||
return render(<CrateTabsShad {...combinedProps} />); | ||
}; | ||
|
||
describe('The CrateTabsShad component', () => { | ||
it('preselects the first tab if no initialActiveTab prop is passed', () => { | ||
setup(); | ||
|
||
expect(screen.getAllByRole('tab')[0].getAttribute('aria-selected')).toBe('true'); | ||
expect(screen.getAllByRole('tab')[1].getAttribute('aria-selected')).toBe( | ||
'false', | ||
); | ||
expect(screen.getAllByRole('tab')[2].getAttribute('aria-selected')).toBe( | ||
'false', | ||
); | ||
}); | ||
|
||
it('preselects the tab using the value of the initialActiveTab prop', () => { | ||
setup({ initialActiveTab: 'tab2' }); | ||
|
||
expect(screen.getAllByRole('tab')[0].getAttribute('aria-selected')).toBe( | ||
'false', | ||
); | ||
expect(screen.getAllByRole('tab')[1].getAttribute('aria-selected')).toBe('true'); | ||
expect(screen.getAllByRole('tab')[2].getAttribute('aria-selected')).toBe( | ||
'false', | ||
); | ||
}); | ||
|
||
describe('when the hideWhenSingleTab prop is true', () => { | ||
it('displays the tabs when there are multiple tabs', () => { | ||
setup({ hideWhenSingleTab: true }); | ||
|
||
expect(screen.getAllByRole('tab').length).toBe(3); | ||
}); | ||
|
||
it('hides the tabs when there is only one', () => { | ||
setup({ | ||
hideWhenSingleTab: true, | ||
items: [ | ||
{ | ||
key: 'tab1', | ||
label: 'Tab 1', | ||
content: <div data-testid="tab_1_content">tab 1</div>, | ||
}, | ||
], | ||
}); | ||
|
||
expect(screen.getByRole('tablist')).toHaveClass('hidden'); | ||
}); | ||
}); | ||
|
||
describe('when the stickyTabBar prop is true', () => { | ||
it('applies the sticky CSS classes to the tablist', () => { | ||
setup({ stickyTabBar: true }); | ||
|
||
expect(screen.getByTestId('tabs-container')).toHaveClass('flex'); | ||
}); | ||
}); | ||
}); |
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,93 @@ | ||
import * as React from 'react'; | ||
import * as TabsPrimitive from '@radix-ui/react-tabs'; | ||
|
||
import { cn } from 'utils'; | ||
|
||
export type CrateTabShadProps = { | ||
key: string; | ||
label: React.ReactNode | string; | ||
content: React.ReactNode; | ||
}; | ||
|
||
export type CrateTabsShadProps = { | ||
initialActiveTab?: string; | ||
hideWhenSingleTab?: boolean; // don't show the tab bar if there is only one tab | ||
items: CrateTabShadProps[]; | ||
stickyTabBar?: boolean; // make the tab bar sticky | ||
}; | ||
|
||
function CrateTabsShad({ | ||
initialActiveTab, | ||
hideWhenSingleTab = false, | ||
items = [], | ||
stickyTabBar = false, | ||
}: CrateTabsShadProps) { | ||
return ( | ||
<Tabs | ||
defaultValue={initialActiveTab || items[0].key} | ||
className={stickyTabBar ? 'flex h-full w-full flex-col' : ''} | ||
data-testid="tabs-container" | ||
> | ||
<TabsList | ||
className={hideWhenSingleTab && items.length == 1 ? 'hidden' : 'border-b'} | ||
> | ||
{items.map(item => ( | ||
<TabsTrigger key={item.key} value={item.key}> | ||
{item.label} | ||
</TabsTrigger> | ||
))} | ||
</TabsList> | ||
{items.map(item => ( | ||
<TabsContent | ||
key={item.key} | ||
value={item.key} | ||
className={stickyTabBar ? 'h-full w-full overflow-hidden' : ''} | ||
> | ||
{item.content} | ||
</TabsContent> | ||
))} | ||
</Tabs> | ||
); | ||
} | ||
|
||
// below here are the shadcn components, with only the tailwind class names | ||
// changed for our own use case | ||
|
||
const Tabs = TabsPrimitive.Root; | ||
|
||
const TabsList = React.forwardRef< | ||
React.ElementRef<typeof TabsPrimitive.List>, | ||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List> | ||
>(({ className, ...props }, ref) => ( | ||
<TabsPrimitive.List | ||
ref={ref} | ||
className={cn('flex h-10 justify-start gap-4 px-2', className)} | ||
{...props} | ||
/> | ||
)); | ||
TabsList.displayName = TabsPrimitive.List.displayName; | ||
|
||
const TabsTrigger = React.forwardRef< | ||
React.ElementRef<typeof TabsPrimitive.Trigger>, | ||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger> | ||
>(({ className, ...props }, ref) => ( | ||
<TabsPrimitive.Trigger | ||
ref={ref} | ||
className={cn( | ||
'border-b-2 border-transparent data-[state=active]:border-crate-blue', | ||
className, | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName; | ||
|
||
const TabsContent = React.forwardRef< | ||
React.ElementRef<typeof TabsPrimitive.Content>, | ||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content> | ||
>(({ className, ...props }, ref) => ( | ||
<TabsPrimitive.Content ref={ref} className={cn('', className)} {...props} /> | ||
)); | ||
TabsContent.displayName = TabsPrimitive.Content.displayName; | ||
|
||
export default CrateTabsShad; |
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,3 @@ | ||
import CrateTabsShad from './CrateTabsShad'; | ||
|
||
export default CrateTabsShad; |
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
Oops, something went wrong.