Skip to content

Commit b851bef

Browse files
committed
修改逻辑缺陷、样式 bug
1 parent a9fa673 commit b851bef

9 files changed

Lines changed: 86 additions & 41 deletions

File tree

biome.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"rules": {
2525
"recommended": true,
2626
"correctness": {
27-
"useHookAtTopLevel": "off",
28-
"useExhaustiveDependencies": "off"
27+
"useHookAtTopLevel": "off"
2928
},
3029
"suspicious": {
3130
"noExplicitAny": "off"

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
},
3434
"devDependencies": {
3535
"@biomejs/biome": "^2.4.15",
36-
"tsup": "^8.5.1"
37-
},
38-
"dependencies": {
39-
"react-is": "^19.2.6"
36+
"tsup": "^8.5.1",
37+
"typescript": "~5.8.3"
4038
},
4139
"packageManager": "pnpm@9.15.4"
4240
}

packages/editable-table/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@
5555
"react-dom": "^18.3.1",
5656
"@testing-library/dom": "^10.4.1",
5757
"@testing-library/jest-dom": "^6.9.1",
58-
"@testing-library/react": "^16.3.2",
58+
"@testing-library/react": "^14.3.1",
5959
"@testing-library/user-event": "^14.6.1",
6060
"@types/react": "^18.3.0",
6161
"@types/react-dom": "^18.3.0",
6262
"jsdom": "^25.0.1",
63-
"typescript": "~6.0.2",
63+
"typescript": "~5.8.3",
6464
"vitest": "^3.1.1"
6565
}
6666
}

packages/editable-table/src/components/EditableTable/EditableTable.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@
283283
opacity: 0.75;
284284
}
285285

286+
.et-btn:focus-visible {
287+
outline: 2px solid var(--et-btn-primary-bg);
288+
outline-offset: 2px;
289+
}
290+
286291
.et-btn-link + .et-btn-link {
287292
margin-left: 4px;
288293
}
@@ -356,3 +361,40 @@
356361
.et-scroll-right .et-header-cell.et-fixed-right {
357362
box-shadow: -6px 0 6px -4px rgba(0, 0, 0, 0.08);
358363
}
364+
365+
/* ===== 暗色模式 ===== */
366+
@media (prefers-color-scheme: dark) {
367+
:root {
368+
--et-border-color: #3a3a3a;
369+
--et-header-bg: #1f1f1f;
370+
--et-header-color: #e0e0e0;
371+
--et-row-hover-bg: #2a2a2a;
372+
--et-row-stripe-bg: #1a1a1a;
373+
--et-error-color: #ff6b6b;
374+
--et-btn-primary-bg: #4096ff;
375+
--et-btn-primary-color: #fff;
376+
--et-btn-default-bg: #1f1f1f;
377+
--et-btn-default-color: #e0e0e0;
378+
--et-btn-default-border: #3a3a3a;
379+
}
380+
381+
.et-wrapper {
382+
color: #e0e0e0;
383+
}
384+
385+
.et-cell.et-fixed {
386+
background: #141414;
387+
}
388+
389+
.et-row:nth-child(even) .et-cell.et-fixed {
390+
background: var(--et-row-stripe-bg);
391+
}
392+
393+
.et-row:hover .et-cell.et-fixed {
394+
background: var(--et-row-hover-bg);
395+
}
396+
397+
.et-empty {
398+
color: #666;
399+
}
400+
}

packages/editable-table/src/components/EditableTable/index.tsx

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,18 @@ function EditableTable<T extends object = Record<string, unknown>>(
193193
const updatedRow = { ...currentRow, [dataIndex]: value };
194194
const linkedResult = col.onFieldChange(value, updatedRow);
195195
if (linkedResult instanceof Promise) {
196+
// 捕获当前行的 rowKey,避免异步期间行增删导致 rowIndex 错位
197+
const rowId = String(currentRow[rowKey]);
196198
linkedResult.then((updates) => {
197-
if (updates) {
198-
setData((prev) => {
199-
const next = [...prev];
200-
next[rowIndex] = { ...next[rowIndex], ...updates };
201-
onChange?.(next);
202-
return next;
203-
});
204-
}
199+
if (!updates) return;
200+
setData((prev) => {
201+
const idx = prev.findIndex((r) => String(r[rowKey]) === rowId);
202+
if (idx === -1) return prev;
203+
const next = [...prev];
204+
next[idx] = { ...next[idx], ...updates };
205+
onChange?.(next);
206+
return next;
207+
});
205208
});
206209
} else if (linkedResult) {
207210
setData((prev) => {
@@ -228,7 +231,7 @@ function EditableTable<T extends object = Record<string, unknown>>(
228231
}
229232
}
230233
},
231-
[columns, onChange, validateTrigger],
234+
[columns, onChange, validateTrigger, rowKey],
232235
);
233236

234237
const handleSubmit = useCallback(() => {
@@ -277,19 +280,16 @@ function EditableTable<T extends object = Record<string, unknown>>(
277280

278281
const handleCancelEdit = useCallback(
279282
(id: string) => {
283+
const rowIndex = data.findIndex((r) => String(r[rowKey]) === id);
280284
const original = originalDataRef.current.get(id);
281-
if (original) {
282-
const rowIndex = data.findIndex((r) => String(r[rowKey]) === id);
283-
if (rowIndex !== -1) {
284-
setData((prev) => {
285-
const n = [...prev];
286-
n[rowIndex] = original;
287-
return n;
288-
});
289-
}
290-
originalDataRef.current.delete(id);
285+
if (original && rowIndex !== -1) {
286+
setData((prev) => {
287+
const n = [...prev];
288+
n[rowIndex] = original;
289+
return n;
290+
});
291291
}
292-
const rowIndex = data.findIndex((r) => String(r[rowKey]) === id);
292+
originalDataRef.current.delete(id);
293293
if (rowIndex !== -1) {
294294
setErrors((prev) => {
295295
const n: Record<string, string> = {};
@@ -326,7 +326,9 @@ function EditableTable<T extends object = Record<string, unknown>>(
326326
? opsWidthProp != null
327327
? typeof opsWidthProp === 'number'
328328
? `${opsWidthProp}px`
329-
: opsWidthProp
329+
: /^\d+$/.test(opsWidthProp)
330+
? `${opsWidthProp}px`
331+
: opsWidthProp
330332
: '120px'
331333
: undefined;
332334

@@ -343,7 +345,7 @@ function EditableTable<T extends object = Record<string, unknown>>(
343345

344346
// ===== 列固定偏移量 =====
345347
const fixedOffsets = useMemo(() => {
346-
const offsets: { left?: number; right?: number }[] = new Array(columns.length).fill({});
348+
const offsets: { left?: number; right?: number }[] = Array.from({ length: columns.length }, () => ({}));
347349
const widths = columns.map((col) => (typeof col.width === 'number' ? col.width : 150));
348350

349351
// 从左往右累计 left fixed

packages/fast-editable-table/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060
"@formily/reactive": "^2.3.1",
6161
"@types/react": "^18.3.0",
6262
"@types/react-dom": "^18.3.0",
63-
"typescript": "^5.6.0",
63+
"typescript": "~5.8.3",
6464
"@testing-library/dom": "^10.4.1",
6565
"@testing-library/jest-dom": "^6.9.1",
66-
"@testing-library/react": "^16.3.2",
66+
"@testing-library/react": "^14.3.1",
6767
"@testing-library/user-event": "^14.6.1",
6868
"jsdom": "^25.0.1",
6969
"vitest": "^3.1.1"

packages/fast-editable-table/src/FormilyEditableTable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ const FormilyEditableTableInner: React.FC<Omit<IFormilyEditableTableProps, 'name
135135
// 添加行
136136
const add = React.useCallback(async () => {
137137
if (validateBeforeAdd && form) {
138+
const arrayPath = field.address.toString();
138139
try {
139-
const arrayPath = field.address.toString();
140140
await form.validate(`${arrayPath}.*`);
141141
} catch {
142+
// 校验未通过,阻止添加;错误已由 Formily 渲染到各字段
142143
return;
143144
}
144145
}

packages/fast-editable-table/src/FormilyEditableTableField.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { Field } from '@formily/react';
22
import * as React from 'react';
33
import type { IFormilyEditableTableFieldProps } from './types';
44

5+
const CHECKED_DISPLAY_NAMES = new Set(['Switch', 'InternalSwitch', 'Checkbox', 'InternalCheckbox']);
6+
57
function isCheckedType(v: any): boolean {
6-
if (v == null || typeof v !== 'object') return false;
7-
return !!v.__ANT_SWITCH || !!v.__ANT_CHECKBOX;
8+
if (v == null || typeof v !== 'function') return false;
9+
const name: string = v.displayName || v.name || '';
10+
return CHECKED_DISPLAY_NAMES.has(name);
811
}
912

1013
/**
@@ -42,7 +45,12 @@ export const FormilyEditableTableField: React.FC<IFormilyEditableTableFieldProps
4245
}
4346

4447
if (children == null || !React.isValidElement(children)) {
45-
return <>{children}</>;
48+
return (
49+
<div className="fet-field" data-has-error={!!error || undefined}>
50+
{children}
51+
{error && <div className="fet-error">{error}</div>}
52+
</div>
53+
);
4654
}
4755

4856
const injectProps: Record<string, any> = {

pnpm-workspace.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,3 @@ packages:
22
- 'packages/*'
33
allowBuilds:
44
esbuild: true
5-
overrides:
6-
react: "^18.3.1"
7-
react-dom: "^18.3.1"
8-
"@types/react": "^18.3.0"
9-
"@types/react-dom": "^18.3.0"

0 commit comments

Comments
 (0)