Skip to content

Commit

Permalink
feat(tabs): disabled view (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Feb 18, 2021
1 parent c603562 commit 8bdb1a7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 56 deletions.
10 changes: 5 additions & 5 deletions packages/tabs/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const titles = Array(7).fill(0).map((_, i) => ({ title: `Таб ${i + 1}`
<Tab title='Таб 1' id='tab-1'>
Таб 1
</Tab>
<Tab title='Таб 2' id='tab-2'>
<Tab title='Таб 2' id='tab-2' disabled>
Таб 2
</Tab>
<Tab title='Таб 3' id='tab-3'>
Expand All @@ -45,7 +45,7 @@ export const titles = Array(7).fill(0).map((_, i) => ({ title: `Таб ${i + 1}`
<Tab title='Таб 4' id='tab-4'>
Таб 4
</Tab>
<Tab title='Таб 5' id='tab-5'>
<Tab title='Таб 5' id='tab-5' disabled>
Таб 5
</Tab>
</TabsDesktop>
Expand All @@ -71,7 +71,7 @@ export const titles = Array(7).fill(0).map((_, i) => ({ title: `Таб ${i + 1}`
<Tab title='Таб 2' id='tab-2'>
Таб 2
</Tab>
<Tab title='Таб 3' id='tab-3'>
<Tab title='Таб 3' id='tab-3' disabled>
Таб 3
</Tab>
<Tab title='Таб 4' id='tab-4'>
Expand Down Expand Up @@ -107,10 +107,10 @@ export const titles = Array(7).fill(0).map((_, i) => ({ title: `Таб ${i + 1}`
<Tab title='Таб 3' id='tab-3'>
Таб 3
</Tab>
<Tab title='Таб 4' id='tab-4'>
<Tab title='Таб 4' id='tab-4' disabled>
Таб 4
</Tab>
<Tab title='Таб 5' id='tab-5'>
<Tab title='Таб 5' id='tab-5' disabled>
Таб 5
</Tab>
</TabsResponsive>
Expand Down
5 changes: 4 additions & 1 deletion packages/tabs/src/components/primary-tablist/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ export const PrimaryTabList = ({
type='button'
className={cn(styles.title, {
[styles.selected]: item.id === selectedId,
[styles.disabled]: item.disabled,
})}
>
<span className={cn(focused && styles.focused)}>{item.title}</span>
<span className={focused ? styles.focused : undefined}>
{item.title}
</span>
{item.rightAddons && (
<span className={styles.rightAddons}>{item.rightAddons}</span>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
cursor: pointer;
outline: none;

&:hover {
&:not(.disabled):hover {
color: var(--primary-tablist-hover-color);
}
}
Expand All @@ -52,6 +52,11 @@
color: var(--primary-tablist-selected-color);
}

.disabled {
cursor: var(--disabled-cursor);
color: var(--primary-tablist-disabled-color);
}

.line {
position: absolute;
height: 3px;
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/src/components/tab/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TabProps } from '../../typings';

import styles from './index.module.css';

export const Tab = ({ children, hidden, className }: TabProps) => (
export const Tab = ({ children, hidden, className, disabled }: TabProps) => (
<div
className={cn(
styles.component,
Expand All @@ -15,7 +15,7 @@ export const Tab = ({ children, hidden, className }: TabProps) => (
)}
hidden={hidden}
role='tabpanel'
tabIndex={0}
tabIndex={disabled ? -1 : 0}
>
{children}
</div>
Expand Down
3 changes: 2 additions & 1 deletion packages/tabs/src/components/tabs/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const Tabs = ({
onChange,
}: Omit<TabsProps, 'view'>) => {
const tabsArray = React.Children.toArray(children) as TabsProps['children'];
const titles = tabsArray.map(({ props: { title, id, rightAddons } }) => ({
const titles = tabsArray.map(({ props: { title, id, rightAddons, disabled } }) => ({
title,
id,
disabled,
rightAddons,
}));
const tabs = tabsArray.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ exports[`Tabs Snapshots tests should match snapshot 1`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 1
</span>
<span
Expand All @@ -35,9 +33,7 @@ exports[`Tabs Snapshots tests should match snapshot 1`] = `
tabindex="0"
type="button"
>
<span
class=""
>
<span>
Таб 2
</span>
</button>
Expand All @@ -48,9 +44,7 @@ exports[`Tabs Snapshots tests should match snapshot 1`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 3
</span>
</button>
Expand All @@ -61,9 +55,7 @@ exports[`Tabs Snapshots tests should match snapshot 1`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 4
</span>
</button>
Expand All @@ -74,9 +66,7 @@ exports[`Tabs Snapshots tests should match snapshot 1`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 5
</span>
</button>
Expand Down Expand Up @@ -200,9 +190,7 @@ exports[`Tabs Snapshots tests should match snapshot 3`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 1
</span>
<span>
Expand All @@ -211,14 +199,12 @@ exports[`Tabs Snapshots tests should match snapshot 3`] = `
</button>
<button
aria-selected="true"
class="undefined"
class=""
role="tab"
tabindex="0"
type="button"
>
<span
class=""
>
<span>
Таб 2
</span>
</button>
Expand All @@ -229,9 +215,7 @@ exports[`Tabs Snapshots tests should match snapshot 3`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 3
</span>
</button>
Expand All @@ -242,9 +226,7 @@ exports[`Tabs Snapshots tests should match snapshot 3`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 4
</span>
</button>
Expand All @@ -255,9 +237,7 @@ exports[`Tabs Snapshots tests should match snapshot 3`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 5
</span>
</button>
Expand Down Expand Up @@ -382,9 +362,7 @@ exports[`Tabs Snapshots tests should match snapshot 5`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 1
</span>
<span
Expand All @@ -400,9 +378,7 @@ exports[`Tabs Snapshots tests should match snapshot 5`] = `
tabindex="0"
type="button"
>
<span
class=""
>
<span>
Таб 2
</span>
</button>
Expand All @@ -413,9 +389,7 @@ exports[`Tabs Snapshots tests should match snapshot 5`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 3
</span>
</button>
Expand All @@ -426,9 +400,7 @@ exports[`Tabs Snapshots tests should match snapshot 5`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 4
</span>
</button>
Expand All @@ -439,9 +411,7 @@ exports[`Tabs Snapshots tests should match snapshot 5`] = `
tabindex="-1"
type="button"
>
<span
class=""
>
<span>
Таб 5
</span>
</button>
Expand Down
6 changes: 6 additions & 0 deletions packages/tabs/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export type TabProps = {
*/
className?: string;

/**
* Блокирует таб
*/
disabled?: boolean;

/**
* Управление видимостью таба
*/
Expand Down Expand Up @@ -118,6 +123,7 @@ export type TabListProps = Pick<
titles?: Array<{
title: string;
id: SelectedId;
disabled?: boolean;
rightAddons?: ReactNode;
}>;
};
Expand Down
10 changes: 10 additions & 0 deletions packages/tabs/src/useTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function useTabs({ titles = [], selectedId, onChange }: UseTabsProps) {
(position: 'prev' | 'next' | 'start' | 'end') => {
const refs = itemRefs.current;

if (refs.every(ref => ref.disabled)) return;

let focusedTabIndex = refs.findIndex(node => document.activeElement === node);

if (focusedTabIndex === -1) {
Expand All @@ -49,6 +51,12 @@ export function useTabs({ titles = [], selectedId, onChange }: UseTabsProps) {
break;
}

const shift = ['prev', 'end'].includes(position) ? -1 : 1;

while (refs[newFocusIndex].disabled) {
newFocusIndex = (refs.length + newFocusIndex + shift) % refs.length;
}

refs[newFocusIndex].focus();

setFocusedTab(refs[newFocusIndex]);
Expand Down Expand Up @@ -89,10 +97,12 @@ export function useTabs({ titles = [], selectedId, onChange }: UseTabsProps) {
const getTabListItemProps = (index: number, outerRef?: MutableRefObject<HTMLElement>) => {
const item = titles[index];
const itemSelected = item.id === selectedId;

return {
role: 'tab',
tabIndex: itemSelected ? 0 : -1,
'aria-selected': itemSelected,
disabled: item.disabled,
ref: (node: HTMLButtonElement) => {
// eslint-disable-next-line no-param-reassign
if (outerRef) outerRef.current = node;
Expand Down
1 change: 1 addition & 0 deletions packages/tabs/src/vars.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--primary-tablist-color: var(--color-light-text-secondary);
--primary-tablist-hover-color: var(--color-light-text-primary);
--primary-tablist-selected-color: var(--color-light-text-primary);
--primary-tablist-disabled-color: var(--color-light-text-tertiary);
--primary-tablist-bottom-border-color: var(--color-light-border-primary);
--primary-tablist-line-color: var(--color-light-border-accent);

Expand Down

0 comments on commit 8bdb1a7

Please sign in to comment.