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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 { 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. <TextField
  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. type = 'button'>
  115. { t('dialog.add') }
  116. </Button>
  117. </div>
  118. </div>
  119. );
  120. }
  121. /**
  122. * Mimics form behavior by listening for enter key press and submitting the
  123. * entered password.
  124. *
  125. * @param {Object} event - DOM Event for keydown.
  126. * @private
  127. * @returns {void}
  128. */
  129. _onKeyDown(event) {
  130. event.stopPropagation();
  131. if (event.keyCode === /* Enter */ 13) {
  132. this._onSubmit();
  133. }
  134. }
  135. /**
  136. * Updates the internal state of the entered password.
  137. *
  138. * @param {Object} event - DOM Event for value change.
  139. * @private
  140. * @returns {void}
  141. */
  142. _onPasswordChange(event) {
  143. this.setState({ password: event.target.value });
  144. }
  145. /**
  146. * Dispatches a request to lock the conference with a password.
  147. *
  148. * @private
  149. * @returns {void}
  150. */
  151. _onSubmit() {
  152. if (!this.state.password) {
  153. return;
  154. }
  155. const { conference } = this.props;
  156. this.props.dispatch(setPassword(
  157. conference,
  158. conference.lock,
  159. this.state.password
  160. ));
  161. this.setState({ password: '' });
  162. }
  163. /**
  164. * Sets the instance variable for the React Component used for entering a
  165. * password.
  166. *
  167. * @param {Object} inputComponent - The React Component for the input
  168. * field.
  169. * @private
  170. * @returns {void}
  171. */
  172. _setInput(inputComponent) {
  173. if (inputComponent !== this._inputComponent) {
  174. this._inputComponent = inputComponent;
  175. }
  176. }
  177. }
  178. export default translate(connect()(AddPasswordForm));