-
Notifications
You must be signed in to change notification settings - Fork 3
fix(apollo-react): add loop node iteration navigator #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/apollo-react/src/canvas/components/LoopNode/IterationNavigator.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { IterationNavigator } from './IterationNavigator'; | ||
| import type { LoopIterationState } from './LoopNode.types'; | ||
|
|
||
| function renderNavigator(iterationState: Partial<LoopIterationState> = {}) { | ||
| const onActiveIndexChange = vi.fn(); | ||
|
|
||
| render( | ||
| <IterationNavigator | ||
| iterationState={{ | ||
| activeIndex: 1, | ||
| total: 3, | ||
| onActiveIndexChange, | ||
| ...iterationState, | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| return { onActiveIndexChange }; | ||
| } | ||
|
|
||
| describe('IterationNavigator', () => { | ||
| it.each([ | ||
| ['zero', 0], | ||
| ['negative', -1], | ||
| ['NaN', Number.NaN], | ||
| ['Infinity', Number.POSITIVE_INFINITY], | ||
| ])('does not render for %s total', (_caseName, total) => { | ||
| renderNavigator({ total }); | ||
|
|
||
| expect(screen.queryByTestId('loop-iteration-navigator')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('clamps activeIndex before displaying and computing callbacks', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onActiveIndexChange } = renderNavigator({ activeIndex: 99, total: 3 }); | ||
|
|
||
| expect(screen.getByTestId('loop-iteration-label')).toHaveTextContent('3 / 3'); | ||
| expect(screen.getByRole('button', { name: 'Next loop iteration' })).toBeDisabled(); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Previous loop iteration' })); | ||
|
|
||
| expect(onActiveIndexChange).toHaveBeenCalledOnce(); | ||
| expect(onActiveIndexChange).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('fires previous and next callbacks with the adjacent index', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onActiveIndexChange } = renderNavigator({ activeIndex: 1, total: 3 }); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Previous loop iteration' })); | ||
| await user.click(screen.getByRole('button', { name: 'Next loop iteration' })); | ||
|
|
||
| expect(onActiveIndexChange).toHaveBeenNthCalledWith(1, 0); | ||
| expect(onActiveIndexChange).toHaveBeenNthCalledWith(2, 2); | ||
| }); | ||
|
|
||
| it('disables previous and next at iteration boundaries', () => { | ||
| const { rerender } = render( | ||
| <IterationNavigator | ||
| iterationState={{ | ||
| activeIndex: 0, | ||
| total: 3, | ||
| onActiveIndexChange: vi.fn(), | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Previous loop iteration' })).toBeDisabled(); | ||
| expect(screen.getByRole('button', { name: 'Next loop iteration' })).not.toBeDisabled(); | ||
|
|
||
| rerender( | ||
| <IterationNavigator | ||
| iterationState={{ | ||
| activeIndex: 2, | ||
| total: 3, | ||
| onActiveIndexChange: vi.fn(), | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Previous loop iteration' })).not.toBeDisabled(); | ||
| expect(screen.getByRole('button', { name: 'Next loop iteration' })).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('renders as label-only when onActiveIndexChange is omitted', () => { | ||
| renderNavigator({ onActiveIndexChange: undefined }); | ||
|
|
||
| expect(screen.getByTestId('loop-iteration-label')).toHaveTextContent('2 / 3'); | ||
| expect(screen.getByRole('button', { name: 'Previous loop iteration' })).toBeDisabled(); | ||
| expect(screen.getByRole('button', { name: 'Next loop iteration' })).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('disables navigation when disabled', () => { | ||
| renderNavigator({ disabled: true }); | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Previous loop iteration' })).toBeDisabled(); | ||
| expect(screen.getByRole('button', { name: 'Next loop iteration' })).toBeDisabled(); | ||
| }); | ||
| }); |
130 changes: 130 additions & 0 deletions
130
packages/apollo-react/src/canvas/components/LoopNode/IterationNavigator.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { cn } from '@uipath/apollo-wind'; | ||
| import type { SyntheticEvent } from 'react'; | ||
| import { useCallback } from 'react'; | ||
| import { clamp } from '../../utils'; | ||
| import { CanvasIcon } from '../../utils/icon-registry'; | ||
| import type { LoopIterationState } from './LoopNode.types'; | ||
|
|
||
| interface IterationNavigatorProps { | ||
| iterationState: LoopIterationState; | ||
| } | ||
|
|
||
| function resolveState(iterationState: LoopIterationState): LoopIterationState | undefined { | ||
| if (!Number.isFinite(iterationState.total)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const total = Math.trunc(iterationState.total); | ||
|
|
||
| if (total <= 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const rawActiveIndex = Number.isFinite(iterationState.activeIndex) | ||
| ? Math.trunc(iterationState.activeIndex) | ||
| : 0; | ||
|
|
||
| return { | ||
| ...iterationState, | ||
| total, | ||
| activeIndex: clamp(rawActiveIndex, 0, total - 1), | ||
| }; | ||
| } | ||
|
|
||
| function stopCanvasControlEvent(event: SyntheticEvent) { | ||
| event.stopPropagation(); | ||
| } | ||
|
|
||
| export function IterationNavigator({ iterationState }: IterationNavigatorProps) { | ||
| const resolvedState = resolveState(iterationState); | ||
|
|
||
| if (!resolvedState) { | ||
| return null; | ||
| } | ||
|
|
||
| return <NavigatorContent iterationState={resolvedState} />; | ||
| } | ||
|
|
||
| function NavigatorContent({ iterationState }: { iterationState: LoopIterationState }) { | ||
| const { activeIndex, total, onActiveIndexChange, disabled, ariaLabel } = iterationState; | ||
| const canInteract = !disabled && typeof onActiveIndexChange === 'function'; | ||
| const canGoPrevious = canInteract && activeIndex > 0; | ||
| const canGoNext = canInteract && activeIndex < total - 1; | ||
| const label = ariaLabel ?? 'Loop iteration'; | ||
| const visibleIndex = activeIndex + 1; | ||
|
|
||
| const handlePrevious = useCallback( | ||
| (event: SyntheticEvent) => { | ||
| event.stopPropagation(); | ||
|
|
||
| if (!canGoPrevious) return; | ||
| onActiveIndexChange?.(activeIndex - 1); | ||
| }, | ||
| [activeIndex, canGoPrevious, onActiveIndexChange] | ||
| ); | ||
|
|
||
| const handleNext = useCallback( | ||
| (event: SyntheticEvent) => { | ||
| event.stopPropagation(); | ||
|
|
||
| if (!canGoNext) return; | ||
| onActiveIndexChange?.(activeIndex + 1); | ||
| }, | ||
| [activeIndex, canGoNext, onActiveIndexChange] | ||
| ); | ||
|
|
||
| return ( | ||
| <fieldset | ||
| className={cn( | ||
| 'nodrag nopan pointer-events-auto m-0 flex h-6 min-w-0 shrink-0 items-center gap-1 rounded-full px-1 py-0', | ||
| 'border border-border bg-surface text-foreground shadow-sm' | ||
| )} | ||
| data-testid="loop-iteration-navigator" | ||
| onPointerDown={stopCanvasControlEvent} | ||
| onMouseDown={stopCanvasControlEvent} | ||
| onDoubleClick={stopCanvasControlEvent} | ||
| > | ||
| <legend className="sr-only"> | ||
| {label}: {visibleIndex} of {total} | ||
| </legend> | ||
| <button | ||
| type="button" | ||
| className={cn( | ||
| 'nodrag nopan inline-flex h-4 w-4 items-center justify-center rounded-full', | ||
| 'text-foreground transition-opacity', | ||
| canGoPrevious ? 'cursor-pointer opacity-100' : 'cursor-not-allowed opacity-40' | ||
| )} | ||
| disabled={!canGoPrevious} | ||
| aria-label="Previous loop iteration" | ||
| onClick={handlePrevious} | ||
| onPointerDown={stopCanvasControlEvent} | ||
| onMouseDown={stopCanvasControlEvent} | ||
| data-testid="loop-iteration-previous" | ||
| > | ||
| <CanvasIcon icon="chevron-left" size={12} /> | ||
| </button> | ||
| <span | ||
| className="min-w-8 select-none px-1 text-center text-[11px] font-semibold leading-4" | ||
| data-testid="loop-iteration-label" | ||
| > | ||
| {visibleIndex} / {total} | ||
| </span> | ||
| <button | ||
| type="button" | ||
| className={cn( | ||
| 'nodrag nopan inline-flex h-4 w-4 items-center justify-center rounded-full', | ||
| 'text-foreground transition-opacity', | ||
| canGoNext ? 'cursor-pointer opacity-100' : 'cursor-not-allowed opacity-40' | ||
| )} | ||
| disabled={!canGoNext} | ||
| aria-label="Next loop iteration" | ||
| onClick={handleNext} | ||
| onPointerDown={stopCanvasControlEvent} | ||
| onMouseDown={stopCanvasControlEvent} | ||
| data-testid="loop-iteration-next" | ||
| > | ||
| <CanvasIcon icon="chevron-right" size={12} /> | ||
| </button> | ||
| </fieldset> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.