mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
fix(mobile): a day tapped in the day rail moves the pager on short trips too
The strip re-centred only when its window of pages changed. On a trip of up to five days a tap can keep the same pages, so the rail showed the new day while the card below stayed on the old one.
This commit is contained in:
@@ -199,6 +199,39 @@ describe('MDayPager', () => {
|
||||
expect(strip.scrollLeft).toBe(390);
|
||||
});
|
||||
|
||||
it('moves the strip to a day tapped inside the same window, as on a two-day trip', () => {
|
||||
const twoDays = DAYS.slice(0, 2);
|
||||
const { strip, rerender, onSelectDay } = setup(1, { days: twoDays });
|
||||
let scrollLeft = 0;
|
||||
Object.defineProperty(strip, 'scrollLeft', {
|
||||
configurable: true,
|
||||
get: () => scrollLeft,
|
||||
set: (v) => {
|
||||
scrollLeft = v;
|
||||
},
|
||||
});
|
||||
// Both days are in the window either way; only the current page changes.
|
||||
const realQuery = strip.querySelector.bind(strip);
|
||||
vi.spyOn(strip, 'querySelector').mockImplementation((sel: string) => {
|
||||
const el = realQuery(sel) as HTMLElement | null;
|
||||
if (el && sel === '[data-page-active]') el.getBoundingClientRect = () => ({ left: 390 }) as DOMRect;
|
||||
return el;
|
||||
});
|
||||
strip.getBoundingClientRect = () => ({ left: 0 }) as DOMRect;
|
||||
|
||||
rerender(
|
||||
<MDayPager
|
||||
days={twoDays}
|
||||
selectedDayId={2}
|
||||
onSelectDay={onSelectDay}
|
||||
describeDay={() => ''}
|
||||
renderDay={(dayId) => <p>{`page ${dayId}`}</p>}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(strip.scrollLeft).toBe(390);
|
||||
});
|
||||
|
||||
it('holds still while locked', () => {
|
||||
const { strip } = setup(2, { locked: true });
|
||||
expect(strip).toHaveClass('overflow-x-hidden');
|
||||
|
||||
@@ -71,7 +71,10 @@ export default function MDayPager({
|
||||
const stripRef = useRef<HTMLDivElement>(null);
|
||||
const [announcement, setAnnouncement] = useState('');
|
||||
const pages = pagesFor(days, selectedDayId);
|
||||
const windowKey = pages.map((p) => p.key).join(',');
|
||||
// Which page is current belongs in the key: on a trip of up to five days a
|
||||
// chip tap can keep the same pages, and the strip then stayed on the old day
|
||||
// while the rail already showed the new one.
|
||||
const windowKey = pages.map((p) => (p.active ? `*${p.key}` : p.key)).join(',');
|
||||
|
||||
// The handlers below are attached once, so they read the props through here.
|
||||
const latest = useRef({ days, onSelectDay, describeDay, selectedDayId, onPreview });
|
||||
@@ -91,7 +94,7 @@ export default function MDayPager({
|
||||
if (Math.abs(offset) > 0.5) strip.scrollLeft += offset;
|
||||
}, []);
|
||||
|
||||
// Before paint whenever the window moves: a landed swipe or a chip tap.
|
||||
// Before paint whenever the window or its current page moves: a landed swipe or a chip tap.
|
||||
useLayoutEffect(() => {
|
||||
requested.current = null;
|
||||
recentre();
|
||||
|
||||
Reference in New Issue
Block a user