# 23. Description 描述列表
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/03.%20%E7%BB%84%E4%BB%B6%E5%BA%93%E6%96%87%E6%A1%A3/09.%20%E5%85%B6%E4%BB%96%E7%BB%84%E4%BB%B6/25.%20Description%20%E6%8F%8F%E8%BF%B0%E5%88%97%E8%A1%A8

Description 描述列表组件使用指南


# Description 描述列表组件

> [!info] 用于展示只读信息的列表组件，常用于详情页面。

## 基本用法

```vue
<template>
  <Description
    title="用户信息"
    :data="userInfo"
    :schema="schema"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';

const userInfo = ref({
  username: 'admin',
  realname: '管理员',
  email: 'admin@example.com',
  phone: '13800138000',
});

const schema = [
  { field: 'username', label: '用户名' },
  { field: 'realname', label: '姓名' },
  { field: 'email', label: '邮箱' },
  { field: 'phone', label: '手机号' },
];
</script>
```

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| useCollapse | `boolean` | `true` | 是否使用折叠容器 |
| title | `string` | `''` | 标题 |
| size | `'small'\|'default'\|'middle'` | `'small'` | 尺寸 |
| bordered | `boolean` | `true` | 是否显示边框 |
| column | `number \| object` | `{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }` | 每行显示的字段数，可设置为响应式对象 |
| collapseOptions | `CollapseContainerOptions` | - | 内置折叠容器配置 |
| schema | `DescItem[]` | `[]` | 字段配置 |
| data | `Recordable` | `{}` | 数据对象 |

## Emits 事件

| 事件名 | 参数 | 说明 |
|--------|------|------|
| register | `(descInstance: DescInstance) => void` | 组件注册时触发，用于获取组件实例方法 |

## Schema 配置

```typescript
interface DescItem {
  field: string;           // 字段名
  label: string | VNode | JSX.Element;  // 标签
  span?: number;           // 占用列数
  labelMinWidth?: number;  // 标签最小宽度
  contentMinWidth?: number;// 内容最小宽度
  labelStyle?: CSSProperties; // 标签样式
  show?: (data: Recordable) => boolean; // 是否显示
  render?: (val: unknown, data: Recordable) => VNode | JSX.Element | string | number; // 自定义渲染
}

interface CollapseContainerOptions {
  canExpand?: boolean;     // 是否可展开
  title?: string;          // 标题
  helpMessage?: Array<any> | string; // 帮助信息
}
```

## 基础示例

```vue
<template>
  <Description
    title="用户详情"
    :data="userData"
    :schema="userSchema"
    :column="2"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';

const userData = ref({
  name: '张三',
  age: 28,
  email: 'zhangsan@example.com',
  phone: '13800138000',
  address: '北京市朝阳区',
  dept: '技术部',
});

const userSchema = [
  { field: 'name', label: '姓名' },
  { field: 'age', label: '年龄' },
  { field: 'email', label: '邮箱' },
  { field: 'phone', label: '手机号' },
  { field: 'address', label: '地址' },
  { field: 'dept', label: '部门' },
];
</script>
```

## 自定义渲染

```vue
<template>
  <Description
    :data="data"
    :schema="schema"
  />
</template>

<script setup lang="ts">
import { h } from 'vue';
import { Description } from '@/components/Description';
import { Tag } from 'ant-design-vue';

const data = ref({
  username: 'admin',
  status: 1,
  avatar: '/temp/avatar.jpg',
});

const schema = [
  {
    field: 'avatar',
    label: '头像',
    render: (value) => h('img', { src: value, style: { width: '50px', height: '50px' } }),
  },
  {
    field: 'status',
    label: '状态',
    render: (value) => h(Tag, { color: value === 1 ? 'green' : 'red' }, () => value === 1 ? '启用' : '禁用'),
  },
  {
    field: 'username',
    label: '用户名',
    render: (value) => h('span', { style: { color: 'red' } }, () => value),
  },
];
</script>
```

## 字段合并

```vue
<template>
  <Description
    :data="orderData"
    :schema="orderSchema"
    :column="3"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';

const orderData = ref({
  orderNo: 'ORDER20240322001',
  amount: 299.99,
  status: '已支付',
  remark: '这是备注信息，比较长，需要占满整行',
  createTime: '2024-03-22 10:30:00',
});

const orderSchema = [
  { field: 'orderNo', label: '订单号' },
  { field: 'amount', label: '金额' },
  { field: 'status', label: '状态' },
  {
    field: 'remark',
    label: '备注',
    span: 3,  // 占满整行（当 column=3 时）
  },
  { field: 'createTime', label: '创建时间', span: 2 },
];
</script>
```

## 垂直布局

```vue
<template>
  <Description
    layout="vertical"
    :data="data"
    :schema="schema"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';

const data = ref({
  title: '商品标题',
  price: 99.99,
  stock: 100,
});

const schema = [
  { field: 'title', label: '商品标题' },
  { field: 'price', label: '价格' },
  { field: 'stock', label: '库存' },
];
</script>
```

## 动态 Schema

```vue
<template>
  <Description
    :data="data"
    :schema="computedSchema"
  />
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { Description } from '@/components/Description';

const data = ref({
  name: '张三',
  role: 'admin',
  // 根据角色不同，显示不同字段
});

const computedSchema = computed(() => {
  const baseSchema = [
    { field: 'name', label: '姓名' },
    { field: 'role', label: '角色' },
  ];

  if (data.value.role === 'admin') {
    return [
      ...baseSchema,
      { field: 'permissions', label: '权限' },
      { field: 'dept', label: '管理部门' },
    ];
  }

  return baseSchema;
});
</script>
```

## 在抽屉中展示详情

```vue
<template>
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'action'">
        <a-button type="link" @click="handleView(record)">查看详情</a-button>
      </template>
    </template>
  </BasicTable>

  <BasicDrawer @register="registerDrawer" title="用户详情" :width="800">
    <Description
      v-if="currentUser"
      :data="currentUser"
      :schema="detailSchema"
    />
  </BasicDrawer>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { BasicTable, useTable, BasicDrawer, useDrawer, Description } from '@/components';

const currentUser = ref(null);

const detailSchema = [
  { field: 'username', label: '用户名' },
  { field: 'realname', label: '姓名' },
  { field: 'email', label: '邮箱' },
  { field: 'phone', label: '手机号' },
  { field: 'deptName', label: '部门' },
  { field: 'status', label: '状态', render: (v) => (v === 1 ? '启用' : '禁用') },
  { field: 'createTime', label: '创建时间' },
];

const [registerTable] = useTable({
  columns: [
    { title: '用户名', dataIndex: 'username' },
    { title: '姓名', dataIndex: 'realname' },
    { title: '操作', dataIndex: 'action' },
  ],
  api: getUserList,
});

const [registerDrawer, { openDrawer }] = useDrawerInner((data) => {
  currentUser.value = data;
});

function handleView(record) {
  currentUser.value = record;
  openDrawer();
}

async function getUserList() {
  return [];
}
</script>
```

## 使用插槽

```vue
<template>
  <Description
    :data="data"
    :schema="schema"
  >
    <template #status="{ value }">
      <Tag :color="value === 1 ? 'green' : 'red'">
        {{ value === 1 ? '启用' : '禁用' }}
      </Tag>
    </template>

    <template #avatar="{ value }">
      <a-avatar :src="value" />
    </template>
  </Description>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';
import { Tag } from 'ant-design-vue';

const data = ref({
  username: 'admin',
  status: 1,
  avatar: '/temp/avatar.jpg',
});

const schema = [
  { field: 'avatar', label: '头像', slot: 'avatar' },
  { field: 'username', label: '用户名' },
  { field: 'status', label: '状态', slot: 'status' },
];
</script>
```

## 完整示例：订单详情

```vue
<template>
  <Description
    title="订单详情"
    :data="orderData"
    :schema="orderSchema"
    :column="2"
    bordered
  >
    <template #status="{ value }">
      <a-tag :color="getStatusColor(value)">
        {{ getStatusText(value) }}
      </a-tag>
    </template>

    <template #items="{ value }">
      <div v-for="item in value" :key="item.id" class="order-item">
        {{ item.name }} × {{ item.quantity }}
      </div>
    </template>
  </Description>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';
import { Tag } from 'ant-design-vue';

const orderData = ref({
  orderNo: 'ORDER20240322001',
  status: 2,
  totalAmount: 299.99,
  receiver: '张三',
  phone: '13800138000',
  address: '北京市朝阳区xxx',
  items: [
    { id: 1, name: '商品A', quantity: 2 },
    { id: 2, name: '商品B', quantity: 1 },
  ],
  createTime: '2024-03-22 10:30:00',
  payTime: '2024-03-22 10:35:00',
});

const orderSchema = [
  { field: 'orderNo', label: '订单号' },
  { field: 'status', label: '订单状态', slot: 'status' },
  { field: 'totalAmount', label: '订单金额' },
  { field: 'receiver', label: '收货人' },
  { field: 'phone', label: '联系电话' },
  { field: 'address', label: '收货地址' },
  { field: 'items', label: '商品明细', slot: 'items', span: 2 },
  { field: 'createTime', label: '创建时间' },
  { field: 'payTime', label: '支付时间' },
];

function getStatusColor(status) {
  const map = { 1: 'blue', 2: 'green', 3: 'orange', 4: 'red' };
  return map[status] || 'default';
}

function getStatusText(status) {
  const map = { 1: '待支付', 2: '已支付', 3: '已发货', 4: '已取消' };
  return map[status] || '未知';
}
</script>

<style scoped>
.order-item {
  padding: 4px 0;
}
</style>
```

## 组件实例方法

通过 `register` 事件可获取组件实例，调用以下方法：

| 方法名 | 参数 | 说明 |
|--------|------|------|
| setDescProps | `(descProps: Partial<DescriptionProps>) => void` | 动态设置组件属性 |

```vue
<template>
  <Description
    :data="data"
    :schema="schema"
    @register="handleRegister"
  />
  <a-button @click="updateConfig">更新配置</a-button>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Description } from '@/components/Description';
import type { DescInstance } from '@/components/Description';

const data = ref({ username: 'admin' });
const schema = ref([{ field: 'username', label: '用户名' }]);
let descInstance: DescInstance | null = null;

function handleRegister(instance: DescInstance) {
  descInstance = instance;
}

function updateConfig() {
  descInstance?.setDescProps({
    title: '用户信息',
    bordered: false,
  });
}
</script>
```

## Slots 插槽

| 插槽名 | 说明 |
|--------|------|
| action | 操作区域插槽，配合 CollapseContainer 使用 |

## 相关文件

- 组件路径：`@/components/Description/src/Description.vue`
- 类型定义：`@/components/Description/src/typing.ts`

## 相关组件

- [[文档/04.开发文档/01. 前端开发/02. 组件开发/20. Drawer 抽屉]] - 抽屉组件
- [[07. Modal 弹窗]] - 弹窗组件
- [[26. Container 容器]] - 容器组件
