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.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // @flow
  2. import { FieldTextStateless } from '@atlaskit/field-text';
  3. import React, { Component } from 'react';
  4. import type { Dispatch } from 'redux';
  5. import { translate } from '../../../base/i18n';
  6. import { connect } from '../../../base/redux';
  7. import { updateSettings } from '../../../base/settings';
  8. import KeyboardAvoider from './KeyboardAvoider';
  9. /**
  10. * The type of the React {@code Component} props of {@DisplayNameForm}.
  11. */
  12. type Props = {
  13. /**
  14. * Invoked to set the local participant display name.
  15. */
  16. dispatch: Dispatch<any>,
  17. /**
  18. * Whether the polls feature is enabled or not.
  19. */
  20. isPollsEnabled: boolean,
  21. /**
  22. * Invoked to obtain translated strings.
  23. */
  24. t: Function
  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. * @extends 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. <FieldTextStateless
  69. aria-describedby = 'nickname-title'
  70. autoComplete = 'name'
  71. autoFocus = { true }
  72. compact = { true }
  73. id = 'nickinput'
  74. label = { t(isPollsEnabled ? 'chat.nickname.titleWithPolls' : 'chat.nickname.title') }
  75. onChange = { this._onDisplayNameChange }
  76. placeholder = { t('chat.nickname.popover') }
  77. shouldFitContainer = { true }
  78. type = 'text'
  79. value = { this.state.displayName } />
  80. </form>
  81. <div
  82. className = { `enter-chat${this.state.displayName.trim() ? '' : ' disabled'}` }
  83. onClick = { this._onSubmit }
  84. onKeyPress = { this._onKeyPress }
  85. role = 'button'
  86. tabIndex = { 0 }>
  87. { t('chat.enter') }
  88. </div>
  89. <KeyboardAvoider />
  90. </div>
  91. );
  92. }
  93. _onDisplayNameChange: (Object) => void;
  94. /**
  95. * Dispatches an action update the entered display name.
  96. *
  97. * @param {event} event - Keyboard event.
  98. * @private
  99. * @returns {void}
  100. */
  101. _onDisplayNameChange(event: Object) {
  102. this.setState({ displayName: event.target.value });
  103. }
  104. _onSubmit: (Object) => void;
  105. /**
  106. * Dispatches an action to hit enter to change your display name.
  107. *
  108. * @param {event} event - Keyboard event
  109. * that will check if user has pushed the enter key.
  110. * @private
  111. * @returns {void}
  112. */
  113. _onSubmit(event: Object) {
  114. event.preventDefault();
  115. // Store display name in settings
  116. this.props.dispatch(updateSettings({
  117. displayName: this.state.displayName
  118. }));
  119. }
  120. _onKeyPress: (Object) => void;
  121. /**
  122. * KeyPress handler for accessibility.
  123. *
  124. * @param {Object} e - The key event to handle.
  125. *
  126. * @returns {void}
  127. */
  128. _onKeyPress(e) {
  129. if (e.key === ' ' || e.key === 'Enter') {
  130. this._onSubmit(e);
  131. }
  132. }
  133. }
  134. export default translate(connect()(DisplayNameForm));