App: rozwijanie dlugiej nazwy cwiczenia w edytorze planu (klik zwija/rozwija, hover = tooltip)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Activity, ChevronDown, ChevronUp, Minus, Plus, X } from 'lucide-react';
|
||||
import { Badge, Button } from '../../../components/ui';
|
||||
import { cn } from '../../../lib/utils';
|
||||
import { MUSCLE_COLORS, muscleName } from '../../../lib/labels';
|
||||
import type { Exercise, ExerciseProgressPoint, LatestExerciseResult } from '../../../api/types';
|
||||
import type { EditorExercise, EditorSet } from './types';
|
||||
@@ -80,13 +81,56 @@ export function ExerciseEditor({
|
||||
const [advanced, setAdvanced] = useState(false);
|
||||
const columns = SET_COLUMNS.filter((c) => !c.advanced || advanced);
|
||||
|
||||
/* Długa nazwa ćwiczenia jest przycinana — kliknięcie zwija/rozwija pełną nazwę,
|
||||
hover (desktop) pokazuje title. Przycisk pojawia się dopiero, gdy nazwa
|
||||
realnie się nie mieści (scrollWidth > clientWidth — nie zmieści się w całości). */
|
||||
const name = exerciseName(exercise.exerciseId);
|
||||
const [nameExpanded, setNameExpanded] = useState(false);
|
||||
const [nameTruncated, setNameTruncated] = useState(false);
|
||||
const nameRef = useRef<HTMLElement | null>(null);
|
||||
const attachNameRef = (el: HTMLElement | null) => {
|
||||
nameRef.current = el;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setNameExpanded(false);
|
||||
}, [name]);
|
||||
|
||||
useEffect(() => {
|
||||
const el = nameRef.current;
|
||||
if (!el) return;
|
||||
const check = () => setNameTruncated(el.scrollWidth > el.clientWidth + 1);
|
||||
check();
|
||||
const ro = new ResizeObserver(check);
|
||||
ro.observe(el);
|
||||
return () => ro.disconnect();
|
||||
}, [name, nameExpanded]);
|
||||
|
||||
const nameInteractive = nameTruncated || nameExpanded;
|
||||
|
||||
return (
|
||||
<div className="rounded-[10px] border border-line bg-card/40 p-3">
|
||||
<div className="mb-2 flex items-center justify-between gap-2">
|
||||
<div className="flex min-w-0 flex-1 items-center gap-2">
|
||||
<strong className="min-w-0 truncate text-[0.95rem]">
|
||||
{exerciseName(exercise.exerciseId)}
|
||||
</strong>
|
||||
{nameInteractive ? (
|
||||
<button
|
||||
type="button"
|
||||
ref={attachNameRef}
|
||||
title={name}
|
||||
aria-expanded={nameExpanded}
|
||||
onClick={() => setNameExpanded((v) => !v)}
|
||||
className={cn(
|
||||
'min-w-0 cursor-pointer text-left text-[0.95rem] font-bold underline decoration-dotted decoration-dim/60 underline-offset-2',
|
||||
nameExpanded ? 'break-words' : 'truncate',
|
||||
)}
|
||||
>
|
||||
{name}
|
||||
</button>
|
||||
) : (
|
||||
<strong ref={attachNameRef} className="min-w-0 truncate text-[0.95rem]">
|
||||
{name}
|
||||
</strong>
|
||||
)}
|
||||
{exerciseMeta && (
|
||||
<Badge color={MUSCLE_COLORS[exerciseMeta.primaryMuscle] ?? 'gray'} className="shrink-0">
|
||||
{muscleName(exerciseMeta.primaryMuscle)}
|
||||
|
||||
83
app/test/ExerciseEditor.test.tsx
Normal file
83
app/test/ExerciseEditor.test.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import { ExerciseEditor } from '../src/pages/plans/plan-editor/ExerciseEditor';
|
||||
import type { EditorExercise } from '../src/pages/plans/plan-editor/types';
|
||||
|
||||
const LONG_NAME = 'Wiosłowanie sztangą nachwytem w pełnym opadzie tułowia';
|
||||
|
||||
const EXERCISE: EditorExercise = {
|
||||
exerciseId: 'ex-1',
|
||||
sets: [{ reps: '10', loadKg: '50', restSec: '90', rir: '2' }],
|
||||
};
|
||||
|
||||
function renderEditor() {
|
||||
return render(
|
||||
<ExerciseEditor
|
||||
exercise={EXERCISE}
|
||||
exerciseMeta={undefined}
|
||||
exerciseName={() => LONG_NAME}
|
||||
isTraineeMode={false}
|
||||
resultsOpen={false}
|
||||
onToggleResults={() => {}}
|
||||
onRemove={() => {}}
|
||||
onMoveUp={() => {}}
|
||||
onMoveDown={() => {}}
|
||||
canMoveUp={false}
|
||||
canMoveDown={false}
|
||||
onSetField={() => {}}
|
||||
onAddSetCopy={() => {}}
|
||||
onRemoveSet={() => {}}
|
||||
latest={undefined}
|
||||
points={[]}
|
||||
onPrefill={() => {}}
|
||||
/>,
|
||||
);
|
||||
}
|
||||
|
||||
/** Symulacja realnego przycięcia tekstu: scrollWidth > clientWidth (jsdom zwraca same 0). */
|
||||
function mockTruncated(truncated: boolean) {
|
||||
Object.defineProperty(Element.prototype, 'scrollWidth', {
|
||||
configurable: true,
|
||||
get: () => (truncated ? 400 : 0),
|
||||
});
|
||||
}
|
||||
|
||||
const scrollWidthDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollWidth');
|
||||
|
||||
afterEach(() => {
|
||||
if (scrollWidthDescriptor) {
|
||||
Object.defineProperty(Element.prototype, 'scrollWidth', scrollWidthDescriptor);
|
||||
} else {
|
||||
delete (Element.prototype as unknown as Record<string, unknown>).scrollWidth;
|
||||
}
|
||||
});
|
||||
|
||||
describe('ExerciseEditor — rozwijanie długiej nazwy ćwiczenia', () => {
|
||||
it('bez przycięcia renderuje samą nazwę (bez przycisku)', () => {
|
||||
mockTruncated(false);
|
||||
renderEditor();
|
||||
expect(screen.getByText(LONG_NAME)).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: LONG_NAME })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('po przycięciu nazwa staje się przyciskiem z title i rozwija się po kliknięciu', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockTruncated(true);
|
||||
renderEditor();
|
||||
|
||||
const btn = screen.getByRole('button', { name: LONG_NAME });
|
||||
expect(btn).toHaveAttribute('title', LONG_NAME);
|
||||
expect(btn).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(btn).toHaveClass('truncate');
|
||||
|
||||
await user.click(btn);
|
||||
expect(btn).toHaveAttribute('aria-expanded', 'true');
|
||||
expect(btn).toHaveClass('break-words');
|
||||
expect(btn).not.toHaveClass('truncate');
|
||||
|
||||
await user.click(btn);
|
||||
expect(btn).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(btn).toHaveClass('truncate');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user