mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-19 11:58:41 +08:00
40207c80a4
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
* fix(tabs): adjust padding for nav add button * fix: lint fix * fix: lint fix * fix: lint fix * fix: lint fix * fix: lint fix * chore: adjust the used design token * docs(tabs): size demo improvement * test: update snapshot * test: update snapshot * test: update snapshot
114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import React, { useState } from 'react';
|
|
import type { RadioChangeEvent, TabsProps } from 'antd';
|
|
import { Radio, Tabs } from 'antd';
|
|
|
|
type TargetKey = React.MouseEvent | React.KeyboardEvent | string;
|
|
|
|
const App: React.FC = () => {
|
|
const [size, setSize] = useState<'small' | 'middle' | 'large'>('small');
|
|
const [activeKey, setActiveKey] = useState('1');
|
|
const [items, setItems] = useState<TabsProps['items']>([
|
|
{
|
|
label: 'Tab 1',
|
|
key: '1',
|
|
children: 'Content of editable tab 1',
|
|
},
|
|
{
|
|
label: 'Tab 2',
|
|
key: '2',
|
|
children: 'Content of editable tab 2',
|
|
},
|
|
{
|
|
label: 'Tab 3',
|
|
key: '3',
|
|
children: 'Content of editable tab 3',
|
|
},
|
|
]);
|
|
|
|
const add = () => {
|
|
const newKey = String((items || []).length + 1);
|
|
setItems([
|
|
...(items || []),
|
|
{
|
|
label: `Tab ${newKey}`,
|
|
key: newKey,
|
|
children: `Content of editable tab ${newKey}`,
|
|
},
|
|
]);
|
|
setActiveKey(newKey);
|
|
};
|
|
|
|
const remove = (targetKey: TargetKey) => {
|
|
if (!items) return;
|
|
const targetIndex = items.findIndex((item) => item.key === targetKey);
|
|
const newItems = items.filter((item) => item.key !== targetKey);
|
|
|
|
if (newItems.length && targetKey === activeKey) {
|
|
const newActiveKey =
|
|
newItems[targetIndex === newItems.length ? targetIndex - 1 : targetIndex].key;
|
|
setActiveKey(newActiveKey);
|
|
}
|
|
|
|
setItems(newItems);
|
|
};
|
|
|
|
const onEdit = (targetKey: TargetKey, action: 'add' | 'remove') => {
|
|
if (action === 'add') {
|
|
add();
|
|
} else {
|
|
remove(targetKey);
|
|
}
|
|
};
|
|
|
|
const onChange = (e: RadioChangeEvent) => {
|
|
setSize(e.target.value);
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<Radio.Group value={size} onChange={onChange} style={{ marginBottom: 16 }}>
|
|
<Radio.Button value="small">Small</Radio.Button>
|
|
<Radio.Button value="middle">Middle</Radio.Button>
|
|
<Radio.Button value="large">Large</Radio.Button>
|
|
</Radio.Group>
|
|
<Tabs
|
|
defaultActiveKey="1"
|
|
size={size}
|
|
style={{ marginBottom: 32 }}
|
|
items={new Array(3).fill(null).map((_, i) => {
|
|
const id = String(i + 1);
|
|
return {
|
|
label: `Tab ${id}`,
|
|
key: id,
|
|
children: `Content of tab ${id}`,
|
|
};
|
|
})}
|
|
/>
|
|
<Tabs
|
|
defaultActiveKey="1"
|
|
type="card"
|
|
size={size}
|
|
style={{ marginBottom: 32 }}
|
|
items={new Array(3).fill(null).map((_, i) => {
|
|
const id = String(i + 1);
|
|
return {
|
|
label: `Card Tab ${id}`,
|
|
key: id,
|
|
children: `Content of card tab ${id}`,
|
|
};
|
|
})}
|
|
/>
|
|
<Tabs
|
|
type="editable-card"
|
|
size={size}
|
|
activeKey={activeKey}
|
|
onChange={setActiveKey}
|
|
onEdit={onEdit}
|
|
items={items}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default App;
|