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.0KB

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