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.

PasswordForm.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { translate } from '../../../base/i18n';
  4. import { LOCKED_LOCALLY } from '../../../room-lock';
  5. /**
  6. * The type of the React {@code Component} props of {@link PasswordForm}.
  7. */
  8. type Props = {
  9. /**
  10. * Whether or not to show the password editing field.
  11. */
  12. editEnabled: boolean,
  13. /**
  14. * The value for how the conference is locked (or undefined if not locked)
  15. * as defined by room-lock constants.
  16. */
  17. locked: string,
  18. /**
  19. * Callback to invoke when the local participant is submitting a password
  20. * set request.
  21. */
  22. onSubmit: Function,
  23. /**
  24. * The current known password for the JitsiConference.
  25. */
  26. password: string,
  27. /**
  28. * The number of digits to be used in the password.
  29. */
  30. passwordNumberOfDigits: boolean,
  31. /**
  32. * Invoked to obtain translated strings.
  33. */
  34. t: Function
  35. };
  36. /**
  37. * The type of the React {@code Component} state of {@link PasswordForm}.
  38. */
  39. type State = {
  40. /**
  41. * The value of the password being entered by the local participant.
  42. */
  43. enteredPassword: string
  44. };
  45. /**
  46. * React {@code Component} for displaying and editing the conference password.
  47. *
  48. * @extends Component
  49. */
  50. class PasswordForm extends Component<Props, State> {
  51. /**
  52. * Implements React's {@link Component#getDerivedStateFromProps()}.
  53. *
  54. * @inheritdoc
  55. */
  56. static getDerivedStateFromProps(props, state) {
  57. return {
  58. enteredPassword: props.editEnabled ? state.enteredPassword : ''
  59. };
  60. }
  61. state = {
  62. enteredPassword: ''
  63. };
  64. /**
  65. * Initializes a new {@code PasswordForm} instance.
  66. *
  67. * @param {Props} props - The React {@code Component} props to initialize
  68. * the new {@code PasswordForm} instance with.
  69. */
  70. constructor(props: Props) {
  71. super(props);
  72. // Bind event handlers so they are only bound once per instance.
  73. this._onEnteredPasswordChange
  74. = this._onEnteredPasswordChange.bind(this);
  75. this._onPasswordSubmit = this._onPasswordSubmit.bind(this);
  76. this._onKeyDown = this._onKeyDown.bind(this);
  77. }
  78. /**
  79. * Implements React's {@link Component#render()}.
  80. *
  81. * @inheritdoc
  82. * @returns {ReactElement}
  83. */
  84. render() {
  85. const { t } = this.props;
  86. return (
  87. <div className = 'info-password'>
  88. <span className = 'info-label'>
  89. { t('info.password') }
  90. </span>
  91. <span className = 'spacer'>&nbsp;</span>
  92. <span className = 'info-password-field info-value'>
  93. { this._renderPasswordField() }
  94. </span>
  95. </div>
  96. );
  97. }
  98. /**
  99. * Returns a ReactElement for showing the current state of the password or
  100. * for editing the current password.
  101. *
  102. * @private
  103. * @returns {ReactElement}
  104. */
  105. _renderPasswordField() {
  106. if (this.props.editEnabled) {
  107. let digitPattern, placeHolderText;
  108. if (this.props.passwordNumberOfDigits) {
  109. placeHolderText = this.props.t('passwordDigitsOnly', {
  110. number: this.props.passwordNumberOfDigits });
  111. digitPattern = '\\d*';
  112. }
  113. return (
  114. <form
  115. className = 'info-password-form'
  116. onKeyDown = { this._onKeyDown }
  117. onSubmit = { this._onPasswordSubmit }>
  118. <input
  119. autoFocus = { true }
  120. className = 'info-password-input'
  121. maxLength = { this.props.passwordNumberOfDigits }
  122. onChange = { this._onEnteredPasswordChange }
  123. pattern = { digitPattern }
  124. placeholder = { placeHolderText }
  125. spellCheck = { 'false' }
  126. type = 'text'
  127. value = { this.state.enteredPassword } />
  128. </form>
  129. );
  130. } else if (this.props.locked === LOCKED_LOCALLY) {
  131. return (
  132. <div className = 'info-password-local'>
  133. { this.props.password }
  134. </div>
  135. );
  136. } else if (this.props.locked) {
  137. return (
  138. <div className = 'info-password-remote'>
  139. { this.props.t('passwordSetRemotely') }
  140. </div>
  141. );
  142. }
  143. return (
  144. <div className = 'info-password-none'>
  145. { this.props.t('info.noPassword') }
  146. </div>
  147. );
  148. }
  149. _onEnteredPasswordChange: (Object) => void;
  150. /**
  151. * Updates the internal state of entered password.
  152. *
  153. * @param {Object} event - DOM Event for value change.
  154. * @private
  155. * @returns {void}
  156. */
  157. _onEnteredPasswordChange(event) {
  158. this.setState({ enteredPassword: event.target.value });
  159. }
  160. _onPasswordSubmit: (Object) => void;
  161. /**
  162. * Invokes the passed in onSubmit callback to notify the parent that a
  163. * password submission has been attempted.
  164. *
  165. * @param {Object} event - DOM Event for form submission.
  166. * @private
  167. * @returns {void}
  168. */
  169. _onPasswordSubmit(event) {
  170. event.preventDefault();
  171. event.stopPropagation();
  172. this.props.onSubmit(this.state.enteredPassword);
  173. }
  174. _onKeyDown: (Object) => void;
  175. /**
  176. * Stops the the EnterKey for propagation in order to prevent the dialog
  177. * to close.
  178. *
  179. * @param {Object} event - The key event.
  180. * @private
  181. * @returns {void}
  182. */
  183. _onKeyDown(event) {
  184. if (event.key === 'Enter') {
  185. event.stopPropagation();
  186. }
  187. }
  188. }
  189. export default translate(PasswordForm);