-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
50 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
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
58 changes: 58 additions & 0 deletions
58
Composer/packages/lib/ui-shared/src/components/CopyableText.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,58 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import React from 'react'; | ||
import { Stack } from 'office-ui-fabric-react/lib/Stack'; | ||
import { IconButton } from 'office-ui-fabric-react/lib/Button'; | ||
import { FluentTheme } from '@uifabric/fluent-theme'; | ||
|
||
const buttonIconProps = { iconName: 'Copy' }; | ||
const rootStyles = { | ||
root: { background: 'rgba(0, 0, 0, .08)', margin: '8px 0 4px 0' }, | ||
}; | ||
const textStyles = { root: { padding: '6px 4px 6px 12px' } }; | ||
const preStyles: React.CSSProperties = { | ||
margin: 0, | ||
padding: 0, | ||
fontSize: FluentTheme.fonts.small.fontSize, | ||
whiteSpace: 'pre-line', | ||
wordBreak: 'break-all', | ||
fontFamily: 'Menlo,Consolas,Courier New,monospace', | ||
}; | ||
|
||
type Props = { | ||
className?: string; | ||
text: string; | ||
buttonAriaLabel?: string; | ||
buttonTitle?: string; | ||
}; | ||
|
||
export const CopyableText = (props: Props) => { | ||
const { className, text, buttonAriaLabel = 'Copy text to clipboard', buttonTitle = 'Copy text to clipboard' } = props; | ||
|
||
const copyToClipboard = React.useCallback(async () => { | ||
try { | ||
await window.navigator.clipboard.writeText(text); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error: Copy text to clipboard. details: ', e); | ||
} | ||
}, [text]); | ||
|
||
return ( | ||
<Stack horizontal className={className} styles={rootStyles}> | ||
<Stack.Item grow styles={textStyles}> | ||
<pre style={preStyles}>{text}</pre> | ||
</Stack.Item> | ||
<Stack.Item verticalFill> | ||
<IconButton | ||
ariaLabel={buttonAriaLabel} | ||
data-testid="copy-to-clipboard-button" | ||
iconProps={buttonIconProps} | ||
title={buttonTitle} | ||
onClick={copyToClipboard} | ||
/> | ||
</Stack.Item> | ||
</Stack> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
Composer/packages/lib/ui-shared/src/components/__tests__/CopyableText.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,33 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { render, fireEvent } from '@botframework-composer/test-utils'; | ||
import React from 'react'; | ||
|
||
import { CopyableText } from '../CopyableText'; | ||
|
||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: () => {}, | ||
}, | ||
}); | ||
|
||
describe('<CopyableText />', () => { | ||
it('Should render the given text', async () => { | ||
const textToCopy = 'This is a command'; | ||
const { container } = render(<CopyableText text={textToCopy} />); | ||
|
||
expect(container).toHaveTextContent(textToCopy); | ||
}); | ||
|
||
it('Should copy text to clipboard when the copy button ios clicked', async () => { | ||
jest.spyOn(navigator.clipboard, 'writeText'); | ||
const textToCopy = 'This is a command'; | ||
const { findByTestId } = render(<CopyableText text={textToCopy} />); | ||
|
||
const copyButton = await findByTestId('copy-to-clipboard-button'); | ||
fireEvent.click(copyButton); | ||
|
||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(textToCopy); | ||
}); | ||
}); |
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