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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 {@code Component} for displaying a value with a copy button to copy
  6. * the value into the clipboard.
  7. */
  8. class ShareLinkForm extends Component {
  9. /**
  10. * {@code ShareLinkForm}'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 into the clipboard.
  21. */
  22. toCopy: React.PropTypes.string
  23. };
  24. /**
  25. * Initializes a new {@code 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. /**
  33. * The internal reference to the DOM/HTML element backing the React
  34. * {@code Component} input with id {@code inviteLinkRef}. It is
  35. * necessary for the implementation of copying to the clipboard.
  36. *
  37. * @private
  38. * @type {HTMLInputElement}
  39. */
  40. this._inputElement = null;
  41. // Bind event handlers so they are only bound once for every instance.
  42. this._onClick = this._onClick.bind(this);
  43. this._setInput = this._setInput.bind(this);
  44. }
  45. /**
  46. * Implements React's {@link Component#render()}.
  47. *
  48. * @inheritdoc
  49. * @returns {ReactElement}
  50. */
  51. render() {
  52. const { t } = this.props;
  53. const inputValue = this.props.toCopy || t('inviteUrlDefaultMsg');
  54. // FIXME An input HTML element is used here instead of atlaskit's
  55. // field-text because the latter does not currently support readOnly.
  56. return (
  57. <div className = 'form-control'>
  58. <label className = 'form-control__label'>
  59. { t('dialog.shareLink') }
  60. </label>
  61. <div className = 'form-control__container'>
  62. <input
  63. className = 'input-control inviteLink'
  64. id = 'inviteLinkRef'
  65. readOnly = { true }
  66. ref = { this._setInput }
  67. type = 'text'
  68. value = { inputValue } />
  69. <button
  70. className =
  71. 'button-control button-control_light copyInviteLink'
  72. onClick = { this._onClick }
  73. type = 'button'>
  74. { t('dialog.copy') }
  75. </button>
  76. </div>
  77. </div>
  78. );
  79. }
  80. /**
  81. * Copies the passed in value to the clipboard.
  82. *
  83. * @private
  84. * @returns {void}
  85. */
  86. _onClick() {
  87. try {
  88. this._inputElement.select();
  89. document.execCommand('copy');
  90. this._inputElement.blur();
  91. } catch (err) {
  92. logger.error('error when copying the text', err);
  93. }
  94. }
  95. /**
  96. * Sets the internal reference to the DOM/HTML element backing the React
  97. * {@code Component} input with id {@code inviteLinkRef}.
  98. *
  99. * @param {HTMLInputElement} element - The DOM/HTML element for this
  100. * {@code Component}'s input.
  101. * @private
  102. * @returns {void}
  103. */
  104. _setInput(element) {
  105. this._inputElement = element;
  106. }
  107. }
  108. export default translate(ShareLinkForm);