Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DisplayNameForm.tsx 4.1KB

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