Skip to content

SimpleList 简易列表

SimpleList是一个功能丰富的列表组件,支持基础显示、单选、多选模式,并可配置虚拟列表以处理大量数据,同时提供自定义渲染能力。

导入

js
import SimpleList from '@/components/list/SimpleList.vue';

使用方法

基础用法

简单的列表展示,点击时触发itemClick事件。

vue
<template>
  <SimpleList 
    :data="data"
    @itemClick="(item) => toast.info('点击了:' + item)"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import SimpleList from '@/components/list/SimpleList.vue';
import Toast from '@/components/feedback/Toast.vue';

const toast = ref();
const data = [
  'Robert Davis',
  'Sandra Allen',
  'Charles Davis',
  'Michael Brown',
  'Joseph Miller',
  'Carol Allen',
  'Deborah Hernandez',
  'Michael Jones',
  'Amy Clark',
  'Daniel Robinson'
];
</script>

单选择式

列表项右侧显示单选框,每次只能选择一项。

vue
<template>
  <SimpleList 
    :data="data"
    mode="single-check"
  />
</template>

多选模式

列表项右侧显示多选框,可以选择多项。

vue
<template>
  <SimpleList 
    :data="data"
    mode="mulit-check"
  />
</template>

自定义渲染

通过itemContent插槽自定义列表项的内容。

vue
<template>
  <SimpleList 
    :data="data"
  >
    <template #itemContent="{ item, index }">
      <FlexCol>
        <Text>Index: {{index}}</Text>
        <Text :color="index % 2 === 0 ? 'danger' : 'success'">
          {{ item }}
        </Text>
      </FlexCol>
    </template>
  </SimpleList>
</template>

<script setup lang="ts">
import SimpleList from '@/components/list/SimpleList.vue';
import Text from '@/components/basic/Text.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
</script>

虚拟列表

当数据量较大时,可以启用虚拟列表模式以提升性能。

vue
<template>
  <SimpleList 
    :data="largeData"
    :virtual="true"
    :innerStyle="{
      height: '80vh',
    }"
    @itemClick="(item) => toast.info('点击了:' + item)"
  />
</template>

<script setup lang="ts">
import { computed, ref } from 'vue';
import SimpleList from '@/components/list/SimpleList.vue';
import Toast from '@/components/feedback/Toast.vue';

const toast = ref();
const data = [
  'Robert Davis',
  'Sandra Allen',
  'Charles Davis',
  'Michael Brown',
  'Joseph Miller'
];

// 生成大量数据用于虚拟列表演示
const largeData = computed(() => {
  let result = [];
  for (let i = 0; i < 100; i++) {
    result = result.concat(data);
  }
  return result;
});
</script>

API 参考

Props

名称说明类型必填默认值
virtual
是否使用虚拟列表
boolean-false
virtualItemHeight虚拟列表条目高度(px)number-40
itemStyle条目的自定义样式ViewStyle--
textStyle条目文字的自定义样式TextStyle--
checkedItemStyle选中的条目的自定义样式ViewStyle--
checkedTextStyle选中的条目文字的自定义样式TextStyle--
emptyText空数据时显示的文字string--
data源数据Array<T>-
dataKey数据项的唯一键名,用于vue循环优化string--
dataDisplayProp显示数据的prop,如果为空,则尝试直接把数据当string显示string--
colorProp控制数据条目颜色的字段名称,为空则使用默认颜色string--
disabledProp控制是否禁用数据条目的字段名称,为空则不禁用string--
mode列表的选择模式

* select 点击选择模式
* single-check 单选选择模式
* mulit-check 多选选择模式
'select' | 'single-check' | 'mulit-check'-'select'
checkProps当列表显示选择框时,选择框的自定义属性CheckBoxDefaultButtonProps-{borderColor: 'border.cell', checkColor: 'white', color: 'primary', size: 40, iconSize: mode === 'single-check' ? 10 : 16, type: mode === 'single-check' ? 'radio' : 'icon', disabled: false}
checkedItems当用使用选择框模式时,默认选中条目Array<T>-[]
innerStyle内部容器样式ViewStyle--

Slots

名称说明
itemContent自定义列表项内容,接收item和index参数
empty空数据时的内容插槽
inner内部容器插槽,可用于在列表前后添加内容

Events

名称说明参数
itemClick点击列表项事件item: T, index: number
selectedItemChanged选中项改变事件,仅在选择模式下触发selectedItems: T[]

主题变量

变量名类型默认值说明
SimpleListItemPaddingVerticalnumber20列表项垂直内边距
SimpleListItemPaddingHorizontalnumber35列表项水平内边距
SimpleListItemFontSizenumber28列表项字体大小
SimpleListItemBorderTopWidthnumber0列表项顶部边框宽度
SimpleListItemBorderBottomWidthnumber2列表项底部边框宽度
SimpleListItemBorderColorstringborder.cell列表项边框颜色
SimpleListItemColorstringtext.content列表项文字颜色
SimpleListItemBackgroundColorstringwhite列表项背景颜色
SimpleListItemTextFontSizenumber28列表项文字字体大小
SimpleListItemTextFontWeightstringnormal列表项文字字体粗细
SimpleListItemTextColorstringtext.content列表项文字颜色
SimpleListItemCheckedTextFontSizenumber28选中状态列表项文字字体大小
SimpleListItemCheckedTextFontWeightstringbold选中状态列表项文字字体粗细
SimpleListItemCheckedTextColorstringprimary选中状态列表项文字颜色
SimpleListItemPressedColorstringpressed.white列表项按下时的背景颜色