增加禁用标签页功能。
This commit is contained in:
parent
4b56d3a625
commit
e980eeec3d
|
@ -18,5 +18,9 @@
|
|||
&:hover {
|
||||
color: var(--color-primary-hover);
|
||||
}
|
||||
&.disabled {
|
||||
color: var(--color-primary-disabled);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) => (
|
||||
<div
|
||||
key={`tab_${index}_${tab.id}`}
|
||||
className={cx(styles.tab, isEqual(index, active) && styles.actived)}
|
||||
className={cx(
|
||||
styles.tab,
|
||||
isEqual(index, active) && styles.actived,
|
||||
(disabled?.[tab.id] ?? false) && styles.disabled,
|
||||
)}
|
||||
onClick={() => handleActivate(index)}>
|
||||
{tab.title}
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue
Block a user