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.

DisplayNamePrompt.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { translate } from '../../../base/i18n/functions';
  4. import Dialog from '../../../base/ui/components/web/Dialog';
  5. import Input from '../../../base/ui/components/web/Input';
  6. import { onSetDisplayName } from '../../functions';
  7. import { IProps } from '../../types';
  8. const INITIAL_DISPLAY_NAME = '';
  9. /**
  10. * The type of the React {@code Component} props of {@link DisplayNamePrompt}.
  11. */
  12. interface IState {
  13. /**
  14. * The name to show in the display name text field.
  15. */
  16. displayName: string;
  17. /**
  18. * The result of the input validation.
  19. */
  20. isValid: boolean;
  21. }
  22. /**
  23. * Implements a React {@code Component} for displaying a dialog with an field
  24. * for setting the local participant's display name.
  25. *
  26. * @augments Component
  27. */
  28. class DisplayNamePrompt extends Component<IProps, IState> {
  29. _onSetDisplayName: (displayName: string) => boolean;
  30. /**
  31. * Initializes a new {@code DisplayNamePrompt} instance.
  32. *
  33. * @param {Object} props - The read-only properties with which the new
  34. * instance is to be initialized.
  35. */
  36. constructor(props: IProps) {
  37. super(props);
  38. this.state = {
  39. displayName: INITIAL_DISPLAY_NAME,
  40. isValid: this.props.validateInput ? this.props.validateInput(INITIAL_DISPLAY_NAME) : true
  41. };
  42. // Bind event handlers so they are only bound once for every instance.
  43. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  44. this._onSubmit = this._onSubmit.bind(this);
  45. this._onSetDisplayName = onSetDisplayName(props.dispatch, props.onPostSubmit);
  46. }
  47. /**
  48. * Implements React's {@link Component#render()}.
  49. *
  50. * @inheritdoc
  51. * @returns {ReactElement}
  52. */
  53. render() {
  54. const disableCloseDialog = Boolean(this.props.validateInput);
  55. return (
  56. <Dialog
  57. cancel = {{ hidden: true }}
  58. disableBackdropClose = { disableCloseDialog }
  59. disableEnter = { !this.state.isValid }
  60. disableEscape = { disableCloseDialog }
  61. hideCloseButton = { disableCloseDialog }
  62. ok = {{
  63. disabled: !this.state.isValid,
  64. translationKey: 'dialog.Ok'
  65. }}
  66. onSubmit = { this._onSubmit }
  67. titleKey = 'dialog.displayNameRequired'>
  68. <Input
  69. autoFocus = { true }
  70. className = 'dialog-bottom-margin'
  71. id = 'dialog-displayName'
  72. label = { this.props.t('dialog.enterDisplayName') }
  73. name = 'displayName'
  74. onChange = { this._onDisplayNameChange }
  75. type = 'text'
  76. value = { this.state.displayName } />
  77. </Dialog>
  78. );
  79. }
  80. /**
  81. * Updates the entered display name.
  82. *
  83. * @param {string} value - The new value of the input.
  84. * @private
  85. * @returns {void}
  86. */
  87. _onDisplayNameChange(value: string) {
  88. if (this.props.validateInput) {
  89. this.setState({
  90. isValid: this.props.validateInput(value),
  91. displayName: value
  92. });
  93. return;
  94. }
  95. this.setState({
  96. displayName: value
  97. });
  98. }
  99. /**
  100. * Dispatches an action to update the local participant's display name. A
  101. * name must be entered for the action to dispatch.
  102. *
  103. * @private
  104. * @returns {boolean}
  105. */
  106. _onSubmit() {
  107. return this._onSetDisplayName(this.state.displayName);
  108. }
  109. }
  110. export default translate(connect()(DisplayNamePrompt));