-
Notifications
You must be signed in to change notification settings - Fork 3.8k
refactor(ui): share image upload preview tiles #8015
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
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
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
82 changes: 82 additions & 0 deletions
82
packages/emcn/src/components/upload-preview-button/upload-preview-button.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,82 @@ | ||
| /** @vitest-environment jsdom */ | ||
| import { act, createRef, type ReactNode } from 'react' | ||
| import { UploadPreviewButton } from '@sim/emcn' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| let root: Root | null = null | ||
| let container: HTMLDivElement | null = null | ||
|
|
||
| function mount(children: ReactNode) { | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| root = createRoot(container) | ||
| act(() => root?.render(children)) | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| if (root) act(() => root?.unmount()) | ||
| container?.remove() | ||
| root = null | ||
| container = null | ||
| }) | ||
|
|
||
| describe('UploadPreviewButton', () => { | ||
| it('forwards the label, ref and upload action without submitting an enclosing form', () => { | ||
| const ref = createRef<HTMLButtonElement>() | ||
| const onClick = vi.fn() | ||
| const onSubmit = vi.fn((event) => event.preventDefault()) | ||
| mount( | ||
| <form onSubmit={onSubmit}> | ||
| <UploadPreviewButton ref={ref} aria-label='Upload logo' onClick={onClick} /> | ||
| </form> | ||
| ) | ||
|
|
||
| const button = ref.current! | ||
| expect(button).toBe(container?.querySelector('button')) | ||
| expect(button.getAttribute('aria-label')).toBe('Upload logo') | ||
| expect(button.querySelector('svg')?.getAttribute('aria-hidden')).toBe('true') | ||
| act(() => button.click()) | ||
| expect(onClick).toHaveBeenCalledTimes(1) | ||
| expect(onSubmit).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('replaces the preview while busy and preserves a separate disabled restriction', () => { | ||
| const ref = createRef<HTMLButtonElement>() | ||
| const onClick = vi.fn() | ||
| const render = (loading: boolean, disabled = false) => ( | ||
| <UploadPreviewButton | ||
| ref={ref} | ||
| aria-label='Change logo' | ||
| loading={loading} | ||
| disabled={disabled} | ||
| onClick={onClick} | ||
| > | ||
| <img src='/logo.svg' alt='Logo' /> | ||
| </UploadPreviewButton> | ||
| ) | ||
| mount(render(false)) | ||
| const button = ref.current! | ||
| expect(button.querySelector('img')?.alt).toBe('Logo') | ||
|
|
||
| act(() => root?.render(render(true))) | ||
| expect(button.disabled).toBe(true) | ||
| expect(button.getAttribute('aria-busy')).toBe('true') | ||
| expect(button.querySelector('img')).toBeNull() | ||
| act(() => button.click()) | ||
| expect(onClick).not.toHaveBeenCalled() | ||
|
|
||
| act(() => root?.render(render(false, true))) | ||
| expect(button.disabled).toBe(true) | ||
| expect(button.hasAttribute('aria-busy')).toBe(false) | ||
| expect(button.querySelector('img')?.alt).toBe('Logo') | ||
| act(() => button.click()) | ||
| expect(onClick).not.toHaveBeenCalled() | ||
|
|
||
| act(() => root?.render(render(false))) | ||
| expect(button.disabled).toBe(false) | ||
| act(() => button.click()) | ||
| expect(onClick).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
45 changes: 45 additions & 0 deletions
45
packages/emcn/src/components/upload-preview-button/upload-preview-button.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,45 @@ | ||
| import { type ComponentPropsWithoutRef, forwardRef } from 'react' | ||
| import { ImageUp } from '../../icons/image-up' | ||
| import { Loader } from '../../icons/loader' | ||
| import { cn } from '../../lib/cn' | ||
|
|
||
| export interface UploadPreviewButtonProps extends Omit<ComponentPropsWithoutRef<'button'>, 'type'> { | ||
| /** Accessible name describing which image will be uploaded or replaced. */ | ||
| 'aria-label': string | ||
| /** Shows the shared loader and prevents another activation during upload. */ | ||
| loading?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Image-upload preview tile. Renders the caller's image, an empty-image icon, | ||
| * or a loading indicator. File selection, validation, and uploads stay with | ||
| * the caller. The 64px square can expand through layout classes such as `w-full`. | ||
| * | ||
| * @example | ||
| * <UploadPreviewButton aria-label='Change logo' loading={uploading} onClick={selectFile}> | ||
| * {logoUrl ? <img src={logoUrl} alt='' className='size-full object-contain p-1' /> : null} | ||
| * </UploadPreviewButton> | ||
| */ | ||
| export const UploadPreviewButton = forwardRef<HTMLButtonElement, UploadPreviewButtonProps>( | ||
| ({ children, className, loading = false, disabled, ...props }, ref) => ( | ||
| <button | ||
| {...props} | ||
| ref={ref} | ||
| type='button' | ||
| disabled={disabled || loading} | ||
| aria-busy={loading || undefined} | ||
| className={cn( | ||
| 'group relative flex size-16 shrink-0 items-center justify-center overflow-hidden rounded-xl border border-[var(--border)] bg-[var(--surface-2)] transition-colors hover:bg-[var(--surface-3)] disabled:opacity-50', | ||
| className | ||
| )} | ||
| > | ||
| {loading ? ( | ||
| <Loader className='size-5 text-[var(--text-muted)]' animate /> | ||
| ) : ( | ||
| (children ?? <ImageUp className='size-5 text-[var(--text-muted)]' />) | ||
| )} | ||
| </button> | ||
| ) | ||
| ) | ||
|
|
||
| UploadPreviewButton.displayName = 'UploadPreviewButton' | ||
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.