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.

AddPasswordForm.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 { connect } from 'react-redux';
  6. import { setPassword } from '../../base/conference';
  7. import { translate } from '../../base/i18n';
  8. /**
  9. * A React {@code Component} for locking a JitsiConference with a password.
  10. */
  11. class AddPasswordForm extends Component {
  12. /**
  13. * {@code AddPasswordForm}'s property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * The JitsiConference on which to lock and set a password.
  20. *
  21. * @type {JitsiConference}
  22. */
  23. conference: PropTypes.object,
  24. /**
  25. * Invoked to set a password on the conference.
  26. */
  27. dispatch: PropTypes.func,
  28. /**
  29. * Invoked to obtain translated strings.
  30. */
  31. t: PropTypes.func
  32. };
  33. /**
  34. * Initializes a new {@code AddPasswordForm} instance.
  35. *
  36. * @param {Object} props - The read-only properties with which the new
  37. * instance is to be initialized.
  38. */
  39. constructor(props) {
  40. super(props);
  41. this.state = {
  42. /**
  43. * The current value to display in {@code AddPasswordForm}
  44. * component's input field. The value is also used as the desired
  45. * new password when creating a {@code setPassword} action.
  46. *
  47. * @type {string}
  48. */
  49. password: ''
  50. };
  51. /**
  52. * The internal reference to the React {@code component} for entering a
  53. * password.
  54. *
  55. * @private
  56. * @type {ReactComponent}
  57. */
  58. this._inputComponent = null;
  59. // Bind event handlers so they are only bound once for every instance.
  60. this._onKeyDown = this._onKeyDown.bind(this);
  61. this._onPasswordChange = this._onPasswordChange.bind(this);
  62. this._onSubmit = this._onSubmit.bind(this);
  63. this._setInput = this._setInput.bind(this);
  64. }
  65. /**
  66. * Directly bind a handler to the input element. This is done in order to
  67. * intercept enter presses so any outer forms do not become submitted.
  68. * Atlaskit Button does not expose a way to hook onto keydown events.
  69. *
  70. * @inheritdoc
  71. */
  72. componentDidMount() {
  73. this._inputComponent.input.onkeydown = this._onKeyDown;
  74. }
  75. /**
  76. * Remove any handlers set directly on DOM elements.
  77. *
  78. * @inheritdoc
  79. */
  80. componentWillUnmount() {
  81. this._inputComponent.input.onkeydown = null;
  82. }
  83. /**
  84. * Implements React's {@link Component#render()}.
  85. *
  86. * @inheritdoc
  87. * @returns {ReactElement}
  88. */
  89. render() {
  90. const { t } = this.props;
  91. return (
  92. <div
  93. className = 'form-control'
  94. onSubmit = { this._onSubmit } >
  95. <div className = 'form-control__container'>
  96. <div className = 'form-control__input-container'>
  97. <FieldText
  98. autoFocus = { true }
  99. compact = { true }
  100. id = 'newPasswordInput'
  101. isLabelHidden = { true }
  102. label = 'Enter Password'
  103. onChange = { this._onPasswordChange }
  104. onKeyDown = { this._onKeyDown }
  105. placeholder = { t('dialog.createPassword') }
  106. ref = { this._setInput }
  107. shouldFitContainer = { true }
  108. type = 'text' />
  109. </div>
  110. <Button
  111. id = 'addPasswordBtn'
  112. isDisabled = { !this.state.password }
  113. onClick = { this._onSubmit }
  114. shouldFitContainer = { true }
  115. type = 'button'>
  116. { t('dialog.add') }
  117. </Button>
  118. </div>
  119. </div>
  120. );
  121. }
  122. /**
  123. * Mimics form behavior by listening for enter key press and submitting the
  124. * entered password.
  125. *
  126. * @param {Object} event - DOM Event for keydown.
  127. * @private
  128. * @returns {void}
  129. */
  130. _onKeyDown(event) {
  131. event.stopPropagation();
  132. if (event.keyCode === /* Enter */ 13) {
  133. this._onSubmit();
  134. }
  135. }
  136. /**
  137. * Updates the internal state of the entered password.
  138. *
  139. * @param {Object} event - DOM Event for value change.
  140. * @private
  141. * @returns {void}
  142. */
  143. _onPasswordChange(event) {
  144. this.setState({ password: event.target.value });
  145. }
  146. /**
  147. * Dispatches a request to lock the conference with a password.
  148. *
  149. * @private
  150. * @returns {void}
  151. */
  152. _onSubmit() {
  153. if (!this.state.password) {
  154. return;
  155. }
  156. const { conference } = this.props;
  157. this.props.dispatch(setPassword(
  158. conference,
  159. conference.lock,
  160. this.state.password
  161. ));
  162. this.setState({ password: '' });
  163. }
  164. /**
  165. * Sets the instance variable for the React Component used for entering a
  166. * password.
  167. *
  168. * @param {Object} inputComponent - The React Component for the input
  169. * field.
  170. * @private
  171. * @returns {void}
  172. */
  173. _setInput(inputComponent) {
  174. if (inputComponent !== this._inputComponent) {
  175. this._inputComponent = inputComponent;
  176. }
  177. }
  178. }
  179. export default translate(connect()(AddPasswordForm));