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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Button from '@atlaskit/button';
  2. import { FieldText } from '@atlaskit/field-text';
  3. import PropTypes from 'prop-types';
  4. import React, { Component } from 'react';
  5. import { translate } from '../../base/i18n';
  6. const logger = require('jitsi-meet-logger').getLogger(__filename);
  7. /**
  8. * A React {@code Component} for displaying a value with a copy button to copy
  9. * the value into the clipboard.
  10. */
  11. class ShareLinkForm extends Component {
  12. /**
  13. * {@code ShareLinkForm}'s property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * Invoked to obtain translated strings.
  20. */
  21. t: PropTypes.func,
  22. /**
  23. * The value to be displayed and copied into the clipboard.
  24. */
  25. toCopy: PropTypes.string
  26. };
  27. /**
  28. * Initializes a new {@code ShareLinkForm} instance.
  29. *
  30. * @param {Object} props - The read-only properties with which the new
  31. * instance is to be initialized.
  32. */
  33. constructor(props) {
  34. super(props);
  35. /**
  36. * The internal reference to the React {@code component} for display
  37. * the meeting link in an input element.
  38. *
  39. * @private
  40. * @type {ReactComponent}
  41. */
  42. this._inputComponent = null;
  43. // Bind event handlers so they are only bound once for every instance.
  44. this._onClick = this._onClick.bind(this);
  45. this._onDropdownTriggerInputChange
  46. = this._onDropdownTriggerInputChange.bind(this);
  47. this._setInput = this._setInput.bind(this);
  48. }
  49. /**
  50. * Implements React's {@link Component#render()}.
  51. *
  52. * @inheritdoc
  53. * @returns {ReactElement}
  54. */
  55. render() {
  56. const { t } = this.props;
  57. const inputValue = this.props.toCopy || t('inviteUrlDefaultMsg');
  58. return (
  59. <div className = 'form-control'>
  60. <label className = 'form-control__label'>
  61. { t('dialog.shareLink') }
  62. </label>
  63. <div className = 'form-control__container'>
  64. <div className = 'form-control__input-container'>
  65. <FieldText
  66. compact = { true }
  67. id = 'inviteLinkRef'
  68. isLabelHidden = { true }
  69. isReadOnly = { true }
  70. label = 'invite link'
  71. onChange = { this._onDropdownTriggerInputChange }
  72. ref = { this._setInput }
  73. shouldFitContainer = { true }
  74. type = 'text'
  75. value = { inputValue } />
  76. </div>
  77. <Button
  78. appearance = 'default'
  79. onClick = { this._onClick }
  80. shouldFitContainer = { true }
  81. type = 'button'>
  82. { t('dialog.copy') }
  83. </Button>
  84. </div>
  85. </div>
  86. );
  87. }
  88. /**
  89. * Copies the passed in value to the clipboard.
  90. *
  91. * @private
  92. * @returns {void}
  93. */
  94. _onClick() {
  95. try {
  96. const { input } = this._inputComponent;
  97. input.select();
  98. document.execCommand('copy');
  99. input.blur();
  100. } catch (err) {
  101. logger.error('error when copying the text', err);
  102. }
  103. }
  104. /**
  105. * This is a no-op function used to stub out FieldText's onChange in order
  106. * to prevent FieldText from printing prop type validation errors. FieldText
  107. * is used as a trigger for the dropdown in {@code ShareLinkForm} to get the
  108. * desired AtlasKit input look for the UI.
  109. *
  110. * @returns {void}
  111. */
  112. _onDropdownTriggerInputChange() {
  113. // Intentionally left empty.
  114. }
  115. /**
  116. * Sets the internal reference to the React Component wrapping the input
  117. * with id {@code inviteLinkRef}.
  118. *
  119. * @param {ReactComponent} inputComponent - React Component for displaying
  120. * an input for displaying the meeting link.
  121. * @private
  122. * @returns {void}
  123. */
  124. _setInput(inputComponent) {
  125. this._inputComponent = inputComponent;
  126. }
  127. }
  128. export default translate(ShareLinkForm);