Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(descriptions): add descriptions #530

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/descriptions/__tests__/__snapshots__/descriptions.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test Descriptions match snapshot: As same as antd's Descriptions 1`] = `
<DocumentFragment>
<div
class="ant-descriptions ant-descriptions-bordered"
>
<div
class="ant-descriptions-view"
>
<table>
<tbody>
<tr
class="ant-descriptions-row"
>
<th
class="ant-descriptions-item-label"
colspan="1"
>
<span>
名称
</span>
</th>
<td
class="ant-descriptions-item-content"
colspan="1"
>
<span>
我是 dt-react-component 组件库
</span>
</td>
<th
class="ant-descriptions-item-label"
colspan="1"
>
<span>
描述
</span>
</th>
<td
class="ant-descriptions-item-content"
colspan="1"
>
<span>
<div
style="width: 0px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; min-width: 100%;"
>
很长很长的描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述,长度甚至超过了一行
</div>
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</DocumentFragment>
`;

exports[`Test Descriptions match snapshot: Fixed table layout 1`] = `
<DocumentFragment>
<div
class="ant-descriptions ant-descriptions-bordered dtc-descriptions__fixed"
>
<div
class="ant-descriptions-view"
>
<table>
<tbody>
<tr
class="ant-descriptions-row"
>
<th
class="ant-descriptions-item-label"
colspan="1"
>
<span>
名称
</span>
</th>
<td
class="ant-descriptions-item-content"
colspan="1"
>
<span>
我是 dt-react-component 组件库
</span>
</td>
<th
class="ant-descriptions-item-label"
colspan="1"
>
<span>
描述
</span>
</th>
<td
class="ant-descriptions-item-content"
colspan="1"
>
<span>
<div
style="width: 0px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; min-width: 100%;"
>
很长很长的描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述,长度甚至超过了一行
</div>
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</DocumentFragment>
`;
37 changes: 37 additions & 0 deletions src/descriptions/__tests__/descriptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { render } from '@testing-library/react';

import Descriptions from '..';

const TestDescriptions = ({ fixed }: { fixed?: boolean }) => {
return (
<Descriptions bordered column={2} tableLayout={fixed ? 'fixed' : 'auto'}>
<Descriptions.Item label="名称">我是 dt-react-component 组件库</Descriptions.Item>
<Descriptions.Item label="描述">
<div
style={{
width: 0,
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
minWidth: '100%',
}}
>
很长很长的描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述,长度甚至超过了一行
</div>
</Descriptions.Item>
</Descriptions>
);
};

describe('Test Descriptions', () => {
it('match snapshot', () => {
expect(render(<TestDescriptions />).asFragment()).toMatchSnapshot(
"As same as antd's Descriptions"
);

expect(render(<TestDescriptions fixed />).asFragment()).toMatchSnapshot(
'Fixed table layout'
);
});
});
35 changes: 35 additions & 0 deletions src/descriptions/demos/basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useState } from 'react';
import { Space, Switch } from 'antd';
import { Descriptions, EllipsisText } from 'dt-react-component';

export default function Basic() {
const [fixed, setFixed] = useState(true);
return (
<Space size={8} direction="vertical">
<Switch
checkedChildren="fixed"
unCheckedChildren="auto"
checked={fixed}
onChange={setFixed}
/>
<Descriptions bordered column={2} tableLayout={fixed ? 'fixed' : 'auto'}>
<Descriptions.Item label="名称">
<EllipsisText value="我是 dt-react-component 组件库" maxWidth={120} />
</Descriptions.Item>
<Descriptions.Item label="描述">
<div
style={{
width: 0,
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
minWidth: '100%',
}}
>
很长很长的描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述,长度甚至超过了一行
</div>
</Descriptions.Item>
</Descriptions>
</Space>
);
}
29 changes: 29 additions & 0 deletions src/descriptions/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Descriptions 描述列表
group: 组件
toc: content
demo:
cols: 1
---

# Descriptions 描述列表

## 何时使用

展示多个只读字段的组合。(支持设置 table-layout 为 fixed)

## 示例

<code src='./demos/basic.tsx' title="设置 tableLayout"></code>

### API

| 参数 | 说明 | 类型 | 默认值 |
| ----------- | ---- | ------------------- | ------ |
| tableLayout | - | `'auto' \| 'fixed'` | 'auto' |

## FAQ

### 为什么要把 table-layout 设置为 fixed?

参考 [MDN](https://developer.mozilla.org/zh-CN/docs/Web/CSS/table-layout#fixed) 中关于 `table-layout` 的相关描述可以看出,当我们的需求是用 `Descriptions` 组件用固定比例展示字段信息的时候,相比 `auto` 的属性,更好地是 `fixed` 属性
7 changes: 7 additions & 0 deletions src/descriptions/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.dtc-descriptions {
&__fixed {
.ant-descriptions-view > table {
table-layout: fixed;
}
}
}
22 changes: 22 additions & 0 deletions src/descriptions/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Descriptions as AntdDescriptions, DescriptionsProps as AntdDescriptionsProps } from 'antd';
import classNames from 'classnames';

import './index.scss';

interface DescriptionsProps extends AntdDescriptionsProps {
tableLayout?: 'auto' | 'fixed';
}

function Descriptions({ className, tableLayout, ...rest }: DescriptionsProps) {
return (
<AntdDescriptions
className={classNames(tableLayout === 'fixed' && 'dtc-descriptions__fixed', className)}
{...rest}
/>
);
}

Descriptions.Item = AntdDescriptions.Item;

export default Descriptions;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as CollapsibleActionItems } from './collapsibleActionItems';
export { default as ContentLayout } from './contentLayout';
export { default as ContextMenu } from './contextMenu';
export { default as Copy } from './copy';
export { default as Descriptions } from './descriptions';
export { default as Drawer } from './drawer';
export { default as Dropdown } from './dropdown';
export { default as EllipsisText } from './ellipsisText';
Expand Down
Loading