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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Invoked to obtain translated strings.
  19. */
  20. t: Function
  21. };
  22. /**
  23. * The type of the React {@code Component} state of {@DisplayNameForm}.
  24. */
  25. type State = {
  26. /**
  27. * User provided display name when the input text is provided in the view.
  28. */
  29. displayName: string
  30. };
  31. /**
  32. * React Component for requesting the local participant to set a display name.
  33. *
  34. * @extends Component
  35. */
  36. class DisplayNameForm extends Component<Props, State> {
  37. state = {
  38. displayName: ''
  39. };
  40. /**
  41. * Initializes a new {@code DisplayNameForm} instance.
  42. *
  43. * @param {Object} props - The read-only properties with which the new
  44. * instance is to be initialized.
  45. */
  46. constructor(props: Props) {
  47. super(props);
  48. // Bind event handlers so they are only bound once for every instance.
  49. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  50. this._onSubmit = this._onSubmit.bind(this);
  51. this._onKeyPress = this._onKeyPress.bind(this);
  52. }
  53. /**
  54. * Implements React's {@link Component#render()}.
  55. *
  56. * @inheritdoc
  57. * @returns {ReactElement}
  58. */
  59. render() {
  60. const { t } = this.props;
  61. return (
  62. <div id = 'nickname'>
  63. <form onSubmit = { this._onSubmit }>
  64. <FieldTextStateless
  65. aria-describedby = 'nickname-title'
  66. autoComplete = 'name'
  67. autoFocus = { true }
  68. compact = { true }
  69. id = 'nickinput'
  70. label = { t('chat.nickname.title') }
  71. onChange = { this._onDisplayNameChange }
  72. placeholder = { t('chat.nickname.popover') }
  73. shouldFitContainer = { true }
  74. type = 'text'
  75. value = { this.state.displayName } />
  76. </form>
  77. <div
  78. className = { `enter-chat${this.state.displayName.trim() ? '' : ' disabled'}` }
  79. onClick = { this._onSubmit }
  80. onKeyPress = { this._onKeyPress }
  81. role = 'button'
  82. tabIndex = { 0 }>
  83. { t('chat.enter') }
  84. </div>
  85. <KeyboardAvoider />
  86. </div>
  87. );
  88. }
  89. _onDisplayNameChange: (Object) => void;
  90. /**
  91. * Dispatches an action update the entered display name.
  92. *
  93. * @param {event} event - Keyboard event.
  94. * @private
  95. * @returns {void}
  96. */
  97. _onDisplayNameChange(event: Object) {
  98. this.setState({ displayName: event.target.value });
  99. }
  100. _onSubmit: (Object) => void;
  101. /**
  102. * Dispatches an action to hit enter to change your display name.
  103. *
  104. * @param {event} event - Keyboard event
  105. * that will check if user has pushed the enter key.
  106. * @private
  107. * @returns {void}
  108. */
  109. _onSubmit(event: Object) {
  110. event.preventDefault();
  111. // Store display name in settings
  112. this.props.dispatch(updateSettings({
  113. displayName: this.state.displayName
  114. }));
  115. }
  116. _onKeyPress: (Object) => void;
  117. /**
  118. * KeyPress handler for accessibility.
  119. *
  120. * @param {Object} e - The key event to handle.
  121. *
  122. * @returns {void}
  123. */
  124. _onKeyPress(e) {
  125. if (e.key === ' ' || e.key === 'Enter') {
  126. this._onSubmit(e);
  127. }
  128. }
  129. }
  130. export default translate(connect()(DisplayNameForm));