Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

components: Small Tabs and FormSelect updates #153

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "DoltHub"
},
"description": "A collection of React components for common tasks",
"version": "0.2.3",
"version": "0.2.4",
"repository": {
"type": "git",
"url": "git+https://github.com/dolthub/react-library.git"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export default function CustomOption<T, OptionType extends Option<T>>(
const label = `${props.labelPrefix}-${props.data.value}`;
return (
<div
className={cx({ [css.optionWithIconPath]: !!props.data.iconPath })}
className={cx({
[css.optionWithIconPath]: !!props.data.iconPath,
[css.optionWithDetails]: !!props.data.details,
})}
aria-label={label}
data-cy={label}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@
}
}

.optionWithDetails {
@apply mt-1;
}

.optionDetails {
@apply text-stone-500 text-xs mt-1 mb-1;
@apply text-stone-500 text-xs my-1.5;
}

.menuList {
@apply py-0;
}

.footer {
@apply border-t flex justify-center text-sm py-2;
@apply border-t flex justify-center text-sm py-2;
}

.noOpts {
Expand Down
29 changes: 21 additions & 8 deletions packages/components/src/Tabs/context.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { createCustomContext } from "@dolthub/react-contexts";
import { useContextWithError } from "@dolthub/react-hooks";
import React, { ReactNode, useMemo, useState } from "react";
import React, { ReactNode, useCallback, useMemo, useState } from "react";

type TabsContextType = {
activeTabIndex: number;
setActiveTabIndex: React.Dispatch<React.SetStateAction<number>>;
setActiveTabIndex: (i: number) => void;
};

export const TabsContext = createCustomContext<TabsContextType>("TabsContext");

type Props = {
children: ReactNode;
initialActiveIndex?: number;
afterSetTabIndex?: (i: number) => void;
};

export function TabsProvider(props: Props) {
const [activeTabIndex, setActiveTabIndex] = useState(
props.initialActiveIndex ?? 0,
export function TabsProvider({
children,
initialActiveIndex,
afterSetTabIndex,
}: Props) {
const [activeTabIndex, _setActiveTabIndex] = useState(
initialActiveIndex ?? 0,
);

const setActiveTabIndex = useCallback(
(i: number) => {
_setActiveTabIndex(i);
if (afterSetTabIndex) {
afterSetTabIndex(i);
}
},
[afterSetTabIndex],
);

const value = useMemo(() => {
Expand All @@ -26,9 +41,7 @@ export function TabsProvider(props: Props) {
};
}, [activeTabIndex, setActiveTabIndex]);

return (
<TabsContext.Provider value={value}>{props.children}</TabsContext.Provider>
);
return <TabsContext.Provider value={value}>{children}</TabsContext.Provider>;
}

export function useTabsContext(): TabsContextType {
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ type Props = {
children: ReactNode;
initialActiveIndex?: number;
className?: string;
afterSetTabIndex?: (i: number) => void;
};

function Tabs({ children, ...props }: Props) {
function Tabs({ children, className, ...props }: Props) {
return (
<TabsProvider {...props}>
<div className={cx(css.tabs, props.className)}>{children}</div>
<div className={cx(css.tabs, className)}>{children}</div>
</TabsProvider>
);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/__tests__/Tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const testingActiveTab = (

describe("test Tabs", () => {
it(`renders active tabs and panels correctly`, () => {
const afterSetTabIndex = jest.fn();
render(
<Tabs>
<Tabs afterSetTabIndex={afterSetTabIndex}>
<TabList>
{mocks.map((mock, i) => (
<Tab
Expand Down Expand Up @@ -58,6 +59,7 @@ describe("test Tabs", () => {
} else {
if (i !== 0) {
fireEvent.click(buttons[i]);
expect(afterSetTabIndex).toHaveBeenCalledWith(i);
}

testingActiveTab(listItems, i);
Expand All @@ -76,6 +78,7 @@ describe("test Tabs", () => {
}
}
});

it(`renders initialActiveIndex correctly`, () => {
render(
<Tabs initialActiveIndex={2}>
Expand Down
Loading