Skip to content

Commit

Permalink
feat: remove title prop from Tabs (#1190)
Browse files Browse the repository at this point in the history
* feat: remove title prop from Tabs

* fix: cleaner logic for tab title
  • Loading branch information
jorgemoya authored Jul 31, 2024
1 parent 5c77f41 commit d01b4e0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 107 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-rings-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Remove title prop from Tabs, remove Tabs from /account since it's not needed.
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
'use client';

import {
createContext,
ReactNode,
RefObject,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import { createContext, ReactNode, useContext, useEffect, useState } from 'react';

import { State as AccountState } from '../_actions/submit-customer-change-password-form';

export const AccountStatusContext = createContext<{
activeTabRef: RefObject<HTMLAnchorElement>;
accountState: AccountState;
setAccountState: (state: AccountState | ((prevState: AccountState) => AccountState)) => void;
} | null>(null);
Expand All @@ -26,7 +17,6 @@ export const AccountStatusProvider = ({
isPermanentBanner?: boolean;
}) => {
const [accountState, setAccountState] = useState<AccountState>({ status: 'idle', message: '' });
const activeTabRef = useRef<HTMLAnchorElement>(null);

useEffect(() => {
if (accountState.status !== 'idle' && !isPermanentBanner) {
Expand All @@ -37,7 +27,7 @@ export const AccountStatusProvider = ({
}, [accountState, setAccountState, isPermanentBanner]);

return (
<AccountStatusContext.Provider value={{ accountState, setAccountState, activeTabRef }}>
<AccountStatusContext.Provider value={{ accountState, setAccountState }}>
{children}
</AccountStatusContext.Provider>
);
Expand Down

This file was deleted.

40 changes: 35 additions & 5 deletions core/app/[locale]/(default)/account/[tab]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { NextIntlClientProvider } from 'next-intl';
import { getMessages, getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { PropsWithChildren } from 'react';

import { Link } from '~/components/link';
import { LocaleType } from '~/i18n';
import { cn } from '~/lib/utils';

import { AccountStatusProvider } from './_components/account-status-provider';
import { AccountTabs } from './_components/account-tabs';

const tabList = [
'orders',
Expand All @@ -22,20 +23,49 @@ interface Props extends PropsWithChildren {
params: { locale: LocaleType; tab?: TabType };
}

export default async function AccountTabLayout({ children, params: { locale, tab } }: Props) {
export default async function AccountTabLayout({
children,
params: { locale, tab: activeTab },
}: Props) {
unstable_setRequestLocale(locale);

const t = await getTranslations({ locale, namespace: 'Account.Home' });

const messages = await getMessages();

const tabsTitles = {
orders: t('orders'),
messages: t('messages'),
addresses: t('addresses'),
wishlists: t('wishlists'),
'recently-viewed': t('recentlyViewed'),
settings: t('settings'),
};

return (
<NextIntlClientProvider locale={locale} messages={{ Account: messages.Account ?? {} }}>
<AccountStatusProvider>
<h1 className="my-8 text-4xl font-black lg:my-8 lg:text-5xl">{t('heading')}</h1>
<AccountTabs activeTab={tab} tabs={[...tabList]}>
{children}
</AccountTabs>
<nav aria-label={t('accountTabsLabel')}>
<ul className="mb-8 flex items-start overflow-x-auto">
{tabList.map((tab) => (
<li key={tab}>
<Link
className={cn(
'block whitespace-nowrap px-4 pb-2 font-semibold',
activeTab === tab && 'border-b-4 border-primary text-primary',
)}
href={`/account/${tab}`}
prefetch="viewport"
prefetchKind="full"
>
{tabsTitles[tab]}
</Link>
</li>
))}
</ul>
</nav>
{children}
</AccountStatusProvider>
</NextIntlClientProvider>
);
Expand Down
3 changes: 1 addition & 2 deletions core/components/ui/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ComponentPropsWithoutRef, ReactNode } from 'react';

interface Tab {
content: ReactNode;
title: ReactNode;
value: string;
}

Expand All @@ -26,7 +25,7 @@ export function Tabs({ className, defaultValue, label, tabs, ...props }: Props)
key={tab.value}
value={tab.value}
>
{tab.title}
{tab.value}
</TabsPrimitive.Trigger>
))}
</TabsPrimitive.List>
Expand Down
26 changes: 13 additions & 13 deletions core/tests/ui/desktop/e2e/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ test('My Account tabs are displayed and clickable', async ({ page }) => {
await loginWithUserAccount(page, testUserEmail, testUserPassword);

await expect(page).toHaveURL('/account/');
await expect(page.getByRole('heading', { name: 'Orders' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Messages' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Addresses' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Wish lists' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Recently viewed' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Account settings' })).toBeVisible();

await page.getByRole('heading', { name: 'Orders' }).click();
await expect(page.getByRole('link', { name: 'Orders' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Messages' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Addresses' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Wish lists' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Recently viewed' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Account settings' })).toBeVisible();

await page.getByRole('link', { name: 'Orders' }).click();
await expect(page).toHaveURL('/account/orders/');
await expect(page.getByRole('heading', { name: 'Orders' })).toBeVisible();

await page.getByRole('tab', { name: 'Messages' }).click();
await page.getByRole('link', { name: 'Messages' }).click();
await expect(page).toHaveURL('/account/messages/');
await expect(page.getByRole('heading', { name: 'Messages' })).toBeVisible();

await page.getByRole('tab', { name: 'Addresses' }).click();
await page.getByRole('link', { name: 'Addresses' }).click();
await expect(page).toHaveURL('/account/addresses/');
await expect(page.getByRole('heading', { name: 'Addresses' })).toBeVisible();

await page.getByRole('tab', { name: 'Wish lists' }).click();
await page.getByRole('link', { name: 'Wish lists' }).click();
await expect(page).toHaveURL('/account/wishlists/');
await expect(page.getByRole('heading', { name: 'Wish lists' })).toBeVisible();

await page.getByRole('tab', { name: 'Recently viewed' }).click();
await page.getByRole('link', { name: 'Recently viewed' }).click();
await expect(page).toHaveURL('/account/recently-viewed/');
await expect(page.getByRole('heading', { name: 'Recently viewed' })).toBeVisible();

await page.getByRole('tab', { name: 'Account settings' }).click();
await page.getByRole('link', { name: 'Account settings' }).click();
await expect(page).toHaveURL('/account/settings/');
await expect(page.getByRole('heading', { name: 'Account settings' })).toBeVisible();
});
Expand Down
23 changes: 0 additions & 23 deletions core/tests/visual-regression/components/tabs.spec.ts

This file was deleted.

Binary file not shown.

0 comments on commit d01b4e0

Please sign in to comment.