Skip to content
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
10 changes: 10 additions & 0 deletions .changeset/39680-related-list-count-badge-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@object-ui/plugin-detail': patch
---

fix(plugin-detail): a related list's count badge announces its count in the console language

Clause-②: no
Metadata-protocol: none

The count badge in a related list's header showed a number and announced it to screen readers in English ("3 records") whatever the console language. It now reads the translated pair the related list already uses elsewhere: a zh-CN console announces 「3 条记录」, and an English console says "1 record" at one record rather than "1 records". What the badge draws is unchanged.
83 changes: 83 additions & 0 deletions ui/packages/plugin-detail/src/RelatedList.i18n.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* A related list's header count badge speaks the session locale.
*
* The badge draws a bare number, so its `aria-label` is what a screen reader
* announces for it. It reads `detail.relatedRecordOne` at one record and
* `detail.relatedRecords` otherwise — the same pair the tab list's summary
* row lead reads — so a zh-CN console announces 「3 条记录」 and an English
* one never says "1 records".
*/
import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest';
import { cleanup, render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import React from 'react';
import { I18nProvider } from '@object-ui/i18n';
import { RelatedList } from './RelatedList';

beforeAll(() => {
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 });
});

afterEach(() => cleanup());

const rows = (n: number) => Array.from({ length: n }, (_, i) => ({ id: `c${i + 1}`, name: `Contact ${i + 1}` }));

function renderListIn(language: string, n: number) {
const find = vi.fn(async () => ({ data: rows(n), total: n }));
render(
<I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }}>
<RelatedList
title="Contacts"
type="table"
api="contact"
objectName="contact"
referenceField="account"
parentId="ACC-1"
columns={[{ accessorKey: 'name', header: 'Name' }]}
dataSource={{ find } as any}
/>
</I18nProvider>,
);
}

/** The accessible name of the list header's count badge, once it draws `n`. */
async function headBadgeName(n: number): Promise<string | null> {
let name: string | null = null;
await waitFor(() => {
const badges = Array.from(document.querySelectorAll('[data-related-list-head] [aria-label]'))
.filter((el) => el.textContent === String(n));
expect(badges).toHaveLength(1);
name = badges[0].getAttribute('aria-label');
});
return name;
}

describe('related list count badge — accessible name is translated', () => {
it('announces the zh bundle value under a zh-CN session', async () => {
renderListIn('zh-CN', 3);
expect(await headBadgeName(3)).toBe('3 条记录');
});

it('announces the singular under an en session at one record', async () => {
renderListIn('en', 1);
expect(await headBadgeName(1)).toBe('1 record');
});

it('announces the plural under an en session', async () => {
renderListIn('en', 3);
expect(await headBadgeName(3)).toBe('3 records');
});

it('announces the singular under a de session at one record', async () => {
renderListIn('de', 1);
expect(await headBadgeName(1)).toBe('1 Datensatz');
});
});
2 changes: 1 addition & 1 deletion ui/packages/plugin-detail/src/RelatedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4758,7 +4758,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
'text-xs font-normal h-5 px-1.5',
recordCount === 0 && 'bg-muted text-muted-foreground'
)}
aria-label={`${recordCount} records`}
aria-label={tabRecordCountLead(recordCount)}
>
{recordCount}
</Badge>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function flush() {

/** The list header's count badge (the only `N records` label in the head). */
const headBadge = () =>
document.querySelector('[data-related-list-head] [aria-label$=" records"]')?.textContent ?? null;
document.querySelector('[data-related-list-head] [aria-label$=" records"], [data-related-list-head] [aria-label$=" record"]')?.textContent ?? null;
const cardCount = () => document.querySelector('[data-related-card-count]')?.textContent ?? null;

function renderList(find: ReturnType<typeof vi.fn>) {
Expand Down
Loading