You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShareLinkForm.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { Component } from 'react';
  2. import { translate } from '../../base/i18n';
  3. const logger = require('jitsi-meet-logger').getLogger(__filename);
  4. /**
  5. * A React Component for displaying a value with a copy button that can be
  6. * clicked to copy the value onto the clipboard.
  7. */
  8. class ShareLinkForm extends Component {
  9. /**
  10. * ShareLinkForm component's property types.
  11. *
  12. * @static
  13. */
  14. static propTypes = {
  15. /**
  16. * Invoked to obtain translated strings.
  17. */
  18. t: React.PropTypes.func,
  19. /**
  20. * The value to be displayed and copied onto the clipboard.
  21. */
  22. toCopy: React.PropTypes.string
  23. }
  24. /**
  25. * Initializes a new ShareLinkForm instance.
  26. *
  27. * @param {Object} props - The read-only properties with which the new
  28. * instance is to be initialized.
  29. */
  30. constructor(props) {
  31. super(props);
  32. this._inputElement = null;
  33. this._onClick = this._onClick.bind(this);
  34. this._setInput = this._setInput.bind(this);
  35. }
  36. /**
  37. * Implements React's {@link Component#render()}.
  38. *
  39. * @inheritdoc
  40. * @returns {ReactElement}
  41. */
  42. render() {
  43. const inputValue = this.props.toCopy
  44. || this.props.t('inviteUrlDefaultMsg');
  45. // FIXME input is used here instead of atlaskit field-text because
  46. // field-text does not currently support readonly
  47. return (
  48. <div className = 'form-control'>
  49. <label className = 'form-control__label'>
  50. { this.props.t('dialog.shareLink') }
  51. </label>
  52. <div className = 'form-control__container'>
  53. <input
  54. className = 'input-control inviteLink'
  55. id = 'inviteLinkRef'
  56. readOnly = { true }
  57. ref = { this._setInput }
  58. type = 'text'
  59. value = { inputValue } />
  60. <button
  61. className =
  62. 'button-control button-control_light copyInviteLink'
  63. onClick = { this._onClick }
  64. type = 'button'>
  65. { this.props.t('dialog.copy') }
  66. </button>
  67. </div>
  68. </div>
  69. );
  70. }
  71. /**
  72. * Copies the passed in value to the clipboard.
  73. *
  74. * @private
  75. * @returns {void}
  76. */
  77. _onClick() {
  78. try {
  79. this._inputElement.select();
  80. document.execCommand('copy');
  81. this._inputElement.blur();
  82. } catch (err) {
  83. logger.error('error when copying the text', err);
  84. }
  85. }
  86. /**
  87. * Sets the internal reference to the DOM element for the input field so it
  88. * may be accessed directly.
  89. *
  90. * @param {Object} element - DOM element for the component's input.
  91. * @private
  92. * @returns {void}
  93. */
  94. _setInput(element) {
  95. this._inputElement = element;
  96. }
  97. }
  98. export default translate(ShareLinkForm);