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

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