diff --git a/src/components/Tab.module.css b/src/components/Tab.module.css index d2d0581..986d03e 100644 --- a/src/components/Tab.module.css +++ b/src/components/Tab.module.css @@ -18,5 +18,9 @@ &:hover { color: var(--color-primary-hover); } + &.disabled { + color: var(--color-primary-disabled); + cursor: not-allowed; + } } } diff --git a/src/components/Tab.tsx b/src/components/Tab.tsx index 38a4b02..00a1ae6 100644 --- a/src/components/Tab.tsx +++ b/src/components/Tab.tsx @@ -3,20 +3,30 @@ import { isEqual, isNil } from 'lodash-es'; import { useCallback, useEffect, useState } from 'react'; import styles from './Tab.module.css'; -type TabProps = { - tabs: { title: string; id: unknown }[]; - activeTab?: unknown; - onActive?: (id: unknown) => void; +type TabOption = { + title: string; + id: unknown; }; -export function Tab({ tabs = [], activeTab, onActive }: TabProps) { +type TabProps = { + tabs: TabOption[]; + activeTab?: unknown; + onActive?: (id: TabOption['id']) => void; + disabled?: { [d: keyof TabOption['id']]: boolean }; +}; + +export function Tab({ tabs = [], activeTab, onActive, disabled }: TabProps) { const [active, setActive] = useState(() => isNil(activeTab) ? 0 : tabs.findIndex((tab) => isEqual(tab.id, activeTab)), ); - const handleActivate = useCallback((index: number) => { - setActive(index); - onActive?.(tabs[index].id); - }, []); + const handleActivate = useCallback( + (index: number) => { + if (disabled?.[tabs[index].id] ?? false) return; + setActive(index); + onActive?.(tabs[index].id); + }, + [tabs, onActive, disabled], + ); useEffect(() => { const activeIndex = tabs.findIndex((tab) => isEqual(tab.id, activeTab)); @@ -30,7 +40,11 @@ export function Tab({ tabs = [], activeTab, onActive }: TabProps) { {tabs.map((tab, index) => (