Skip to content

Cell 单元格

介绍

单元格为列表中的单个展示项。

导入

js
import CellGroup from '@/components/basic/CellGroup.vue'
import Cell from '@/components/basic/Cell.vue'

基础用法

Cell 可以单独使用,也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。

html
<CellGroup title="基础用法">
  <Cell title="单元格标题" value="内容" />
  <Cell title="单元格标题" label="单元格说明" value="内容" />
</CellGroup>

单独使用

html
<Cell title="单元格标题" value="内容" />
<Cell title="单元格标题" label="单元格说明" value="内容" />

显示箭头的单元格

html
<Cell title="显示箭头" value="内容" showArrow />

可以点击的单元格

html
<Cell title="可以点击的单元格" showArrow @click="console.log('点击了!')" />

卡片风格

通过 CellGroup 的 inset 属性,可以将单元格转换为圆角卡片风格。

html
<CellGroup title="卡片风格" inset>
  <Cell title="单元格标题" value="内容" />
  <Cell title="单元格标题" label="单元格说明" value="内容" />
</CellGroup>

单元格大小

通过 size 属性可以控制单元格的大小。

html
<CellGroup title="单元格大小">
  <Cell title="单元格 large" value="内容" size="large" />
  <Cell title="单元格 medium" value="内容" size="medium" />
  <Cell title="单元格 small" value="内容" size="small" />
</CellGroup>

展示图标

通过 icon 属性在标题左侧或者右侧展示图标。

html
<CellGroup title="展示图标">
  <Cell title="图标" value="内容" icon="setting" />
  <Cell title="右侧图标" icon="help" rightIcon="map" />
</CellGroup>

自定义渲染

如以上用法不能满足你的需求,可以使用自定义渲染函数来自定义内容。

html
<CellGroup title="自定义渲染">
  <Cell title="右侧是自定义渲染的图片">
    <template #right>
      <image src="http://test.com/images/defaultAvatar.png" key="right" />
    </template>
  </Cell>
</CellGroup>

API 参考

Cell

Props

名称说明类型必填默认值
title左侧标题string--
value右侧内容string ︱ number--
valueSelectable设置右侧内容是否可以选择boolean-false
label标题下方的描述信息string--
icon左侧图标名称或图片链接(http/https),等同于 Icon 组件的 iconstring--
iconProps当使用图标时,左图标的附加属性IconProps--
iconPlaceholder当左侧图标未设置时,是否在左侧追加一个占位区域,以和其他单元格对齐boolean-false
iconWidth左侧图标区域的宽度number ︱ "auto"-20
iconSize左侧图标的大小number-15
rightIconSize右侧图标的大小number-15
rightIcon右侧图标名称或图片链接(http/https),等同于 Icon 组件的 iconstring--
rightIconProps当使用图标时,右图标的附加属性IconProps--
touchable是否可以点击boolean-false
showArrow是否展示右侧箭头boolean-false
center是否使内容垂直居中boolean-false
size大小"small" ︱ "medium" ︱ "large"-medium
backgroundColor背景颜色string--
children自定义渲染子级Element--
topBorder是否显示顶部边框boolean-false
bottomBorder是否显示底部边框boolean-true
pressedColor按下的背景颜色string--
innerStyle自定义样式ViewStyle--
iconStyle自定义左侧图标样式object--
rightIconStyle自定义右侧图标样式object--
padding强制控制按钮的边距。如果是数字,则设置所有方向边距;两位数组 [vetical,horizontal];四位数组 [top,right,down,left]number ︱ number[]--

Slots

名称说明
default单元格的内容
left自定义左侧渲染,优先级高会覆盖 title,label,leftIcon插槽
right自定义右侧渲染,优先级高会覆盖 rightPrepend,value,rightIcon插槽
leftIcon自定义左侧图标渲染
title自定义标题渲染
label自定义标题下方的描述信息渲染
rightPrepend自定义右侧内容前的渲染
value自定义右侧内容渲染
rightIcon自定义右侧图标渲染

Events

名称说明参数
click点击事件$event

CellGroup

Props

名称说明类型必填默认值
title分组标题string--
round是否展示为圆角卡片风格boolean-false
inset是否展示为内边距风格boolean-false
showTopMargin是否显示没有标题时顶部边距。默认是boolean-true
showBottomMargin是否显示底部边距。默认否boolean-false
showTopMarginSize是否显示顶部边距。默认是 10rpxboolean-true
titleStyle标题的样式object--
titleDark标题背景是否变暗boolean-false

CellContext

CellContext 是 Cell 组件提供的上下文功能,用于子组件设置单元格的点击事件。

导入

js
import { useCellContext } from '@/components/basic/CellContext.ts'

使用方法

在 Cell 组件的子组件中,可以通过 useCellContext 钩子获取上下文,并设置点击事件:

vue
<script setup lang="ts">
import { useCellContext, onMounted } from 'vue'

// 获取单元格上下文
const cellContext = useCellContext()

onMounted(() => {
  // 设置单元格点击事件处理函数
  cellContext.setOnClickListener(() => {
    console.log('Cell clicked from child component')
    // 执行点击逻辑
  })
})
</script>

API

方法说明参数
useCellContext()获取单元格上下文无,返回 CellContext 对象

CellContext 接口

方法说明参数
setOnClickListener(listener)设置单元格点击事件监听器
注意:只能设置一次,后续设置会覆盖之前的设置
listener: () => void - 点击事件处理函数

主题变量

名称类型默认值
CellBackgroundstringwhite
CellSizestring or number'medium'
CellPressedColorstringpressed.white
CellPadding-[]
CellBottomBorderbooleantrue
CellTopBorderbooleanfalse
CellIconWidthnumber20
CellIconSizenumber15
CellBorderColorstringboder.default
CellBorderWidthnumber2
CellFontSizeLargenumber31
CellFontSizeMediumnumber26
CellFontSizeSmallnumber23
CellTitleColorstringtext
CellLabelColorstringtext.second
CellValueColorstringtext.second
CellHeightLargenumber125
CellHeightMediumnumber100
CellHeightSmallnumber80
CellPaddingLargenumber15
CellPaddingMediumnumber10
CellPaddingSmallnumber7
CellPaddingHorizontalnumber20
CellValuePaddingHorizontalnumber20
CellGroupDarkTitleBackgroundColorstringborder.light
CellGroupInsetPaddingHorizontalnumber10
CellGroupPaddingHorizontalnumber20
CellGroupTitleDarkbooleanfalse
CellGroupInsetbooleanfalse
CellGroupShowTopMarginbooleantrue
CellGroupShowBottomMarginbooleanfalse
CellGroupTopMarginSizenumber10
CellGroupTitleColorstringtext.second
CellGroupTitlePaddingTopnumber24
CellGroupTitlePaddingBottomnumber12
CellGroupInsetBorderRadiusnumber20
CellGroupInsetBackgroundColorstringwhite
CellGroupInsetMarginVerticalnumber0
CellGroupInsetMarginHorizontalnumber30