Skip to content
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ A summary of notable changes per release. For the full commit history see the [r

- **`textarea` cell editor** — multi-line text editor with newline insertion, modifier-key/blur commit, and row height expansion. → [Inline editing](/docs/inline-editing) ([#1354](https://github.com/jbetancur/react-data-table-component/issues/1354))

### Bug fixes

- **Pinned scrollbar** now sets `aria-controls` to the scroll container's real id. When the container already carried an id, or was labelled again after an unpin and re-pin, the thumb kept pointing at a generated id no element had, so assistive technology could not resolve what the scrollbar controlled.

---

## 8.11.0
Expand Down
193 changes: 193 additions & 0 deletions src/__tests__/pinning.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ describe('DataTable column pinning', () => {
expect(offsets).toContain(0);
expect(offsets.some(v => v > 0)).toBe(true);
});

test('pinned scrollbar aria-controls still resolves after a column is unpinned and re-pinned', async () => {
const unpinnedCols: TableColumn<Row>[] = columns.map(c => ({ ...c, pinned: undefined }));
const { container, rerender } = render(<DataTable columns={columns} data={data} responsive />);

const wrapper = container.querySelector('.rdt_responsiveWrapper') as HTMLElement;
Object.defineProperty(wrapper, 'scrollWidth', { configurable: true, get: () => 1000 });
Object.defineProperty(wrapper, 'clientWidth', { configurable: true, get: () => 400 });
await act(async () => {
wrapper.dispatchEvent(new Event('scroll'));
});

// Unpinning every column drops hasPinnedColumns, which unmounts the
// scrollbar; re-pinning mounts a fresh one with a new useId, while the
// wrapper keeps the id the first mount stamped on it.
rerender(<DataTable columns={unpinnedCols} data={data} responsive />);
rerender(<DataTable columns={columns} data={data} responsive />);
await act(async () => {
wrapper.dispatchEvent(new Event('scroll'));
});

const thumb = container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
const controls = thumb.getAttribute('aria-controls');
expect(controls).toBe(wrapper.id);
expect(document.getElementById(controls!)).toBe(wrapper);
});
});

// ── PinnedScrollbar ──────────────────────────────────────────────────────────
Expand All @@ -113,6 +139,18 @@ function makeScrollRef(scrollWidth = 1000, clientWidth = 400): React.RefObject<H
}

describe('PinnedScrollbar', () => {
const attached: HTMLElement[] = [];
const attach = <T extends HTMLElement>(el: T): T => {
document.body.appendChild(el);
attached.push(el);
return el;
};
afterEach(() => {
for (const el of attached.splice(0)) {
el.remove();
}
});

test('renders null before ResizeObserver fires (no overflow detected yet)', () => {
const ref = makeScrollRef(400, 400); // no overflow
const { container } = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);
Expand Down Expand Up @@ -195,4 +233,159 @@ describe('PinnedScrollbar', () => {
expect(ref.current!.scrollLeft).toBeGreaterThanOrEqual(0);
}
});

// With no host id the effect labels the container itself.
test('aria-controls resolves to the scroll container it labelled', async () => {
const ref = makeScrollRef(1000, 400);
attach(ref.current!);
const { container, unmount } = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);

await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});

const thumb = container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
const controls = thumb.getAttribute('aria-controls');
expect(controls).toBe(ref.current!.id);
expect(document.getElementById(controls!)).toBe(ref.current);

unmount();
});

test('aria-controls points at a host-supplied container id instead of a fresh one', async () => {
const ref = makeScrollRef(1000, 400);
ref.current!.id = 'host-app-scroll-container';
attach(ref.current!);
const { container, unmount } = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);

await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});

const thumb = container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
expect(ref.current!.id).toBe('host-app-scroll-container');
expect(thumb.getAttribute('aria-controls')).toBe('host-app-scroll-container');
expect(document.getElementById(thumb.getAttribute('aria-controls')!)).toBe(ref.current);

unmount();
});

test('aria-controls survives a remount onto the same container', async () => {
const ref = makeScrollRef(1000, 400);
attach(ref.current!);

// First mount stamps the container with its useId value.
const first = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);
await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});
const stampedId = ref.current!.id;
first.unmount();

// Unmounting doesn't clear the id, and the remount gets a *different* useId.
// Unpinning then re-pinning a column does exactly this in DataTable.
const second = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);
await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});

const thumb = second.container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
expect(ref.current!.id).toBe(stampedId);
expect(thumb.getAttribute('aria-controls')).toBe(stampedId);
expect(document.getElementById(thumb.getAttribute('aria-controls')!)).toBe(ref.current);

second.unmount();
});

// The effect re-runs when the ref object changes, and the container swapped
// in keeps its own id.
test('aria-controls re-points when the scroll container is swapped for another one', async () => {
const first = attach(makeScrollRef(1000, 400).current!);
const second = attach(makeScrollRef(1000, 400).current!);
second.id = 'second-scroll-container';

let swap!: (el: HTMLDivElement) => void;
function SwapHarness({ initial }: { initial: HTMLDivElement }): JSX.Element {
const [target, setTarget] = React.useState(initial);
swap = setTarget;
const ref = React.useMemo(() => ({ current: target }) as React.RefObject<HTMLDivElement>, [target]);
return <PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />;
}

const { container, unmount } = renderWithTheme(<SwapHarness initial={first} />);
await act(async () => {
first.dispatchEvent(new Event('scroll'));
});
await act(async () => {
swap(second);
});

const thumb = container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
const controls = thumb.getAttribute('aria-controls');
expect(controls).toBe('second-scroll-container');
expect(document.getElementById(controls!)).toBe(second);

unmount();
});

// The host takes the id over after the first mount stamped one.
test('aria-controls follows an id the host assigns after the first mount stamped one', async () => {
const ref = makeScrollRef(1000, 400);
attach(ref.current!);

const first = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);
await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});
first.unmount();

ref.current!.id = 'host-took-over-later';
const second = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);
await act(async () => {
ref.current!.dispatchEvent(new Event('scroll'));
});

const thumb = second.container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
expect(thumb.getAttribute('aria-controls')).toBe('host-took-over-later');
expect(document.getElementById(thumb.getAttribute('aria-controls')!)).toBe(ref.current);

second.unmount();
});

test('two concurrent scrollbars each control their own container', async () => {
const a = makeScrollRef(1000, 400);
const b = makeScrollRef(1000, 400);
attach(a.current!);
attach(b.current!);

const firstRender = renderWithTheme(<PinnedScrollbar scrollRef={a} leftInset={0} rightInset={0} />);
const secondRender = renderWithTheme(<PinnedScrollbar scrollRef={b} leftInset={0} rightInset={0} />);
await act(async () => {
a.current!.dispatchEvent(new Event('scroll'));
b.current!.dispatchEvent(new Event('scroll'));
});

const thumbA = firstRender.container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
const thumbB = secondRender.container.querySelector('.rdt_pinnedScrollbarThumb') as HTMLElement;
const controlsA = thumbA.getAttribute('aria-controls');
const controlsB = thumbB.getAttribute('aria-controls');
expect(controlsA).not.toBe(controlsB);
expect(document.getElementById(controlsA!)).toBe(a.current);
expect(document.getElementById(controlsB!)).toBe(b.current);

firstRender.unmount();
secondRender.unmount();
});

// An empty scroll ref renders no scrollbar.
test('emits no thumb and no aria-controls when the scroll ref is empty', () => {
const ref = { current: null } as React.RefObject<HTMLDivElement>;
const { container, unmount } = renderWithTheme(<PinnedScrollbar scrollRef={ref} leftInset={0} rightInset={0} />);

expect(container.querySelector('.rdt_pinnedScrollbarTrack')).toBeNull();
expect(container.querySelector('.rdt_pinnedScrollbarThumb')).toBeNull();
expect(container.querySelector('[aria-controls]')).toBeNull();

unmount();
});
});
7 changes: 6 additions & 1 deletion src/components/PinnedScrollbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default function PinnedScrollbar({
rightInset,
}: PinnedScrollbarProps): JSX.Element | null {
const scrollContainerId = React.useId();
// The scroll container id aria-controls points at.
const [controlsId, setControlsId] = React.useState(scrollContainerId);
const trackRef = React.useRef<HTMLDivElement>(null);
const thumbRef = React.useRef<HTMLDivElement>(null);
const [thumbWidth, setThumbWidth] = React.useState(0);
Expand Down Expand Up @@ -62,6 +64,9 @@ export default function PinnedScrollbar({
if (!el.id) {
el.id = scrollContainerId;
}
// An existing container id is preserved and is what aria-controls must
// name, or the reference dangles.
setControlsId(el.id);
el.addEventListener('scroll', update, { passive: true });
const ro = new ResizeObserver(update);
ro.observe(el);
Expand Down Expand Up @@ -191,7 +196,7 @@ export default function PinnedScrollbar({
ref={thumbRef}
role="scrollbar"
tabIndex={0}
aria-controls={scrollContainerId}
aria-controls={controlsId}
aria-orientation="horizontal"
aria-valuenow={scrollPercent}
aria-valuemin={0}
Expand Down
Loading