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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import Button from '@atlaskit/button';
  2. import { FieldText } from '@atlaskit/field-text';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { setPassword } from '../../base/conference';
  6. import { translate } from '../../base/i18n';
  7. /**
  8. * A React {@code Component} for locking a JitsiConference with a password.
  9. */
  10. class AddPasswordForm extends Component {
  11. /**
  12. * {@code AddPasswordForm}'s property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. /**
  18. * The JitsiConference on which to lock and set a password.
  19. *
  20. * @type {JitsiConference}
  21. */
  22. conference: React.PropTypes.object,
  23. /**
  24. * Invoked to set a password on the conference.
  25. */
  26. dispatch: React.PropTypes.func,
  27. /**
  28. * Invoked to obtain translated strings.
  29. */
  30. t: React.PropTypes.func
  31. };
  32. /**
  33. * Initializes a new {@code AddPasswordForm} instance.
  34. *
  35. * @param {Object} props - The read-only properties with which the new
  36. * instance is to be initialized.
  37. */
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. /**
  42. * The current value to display in {@code AddPasswordForm}
  43. * component's input field. The value is also used as the desired
  44. * new password when creating a {@code setPassword} action.
  45. *
  46. * @type {string}
  47. */
  48. password: ''
  49. };
  50. /**
  51. * The internal reference to the React {@code component} for entering a
  52. * password.
  53. *
  54. * @private
  55. * @type {ReactComponent}
  56. */
  57. this._inputComponent = null;
  58. // Bind event handlers so they are only bound once for every instance.
  59. this._onKeyDown = this._onKeyDown.bind(this);
  60. this._onPasswordChange = this._onPasswordChange.bind(this);
  61. this._onSubmit = this._onSubmit.bind(this);
  62. this._setInput = this._setInput.bind(this);
  63. }
  64. /**
  65. * Directly bind a handler to the input element. This is done in order to
  66. * intercept enter presses so any outer forms do not become submitted.
  67. * Atlaskit Button does not expose a way to hook onto keydown events.
  68. *
  69. * @inheritdoc
  70. */
  71. componentDidMount() {
  72. this._inputComponent.input.onkeydown = this._onKeyDown;
  73. }
  74. /**
  75. * Remove any handlers set directly on DOM elements.
  76. *
  77. * @inheritdoc
  78. */
  79. componentWillUnmount() {
  80. this._inputComponent.input.onkeydown = null;
  81. }
  82. /**
  83. * Implements React's {@link Component#render()}.
  84. *
  85. * @inheritdoc
  86. * @returns {ReactElement}
  87. */
  88. render() {
  89. const { t } = this.props;
  90. return (
  91. <div
  92. className = 'form-control'
  93. onSubmit = { this._onSubmit } >
  94. <div className = 'form-control__container'>
  95. <div className = 'form-control__input-container'>
  96. <FieldText
  97. autoFocus = { true }
  98. compact = { true }
  99. id = 'newPasswordInput'
  100. isLabelHidden = { true }
  101. label = 'Enter Password'
  102. onChange = { this._onPasswordChange }
  103. onKeyDown = { this._onKeyDown }
  104. placeholder = { t('dialog.createPassword') }
  105. ref = { this._setInput }
  106. shouldFitContainer = { true }
  107. type = 'text' />
  108. </div>
  109. <Button
  110. id = 'addPasswordBtn'
  111. isDisabled = { !this.state.password }
  112. onClick = { this._onSubmit }
  113. shouldFitContainer = { true }
  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));