@fibrae/semi-ui-svelte 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/README.zh-CN.md +8 -4
- package/dist/components/anchor/interface.d.ts +14 -0
- package/dist/components/audio-player/interface.d.ts +5 -0
- package/dist/components/auto-complete/interface.d.ts +29 -0
- package/dist/components/avatar/interface.d.ts +24 -0
- package/dist/components/backtop/interface.d.ts +2 -0
- package/dist/components/badge/interface.d.ts +7 -0
- package/dist/components/banner/interface.d.ts +7 -0
- package/dist/components/breadcrumb/interface.d.ts +10 -0
- package/dist/components/button/interface.d.ts +18 -0
- package/dist/components/calendar/interface.d.ts +16 -0
- package/dist/components/card/interface.d.ts +18 -0
- package/dist/components/carousel/interface.d.ts +16 -0
- package/dist/components/cascader/cascader.svelte +1 -0
- package/dist/components/cascader/interface.d.ts +57 -0
- package/dist/components/checkbox/interface.d.ts +19 -0
- package/dist/components/code-highlight/interface.d.ts +3 -0
- package/dist/components/collapse/interface.d.ts +16 -0
- package/dist/components/collapsible/interface.d.ts +10 -0
- package/dist/components/color-picker/interface.d.ts +10 -0
- package/dist/components/config-provider/interface.d.ts +7 -0
- package/dist/components/cropper/interface.d.ts +11 -0
- package/dist/components/date-picker/interface.d.ts +47 -0
- package/dist/components/descriptions/interface.d.ts +6 -0
- package/dist/components/divider/interface.d.ts +4 -0
- package/dist/components/dropdown/interface.d.ts +8 -0
- package/dist/components/empty/interface.d.ts +7 -0
- package/dist/components/feedback/interface.d.ts +16 -0
- package/dist/components/float-button/interface.d.ts +14 -0
- package/dist/components/form/interface.d.ts +47 -0
- package/dist/components/form/label.svelte +2 -2
- package/dist/components/grid/interface.d.ts +13 -0
- package/dist/components/highlight/interface.d.ts +7 -0
- package/dist/components/hot-keys/interface.d.ts +3 -0
- package/dist/components/image/interface.d.ts +59 -0
- package/dist/components/input/interface.d.ts +67 -0
- package/dist/components/input-number/interface.d.ts +24 -0
- package/dist/components/json-viewer/interface.d.ts +7 -0
- package/dist/components/layout/interface.d.ts +3 -0
- package/dist/components/list/interface.d.ts +18 -0
- package/dist/components/locale/interface.d.ts +4 -0
- package/dist/components/lottie/interface.d.ts +2 -0
- package/dist/components/markdown-render/interface.d.ts +5 -0
- package/dist/components/modal/interface.d.ts +29 -0
- package/dist/components/navigation/interface.d.ts +47 -0
- package/dist/components/overflow-list/interface.d.ts +11 -0
- package/dist/components/pagination/interface.d.ts +12 -0
- package/dist/components/pincode/interface.d.ts +8 -0
- package/dist/components/popconfirm/interface.d.ts +12 -0
- package/dist/components/popover/interface.d.ts +30 -0
- package/dist/components/progress/interface.d.ts +13 -0
- package/dist/components/radio/interface.d.ts +22 -0
- package/dist/components/rating/interface.d.ts +12 -0
- package/dist/components/resizable/interface.d.ts +30 -0
- package/dist/components/scroll-list/interface.d.ts +6 -0
- package/dist/components/select/interface.d.ts +63 -0
- package/dist/components/side-sheet/interface.d.ts +19 -0
- package/dist/components/skeleton/interface.d.ts +7 -0
- package/dist/components/slider/interface.d.ts +23 -0
- package/dist/components/space/interface.d.ts +4 -0
- package/dist/components/spin/interface.d.ts +7 -0
- package/dist/components/steps/interface.d.ts +9 -0
- package/dist/components/switch/interface.d.ts +7 -0
- package/dist/components/table/interface.d.ts +48 -0
- package/dist/components/tabs/interface.d.ts +29 -0
- package/dist/components/tag/interface.d.ts +22 -0
- package/dist/components/tag-input/interface.d.ts +29 -0
- package/dist/components/time-picker/interface.d.ts +31 -0
- package/dist/components/timeline/interface.d.ts +5 -0
- package/dist/components/tooltip/interface.d.ts +20 -0
- package/dist/components/tooltip/tooltip.svelte +3 -0
- package/dist/components/transfer/interface.d.ts +18 -0
- package/dist/components/tree/interface.d.ts +45 -0
- package/dist/components/tree-select/interface.d.ts +55 -0
- package/dist/components/typography/interface.d.ts +33 -0
- package/dist/components/upload/interface.d.ts +64 -0
- package/dist/components/user-guide/interface.d.ts +15 -0
- package/dist/components/video-player/interface.d.ts +22 -0
- package/dist/styles.css +1 -1
- package/docs/advanced/dark-mode.md +288 -0
- package/docs/basic/button.md +605 -0
- package/docs/basic/divider.md +103 -0
- package/docs/basic/float-button.md +246 -0
- package/docs/basic/grid.md +427 -0
- package/docs/basic/layout.md +459 -0
- package/docs/basic/resizable.md +584 -0
- package/docs/basic/space.md +163 -0
- package/docs/basic/tokens.md +425 -0
- package/docs/basic/typography.md +615 -0
- package/docs/changes-from-semi.md +105 -0
- package/docs/ecosystem/tailwind.md +210 -0
- package/docs/experience/accessibility.md +82 -0
- package/docs/experience/content-guidelines.md +819 -0
- package/docs/experience/internationalization.md +7 -0
- package/docs/feedback/banner.md +198 -0
- package/docs/feedback/feedback.md +294 -0
- package/docs/feedback/notification.md +401 -0
- package/docs/feedback/popconfirm.md +243 -0
- package/docs/feedback/progress.md +389 -0
- package/docs/feedback/skeleton.md +388 -0
- package/docs/feedback/spin.md +172 -0
- package/docs/feedback/toast.md +393 -0
- package/docs/input/auto-complete.md +399 -0
- package/docs/input/cascader.md +1558 -0
- package/docs/input/checkbox.md +408 -0
- package/docs/input/color-picker.md +175 -0
- package/docs/input/date-picker.md +942 -0
- package/docs/input/form.md +2550 -0
- package/docs/input/input-number.md +451 -0
- package/docs/input/input.md +640 -0
- package/docs/input/pincode.md +191 -0
- package/docs/input/radio.md +337 -0
- package/docs/input/rating.md +214 -0
- package/docs/input/select.md +1230 -0
- package/docs/input/slider.md +347 -0
- package/docs/input/switch.md +206 -0
- package/docs/input/tag-input.md +465 -0
- package/docs/input/time-picker.md +358 -0
- package/docs/input/transfer.md +1061 -0
- package/docs/input/tree-select.md +1300 -0
- package/docs/input/upload.md +1811 -0
- package/docs/llms.txt +147 -0
- package/docs/navigation/anchor.md +377 -0
- package/docs/navigation/backtop.md +100 -0
- package/docs/navigation/breadcrumb.md +284 -0
- package/docs/navigation/navigation.md +851 -0
- package/docs/navigation/pagination.md +217 -0
- package/docs/navigation/steps.md +315 -0
- package/docs/navigation/tabs.md +754 -0
- package/docs/navigation/tree.md +1956 -0
- package/docs/other/config-provider.md +599 -0
- package/docs/other/locale.md +353 -0
- package/docs/plus/audio-player.md +155 -0
- package/docs/plus/code-highlight.md +150 -0
- package/docs/plus/drag-move.md +214 -0
- package/docs/plus/hot-keys.md +174 -0
- package/docs/plus/json-viewer.md +392 -0
- package/docs/plus/lottie.md +176 -0
- package/docs/plus/markdown-render.md +240 -0
- package/docs/plus/video-player.md +325 -0
- package/docs/show/avatar.md +475 -0
- package/docs/show/badge.md +276 -0
- package/docs/show/calendar.md +352 -0
- package/docs/show/card.md +485 -0
- package/docs/show/carousel.md +592 -0
- package/docs/show/collapse.md +289 -0
- package/docs/show/collapsible.md +216 -0
- package/docs/show/cropper.md +285 -0
- package/docs/show/descriptions.md +325 -0
- package/docs/show/dropdown.md +466 -0
- package/docs/show/empty.md +225 -0
- package/docs/show/highlight.md +144 -0
- package/docs/show/image.md +433 -0
- package/docs/show/list.md +927 -0
- package/docs/show/modal.md +517 -0
- package/docs/show/overflow-list.md +272 -0
- package/docs/show/popover.md +534 -0
- package/docs/show/scroll-list.md +156 -0
- package/docs/show/side-sheet.md +274 -0
- package/docs/show/table.md +5437 -0
- package/docs/show/tag.md +428 -0
- package/docs/show/timeline.md +277 -0
- package/docs/show/tooltip.md +442 -0
- package/docs/show/user-guide.md +419 -0
- package/docs/start/getting-started.md +234 -0
- package/docs/start/introduction.md +57 -0
- package/docs/start/overview.md +21 -0
- package/package.json +5 -4
|
@@ -0,0 +1,927 @@
|
|
|
1
|
+
# List
|
|
2
|
+
|
|
3
|
+
> Lists display a set of related contents
|
|
4
|
+
|
|
5
|
+
Package: `@fibrae/semi-ui-svelte`. Web page: https://semi-design-next.pages.dev/en-US/svelte/show/list/
|
|
6
|
+
|
|
7
|
+
## Demos
|
|
8
|
+
|
|
9
|
+
### How to import
|
|
10
|
+
|
|
11
|
+
```svelte
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { List, ListItem } from '@fibrae/semi-ui-svelte';
|
|
14
|
+
</script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Basic Usage
|
|
18
|
+
|
|
19
|
+
You can use `size` to size list. Supported values include `large`, `default`, `small`. Customize the header and footer with the `header` and `footer` snippets, and render each entry of `dataSource` with the `item` snippet (argument `{ item, index }`).
|
|
20
|
+
|
|
21
|
+
```svelte
|
|
22
|
+
<script lang="ts">
|
|
23
|
+
import { List } from '@fibrae/semi-ui-svelte';
|
|
24
|
+
|
|
25
|
+
const data = ['从明天起,做一个幸福的人', '喂马,劈柴,周游世界', '从明天起,关心粮食和蔬菜', '我有一所房子,面朝大海,春暖花开'];
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<div>
|
|
29
|
+
<div style="margin-inline-end: 16px">
|
|
30
|
+
<h3 style="margin-block-end: 16px">Default Size</h3>
|
|
31
|
+
<List bordered dataSource={data}>
|
|
32
|
+
{#snippet header()}
|
|
33
|
+
<div>Header</div>
|
|
34
|
+
{/snippet}
|
|
35
|
+
{#snippet footer()}
|
|
36
|
+
<div>Footer</div>
|
|
37
|
+
{/snippet}
|
|
38
|
+
{#snippet item(row)}
|
|
39
|
+
<List.Item>{row.item}</List.Item>
|
|
40
|
+
{/snippet}
|
|
41
|
+
</List>
|
|
42
|
+
</div>
|
|
43
|
+
<div style="margin-inline-end: 16px">
|
|
44
|
+
<h3 style="margin: 16px 0">Small Size</h3>
|
|
45
|
+
<List bordered dataSource={data} size="small">
|
|
46
|
+
{#snippet header()}
|
|
47
|
+
<div>Header</div>
|
|
48
|
+
{/snippet}
|
|
49
|
+
{#snippet footer()}
|
|
50
|
+
<div>Footer</div>
|
|
51
|
+
{/snippet}
|
|
52
|
+
{#snippet item(row)}
|
|
53
|
+
<List.Item>{row.item}</List.Item>
|
|
54
|
+
{/snippet}
|
|
55
|
+
</List>
|
|
56
|
+
</div>
|
|
57
|
+
<div style="margin-inline-end: 16px">
|
|
58
|
+
<h3 style="margin: 16px 0">Large Size</h3>
|
|
59
|
+
<List bordered dataSource={data} size="large">
|
|
60
|
+
{#snippet header()}
|
|
61
|
+
<div>Header</div>
|
|
62
|
+
{/snippet}
|
|
63
|
+
{#snippet footer()}
|
|
64
|
+
<div>Footer</div>
|
|
65
|
+
{/snippet}
|
|
66
|
+
{#snippet item(row)}
|
|
67
|
+
<List.Item>{row.item}</List.Item>
|
|
68
|
+
{/snippet}
|
|
69
|
+
</List>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Template
|
|
75
|
+
|
|
76
|
+
ListItem has a built-in template provided by the `header`, `main` and `extra` snippets. The alignment of `header` and `main` set by `align` properties using one of `flex-start`(default), `flex-end`, `center`, `baseline`, and `stretch` .
|
|
77
|
+
|
|
78
|
+
```svelte
|
|
79
|
+
<script lang="ts">
|
|
80
|
+
import { Avatar, Button, ButtonGroup, List } from '@fibrae/semi-ui-svelte';
|
|
81
|
+
|
|
82
|
+
const text = 'Semi Design Next 是 Semi Design 的社区版设计系统,一套组件在 React、Vue、Solid、Svelte 中原生可用。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。';
|
|
83
|
+
|
|
84
|
+
const data = [
|
|
85
|
+
{
|
|
86
|
+
text,
|
|
87
|
+
style: 'color: var(--semi-color-text-2); margin: 4px 0; width: 420px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis',
|
|
88
|
+
},
|
|
89
|
+
{ text, style: 'color: var(--semi-color-text-2); margin: 4px 0; width: 500px' },
|
|
90
|
+
{
|
|
91
|
+
text: 'Semi Design 以用户中心、内容优先、设计人性化的设计系统,打造一致、好看、好用、高效的用户体验。',
|
|
92
|
+
style: 'color: var(--semi-color-text-2); margin: 4px 0; width: 500px',
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<div style="padding: 12px; border: 1px solid var(--semi-color-border); margin: 12px">
|
|
98
|
+
<List dataSource={data}>
|
|
99
|
+
{#snippet item(row)}
|
|
100
|
+
<List.Item>
|
|
101
|
+
{#snippet header()}
|
|
102
|
+
<Avatar color="blue">SE</Avatar>
|
|
103
|
+
{/snippet}
|
|
104
|
+
{#snippet main()}
|
|
105
|
+
<div>
|
|
106
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">示例标题</span>
|
|
107
|
+
<p style={row.item.style}>{row.item.text}</p>
|
|
108
|
+
</div>
|
|
109
|
+
{/snippet}
|
|
110
|
+
{#snippet extra()}
|
|
111
|
+
<ButtonGroup theme="borderless">
|
|
112
|
+
<Button>编辑</Button>
|
|
113
|
+
<Button>更多</Button>
|
|
114
|
+
</ButtonGroup>
|
|
115
|
+
{/snippet}
|
|
116
|
+
</List.Item>
|
|
117
|
+
{/snippet}
|
|
118
|
+
</List>
|
|
119
|
+
</div>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Layout
|
|
123
|
+
|
|
124
|
+
Use `layout` property to set list layout, one of `vertical`(default) or `horizontal`.
|
|
125
|
+
|
|
126
|
+
```svelte
|
|
127
|
+
<script lang="ts">
|
|
128
|
+
import { Avatar, List, type AvatarColor } from '@fibrae/semi-ui-svelte';
|
|
129
|
+
|
|
130
|
+
interface Item {
|
|
131
|
+
title: string;
|
|
132
|
+
color: AvatarColor;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const data: Item[] = [
|
|
136
|
+
{ title: 'Semi Design Title 1', color: 'light-blue' },
|
|
137
|
+
{ title: 'Semi Design Title 2', color: 'grey' },
|
|
138
|
+
{ title: 'Semi Design Title 3', color: 'light-green' },
|
|
139
|
+
];
|
|
140
|
+
</script>
|
|
141
|
+
|
|
142
|
+
<div style="padding: 12px; border: 1px solid var(--semi-color-border); margin: 12px">
|
|
143
|
+
<List dataSource={data} layout="horizontal">
|
|
144
|
+
{#snippet item(row)}
|
|
145
|
+
<List.Item>
|
|
146
|
+
{#snippet header()}
|
|
147
|
+
<Avatar color={row.item.color}>SE</Avatar>
|
|
148
|
+
{/snippet}
|
|
149
|
+
{#snippet main()}
|
|
150
|
+
<div>
|
|
151
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">{row.item.title}</span>
|
|
152
|
+
<p style="color: var(--semi-color-text-2); margin: 4px 0">Semi Design 设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。</p>
|
|
153
|
+
</div>
|
|
154
|
+
{/snippet}
|
|
155
|
+
</List.Item>
|
|
156
|
+
{/snippet}
|
|
157
|
+
</List>
|
|
158
|
+
</div>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Grid
|
|
162
|
+
|
|
163
|
+
Use `grid` property to set grid layout. Use `span` to set the number of occupying spaces for each item and `gutter` for spacing between items.
|
|
164
|
+
|
|
165
|
+
```svelte
|
|
166
|
+
<script lang="ts">
|
|
167
|
+
import { Button, ButtonGroup, Descriptions, List, Rating } from '@fibrae/semi-ui-svelte';
|
|
168
|
+
|
|
169
|
+
const data = [
|
|
170
|
+
{ title: 'Semi UI', rating: 4.5, feedbacks: 124 },
|
|
171
|
+
{ title: 'Semi Icons', rating: 4, feedbacks: 108 },
|
|
172
|
+
{ title: 'Semi Theme', rating: 4.5, feedbacks: 244 },
|
|
173
|
+
];
|
|
174
|
+
|
|
175
|
+
const grid = { gutter: 12, span: 6 };
|
|
176
|
+
|
|
177
|
+
const itemStyle = 'border: 1px solid var(--semi-color-border); background-color: var(--semi-color-bg-2); border-radius: 3px; padding-inline-start: 20px';
|
|
178
|
+
</script>
|
|
179
|
+
|
|
180
|
+
<div>
|
|
181
|
+
<List dataSource={data} {grid}>
|
|
182
|
+
{#snippet item(row)}
|
|
183
|
+
<List.Item style={itemStyle}>
|
|
184
|
+
<div>
|
|
185
|
+
<h3 style="color: var(--semi-color-text-0); font-weight: 500">{row.item.title}</h3>
|
|
186
|
+
<Descriptions align="center" data={[{ key: '满意度' }, { key: '反馈数', value: row.item.feedbacks }]} row size="small">
|
|
187
|
+
{#snippet value(cell)}
|
|
188
|
+
{#if cell.index === 0}
|
|
189
|
+
<Rating allowHalf size="small" value={row.item.rating} />
|
|
190
|
+
{:else}
|
|
191
|
+
{cell.item.value}
|
|
192
|
+
{/if}
|
|
193
|
+
{/snippet}
|
|
194
|
+
</Descriptions>
|
|
195
|
+
<div style="margin: 12px 0; display: flex; justify-content: flex-end">
|
|
196
|
+
<ButtonGroup style="margin-block-start: 8px" theme="borderless">
|
|
197
|
+
<Button>编辑</Button>
|
|
198
|
+
<Button>更多</Button>
|
|
199
|
+
</ButtonGroup>
|
|
200
|
+
</div>
|
|
201
|
+
</div>
|
|
202
|
+
</List.Item>
|
|
203
|
+
{/snippet}
|
|
204
|
+
</List>
|
|
205
|
+
</div>
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Responsive List
|
|
209
|
+
|
|
210
|
+
Refer to [Grid](../basic/grid.md) for responsive dimensions.
|
|
211
|
+
|
|
212
|
+
```svelte
|
|
213
|
+
<script lang="ts">
|
|
214
|
+
import { Button, ButtonGroup, Descriptions, List, Rating } from '@fibrae/semi-ui-svelte';
|
|
215
|
+
|
|
216
|
+
const data = [
|
|
217
|
+
{ title: '审核管理平台', rating: 4.5, feedbacks: 124 },
|
|
218
|
+
{ title: '扁鹊', rating: 4, feedbacks: 108 },
|
|
219
|
+
{ title: '直播审核平台', rating: 3.5, feedbacks: 244 },
|
|
220
|
+
{ title: '短视频安全测试', feedbacks: 189 },
|
|
221
|
+
{ title: '内容平台', rating: 3, feedbacks: 128 },
|
|
222
|
+
{ title: '策略平台', rating: 4, feedbacks: 156 },
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
const grid = { gutter: 12, xs: 0, sm: 0, md: 12, lg: 8, xl: 8, xxl: 6 };
|
|
226
|
+
|
|
227
|
+
const style = 'border: 1px solid var(--semi-color-border); background-color: var(--semi-color-bg-2); border-radius: 3px; padding-inline-start: 20px; margin: 8px 2px';
|
|
228
|
+
</script>
|
|
229
|
+
|
|
230
|
+
<div>
|
|
231
|
+
<List dataSource={data} {grid}>
|
|
232
|
+
{#snippet item(row)}
|
|
233
|
+
<List.Item {style}>
|
|
234
|
+
<div>
|
|
235
|
+
<h3 style="color: var(--semi-color-text-0); font-weight: 500">{row.item.title}</h3>
|
|
236
|
+
<Descriptions align="center" data={[{ key: '满意度' }, { key: '反馈数', value: row.item.feedbacks }]} row size="small">
|
|
237
|
+
{#snippet value(cell)}
|
|
238
|
+
{#if cell.index === 0}
|
|
239
|
+
<Rating allowHalf size="small" value={row.item.rating} />
|
|
240
|
+
{:else}
|
|
241
|
+
{cell.item.value}
|
|
242
|
+
{/if}
|
|
243
|
+
{/snippet}
|
|
244
|
+
</Descriptions>
|
|
245
|
+
<div style="margin: 12px 0; display: flex; justify-content: flex-end">
|
|
246
|
+
<ButtonGroup style="margin-block-start: 8px" theme="borderless">
|
|
247
|
+
<Button>编辑</Button>
|
|
248
|
+
<Button>更多</Button>
|
|
249
|
+
</ButtonGroup>
|
|
250
|
+
</div>
|
|
251
|
+
</div>
|
|
252
|
+
</List.Item>
|
|
253
|
+
{/snippet}
|
|
254
|
+
</List>
|
|
255
|
+
</div>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Load More
|
|
259
|
+
|
|
260
|
+
You can use the `loadMore` snippet to achieve loading state for more incoming contents.
|
|
261
|
+
|
|
262
|
+
```svelte
|
|
263
|
+
<script lang="ts">
|
|
264
|
+
import { onDestroy } from 'svelte';
|
|
265
|
+
import { Avatar, Button, List, Skeleton, type AvatarColor } from '@fibrae/semi-ui-svelte';
|
|
266
|
+
|
|
267
|
+
interface Book {
|
|
268
|
+
color?: AvatarColor;
|
|
269
|
+
title?: string;
|
|
270
|
+
loading: boolean;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const count = 3;
|
|
274
|
+
const data: Book[] = [];
|
|
275
|
+
for (let i = 0; i < 40; i++) {
|
|
276
|
+
data.push({ color: 'grey', title: `Semi Design Title ${i}`, loading: false });
|
|
277
|
+
}
|
|
278
|
+
const placeholders: Book[] = [0, 1, 2].map(() => ({ loading: true }));
|
|
279
|
+
|
|
280
|
+
const fetchData = (page: number) =>
|
|
281
|
+
new Promise<Book[]>((resolve) => {
|
|
282
|
+
setTimeout(() => resolve(data.slice(page * count, page * count + count)), 1000);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
let dataSource = $state.raw<Book[]>([]);
|
|
286
|
+
let loading = $state(true);
|
|
287
|
+
let noMore = $state(false);
|
|
288
|
+
let page = 0;
|
|
289
|
+
let disposed = false;
|
|
290
|
+
onDestroy(() => {
|
|
291
|
+
disposed = true;
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const load = async () => {
|
|
295
|
+
loading = true;
|
|
296
|
+
const newDataSource = await fetchData(page);
|
|
297
|
+
if (disposed) return;
|
|
298
|
+
dataSource = [...dataSource, ...newDataSource];
|
|
299
|
+
loading = false;
|
|
300
|
+
noMore = !newDataSource.length;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const onLoadMore = () => {
|
|
304
|
+
page += 1;
|
|
305
|
+
void load();
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
void load();
|
|
309
|
+
</script>
|
|
310
|
+
|
|
311
|
+
<List dataSource={loading ? [...dataSource, ...placeholders] : dataSource} {loading}>
|
|
312
|
+
{#snippet item(row)}
|
|
313
|
+
<Skeleton loading={row.item.loading}>
|
|
314
|
+
{#snippet placeholder()}
|
|
315
|
+
<div style="display: flex; align-items: flex-start; padding: 12px; border-bottom: 1px solid var(--semi-color-border)">
|
|
316
|
+
<Skeleton.Avatar style="margin-inline-end: 12px" />
|
|
317
|
+
<div>
|
|
318
|
+
<Skeleton.Title style="width: 120px; margin-block-end: 12px; margin-block-start: 12px" />
|
|
319
|
+
<Skeleton.Paragraph rows={2} style="width: 600px" />
|
|
320
|
+
</div>
|
|
321
|
+
</div>
|
|
322
|
+
{/snippet}
|
|
323
|
+
<List.Item>
|
|
324
|
+
{#snippet header()}
|
|
325
|
+
<Avatar color={row.item.color}>SE</Avatar>
|
|
326
|
+
{/snippet}
|
|
327
|
+
{#snippet main()}
|
|
328
|
+
<div>
|
|
329
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">{row.item.title}</span>
|
|
330
|
+
<p style="color: var(--semi-color-text-2); margin: 4px 0">Semi Design 设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。</p>
|
|
331
|
+
</div>
|
|
332
|
+
{/snippet}
|
|
333
|
+
</List.Item>
|
|
334
|
+
</Skeleton>
|
|
335
|
+
{/snippet}
|
|
336
|
+
{#snippet loadMore()}
|
|
337
|
+
{#if !loading && !noMore}
|
|
338
|
+
<div style="text-align: center; margin-block-start: 12px; height: 32px; line-height: 32px">
|
|
339
|
+
<Button onclick={onLoadMore}>显示更多</Button>
|
|
340
|
+
</div>
|
|
341
|
+
{/if}
|
|
342
|
+
{/snippet}
|
|
343
|
+
</List>
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
### Scroll to Load
|
|
347
|
+
|
|
348
|
+
You can listen to the `onscroll` event of the scroll container and load the next page near the bottom. Recommended interaction could be reveal a loadmore button after three scrolling loads.
|
|
349
|
+
|
|
350
|
+
```svelte
|
|
351
|
+
<script lang="ts">
|
|
352
|
+
import { onDestroy } from 'svelte';
|
|
353
|
+
import { Avatar, Button, List, Spin } from '@fibrae/semi-ui-svelte';
|
|
354
|
+
|
|
355
|
+
interface Book {
|
|
356
|
+
title: string;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const count = 5;
|
|
360
|
+
const dataList: Book[] = [];
|
|
361
|
+
for (let i = 0; i < 100; i++) {
|
|
362
|
+
dataList.push({ title: `Semi Design Title ${i}` });
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const fetchData = (page: number) =>
|
|
366
|
+
new Promise<Book[]>((resolve) => {
|
|
367
|
+
setTimeout(() => resolve(dataList.slice(page * count, page * count + count)), 1000);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
let dataSource = $state.raw<Book[]>([]);
|
|
371
|
+
let loading = $state(true);
|
|
372
|
+
let hasMore = $state(true);
|
|
373
|
+
let page = $state(0);
|
|
374
|
+
let disposed = false;
|
|
375
|
+
onDestroy(() => {
|
|
376
|
+
disposed = true;
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
const load = async () => {
|
|
380
|
+
loading = true;
|
|
381
|
+
const newDataSource = await fetchData(page);
|
|
382
|
+
if (disposed) return;
|
|
383
|
+
dataSource = [...dataSource, ...newDataSource];
|
|
384
|
+
loading = false;
|
|
385
|
+
hasMore = !!newDataSource.length;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const onLoadMore = () => {
|
|
389
|
+
page += 1;
|
|
390
|
+
void load();
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const showLoadMore = $derived((page + 1) % 4 === 0);
|
|
394
|
+
|
|
395
|
+
const onScroll = (event: Event) => {
|
|
396
|
+
const el = event.currentTarget as HTMLElement;
|
|
397
|
+
if (loading || !hasMore || showLoadMore) return;
|
|
398
|
+
if (el.scrollHeight - el.scrollTop - el.clientHeight <= 20) onLoadMore();
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
void load();
|
|
402
|
+
</script>
|
|
403
|
+
|
|
404
|
+
<section aria-label="可滚动列表" class="light-scrollbar" style="height: 420px; overflow: auto; border: 1px solid var(--semi-color-border); padding: 10px" tabindex="0" onscroll={onScroll}>
|
|
405
|
+
<List {dataSource}>
|
|
406
|
+
{#snippet item(row)}
|
|
407
|
+
<List.Item>
|
|
408
|
+
{#snippet header()}
|
|
409
|
+
<Avatar color="grey">SE</Avatar>
|
|
410
|
+
{/snippet}
|
|
411
|
+
{#snippet main()}
|
|
412
|
+
<div>
|
|
413
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">{row.item.title}</span>
|
|
414
|
+
<p style="color: var(--semi-color-text-2); margin: 4px 0">Semi Design 设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。</p>
|
|
415
|
+
</div>
|
|
416
|
+
{/snippet}
|
|
417
|
+
</List.Item>
|
|
418
|
+
{/snippet}
|
|
419
|
+
{#snippet loadMore()}
|
|
420
|
+
{#if !loading && hasMore && showLoadMore}
|
|
421
|
+
<div style="text-align: center; margin-block-start: 12px; height: 32px; line-height: 32px">
|
|
422
|
+
<Button onclick={onLoadMore}>显示更多</Button>
|
|
423
|
+
</div>
|
|
424
|
+
{/if}
|
|
425
|
+
{/snippet}
|
|
426
|
+
</List>
|
|
427
|
+
{#if loading && hasMore}
|
|
428
|
+
<div style="text-align: center">
|
|
429
|
+
<Spin />
|
|
430
|
+
</div>
|
|
431
|
+
{/if}
|
|
432
|
+
</section>
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### Scroll to Load Infinite Lists
|
|
436
|
+
|
|
437
|
+
You can integrate [TanStack Virtual](https://tanstack.com/virtual) (`@tanstack/svelte-virtual`) to implement infinite scrolling lists with virtualization, which renders only the visible rows and improves the performance for large amounts of data. `createVirtualizer` returns a store; read it as `$virtualizer`.
|
|
438
|
+
|
|
439
|
+
```svelte
|
|
440
|
+
<script lang="ts">
|
|
441
|
+
import { untrack } from 'svelte';
|
|
442
|
+
import { SvelteSet } from 'svelte/reactivity';
|
|
443
|
+
import { createVirtualizer } from '@tanstack/svelte-virtual';
|
|
444
|
+
import { Avatar, List, Spin } from '@fibrae/semi-ui-svelte';
|
|
445
|
+
|
|
446
|
+
interface Book {
|
|
447
|
+
title: string;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const rowCount = 50;
|
|
451
|
+
const rowHeight = 118;
|
|
452
|
+
// Rows fetched ahead of the window so they arrive before they scroll into view
|
|
453
|
+
const prefetch = 15;
|
|
454
|
+
const dataList: Book[] = Array.from({ length: rowCount }, (_, i) => ({ title: `Semi Design Title ${i}` }));
|
|
455
|
+
|
|
456
|
+
let scroller = $state<HTMLElement>();
|
|
457
|
+
const loaded = new SvelteSet<number>();
|
|
458
|
+
// A requested index is never fetched again, even while pending
|
|
459
|
+
const requested = new SvelteSet<number>();
|
|
460
|
+
const virtualizer = createVirtualizer<HTMLElement, Element>({ count: rowCount, getScrollElement: () => scroller ?? null, estimateSize: () => rowHeight, overscan: 2 });
|
|
461
|
+
|
|
462
|
+
// The scroller exists only after mount, so the virtualizer is handed it then. setOptions updates the store this
|
|
463
|
+
// effect would otherwise be subscribed to, so it runs untracked.
|
|
464
|
+
$effect(() => {
|
|
465
|
+
if (scroller) untrack(() => $virtualizer.setOptions({ getScrollElement: () => scroller ?? null }));
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
const items = $derived($virtualizer.getVirtualItems());
|
|
469
|
+
// Indexes rather than virtual items: the items are new objects on every scroll
|
|
470
|
+
const rows = $derived(items.filter((item) => loaded.has(item.index)).map((item) => item.index));
|
|
471
|
+
|
|
472
|
+
$effect(() => {
|
|
473
|
+
if (!items.length) return;
|
|
474
|
+
const first = items[0].index;
|
|
475
|
+
const last = Math.min(items[items.length - 1].index + prefetch, rowCount - 1);
|
|
476
|
+
// requested is written here, so reading it must not make this effect depend on it
|
|
477
|
+
const wanted = untrack(() => {
|
|
478
|
+
const fresh: number[] = [];
|
|
479
|
+
for (let i = first; i <= last; i++) {
|
|
480
|
+
if (!requested.has(i)) {
|
|
481
|
+
requested.add(i);
|
|
482
|
+
fresh.push(i);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return fresh;
|
|
486
|
+
});
|
|
487
|
+
if (!wanted.length) return;
|
|
488
|
+
setTimeout(() => {
|
|
489
|
+
for (const i of wanted) loaded.add(i);
|
|
490
|
+
}, 1000);
|
|
491
|
+
});
|
|
492
|
+
</script>
|
|
493
|
+
|
|
494
|
+
<div style="border: 1px solid var(--semi-color-border); padding: 10px">
|
|
495
|
+
<section aria-label="虚拟滚动列表" bind:this={scroller} style="height: 500px; overflow: auto" tabindex="0">
|
|
496
|
+
<List dataSource={rows} style="position: relative; height: {$virtualizer.getTotalSize()}px">
|
|
497
|
+
<!-- Replaces the locale's empty text until a row of the window arrives -->
|
|
498
|
+
{#snippet emptyContent()}
|
|
499
|
+
<Spin />
|
|
500
|
+
{/snippet}
|
|
501
|
+
{#snippet item(row)}
|
|
502
|
+
<List.Item style="position: absolute; inset-inline: 0; top: {row.item * rowHeight}px; height: {rowHeight}px">
|
|
503
|
+
{#snippet header()}
|
|
504
|
+
<Avatar color="grey">SE</Avatar>
|
|
505
|
+
{/snippet}
|
|
506
|
+
{#snippet main()}
|
|
507
|
+
<div>
|
|
508
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">{dataList[row.item].title}</span>
|
|
509
|
+
<p style="color: var(--semi-color-text-2); margin: 4px 0">Semi Design 设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。</p>
|
|
510
|
+
</div>
|
|
511
|
+
{/snippet}
|
|
512
|
+
</List.Item>
|
|
513
|
+
{/snippet}
|
|
514
|
+
</List>
|
|
515
|
+
</section>
|
|
516
|
+
</div>
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
### Drag Sort
|
|
520
|
+
|
|
521
|
+
You can integrate the Svelte build of [dnd-kit](https://github.com/clauderic/dnd-kit/tree/master) (`@dnd-kit/svelte`) to implement drag and drop sort. The dragged element is the `li` rendered by `ListItem`. `@dnd-kit/svelte` leaves out `OptimisticSortingPlugin` (it moves DOM nodes itself, which collides with Svelte's keyed `each`), so the order during a drag is driven by state set in `onDragOver`. Write the sortable items inside the `List` tag with `{#each ... (item.id)}`, keyed by id; the `dataSource` plus `item` snippet form is keyed by position, so after a reorder a node would be matched to other data.
|
|
522
|
+
|
|
523
|
+
```svelte
|
|
524
|
+
<script lang="ts">
|
|
525
|
+
import { untrack, type ComponentProps } from 'svelte';
|
|
526
|
+
import { RestrictToVerticalAxis } from '@dnd-kit/abstract/modifiers';
|
|
527
|
+
import { Accessibility, DragDropManager } from '@dnd-kit/dom';
|
|
528
|
+
import { isSortable, OptimisticSortingPlugin, Sortable } from '@dnd-kit/dom/sortable';
|
|
529
|
+
import { DragDropProvider } from '@dnd-kit/svelte';
|
|
530
|
+
import { Avatar, List, type AvatarColor } from '@fibrae/semi-ui-svelte';
|
|
531
|
+
|
|
532
|
+
interface Item {
|
|
533
|
+
id: number;
|
|
534
|
+
title: string;
|
|
535
|
+
color: AvatarColor;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
type ProviderProps = ComponentProps<typeof DragDropProvider>;
|
|
539
|
+
|
|
540
|
+
const data: Item[] = [
|
|
541
|
+
{ id: 1, title: 'Semi Design Title 1', color: 'red' },
|
|
542
|
+
{ id: 2, title: 'Semi Design Title 2', color: 'grey' },
|
|
543
|
+
{ id: 3, title: 'Semi Design Title 3', color: 'light-green' },
|
|
544
|
+
{ id: 4, title: 'Semi Design Title 4', color: 'light-blue' },
|
|
545
|
+
{ id: 5, title: 'Semi Design Title 5', color: 'pink' },
|
|
546
|
+
];
|
|
547
|
+
|
|
548
|
+
let listItems = $state.raw(data);
|
|
549
|
+
// The order shown while dragging; cleared on drop, so `listItems` stays the only truth
|
|
550
|
+
let preview = $state.raw<Item[] | null>(null);
|
|
551
|
+
let draggingId = $state<number | string>();
|
|
552
|
+
const ordered = $derived(preview ?? listItems);
|
|
553
|
+
|
|
554
|
+
// The provider overwrites the plugins and modifiers of the manager it is given with its own props, so they go there
|
|
555
|
+
const manager = new DragDropManager();
|
|
556
|
+
// Accessibility would add role="button" and a Tab stop to each li; pointer dragging only
|
|
557
|
+
const plugins: ProviderProps['plugins'] = (defaults) => defaults.filter((plugin) => plugin !== Accessibility);
|
|
558
|
+
const modifiers = [RestrictToVerticalAxis];
|
|
559
|
+
// OptimisticSortingPlugin moves DOM nodes itself, which collides with Svelte's keyed each; `preview` drives the order instead
|
|
560
|
+
const sortablePlugins: ConstructorParameters<typeof Sortable>[0]['plugins'] = (defaults) => defaults.filter((plugin) => plugin !== OptimisticSortingPlugin);
|
|
561
|
+
|
|
562
|
+
// A non-clickable ListItem hands the attachment its li, which is the element dragged
|
|
563
|
+
const sortable = (id: number, position: () => number) => (element: HTMLElement) => {
|
|
564
|
+
const item = new Sortable({ id, index: untrack(position), element, plugins: sortablePlugins }, manager);
|
|
565
|
+
$effect(() => {
|
|
566
|
+
item.index = position();
|
|
567
|
+
});
|
|
568
|
+
return () => item.destroy();
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
const onDragStart: ProviderProps['onDragStart'] = ({ operation: { source } }) => {
|
|
572
|
+
draggingId = source?.id;
|
|
573
|
+
};
|
|
574
|
+
const onDragOver: ProviderProps['onDragOver'] = ({ operation: { source, target } }) => {
|
|
575
|
+
if (!isSortable(source) || !isSortable(target) || source.id === target.id) return;
|
|
576
|
+
const next = [...ordered];
|
|
577
|
+
const from = next.findIndex((entry) => entry.id === source.id);
|
|
578
|
+
const to = next.findIndex((entry) => entry.id === target.id);
|
|
579
|
+
if (from < 0 || to < 0) return;
|
|
580
|
+
next.splice(to, 0, ...next.splice(from, 1));
|
|
581
|
+
preview = next;
|
|
582
|
+
};
|
|
583
|
+
const onDragEnd: ProviderProps['onDragEnd'] = ({ canceled }) => {
|
|
584
|
+
if (!canceled) listItems = ordered;
|
|
585
|
+
preview = null;
|
|
586
|
+
draggingId = undefined;
|
|
587
|
+
};
|
|
588
|
+
</script>
|
|
589
|
+
|
|
590
|
+
<div style="padding: 12px; border: 1px solid var(--semi-color-border); margin: 12px">
|
|
591
|
+
<DragDropProvider {manager} {modifiers} {onDragEnd} {onDragOver} {onDragStart} {plugins}>
|
|
592
|
+
<List>
|
|
593
|
+
<!-- Keyed by id: dnd-kit reorders li nodes mid-drag, and the keyed each moves them with their data -->
|
|
594
|
+
{#each ordered as entry, position (entry.id)}
|
|
595
|
+
{@const dragging = draggingId === entry.id}
|
|
596
|
+
<List.Item {@attach sortable(entry.id, () => position)} class={dragging ? 'isDragging' : undefined} style="border: 1px solid var(--semi-color-border); margin-bottom: 12px; cursor: grabbing{dragging ? '; z-index: 999; position: relative; background-color: var(--semi-color-bg-0)' : ''}">
|
|
597
|
+
{#snippet header()}
|
|
598
|
+
<Avatar color={entry.color}>SE</Avatar>
|
|
599
|
+
{/snippet}
|
|
600
|
+
{#snippet main()}
|
|
601
|
+
<div>
|
|
602
|
+
<span style="color: var(--semi-color-text-0); font-weight: 500">{entry.title}</span>
|
|
603
|
+
<p style="color: var(--semi-color-text-2); margin: 4px 0">Semi Design 设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的 Web 应用。</p>
|
|
604
|
+
</div>
|
|
605
|
+
{/snippet}
|
|
606
|
+
</List.Item>
|
|
607
|
+
{/each}
|
|
608
|
+
</List>
|
|
609
|
+
</DragDropProvider>
|
|
610
|
+
</div>
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
### With Pagination
|
|
614
|
+
|
|
615
|
+
You can use Pagination in combination to achieve a paged List
|
|
616
|
+
|
|
617
|
+
```svelte
|
|
618
|
+
<script lang="ts">
|
|
619
|
+
import { List, Pagination } from '@fibrae/semi-ui-svelte';
|
|
620
|
+
|
|
621
|
+
const data = ['围城', '平凡的世界(全三册)', '三体(全集)', '雪中悍刀行(全集)', '撒哈拉的故事', '明朝那些事', '一禅小和尚', '沙丘', '被讨厌的勇气', '罪与罚', '月亮与六便士', '沉默的大多数'];
|
|
622
|
+
const pageSize = 4;
|
|
623
|
+
const getData = (current: number) => data.slice((current - 1) * pageSize, current * pageSize);
|
|
624
|
+
let page = $state(1);
|
|
625
|
+
</script>
|
|
626
|
+
|
|
627
|
+
<div>
|
|
628
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap">
|
|
629
|
+
<List class="component-list-demo-booklist" dataSource={getData(page)} size="small" split={false} style="border: 1px solid var(--semi-color-border); flex-basis: 100%; flex-shrink: 0">
|
|
630
|
+
{#snippet item(row)}
|
|
631
|
+
<List.Item class="list-item">{row.item}</List.Item>
|
|
632
|
+
{/snippet}
|
|
633
|
+
</List>
|
|
634
|
+
<Pagination bind:currentPage={page} {pageSize} size="small" style="width: 100%; flex-basis: 100%; justify-content: center" total={data.length} />
|
|
635
|
+
</div>
|
|
636
|
+
</div>
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
### With filter
|
|
640
|
+
|
|
641
|
+
You can use it by assembling Input to filter the List
|
|
642
|
+
|
|
643
|
+
```svelte
|
|
644
|
+
<script lang="ts">
|
|
645
|
+
import { IconSearch } from '@fibrae/semi-icons-svelte';
|
|
646
|
+
import { Input, List } from '@fibrae/semi-ui-svelte';
|
|
647
|
+
|
|
648
|
+
const data = ['围城', '平凡的世界(全三册)', '三体(全集)', '雪中悍刀行(全集)', '撒哈拉的故事', '明朝那些事', '一禅小和尚', '沙丘', '被讨厌的勇气', '罪与罚'];
|
|
649
|
+
|
|
650
|
+
let list = $state.raw(data);
|
|
651
|
+
|
|
652
|
+
const onSearch = (keyword?: string) => {
|
|
653
|
+
list = keyword ? data.filter((item) => item.includes(keyword)) : data;
|
|
654
|
+
};
|
|
655
|
+
</script>
|
|
656
|
+
|
|
657
|
+
<div>
|
|
658
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap; border: 1px solid var(--semi-color-border)">
|
|
659
|
+
<List class="component-list-demo-booklist" dataSource={list} size="small" split={false} style="flex-basis: 100%; flex-shrink: 0; border-bottom: 1px solid var(--semi-color-border)">
|
|
660
|
+
{#snippet header()}
|
|
661
|
+
<Input onChange={(value) => !value && onSearch()} onCompositionEnd={(event) => onSearch((event.target as HTMLInputElement).value)} placeholder="搜索">
|
|
662
|
+
{#snippet prefix()}
|
|
663
|
+
<IconSearch />
|
|
664
|
+
{/snippet}
|
|
665
|
+
</Input>
|
|
666
|
+
{/snippet}
|
|
667
|
+
{#snippet item(row)}
|
|
668
|
+
<List.Item class="list-item">{row.item}</List.Item>
|
|
669
|
+
{/snippet}
|
|
670
|
+
</List>
|
|
671
|
+
</div>
|
|
672
|
+
</div>
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
### Add delete item
|
|
676
|
+
|
|
677
|
+
```svelte
|
|
678
|
+
<script lang="ts">
|
|
679
|
+
import { IconMinusCircle, IconPlusCircle } from '@fibrae/semi-icons-svelte';
|
|
680
|
+
import { Button, List } from '@fibrae/semi-ui-svelte';
|
|
681
|
+
|
|
682
|
+
const data = ['围城', '平凡的世界(全三册)', '三体(全集)', '雪中悍刀行(全集)', '撒哈拉的故事', '明朝那些事', '一禅小和尚', '沙丘', '被讨厌的勇气', '罪与罚', '月亮与六便士', '沉默的大多数', '第一人称单数'];
|
|
683
|
+
|
|
684
|
+
let list = $state.raw(data.slice(0, 8));
|
|
685
|
+
|
|
686
|
+
const updateList = (item?: string) => {
|
|
687
|
+
let newList: string[];
|
|
688
|
+
if (item) {
|
|
689
|
+
newList = list.filter((i) => item !== i);
|
|
690
|
+
} else {
|
|
691
|
+
newList = list.concat(data.slice(list.length, list.length + 1));
|
|
692
|
+
}
|
|
693
|
+
list = newList;
|
|
694
|
+
};
|
|
695
|
+
</script>
|
|
696
|
+
|
|
697
|
+
<div>
|
|
698
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap; border: 1px solid var(--semi-color-border)">
|
|
699
|
+
<List class="component-list-demo-booklist" dataSource={list} size="small" split={false} style="flex-basis: 100%; flex-shrink: 0; border-bottom: 1px solid var(--semi-color-border)">
|
|
700
|
+
{#snippet item(row)}
|
|
701
|
+
<li class="list-item" style="margin: 4px">
|
|
702
|
+
<Button aria-label={`删除${row.item}`} style="margin-inline-end: 4px" theme="borderless" type="danger" onclick={() => updateList(row.item)}>
|
|
703
|
+
{#snippet icon()}
|
|
704
|
+
<IconMinusCircle />
|
|
705
|
+
{/snippet}
|
|
706
|
+
</Button>
|
|
707
|
+
{row.item}
|
|
708
|
+
</li>
|
|
709
|
+
{/snippet}
|
|
710
|
+
</List>
|
|
711
|
+
<div style="margin: 4px; font-size: 14px">
|
|
712
|
+
<Button aria-label="新增书籍" style="margin-inline-end: 4px; color: var(--semi-color-info)" theme="borderless" onclick={() => updateList()}>
|
|
713
|
+
{#snippet icon()}
|
|
714
|
+
<IconPlusCircle />
|
|
715
|
+
{/snippet}
|
|
716
|
+
</Button>
|
|
717
|
+
新增书籍
|
|
718
|
+
</div>
|
|
719
|
+
</div>
|
|
720
|
+
</div>
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
### Single or multiple selection
|
|
724
|
+
|
|
725
|
+
You can enhance the List into a list selector by combining Radio or Checkbox
|
|
726
|
+
|
|
727
|
+
```svelte
|
|
728
|
+
<script lang="ts">
|
|
729
|
+
import { Checkbox, CheckboxGroup, List, Radio, RadioGroup, type CheckboxValue, type RadioValue } from '@fibrae/semi-ui-svelte';
|
|
730
|
+
|
|
731
|
+
const data = ['围城', '平凡的世界(全三册)', '三体(全集)', '雪中悍刀行(全集)', '撒哈拉的故事', '明朝那些事', '一禅小和尚', '沙丘', '被讨厌的勇气', '罪与罚', '月亮与六便士', '沉默的大多数', '第一人称单数'];
|
|
732
|
+
const dataSource = data.slice(0, 8);
|
|
733
|
+
let checkboxVal = $state<CheckboxValue[]>([data[0]]);
|
|
734
|
+
let radioVal = $state<RadioValue>(data[0]);
|
|
735
|
+
const listStyle = 'border: 1px solid var(--semi-color-border); flex-basis: 100%; flex-shrink: 0';
|
|
736
|
+
</script>
|
|
737
|
+
|
|
738
|
+
<div style="display: flex">
|
|
739
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap">
|
|
740
|
+
<CheckboxGroup aria-label="书单多选" bind:value={checkboxVal}>
|
|
741
|
+
<List class="component-list-demo-booklist" {dataSource} size="small" split={false} style={listStyle}>
|
|
742
|
+
{#snippet item(row)}
|
|
743
|
+
<List.Item class="list-item">
|
|
744
|
+
<Checkbox value={row.item}>{row.item}</Checkbox>
|
|
745
|
+
</List.Item>
|
|
746
|
+
{/snippet}
|
|
747
|
+
</List>
|
|
748
|
+
</CheckboxGroup>
|
|
749
|
+
</div>
|
|
750
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap">
|
|
751
|
+
<RadioGroup aria-label="书单单选" bind:value={radioVal}>
|
|
752
|
+
<List class="component-list-demo-booklist" {dataSource} size="small" split={false} style={listStyle}>
|
|
753
|
+
{#snippet item(row)}
|
|
754
|
+
<List.Item class="list-item">
|
|
755
|
+
<Radio value={row.item}>{row.item}</Radio>
|
|
756
|
+
</List.Item>
|
|
757
|
+
{/snippet}
|
|
758
|
+
</List>
|
|
759
|
+
</RadioGroup>
|
|
760
|
+
</div>
|
|
761
|
+
</div>
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
### Keyboard events
|
|
765
|
+
|
|
766
|
+
You can monitor the keyboard events of the corresponding keys by yourself to realize the selection of different items. As in the following example, you can use the up and down arrow keys to select different items
|
|
767
|
+
|
|
768
|
+
```svelte
|
|
769
|
+
<script lang="ts">
|
|
770
|
+
import { List } from '@fibrae/semi-ui-svelte';
|
|
771
|
+
|
|
772
|
+
const list = ['围城', '平凡的世界(全三册)', '三体(全集)', '雪中悍刀行(全集)', '撒哈拉的故事', '明朝那些事', '一禅小和尚', '沙丘', '被讨厌的勇气', '罪与罚'];
|
|
773
|
+
|
|
774
|
+
let hoverIndex = $state(-1);
|
|
775
|
+
|
|
776
|
+
const changeIndex = (offset: number) => {
|
|
777
|
+
let index = hoverIndex + offset;
|
|
778
|
+
if (index < 0) {
|
|
779
|
+
index = list.length - 1;
|
|
780
|
+
}
|
|
781
|
+
if (index >= list.length) {
|
|
782
|
+
index = 0;
|
|
783
|
+
}
|
|
784
|
+
hoverIndex = index;
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
const onKeydown = (event: KeyboardEvent) => {
|
|
788
|
+
switch (event.key) {
|
|
789
|
+
case 'ArrowUp':
|
|
790
|
+
event.preventDefault();
|
|
791
|
+
changeIndex(-1);
|
|
792
|
+
break;
|
|
793
|
+
case 'ArrowDown':
|
|
794
|
+
event.preventDefault();
|
|
795
|
+
changeIndex(1);
|
|
796
|
+
break;
|
|
797
|
+
default:
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
</script>
|
|
802
|
+
|
|
803
|
+
<svelte:window onkeydown={onKeydown} />
|
|
804
|
+
|
|
805
|
+
<div>
|
|
806
|
+
<div style="margin-inline-end: 16px; width: 280px; display: flex; flex-wrap: wrap; border: 1px solid var(--semi-color-border)">
|
|
807
|
+
<List class="component-list-demo-booklist" dataSource={list} size="small" split={false} style="flex-basis: 100%; flex-shrink: 0; border-bottom: 1px solid var(--semi-color-border)">
|
|
808
|
+
{#snippet item(row)}
|
|
809
|
+
<List.Item class={row.index === hoverIndex ? 'component-list-demo-booklist-active-item' : ''}>
|
|
810
|
+
{row.item}
|
|
811
|
+
</List.Item>
|
|
812
|
+
{/snippet}
|
|
813
|
+
</List>
|
|
814
|
+
</div>
|
|
815
|
+
</div>
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
The custom styles involved in the Demo of the above book list example are as follows
|
|
819
|
+
|
|
820
|
+
```scss
|
|
821
|
+
.component-list-demo-booklist {
|
|
822
|
+
.list-item {
|
|
823
|
+
&:hover {
|
|
824
|
+
background-color: var(--semi-color-fill-0);
|
|
825
|
+
}
|
|
826
|
+
&:active {
|
|
827
|
+
background-color: var(--semi-color-fill-1);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
.component-list-demo-booklist-active-item {
|
|
833
|
+
background-color: var(--semi-color-fill-0);
|
|
834
|
+
}
|
|
835
|
+
```
|
|
836
|
+
|
|
837
|
+
## Accessibility
|
|
838
|
+
|
|
839
|
+
Pass `aria-label` or `aria-labelledby` to List to describe the list; the root element then gets `role="group"`.
|
|
840
|
+
|
|
841
|
+
A ListItem with `onClick` or `onRightClick` (including those set on the List) stays an `<li>` list item and renders a native `<button type="button">` inside it as its only focus target, which screen readers announce as a button.
|
|
842
|
+
|
|
843
|
+
- The button wraps the content of the `header` and `main` snippets (or the content inside the tag when there are none) and its click area covers the whole row. After tabbing to it, Enter or Space triggers `onClick` and the context-menu key triggers `onRightClick`
|
|
844
|
+
- `extra` is rendered outside the button on a higher layer, so buttons and links in it can be focused and clicked normally, and clicking them does not trigger the Item's `onClick`
|
|
845
|
+
- When the content inside the tag is combined with `header` or `main`, it sits outside the button and is covered by the full-row button, so put interactive content in `extra`
|
|
846
|
+
- A ListItem without a click callback renders no button and is not focusable
|
|
847
|
+
|
|
848
|
+
A List.Item used outside a List renders as a plain list item without `grid`. With `grid`, the Row that holds the items has `role="list"` and the Col around each item has `role="none"`, so screen readers read the items as members of that list.
|
|
849
|
+
|
|
850
|
+
## API reference
|
|
851
|
+
|
|
852
|
+
### List
|
|
853
|
+
|
|
854
|
+
**Props**
|
|
855
|
+
|
|
856
|
+
| Property | Description | Type | Default |
|
|
857
|
+
| - | - | - | - |
|
|
858
|
+
| bordered | Whether to show a border. | `boolean` | `false` |
|
|
859
|
+
| children | ListItem children, written inside the tag and rendered after the items of dataSource. | `Snippet` | - |
|
|
860
|
+
| dataSource | Data source of the list; each entry is rendered through the item snippet. | `T[]` | - |
|
|
861
|
+
| emptyContent | Snippet for the content shown when there is no data; the locale's empty text by default. A list with content inside its tag (children) is never empty. | `Snippet` | - |
|
|
862
|
+
| footer | Snippet for the list footer. | `Snippet` | - |
|
|
863
|
+
| grid | Grid configuration: align, gutter and justify of Row, and span, offset, order, push, pull, the responsive breakpoints, class and style of Col. class and style land on the Col around each item. | `Grid` | - |
|
|
864
|
+
| header | Snippet for the list header. | `Snippet` | - |
|
|
865
|
+
| item | Snippet that renders each entry of dataSource, receiving `{ item, index }` and usually returning a ListItem. | `Snippet<[ListItemSnippetProps<T>]>` | - |
|
|
866
|
+
| layout | List layout: vertical or horizontal. | `'horizontal' \| 'vertical'` | `"vertical"` |
|
|
867
|
+
| loading | Whether the list is loading; a Spin is shown when true. | `boolean` | `false` |
|
|
868
|
+
| loadMore | Snippet for the load-more button. | `Snippet` | - |
|
|
869
|
+
| onClick | Click handler for the items that have no onClick of their own; an item with a handler renders a native button. It is a function prop rather than the native onclick because its presence decides whether an item renders a button. | `(event: MouseEvent) => void` | - |
|
|
870
|
+
| onRightClick | Context-menu handler for the items that have no onRightClick of their own; those items also render a native button. | `(event: MouseEvent) => void` | - |
|
|
871
|
+
| size | List size: small, default or large. | `Size` | `"default"` |
|
|
872
|
+
| split | Whether to show dividers between items. | `boolean` | `true` |
|
|
873
|
+
|
|
874
|
+
`class`, `style` and native attributes (such as `data-*`) land on the root element; when `aria-label` or `aria-labelledby` is passed without a `role`, the root element gets `role="group"`. The payload of `onClick` and `onRightClick` is the native `MouseEvent`, and they act on the items, not on the root element.
|
|
875
|
+
|
|
876
|
+
### List grid props
|
|
877
|
+
|
|
878
|
+
`grid` accepts only the Row props `align`, `gutter`, `justify`, `type` and the Col props `span`, `offset`, `order`, `push`, `pull`, the responsive breakpoints, `class` and `style`; other attributes are not accepted. `class` and `style` land on the Col around each item, and `style` is a string. Refer to [Grid](../basic/grid.md) for their meaning.
|
|
879
|
+
|
|
880
|
+
| Properties | Instructions | type | Default |
|
|
881
|
+
| - | - | - | - |
|
|
882
|
+
| span | Number of grid spaces | number | - |
|
|
883
|
+
| gutter | Grid spacing | number | 0 |
|
|
884
|
+
| xs | `< 576px` responsive grid, a number or an object containing other attributes | number\|object | - |
|
|
885
|
+
| sm | `≥ 576px` responsive grid, a number or an object containing other properties | number\|object | - |
|
|
886
|
+
| md | `≥ 768px` responsive grid, a number or an object containing other properties | number\|object | - |
|
|
887
|
+
| lg | `≥ 992px` responsive grid, a number or an object containing other properties | number\|object | - |
|
|
888
|
+
| xl | `≥ 1200px` responsive grid, a number or an object containing other properties | number\|object | - |
|
|
889
|
+
| xxl | `≥ 1600px` responsive grid, a number or an object containing other properties | number\|object | - |
|
|
890
|
+
|
|
891
|
+
### List.Item
|
|
892
|
+
|
|
893
|
+
**Props**
|
|
894
|
+
|
|
895
|
+
| Property | Description | Type | Default |
|
|
896
|
+
| - | - | - | - |
|
|
897
|
+
| align | Vertical alignment of header and main: flex-start, flex-end, center, baseline or stretch; flex-start when omitted. | `'baseline' \| 'center' \| 'flex-end' \| 'flex-start' \| 'stretch'` | `"flex-start"` |
|
|
898
|
+
| children | Item content written inside the tag; inside the button when the item is clickable and has no header or main, otherwise outside it. | `Snippet` | - |
|
|
899
|
+
| extra | Snippet for additional item content, rendered outside the button; interactive elements in it stay focusable and clickable, and clicking them does not trigger the row's click. | `Snippet` | - |
|
|
900
|
+
| header | Snippet for the item header content. | `Snippet` | - |
|
|
901
|
+
| main | Snippet for the item main content. | `Snippet` | - |
|
|
902
|
+
| onClick | Click handler, taking precedence over the List's onClick; an item with a handler renders a native button. | `(event: MouseEvent) => void` | - |
|
|
903
|
+
| onRightClick | Context-menu handler, taking precedence over the List's onRightClick; an item with only this handler also renders a native button. | `(event: MouseEvent) => void` | - |
|
|
904
|
+
|
|
905
|
+
`class`, `style` and native attributes (such as `data-*`) land on the `li`, not on the outer Col in grid mode. When clickable, `aria-*` and `tabindex` land on the inner button instead.
|
|
906
|
+
|
|
907
|
+
## Content Guidelines
|
|
908
|
+
|
|
909
|
+
- Capitalize the first letter
|
|
910
|
+
- do not follow punctuation at the end
|
|
911
|
+
- Grammatical parallelism: mixed use of active and passive, declarative and imperative sentences
|
|
912
|
+
|
|
913
|
+
## Design Tokens
|
|
914
|
+
|
|
915
|
+
The design token table of this component is on the web page: <https://semi-design-next.pages.dev/en-US/svelte/show/list/>
|
|
916
|
+
|
|
917
|
+
## Changes from Semi Design
|
|
918
|
+
|
|
919
|
+
From the project release notes (written in Chinese). Your knowledge of Semi Design does not cover these.
|
|
920
|
+
|
|
921
|
+
- 破坏性(类型):List 的 `grid` 只接受 Row 与 Col 的指定属性,不再接受任意 div 属性(如 `id`);Timeline `dataSource` 项的 `Data` 不再接受 `children`,改用 `content`。
|
|
922
|
+
- 可点击的 List.Item 在 li 内渲染原生 `<button>` 作为唯一焦点目标,`tabIndex`、`aria-*` 落在按钮上,点击 `extra` 内元素不再触发 `onClick`,children 中的交互内容需移到 `extra`;新增类名 `semi-list-item-clickable`、`semi-list-item-button`;可点击的 Timeline.Item 的 children 外包一层 `role="button"` 的 `semi-timeline-item-content-main`;依赖 `.semi-list-items .semi-list-empty` 的选择器改为 `.semi-list-empty`;`children` 为空数组时显示空状态。
|
|
923
|
+
- Solid 破坏性(类型):Timeline `dataSource` 项的 `content`、`time`、`extra` 只接受 `string | number`,删除 `dot` 字段,自定义内容用 `renderContent`、`renderDot`、`renderTime`、`renderExtra`(参数 `(item, index)`,`renderDot` 没有默认值)。`Timeline.Item` 的 `dot`、`time`、`extra` 不变。
|
|
924
|
+
- Svelte 破坏性:Timeline 不提供 `Timeline.Item`,只用 `dataSource` 加 `content`、`time`、`extra` snippet(参数 `{ item, index }`);自定义圆点逐项设置:`TimelineData.dot?: Snippet`,设了的节点按自定义圆点(`head-custom`)渲染,未设的节点画默认圆点,没有组件级 `dot` snippet;`dataSource` 项的 `class`、`style`(字符串)与其他 li 属性落在 `li` 上。
|
|
925
|
+
- Svelte:导出为 `List`、`ListItem`;`onClick`、`onRightClick` 是函数 prop,类型中去掉了小写的 `onclick`、`oncontextmenu`;`item`、`header`、`footer`、`emptyContent`、`loadMore`、`children` 都是 snippet,`item` 参数 `{ item, index }`。
|
|
926
|
+
- Svelte:空状态按 `children` snippet 是否存在判断(Vue 版按内容是否为空);ListItem 的 `header`、`main`、`extra` 同样按 snippet 是否存在判断。
|
|
927
|
+
- Timeline(React):`extra`、`time` 为 0 时渲染在各自容器内,不再作为裸文本出现在正文后。
|