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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Button from '@atlaskit/button';
  2. import { FieldTextStateless as TextField } 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. <TextField
  66. id = 'inviteLinkRef'
  67. isLabelHidden = { true }
  68. isReadOnly = { true }
  69. label = 'invite link'
  70. onChange = { this._onDropdownTriggerInputChange }
  71. ref = { this._setInput }
  72. shouldFitContainer = { true }
  73. type = 'text'
  74. value = { inputValue } />
  75. </div>
  76. <Button
  77. appearance = 'default'
  78. onClick = { this._onClick }
  79. type = 'button'>
  80. { t('dialog.copy') }
  81. </Button>
  82. </div>
  83. </div>
  84. );
  85. }
  86. /**
  87. * Copies the passed in value to the clipboard.
  88. *
  89. * @private
  90. * @returns {void}
  91. */
  92. _onClick() {
  93. try {
  94. const { input } = this._inputComponent;
  95. input.select();
  96. document.execCommand('copy');
  97. input.blur();
  98. } catch (err) {
  99. logger.error('error when copying the text', err);
  100. }
  101. }
  102. /**
  103. * This is a no-op function used to stub out TextField's onChange in order
  104. * to prevent TextField from printing prop type validation errors. TextField
  105. * is used as a trigger for the dropdown in {@code ShareLinkForm} to get the
  106. * desired AtlasKit input look for the UI.
  107. *
  108. * @returns {void}
  109. */
  110. _onDropdownTriggerInputChange() {
  111. // Intentionally left empty.
  112. }
  113. /**
  114. * Sets the internal reference to the React Component wrapping the input
  115. * with id {@code inviteLinkRef}.
  116. *
  117. * @param {ReactComponent} inputComponent - React Component for displaying
  118. * an input for displaying the meeting link.
  119. * @private
  120. * @returns {void}
  121. */
  122. _setInput(inputComponent) {
  123. this._inputComponent = inputComponent;
  124. }
  125. }
  126. export default translate(ShareLinkForm);