Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/demo/prefix-suffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: prefix-suffix
nav:
title: Demo
path: /demo
---

# Prefix & Suffix Demo

This demo shows how to use `prefix` and `suffix` props with the Overflow component.

<code src="../../examples/prefix-demo.tsx"></code>
47 changes: 46 additions & 1 deletion examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ function renderRest(items: ItemType[]) {
const Demo = () => {
const [responsive, setResponsive] = React.useState(true);
const [data, setData] = React.useState(createData(1));
const [showPrefix, setShowPrefix] = React.useState(true);
const [showSuffix, setShowSuffix] = React.useState(true);

return (
<div style={{ padding: 32 }}>
Expand All @@ -59,6 +61,26 @@ const Demo = () => {
>
{responsive ? 'Responsive' : 'MaxCount: 6'}
</button>

<button
type="button"
onClick={() => {
setShowPrefix(!showPrefix);
}}
style={{ marginLeft: 8 }}
>
{showPrefix ? 'Hide Prefix' : 'Show Prefix'}
</button>

<button
type="button"
onClick={() => {
setShowSuffix(!showSuffix);
}}
style={{ marginLeft: 8 }}
>
{showSuffix ? 'Hide Suffix' : 'Show Suffix'}
</button>
<select
style={{ width: 200, height: 32 }}
value={data.length}
Expand Down Expand Up @@ -98,7 +120,30 @@ const Demo = () => {
renderItem={renderItem}
renderRest={renderRest}
maxCount={responsive ? 'responsive' : 6}
// suffix={<span>1</span>}
prefix={showPrefix ? (
<div
style={{
margin: '0 8px 0 0',
padding: '4px 8px',
background: 'rgba(0, 255, 0, 0.3)',
border: '1px solid green',
}}
>
前缀:
</div>
) : undefined}
suffix={showSuffix ? (
<div
style={{
margin: '0 0 0 8px',
padding: '4px 8px',
background: 'rgba(0, 0, 255, 0.3)',
border: '1px solid blue',
}}
>
后缀
</div>
) : undefined}
/>
</div>
</div>
Expand Down
254 changes: 254 additions & 0 deletions examples/prefix-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import React from 'react';
import Overflow from '../src';
import '../assets/index.less';
import './common.less';

interface ItemType {
value: string | number;
label: string;
}

function createData(count: number): ItemType[] {
const data: ItemType[] = new Array(count).fill(undefined).map((_, index) => ({
value: index,
label: `Item ${index + 1}`,
}));

return data;
}

function renderItem(item: ItemType) {
return (
<div
style={{
margin: '0 4px',
padding: '6px 12px',
background: '#f0f0f0',
border: '1px solid #d9d9d9',
borderRadius: '4px',
}}
>
{item.label}
</div>
);
}

function renderRest(items: ItemType[]) {
return (
<div
style={{
margin: '0 4px',
padding: '6px 12px',
background: '#fff2e8',
border: '1px solid #ffbb96',
borderRadius: '4px',
color: '#d46b08',
}}
>
+{items.length} more
</div>
);
}

const PrefixDemo = () => {
const [itemCount, setItemCount] = React.useState(8);
const [prefixType, setPrefixType] = React.useState<'none' | 'text' | 'icon' | 'complex'>('text');
const [suffixType, setSuffixType] = React.useState<'none' | 'text' | 'button'>('none');

const data = React.useMemo(() => createData(itemCount), [itemCount]);

const renderPrefix = () => {
switch (prefixType) {
case 'text':
return (
<div
style={{
padding: '6px 12px',
background: '#e6f7ff',
border: '1px solid #91d5ff',
borderRadius: '4px',
color: '#1890ff',
fontWeight: 'bold',
}}
>
标签:
</div>
);
case 'icon':
return (
<div
style={{
padding: '6px 8px',
background: '#f6ffed',
border: '1px solid #b7eb8f',
borderRadius: '4px',
color: '#52c41a',
}}
>
🏷️
</div>
);
case 'complex':
return (
<div
style={{
padding: '4px 8px',
background: 'linear-gradient(45deg, #f0f0f0, #e6f7ff)',
border: '1px solid #91d5ff',
borderRadius: '4px',
display: 'flex',
alignItems: 'center',
gap: '4px',
}}
>
<span style={{ fontSize: '12px' }}>📋</span>
<span style={{ fontSize: '12px', color: '#1890ff' }}>分类:</span>
</div>
);
default:
return undefined;
}
};

const renderSuffix = () => {
switch (suffixType) {
case 'text':
return (
<div
style={{
padding: '6px 12px',
background: '#fff1f0',
border: '1px solid #ffccc7',
borderRadius: '4px',
color: '#f5222d',
fontSize: '12px',
}}
>
(总计)
</div>
);
case 'button':
return (
<button
style={{
padding: '4px 8px',
background: '#f0f0f0',
border: '1px solid #d9d9d9',
borderRadius: '4px',
cursor: 'pointer',
fontSize: '12px',
}}
onClick={() => alert('查看更多')}
>
更多 →
</button>
);
default:
return undefined;
}
};

return (
<div style={{ padding: 32 }}>
<h2>Prefix Demo</h2>

<div style={{ marginBottom: 16 }}>
<label style={{ marginRight: 8 }}>Items Count:</label>
<select
value={itemCount}
onChange={(e) => setItemCount(Number(e.target.value))}
style={{ marginRight: 16, padding: '2px 4px' }}
>
<option value={3}>3</option>
<option value={5}>5</option>
<option value={8}>8</option>
<option value={12}>12</option>
<option value={20}>20</option>
</select>

<label style={{ marginRight: 8 }}>Prefix:</label>
<select
value={prefixType}
onChange={(e) => setPrefixType(e.target.value as any)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

onChange 事件处理器中,使用了 as any 类型断言,这会绕过 TypeScript 的类型检查。为了更好的类型安全,建议使用更具体的类型断言。

Suggested change
onChange={(e) => setPrefixType(e.target.value as any)}
onChange={(e) => setPrefixType(e.target.value as 'none' | 'text' | 'icon' | 'complex')}

style={{ marginRight: 16, padding: '2px 4px' }}
>
<option value="none">None</option>
<option value="text">Text</option>
<option value="icon">Icon</option>
<option value="complex">Complex</option>
</select>

<label style={{ marginRight: 8 }}>Suffix:</label>
<select
value={suffixType}
onChange={(e) => setSuffixType(e.target.value as any)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

同样,在 onChange 事件处理器中,使用了 as any 类型断言。建议使用更具体的类型断言以增强类型安全。

Suggested change
onChange={(e) => setSuffixType(e.target.value as any)}
onChange={(e) => setSuffixType(e.target.value as 'none' | 'text' | 'button')}

style={{ padding: '2px 4px' }}
>
<option value="none">None</option>
<option value="text">Text</option>
<option value="button">Button</option>
</select>
</div>

<div
style={{
border: '2px solid #1890ff',
padding: 16,
borderRadius: 8,
background: '#fafafa',
}}
>
<h3 style={{ margin: '0 0 16px 0', color: '#1890ff' }}>Responsive Mode:</h3>
<div
style={{
border: '1px dashed #d9d9d9',
padding: 12,
maxWidth: 500,
background: 'white',
borderRadius: 4,
}}
>
<Overflow<ItemType>
data={data}
renderItem={renderItem}
renderRest={renderRest}
maxCount="responsive"
prefix={renderPrefix()}
suffix={renderSuffix()}
/>
</div>
</div>

<div
style={{
border: '2px solid #52c41a',
padding: 16,
borderRadius: 8,
background: '#fafafa',
marginTop: 24,
}}
>
<h3 style={{ margin: '0 0 16px 0', color: '#52c41a' }}>Fixed MaxCount (5):</h3>
<div
style={{
border: '1px dashed #d9d9d9',
padding: 12,
background: 'white',
borderRadius: 4,
}}
>
<Overflow<ItemType>
data={data}
renderItem={renderItem}
renderRest={renderRest}
maxCount={5}
prefix={renderPrefix()}
suffix={renderSuffix()}
/>
</div>
</div>
</div>
);
};

export default PrefixDemo;
Loading
Loading