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.

DisplayNameForm.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { IStore } from '../../../app/types';
  4. import { translate } from '../../../base/i18n/functions';
  5. import { connect } from '../../../base/redux/functions';
  6. import { updateSettings } from '../../../base/settings/actions';
  7. import Button from '../../../base/ui/components/web/Button';
  8. import Input from '../../../base/ui/components/web/Input';
  9. // @ts-ignore
  10. import KeyboardAvoider from './KeyboardAvoider';
  11. /**
  12. * The type of the React {@code Component} props of {@DisplayNameForm}.
  13. */
  14. interface IProps extends WithTranslation {
  15. /**
  16. * Invoked to set the local participant display name.
  17. */
  18. dispatch: IStore['dispatch'];
  19. /**
  20. * Whether the polls feature is enabled or not.
  21. */
  22. isPollsEnabled: boolean;
  23. }
  24. /**
  25. * The type of the React {@code Component} state of {@DisplayNameForm}.
  26. */
  27. interface IState {
  28. /**
  29. * User provided display name when the input text is provided in the view.
  30. */
  31. displayName: string;
  32. }
  33. /**
  34. * React Component for requesting the local participant to set a display name.
  35. *
  36. * @augments Component
  37. */
  38. class DisplayNameForm extends Component<IProps, IState> {
  39. state = {
  40. displayName: ''
  41. };
  42. /**
  43. * Initializes a new {@code DisplayNameForm} instance.
  44. *
  45. * @param {Object} props - The read-only properties with which the new
  46. * instance is to be initialized.
  47. */
  48. constructor(props: IProps) {
  49. super(props);
  50. // Bind event handlers so they are only bound once for every instance.
  51. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  52. this._onSubmit = this._onSubmit.bind(this);
  53. this._onKeyPress = this._onKeyPress.bind(this);
  54. }
  55. /**
  56. * Implements React's {@link Component#render()}.
  57. *
  58. * @inheritdoc
  59. * @returns {ReactElement}
  60. */
  61. render() {
  62. const { isPollsEnabled, t } = this.props;
  63. return (
  64. <div id = 'nickname'>
  65. <form onSubmit = { this._onSubmit }>
  66. <Input
  67. accessibilityLabel = { t('chat.nickname.title') }
  68. autoFocus = { true }
  69. id = 'nickinput'
  70. label = { t(isPollsEnabled ? 'chat.nickname.titleWithPolls' : 'chat.nickname.title') }
  71. name = 'name'
  72. onChange = { this._onDisplayNameChange }
  73. placeholder = { t('chat.nickname.popover') }
  74. type = 'text'
  75. value = { this.state.displayName } />
  76. </form>
  77. <br />
  78. <Button
  79. accessibilityLabel = { t('chat.enter') }
  80. disabled = { !this.state.displayName.trim() }
  81. fullWidth = { true }
  82. label = { t('chat.enter') }
  83. onClick = { this._onSubmit } />
  84. <KeyboardAvoider />
  85. </div>
  86. );
  87. }
  88. /**
  89. * Dispatches an action update the entered display name.
  90. *
  91. * @param {string} value - Keyboard event.
  92. * @private
  93. * @returns {void}
  94. */
  95. _onDisplayNameChange(value: string) {
  96. this.setState({ displayName: value });
  97. }
  98. /**
  99. * Dispatches an action to hit enter to change your display name.
  100. *
  101. * @param {event} event - Keyboard event
  102. * that will check if user has pushed the enter key.
  103. * @private
  104. * @returns {void}
  105. */
  106. _onSubmit(event: any) {
  107. event?.preventDefault && event.preventDefault();
  108. // Store display name in settings
  109. this.props.dispatch(updateSettings({
  110. displayName: this.state.displayName
  111. }));
  112. }
  113. /**
  114. * KeyPress handler for accessibility.
  115. *
  116. * @param {Object} e - The key event to handle.
  117. *
  118. * @returns {void}
  119. */
  120. _onKeyPress(e: React.KeyboardEvent) {
  121. if (e.key === ' ' || e.key === 'Enter') {
  122. this._onSubmit(e);
  123. }
  124. }
  125. }
  126. export default translate(connect()(DisplayNameForm));