index.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import CopyButton from '../CopyButton'
  19. import DomUtils from '../../../utils/DomUtils'
  20. import notificationStore from '../../../stores/NotificationStore'
  21. const Wrapper = styled.div`
  22. cursor: pointer;
  23. display: flex;
  24. &:hover > span:last-child {
  25. opacity: 1;
  26. }
  27. `
  28. const Value = styled.span`
  29. width: ${props => `${props.width || 'auto'}`};
  30. ${props => props.maxWidth ? `max-width: ${props.maxWidth};` : ''}
  31. white-space: nowrap;
  32. overflow: hidden;
  33. text-overflow: ellipsis;
  34. display: inline-block;
  35. margin-right: 4px;
  36. `
  37. type Props = {
  38. value: string,
  39. width?: string,
  40. maxWidth?: string,
  41. 'data-test-id'?: string,
  42. onCopy?: (value: string) => void,
  43. }
  44. @observer
  45. class CopyValue extends React.Component<Props> {
  46. handleCopyIdClick(e: Event) {
  47. if (e && e.stopPropagation) e.stopPropagation()
  48. let succesful = DomUtils.copyTextToClipboard(this.props.value)
  49. if (this.props.onCopy) this.props.onCopy(this.props.value)
  50. if (succesful) {
  51. notificationStore.notify('The value has been copied to clipboard.')
  52. } else {
  53. notificationStore.notify('The value couldn\'t be copied', 'error')
  54. }
  55. }
  56. render() {
  57. return (
  58. <Wrapper
  59. onClick={e => { this.handleCopyIdClick(e) }}
  60. onMouseDown={e => { e.stopPropagation() }}
  61. onMouseUp={e => { e.stopPropagation() }}
  62. data-test-id={this.props['data-test-id'] || 'copyValue'}
  63. >
  64. <Value
  65. data-test-id="copyValue-value"
  66. width={this.props.width}
  67. maxWidth={this.props.maxWidth}
  68. >{this.props.value}</Value>
  69. <CopyButton />
  70. </Wrapper>
  71. )
  72. }
  73. }
  74. export default CopyValue