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.tsx 5.3KB

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