-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(content-explorer): Migrate shareDialog
- Loading branch information
1 parent
b64fefa
commit 1728cfc
Showing
8 changed files
with
299 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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 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 * as React from 'react'; | ||
import Modal from 'react-modal'; | ||
import noop from 'lodash/noop'; | ||
import { FormattedMessage, useIntl } from 'react-intl'; | ||
import { Button, Modal as BlueprintModal, Text } from '@box/blueprint-web'; | ||
|
||
import ShareAccessSelect from '../common/share-access-select'; | ||
import { CLASS_MODAL_CONTENT, CLASS_MODAL_OVERLAY, CLASS_MODAL } from '../../constants'; | ||
import type { BoxItem } from '../../common/types/core'; | ||
|
||
import messages from '../common/messages'; | ||
|
||
import './ShareDialog.scss'; | ||
|
||
export interface ShareDialogProps { | ||
appElement: HTMLElement; | ||
canSetShareAccess: boolean; | ||
isLoading: boolean; | ||
isOpen: boolean; | ||
item: BoxItem; | ||
onCancel: () => void; | ||
onShareAccessChange: () => void; | ||
parentElement: HTMLElement; | ||
} | ||
|
||
const ShareDialog = ({ | ||
appElement, | ||
canSetShareAccess, | ||
isLoading, | ||
isOpen, | ||
item, | ||
onCancel, | ||
onShareAccessChange, | ||
parentElement, | ||
}: ShareDialogProps) => { | ||
const { formatMessage } = useIntl(); | ||
let textInput = null; | ||
|
||
const copy = () => { | ||
if (textInput instanceof HTMLInputElement) { | ||
textInput.select(); | ||
document.execCommand('copy'); | ||
} | ||
}; | ||
|
||
const { shared_link: sharedLink }: BoxItem = item; | ||
const { url } = sharedLink || { | ||
url: formatMessage(messages.shareDialogNone), | ||
}; | ||
|
||
return ( | ||
<Modal | ||
appElement={appElement} | ||
className={CLASS_MODAL_CONTENT} | ||
contentLabel={formatMessage(messages.shareDialogLabel)} | ||
isOpen={isOpen} | ||
onRequestClose={onCancel} | ||
overlayClassName={CLASS_MODAL_OVERLAY} | ||
parentSelector={() => parentElement} | ||
portalClassName={`${CLASS_MODAL} be-modal-share`} | ||
> | ||
<BlueprintModal.Body> | ||
<Text as="label"> | ||
<FormattedMessage {...messages.shareDialogText} /> | ||
</Text> | ||
<div className="be-modal-input-group"> | ||
<input | ||
ref={input => { | ||
textInput = input; | ||
}} | ||
onChange={noop} | ||
type="text" | ||
value={url} | ||
/> | ||
<Button autoFocus className="be-modal-button-copy" onClick={copy} variant="primary"> | ||
{formatMessage(messages.copy)} | ||
</Button> | ||
</div> | ||
|
||
<BlueprintModal.Footer className="be-modal-btns"> | ||
<ShareAccessSelect | ||
canSetShareAccess={canSetShareAccess} | ||
className="bce-shared-access-select" | ||
item={item} | ||
onChange={onShareAccessChange} | ||
/> | ||
<Button | ||
loading={isLoading} | ||
loadingAriaLabel={formatMessage(messages.loading)} | ||
onClick={onCancel} | ||
size="large" | ||
variant="secondary" | ||
> | ||
{formatMessage(messages.close)} | ||
</Button> | ||
</BlueprintModal.Footer> | ||
</BlueprintModal.Body> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ShareDialog; |
8 changes: 0 additions & 8 deletions
8
src/elements/content-explorer/__tests__/ShareDialog.stories.test.js
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/elements/content-explorer/__tests__/ShareDialog.test.tsx
This file contains 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,53 @@ | ||
import * as React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen, waitFor } from '../../../test-utils/testing-library'; | ||
import ShareDialog, { ShareDialogProps } from '../ShareDialog'; | ||
|
||
jest.mock('react-modal', () => { | ||
return jest.fn(({ children }) => <div aria-label="Share">{children}</div>); | ||
}); | ||
|
||
describe('elements/content-explorer/ShareDialog', () => { | ||
const defaultProps = { | ||
appElement: document.createElement('div'), | ||
canSetShareAccess: true, | ||
isLoading: false, | ||
isOpen: true, | ||
item: { shared_link: { url: 'http://example.com' } }, | ||
onCancel: jest.fn(), | ||
onShareAccessChange: jest.fn(), | ||
parentElement: document.createElement('div'), | ||
}; | ||
|
||
const renderComponent = (props?: Partial<ShareDialogProps>) => render(<ShareDialog {...defaultProps} {...props} />); | ||
|
||
test('renders', async () => { | ||
renderComponent(); | ||
expect(await screen.findByLabelText('Share')).toBeInTheDocument(); | ||
expect(await screen.findByRole('button', { name: 'Close' })); | ||
}); | ||
|
||
test('calls onCancel when Close button is clicked', async () => { | ||
renderComponent(); | ||
userEvent.click(await screen.findByRole('button', { name: 'Close' })); | ||
await waitFor(() => expect(defaultProps.onCancel).toHaveBeenCalled()); | ||
}); | ||
|
||
test('copies URL to clipboard when Copy button is clicked', async () => { | ||
renderComponent(); | ||
const copyButton = await screen.findByRole('button', { name: 'Copy' }); | ||
document.execCommand = jest.fn(); | ||
userEvent.click(copyButton); | ||
await waitFor(() => expect(document.execCommand).toHaveBeenCalledWith('copy')); | ||
}); | ||
|
||
test('renders loading state when isLoading is true', async () => { | ||
renderComponent({ isLoading: true }); | ||
expect(await screen.findByRole('status', { name: 'Loading' })).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders with empty input when item has no shared link', async () => { | ||
renderComponent({ item: { shared_link: null } }); | ||
expect(await screen.findByRole('textbox')).toHaveValue('None'); | ||
}); | ||
}); |
This file contains 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
Oops, something went wrong.