fix(admin): admin管理页面优化,增加排序过滤等

This commit is contained in:
sophie committed 2026-07-10 14:10:49 +08:00
1 parent 0939aeb0d8
commit fc843905d1
31 files changed
+1090 -671

No files matched your search

@@ -0,0 +1,52 @@
/**
* shared/utils/table-sort.test.ts
* 用途:保护表格列排序纯函数的比较与切换语义。
* 依赖:Vitest、table-sort。
* 逻辑:覆盖空值、数字自然序、同列切换方向、换列重置升序。
*/
import { describe, expect, it } from 'vitest';
import {
compareSortValues,
nextSortState,
normalizeSortValue,
sortByKey,
} from './table-sort';
describe('table-sort utils', () => {
it('normalizes blank strings to null', () => {
expect(normalizeSortValue(' ')).toBeNull();
expect(normalizeSortValue('IT')).toBe('IT');
expect(normalizeSortValue(12)).toBe(12);
});
it('compares numeric-looking codes naturally', () => {
expect(compareSortValues('2', '10')).toBeLessThan(0);
expect(compareSortValues(null, 'a')).toBe(1);
});
it('toggles direction on same key and resets on new key', () => {
expect(nextSortState('name', 'asc', 'name')).toEqual({
sortKey: 'name',
sortDir: 'desc',
});
expect(nextSortState('name', 'desc', 'code')).toEqual({
sortKey: 'code',
sortDir: 'asc',
});
});
it('sorts rows by key with id tie-breaker', () => {
const rows = [
{ id: 2, name: 'B' },
{ id: 1, name: 'A' },
{ id: 3, name: 'A' },
];
const sorted = sortByKey(rows, {
sortKey: 'name',
sortDir: 'asc',
getValue: (row, key) => row[key],
tieBreaker: (a, b) => a.id - b.id,
});
expect(sorted.map((r) => r.id)).toEqual([1, 3, 2]);
});
});
+102
View File
@@ -0,0 +1,102 @@
/**
* shared/utils/table-sort.ts
* 用途:管理端表格列排序的纯函数(比较、切换方向、按列排序)。
* 依赖:无。
* 逻辑:useTableSort / SortableTableHeader 复用;业务页传入取值函数即可。
*/
export type SortDir = 'asc' | 'desc';
/** 可参与比较的原始值(空值统一排后) */
export type SortComparable = string | number | null | undefined;
/**
* 规范化比较值:字符串 trim,空串/null/undefined 视为空。
* @param value - 原始字段值
* @returns 规范化后的 string | number,空则为 null
*/
export function normalizeSortValue(value: SortComparable): string | number | null {
if (value == null) return null;
if (typeof value === 'number') {
return Number.isFinite(value) ? value : null;
}
const trimmed = String(value).trim();
return trimmed ? trimmed : null;
}
/**
* 比较两个规范化后的排序值。
* 核心逻辑:空值排后;数字直接相减;字符串用 localeCompare(numeric)。
* @param a - 左值
* @param b - 右值
* @returns 负/零/正,与 Array.sort 约定一致
*/
export function compareSortValues(
a: string | number | null,
b: string | number | null,
): number {
if (a == null && b == null) return 0;
if (a == null) return 1;
if (b == null) return -1;
if (typeof a === 'number' && typeof b === 'number') return a - b;
return String(a).localeCompare(String(b), undefined, {
numeric: true,
sensitivity: 'base',
});
}
/**
* 切换排序状态:同列在升/降序间切换,换列则从升序开始。
* @param currentKey - 当前排序列
* @param currentDir - 当前方向
* @param nextKey - 点击的列
* @returns 新的 sortKey / sortDir
*/
export function nextSortState<K extends string>(
currentKey: K | undefined,
currentDir: SortDir,
nextKey: K,
): { sortKey: K; sortDir: SortDir } {
if (currentKey === nextKey) {
return {
sortKey: nextKey,
sortDir: currentDir === 'asc' ? 'desc' : 'asc',
};
}
return { sortKey: nextKey, sortDir: 'asc' };
}
export interface SortByKeyOptions<T, K extends string> {
/** 当前排序列;未指定时返回浅拷贝原列表 */
sortKey?: K;
sortDir?: SortDir;
/** 从行中取出比较值 */
getValue: (item: T, key: K) => SortComparable;
/** 主值相等时的稳定次序(如按 id) */
tieBreaker?: (a: T, b: T) => number;
}
/**
* 按指定列对列表排序(不修改原数组)。
* @param items - 源列表
* @param options - 排序列、方向、取值与 tie-break
* @returns 新排序数组
*/
export function sortByKey<T, K extends string>(
items: readonly T[],
options: SortByKeyOptions<T, K>,
): T[] {
const { sortKey, sortDir = 'asc', getValue, tieBreaker } = options;
const list = [...items];
if (!sortKey) return list;
const dir = sortDir === 'asc' ? 1 : -1;
list.sort((a, b) => {
const av = normalizeSortValue(getValue(a, sortKey));
const bv = normalizeSortValue(getValue(b, sortKey));
const cmp = compareSortValues(av, bv);
if (cmp !== 0) return cmp * dir;
return tieBreaker ? tieBreaker(a, b) : 0;
});
return list;
}