1+ import React from 'react' ;
2+ import Overflow from '../src' ;
3+ import '../assets/index.less' ;
4+ import './common.less' ;
5+
6+ interface ItemType {
7+ value : string | number ;
8+ label : string ;
9+ }
10+
11+ function createData ( count : number ) : ItemType [ ] {
12+ const data : ItemType [ ] = new Array ( count ) . fill ( undefined ) . map ( ( _ , index ) => ( {
13+ value : index ,
14+ label : `Item ${ index + 1 } ` ,
15+ } ) ) ;
16+
17+ return data ;
18+ }
19+
20+ function renderItem ( item : ItemType ) {
21+ return (
22+ < div
23+ style = { {
24+ margin : '0 4px' ,
25+ padding : '6px 12px' ,
26+ background : '#f0f0f0' ,
27+ border : '1px solid #d9d9d9' ,
28+ borderRadius : '4px' ,
29+ } }
30+ >
31+ { item . label }
32+ </ div >
33+ ) ;
34+ }
35+
36+ function renderRest ( items : ItemType [ ] ) {
37+ return (
38+ < div
39+ style = { {
40+ margin : '0 4px' ,
41+ padding : '6px 12px' ,
42+ background : '#fff2e8' ,
43+ border : '1px solid #ffbb96' ,
44+ borderRadius : '4px' ,
45+ color : '#d46b08' ,
46+ } }
47+ >
48+ +{ items . length } more
49+ </ div >
50+ ) ;
51+ }
52+
53+ const PrefixDemo = ( ) => {
54+ const [ itemCount , setItemCount ] = React . useState ( 8 ) ;
55+ const [ prefixType , setPrefixType ] = React . useState < 'none' | 'text' | 'icon' | 'complex' > ( 'text' ) ;
56+ const [ suffixType , setSuffixType ] = React . useState < 'none' | 'text' | 'button' > ( 'none' ) ;
57+
58+ const data = React . useMemo ( ( ) => createData ( itemCount ) , [ itemCount ] ) ;
59+
60+ const renderPrefix = ( ) => {
61+ switch ( prefixType ) {
62+ case 'text' :
63+ return (
64+ < div
65+ style = { {
66+ padding : '6px 12px' ,
67+ background : '#e6f7ff' ,
68+ border : '1px solid #91d5ff' ,
69+ borderRadius : '4px' ,
70+ color : '#1890ff' ,
71+ fontWeight : 'bold' ,
72+ } }
73+ >
74+ 标签:
75+ </ div >
76+ ) ;
77+ case 'icon' :
78+ return (
79+ < div
80+ style = { {
81+ padding : '6px 8px' ,
82+ background : '#f6ffed' ,
83+ border : '1px solid #b7eb8f' ,
84+ borderRadius : '4px' ,
85+ color : '#52c41a' ,
86+ } }
87+ >
88+ 🏷️
89+ </ div >
90+ ) ;
91+ case 'complex' :
92+ return (
93+ < div
94+ style = { {
95+ padding : '4px 8px' ,
96+ background : 'linear-gradient(45deg, #f0f0f0, #e6f7ff)' ,
97+ border : '1px solid #91d5ff' ,
98+ borderRadius : '4px' ,
99+ display : 'flex' ,
100+ alignItems : 'center' ,
101+ gap : '4px' ,
102+ } }
103+ >
104+ < span style = { { fontSize : '12px' } } > 📋</ span >
105+ < span style = { { fontSize : '12px' , color : '#1890ff' } } > 分类:</ span >
106+ </ div >
107+ ) ;
108+ default :
109+ return undefined ;
110+ }
111+ } ;
112+
113+ const renderSuffix = ( ) => {
114+ switch ( suffixType ) {
115+ case 'text' :
116+ return (
117+ < div
118+ style = { {
119+ padding : '6px 12px' ,
120+ background : '#fff1f0' ,
121+ border : '1px solid #ffccc7' ,
122+ borderRadius : '4px' ,
123+ color : '#f5222d' ,
124+ fontSize : '12px' ,
125+ } }
126+ >
127+ (总计)
128+ </ div >
129+ ) ;
130+ case 'button' :
131+ return (
132+ < button
133+ style = { {
134+ padding : '4px 8px' ,
135+ background : '#f0f0f0' ,
136+ border : '1px solid #d9d9d9' ,
137+ borderRadius : '4px' ,
138+ cursor : 'pointer' ,
139+ fontSize : '12px' ,
140+ } }
141+ onClick = { ( ) => alert ( '查看更多' ) }
142+ >
143+ 更多 →
144+ </ button >
145+ ) ;
146+ default :
147+ return undefined ;
148+ }
149+ } ;
150+
151+ return (
152+ < div style = { { padding : 32 } } >
153+ < h2 > Prefix Demo</ h2 >
154+
155+ < div style = { { marginBottom : 16 } } >
156+ < label style = { { marginRight : 8 } } > Items Count:</ label >
157+ < select
158+ value = { itemCount }
159+ onChange = { ( e ) => setItemCount ( Number ( e . target . value ) ) }
160+ style = { { marginRight : 16 , padding : '2px 4px' } }
161+ >
162+ < option value = { 3 } > 3</ option >
163+ < option value = { 5 } > 5</ option >
164+ < option value = { 8 } > 8</ option >
165+ < option value = { 12 } > 12</ option >
166+ < option value = { 20 } > 20</ option >
167+ </ select >
168+
169+ < label style = { { marginRight : 8 } } > Prefix:</ label >
170+ < select
171+ value = { prefixType }
172+ onChange = { ( e ) => setPrefixType ( e . target . value as any ) }
173+ style = { { marginRight : 16 , padding : '2px 4px' } }
174+ >
175+ < option value = "none" > None</ option >
176+ < option value = "text" > Text</ option >
177+ < option value = "icon" > Icon</ option >
178+ < option value = "complex" > Complex</ option >
179+ </ select >
180+
181+ < label style = { { marginRight : 8 } } > Suffix:</ label >
182+ < select
183+ value = { suffixType }
184+ onChange = { ( e ) => setSuffixType ( e . target . value as any ) }
185+ style = { { padding : '2px 4px' } }
186+ >
187+ < option value = "none" > None</ option >
188+ < option value = "text" > Text</ option >
189+ < option value = "button" > Button</ option >
190+ </ select >
191+ </ div >
192+
193+ < div
194+ style = { {
195+ border : '2px solid #1890ff' ,
196+ padding : 16 ,
197+ borderRadius : 8 ,
198+ background : '#fafafa' ,
199+ } }
200+ >
201+ < h3 style = { { margin : '0 0 16px 0' , color : '#1890ff' } } > Responsive Mode:</ h3 >
202+ < div
203+ style = { {
204+ border : '1px dashed #d9d9d9' ,
205+ padding : 12 ,
206+ maxWidth : 500 ,
207+ background : 'white' ,
208+ borderRadius : 4 ,
209+ } }
210+ >
211+ < Overflow < ItemType >
212+ data = { data }
213+ renderItem = { renderItem }
214+ renderRest = { renderRest }
215+ maxCount = "responsive"
216+ prefix = { renderPrefix ( ) }
217+ suffix = { renderSuffix ( ) }
218+ />
219+ </ div >
220+ </ div >
221+
222+ < div
223+ style = { {
224+ border : '2px solid #52c41a' ,
225+ padding : 16 ,
226+ borderRadius : 8 ,
227+ background : '#fafafa' ,
228+ marginTop : 24 ,
229+ } }
230+ >
231+ < h3 style = { { margin : '0 0 16px 0' , color : '#52c41a' } } > Fixed MaxCount (5):</ h3 >
232+ < div
233+ style = { {
234+ border : '1px dashed #d9d9d9' ,
235+ padding : 12 ,
236+ background : 'white' ,
237+ borderRadius : 4 ,
238+ } }
239+ >
240+ < Overflow < ItemType >
241+ data = { data }
242+ renderItem = { renderItem }
243+ renderRest = { renderRest }
244+ maxCount = { 5 }
245+ prefix = { renderPrefix ( ) }
246+ suffix = { renderSuffix ( ) }
247+ />
248+ </ div >
249+ </ div >
250+ </ div >
251+ ) ;
252+ } ;
253+
254+ export default PrefixDemo ;
0 commit comments