Commit 19fdacb6 by chenwq

表格组件加line:1

1 parent a8e8b0d6
import type { ReactNode } from 'react'; import type { CSSProperties, ReactNode } from 'react';
import type { ColumnsType, TableProps } from 'antd/es/table'; import type { ColumnsType, TableProps } from 'antd/es/table';
import { UButton, UTable, FillTable } from '@uj/ui'; import { UButton, UTable, UTooltip, FillTable } from '@uj/ui';
import { PermButton } from '@/access'; import { PermButton } from '@/access';
import type { ActionSchema } from '../formSchema'; import type { ActionSchema } from '../formSchema';
/** 列表分页统一默认每页条数。 */ /** 列表分页统一默认每页条数。 */
export const DEFAULT_PAGE_SIZE = 10; export const DEFAULT_PAGE_SIZE = 10;
/** 表格列扩展:line 为 1 时单行省略并显示完整提示;大于 1 时按指定行数截断。 */
export type DataTableColumn<T> = ColumnsType<T>[number] & { line?: number };
export type DataTableColumnsType<T> = DataTableColumn<T>[];
/** 列表行操作配置;配置 permission 时自动使用权限按钮。 */ /** 列表行操作配置;配置 permission 时自动使用权限按钮。 */
export interface RowActionSchema<T> extends Omit<ActionSchema, 'danger' | 'label' | 'onClick'> { export interface RowActionSchema<T> extends Omit<ActionSchema, 'danger' | 'label' | 'onClick'> {
/** 可按当前行动态生成操作文案。 */ /** 可按当前行动态生成操作文案。 */
...@@ -29,7 +33,7 @@ export interface DataTableProps<T> extends Pick< ...@@ -29,7 +33,7 @@ export interface DataTableProps<T> extends Pick<
| 'locale' | 'locale'
| 'className' | 'className'
> { > {
columns: ColumnsType<T>; columns: DataTableColumnsType<T>;
showIndex?: boolean; showIndex?: boolean;
pagination?: pagination?:
| { | {
...@@ -142,7 +146,7 @@ export function DataTable<T extends object>({ ...@@ -142,7 +146,7 @@ export function DataTable<T extends object>({
}), }),
} }
: undefined; : undefined;
const tableColumns = showIndex const sourceColumns: DataTableColumnsType<T> = showIndex
? [ ? [
{ {
title: '序号', title: '序号',
...@@ -155,6 +159,8 @@ export function DataTable<T extends object>({ ...@@ -155,6 +159,8 @@ export function DataTable<T extends object>({
...(actionColumn ? [actionColumn] : []), ...(actionColumn ? [actionColumn] : []),
] ]
: [...columns, ...(actionColumn ? [actionColumn] : [])]; : [...columns, ...(actionColumn ? [actionColumn] : [])];
const hasLineLimit = hasLineConfigured(sourceColumns);
const tableColumns = withLineLimit(sourceColumns);
const TableComponent = fillHeight ? FillTable<T> : UTable<T>; const TableComponent = fillHeight ? FillTable<T> : UTable<T>;
return ( return (
<> <>
...@@ -182,6 +188,7 @@ export function DataTable<T extends object>({ ...@@ -182,6 +188,7 @@ export function DataTable<T extends object>({
{...props} {...props}
dataSource={dataSource} dataSource={dataSource}
columns={tableColumns} columns={tableColumns}
tableLayout={hasLineLimit ? 'fixed' : undefined}
pagination={ pagination={
resolvedPagination === false resolvedPagination === false
? false ? false
...@@ -205,3 +212,56 @@ export function DataTable<T extends object>({ ...@@ -205,3 +212,56 @@ export function DataTable<T extends object>({
</> </>
); );
} }
function hasLineConfigured<T extends object>(columns: ColumnsType<T>): boolean {
return columns.some((column) => {
const customColumn = column as DataTableColumn<T>;
return (
Boolean(customColumn.line) ||
('children' in column &&
Boolean(column.children?.length) &&
hasLineConfigured(column.children))
);
});
}
function withLineLimit<T extends object>(columns: DataTableColumnsType<T>): ColumnsType<T> {
return columns.map((column) => {
if ('children' in column && column.children) {
return { ...column, children: withLineLimit(column.children as DataTableColumnsType<T>) };
}
if (!column.line || column.line < 1) return column;
const { line, render, ...rest } = column;
return {
...rest,
render: (value: unknown, record: T, index: number) => {
const content = toCellContent(render?.(value, record, index), value);
const title =
typeof value === 'string' || typeof value === 'number' ? String(value) : content;
return (
<UTooltip title={title}>
<span style={lineStyle(line)}>{content}</span>
</UTooltip>
);
},
};
});
}
function toCellContent(rendered: unknown, fallback: unknown): ReactNode {
if (rendered && typeof rendered === 'object' && 'children' in rendered && 'props' in rendered) {
return (rendered as { children: ReactNode }).children;
}
return (rendered ?? fallback) as ReactNode;
}
function lineStyle(line: number): CSSProperties {
if (line === 1)
return { display: 'block', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' };
return {
display: '-webkit-box',
overflow: 'hidden',
WebkitBoxOrient: 'vertical',
WebkitLineClamp: line,
};
}
...@@ -5,7 +5,12 @@ export { SearchForm } from './SearchForm'; ...@@ -5,7 +5,12 @@ export { SearchForm } from './SearchForm';
export type { SearchFormProps, SearchPagination } from './SearchForm'; export type { SearchFormProps, SearchPagination } from './SearchForm';
export type { ActionSchema, FieldOption, FieldSchema, FieldType } from './formSchema'; export type { ActionSchema, FieldOption, FieldSchema, FieldType } from './formSchema';
export { DataTable, DEFAULT_PAGE_SIZE } from './DataTable'; export { DataTable, DEFAULT_PAGE_SIZE } from './DataTable';
export type { DataTableProps, RowActionSchema } from './DataTable'; export type {
DataTableColumn,
DataTableColumnsType,
DataTableProps,
RowActionSchema,
} from './DataTable';
export { ConfirmModal } from './ConfirmModal'; export { ConfirmModal } from './ConfirmModal';
export type { ConfirmModalProps } from './ConfirmModal'; export type { ConfirmModalProps } from './ConfirmModal';
export { CombiForm } from './CombiForm'; export { CombiForm } from './CombiForm';
......
import { useMemo, useState } from 'react'; import { useMemo, useState } from 'react';
import { message } from 'antd'; import { message } from 'antd';
import { useMutation, useQuery } from '@tanstack/react-query'; import { useMutation, useQuery } from '@tanstack/react-query';
import type { ColumnsType } from 'antd/es/table';
import { UCard, UTag, UText, UTitle } from '@uj/ui'; import { UCard, UTag, UText, UTitle } from '@uj/ui';
import { import {
DataTable, DataTable,
...@@ -9,6 +8,7 @@ import { ...@@ -9,6 +8,7 @@ import {
ConfirmModal, ConfirmModal,
SearchForm, SearchForm,
type FieldSchema, type FieldSchema,
type DataTableColumnsType,
type RowActionSchema, type RowActionSchema,
type SearchPagination, type SearchPagination,
} from '@/components'; } from '@/components';
...@@ -119,18 +119,18 @@ export default function AccountManage() { ...@@ -119,18 +119,18 @@ export default function AccountManage() {
options: withAllOption(accountStatusOptions), options: withAllOption(accountStatusOptions),
}, },
]; ];
const columns: ColumnsType<AccountRow> = [ const columns: DataTableColumnsType<AccountRow> = [
{ {
title: '账号', title: '账号',
dataIndex: 'account', dataIndex: 'account',
width: 140, width: 180,
render: (value) => ( render: (value) => (
<UText style={{ color: '#315ce8', fontFamily: 'monospace' }}>{value}</UText> <UText style={{ color: '#315ce8', fontFamily: 'monospace' }}>{value}</UText>
), ),
}, },
{ title: '姓名', dataIndex: 'name', width: 110 }, { title: '姓名', dataIndex: 'name', width: 110 },
{ title: '手机号', dataIndex: 'mobile', width: 140 }, { title: '手机号', dataIndex: 'mobile', width: 120 },
{ title: '所属组织', dataIndex: 'orgPath', width: 260 }, { title: '所属组织', dataIndex: 'orgPath', width: 260, line: 1 },
{ {
title: '角色', title: '角色',
dataIndex: 'roleNames', dataIndex: 'roleNames',
......
import { useMemo, useState } from 'react'; import { useMemo, useState } from 'react';
import { message } from 'antd'; import { message } from 'antd';
import { useMutation, useQuery } from '@tanstack/react-query'; import { useMutation, useQuery } from '@tanstack/react-query';
import type { ColumnsType } from 'antd/es/table';
import { UCard, UTag, UText, UTitle } from '@uj/ui'; import { UCard, UTag, UText, UTitle } from '@uj/ui';
import { import {
DataTable, DataTable,
...@@ -9,6 +8,7 @@ import { ...@@ -9,6 +8,7 @@ import {
ConfirmModal, ConfirmModal,
SearchForm, SearchForm,
type FieldSchema, type FieldSchema,
type DataTableColumnsType,
type RowActionSchema, type RowActionSchema,
type SearchPagination, type SearchPagination,
} from '@/components'; } from '@/components';
...@@ -109,8 +109,8 @@ export default function RoleManage() { ...@@ -109,8 +109,8 @@ export default function RoleManage() {
options: getBaseList('dataScope'), options: getBaseList('dataScope'),
}, },
]; ];
const columns: ColumnsType<Row> = [ const columns: DataTableColumnsType<Row> = [
{ title: '角色名称', dataIndex: 'name', width: 150 }, { title: '角色名称', dataIndex: 'name', width: 200 },
{ {
title: '角色编码', title: '角色编码',
dataIndex: 'code', dataIndex: 'code',
...@@ -119,7 +119,7 @@ export default function RoleManage() { ...@@ -119,7 +119,7 @@ export default function RoleManage() {
}, },
{ title: '默认数据域', dataIndex: 'scope', width: 130 }, { title: '默认数据域', dataIndex: 'scope', width: 130 },
{ title: '账号数', dataIndex: 'userCount', width: 90 }, { title: '账号数', dataIndex: 'userCount', width: 90 },
{ title: '角色描述', dataIndex: 'description' }, { title: '角色描述', dataIndex: 'description', width: 320, line: 1 },
{ {
title: '类型', title: '类型',
dataIndex: 'type', dataIndex: 'type',
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!