Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 关注列表的表格与卡片视图都能点整行空白处选中

longmeidao1ea067e8304a
diff --git a/frontend/src/react/components/data-table-frame.tsx b/frontend/src/react/components/data-table-frame.tsx
index 1a2bc8afc..1d240c73d 100644
--- a/frontend/src/react/components/data-table-frame.tsx
+++ b/frontend/src/react/components/data-table-frame.tsx
@@ -1,14 +1,25 @@
-import type { PropsWithChildren } from 'react';
+import type { MouseEvent, PropsWithChildren } from 'react';
+
+import { clickedBlank } from './row-click';
export interface DataTableFrameProps extends PropsWithChildren {
/** 关注来源表需要额外的排序图标规则;外框与行状态仍由这个共享组件负责。 */
follow?: boolean;
+ /** 给了就是整行可选:点在某一行的空白处时以那一行的 `data-key`(React Aria 写在 `tr` 上的
+ * 行 id)回调;点在行里的勾、键、链接上不算。 */
+ onRowClick?(key: string): void;
}
/** BoardUI Table 的 Data Table 外框:主表面、完整边线、圆角与选中行反馈。 */
-export function DataTableFrame({ children, follow = false }: DataTableFrameProps) {
+export function DataTableFrame({ children, follow = false, onRowClick }: DataTableFrameProps) {
+ const click = onRowClick ? (event: MouseEvent<HTMLDivElement>) => {
+ const target = event.target;
+ if (!(target instanceof Element)) return;
+ const row = target.closest<HTMLElement>('[role="row"][data-key]');
+ if (row && clickedBlank(event, row)) onRowClick(row.dataset.key!);
+ } : undefined;
return (
- <div data-board-data-table data-follow-table={follow || undefined}>
+ <div data-board-data-table data-follow-table={follow || undefined} onClick={click}>
{children}
</div>
);
diff --git a/frontend/src/react/components/row-click.ts b/frontend/src/react/components/row-click.ts
new file mode 100644
index 000000000..a041479ed
--- /dev/null
+++ b/frontend/src/react/components/row-click.ts
@@ -0,0 +1,19 @@
+/* 整行可选的列表:点在一行的空白处就是选这一行,点在行里的控件、链接或输入框上不算。
+ *
+ * 勾选框本来就在行首,但它只有 16px,一整行的其余部分点了没反应的话,选几条就要瞄几次。
+ * 判「空白」看的是点到的元素往上找有没有可交互的祖先,找到行为止:行外面的祖先不算,
+ * 否则整张表包在一个 `label` 里时每一下都成了「点在控件上」。 */
+import type { SyntheticEvent } from 'react';
+
+const INTERACTIVE = [
+ 'a', 'button', 'input', 'select', 'textarea', 'label',
+ '[role="checkbox"]', '[role="switch"]', '[role="button"]', '[role="link"]', '[role="menu"]',
+].join(',');
+
+/** 这一下是不是点在 `row` 的空白处。`row` 是整行的元素,事件可以从它的任何后代冒上来。 */
+export function clickedBlank(event: SyntheticEvent | Event, row: Element): boolean {
+ const target = event.target;
+ if (!(target instanceof Element) || !row.contains(target)) return false;
+ const control = target.closest(INTERACTIVE);
+ return !control || !row.contains(control) || control === row;
+}
diff --git a/frontend/src/react/follow-manage/source-list.tsx b/frontend/src/react/follow-manage/source-list.tsx
index 745a9df01..695ac5fa4 100644
--- a/frontend/src/react/follow-manage/source-list.tsx
+++ b/frontend/src/react/follow-manage/source-list.tsx
@@ -35,6 +35,7 @@ import { useBackgroundJob } from '../background-job';
import { cardClass } from '../components/card';
import { DataTableFrame } from '../components/data-table-frame';
import { EmptyState } from '../components/empty-state';
+import { clickedBlank } from '../components/row-click';
import { LoadingDots } from '../components/loading-dots';
import { Note } from '../components/note';
import { Progress } from '../components/progress';
@@ -141,9 +142,11 @@ function SourceRow(
{ source, selected, onToggle, handlers }:
{ source: FollowSource; selected: boolean; onToggle(on: boolean): void; handlers: RowHandlers },
) {
+ /* 点这一行的空白处就是勾上或取消:勾选框只有 16px,一行里其余的地方点了没反应的话,选几条要瞄几次。 */
return (
<div data-selected={selected || undefined}
- className="flex min-h-16 flex-wrap items-center gap-3 px-2 py-3">
+ className="flex min-h-16 flex-wrap items-center gap-3 px-2 py-3"
+ onClick={(event) => { if (clickedBlank(event, event.currentTarget)) onToggle(!selected) }}>
<Checkbox isSelected={selected} onChange={onToggle} aria-label={`选择 ${source.label}`} />
<span className="flex min-w-0 grow flex-col gap-0.5">
<SourceLink source={source} />
@@ -576,7 +579,7 @@ export function SourceList(props: SourceListProps) {
) : null}
{asTable ? (
- <DataTableFrame follow>
+ <DataTableFrame follow onRowClick={(key) => toggleOne(Number(key), !selected.has(Number(key)))}>
<Table aria-label="关注来源" size="sm"
sortDescriptor={sorting[0]
? { column: sorting[0].id, direction: sorting[0].desc ? 'descending' : 'ascending' }
diff --git a/frontend/test/react/follow-manage.test.tsx b/frontend/test/react/follow-manage.test.tsx
index 5b71fe0a5..b278308a3 100644
--- a/frontend/test/react/follow-manage.test.tsx
+++ b/frontend/test/react/follow-manage.test.tsx
@@ -316,6 +316,37 @@ it('表格视图把每条来源摊成一行,勾的还是来源 ID', async () =
expect(sentBody(fetcher, FOLLOW_SOURCE_URL)).toEqual([{ action: 'enabled', id: 2, enabled: false }]);
});
+it('表格里点一行的空白处就是选这一行,再点取消;点行里的链接不算选', async () => {
+ const { host } = await open({}, { layout: 'table' });
+ const row = () => checkboxNamed(host, '选择 甲 · Pawchive')!.closest('[role="row"]')!;
+ const blank = () => [...row().querySelectorAll('[role="rowheader"],[role="gridcell"]')]
+ .find((cell) => cell.textContent?.trim() === 'Pawchive')!;
+
+ await click(blank());
+ expect(row().hasAttribute('data-follow-selected')).toBe(true);
+ expect(host.textContent).toContain('已选 1 个来源');
+
+ await click(blank());
+ expect(row().hasAttribute('data-follow-selected')).toBe(false);
+
+ const link = row().querySelector('a')!;
+ link.addEventListener('click', (event) => event.preventDefault());
+ await click(link);
+ expect(row().hasAttribute('data-follow-selected')).toBe(false);
+});
+
+it('卡片里点来源那一行的空白处同样选中整行,点勾选框仍是勾选框自己在管', async () => {
+ const { host } = await open();
+ const row = () => checkboxNamed(host, '选择 甲 · Pawchive')!.closest('[data-source-divider] > div')!;
+
+ await click(row().querySelector('span[title="Pawchive"]'));
+ expect(row().hasAttribute('data-selected')).toBe(true);
+ expect(host.textContent).toContain('已选 1 个来源');
+
+ await click(checkboxNamed(host, '选择 甲 · Pawchive'));
+ expect(row().hasAttribute('data-selected')).toBe(false);
+});
+
it('表格的可排序列显示并更新排序三角', async () => {
const { host, props } = await open({}, { layout: 'table' });
const author = [...host.querySelectorAll<HTMLElement>('[role="columnheader"]')]
diff --git a/tests/test_follow_web.py b/tests/test_follow_web.py
index 6a233e00d..6e13d9a70 100644
--- a/tests/test_follow_web.py
+++ b/tests/test_follow_web.py
@@ -3105,7 +3105,7 @@ class FollowWebSourceTests(unittest.TestCase):
"""列表外框、作者与紧凑表格分三层;行内操作不借主动作的蓝色。"""
sources = self.read_react("follow-manage/source-list.tsx")
self.assertIn('data-follow-author-header data-open={open || undefined}', sources)
- self.assertIn('<DataTableFrame follow>', sources)
+ self.assertIn('<DataTableFrame follow onRowClick=', sources)
# 整屏只有「检查全部」一枚主动作,行内动作都不借它的蓝;画出来几枚图标键、
# 分别叫什么,由 `frontend/test/react/follow-manage.test.tsx` 在渲染结果上判。
# 空态里那枚「添加关注」链接画在没有工具条的那一屏上,两枚不同屏,也由那份用例判。