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.web.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // @flow
  2. import { FieldTextStateless } from '@atlaskit/field-text';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../base/i18n';
  6. import {
  7. getLocalParticipant,
  8. participantDisplayNameChanged
  9. } from '../../base/participants';
  10. /**
  11. * The type of the React {@code Component} props of {@DisplayNameForm}.
  12. */
  13. type Props = {
  14. /**
  15. * The ID of the local participant.
  16. */
  17. _localParticipantId: string,
  18. /**
  19. * Invoked to set the local participant display name.
  20. */
  21. dispatch: Dispatch<*>,
  22. /**
  23. * Invoked to obtain translated strings.
  24. */
  25. t: Function
  26. };
  27. /**
  28. * The type of the React {@code Component} state of {@DisplayNameForm}.
  29. */
  30. type State = {
  31. /**
  32. * User provided display name when the input text is provided in the view.
  33. */
  34. displayName: string
  35. };
  36. /**
  37. * React Component for requesting the local participant to set a display name.
  38. *
  39. * @extends Component
  40. */
  41. class DisplayNameForm extends Component<Props, State> {
  42. state = {
  43. displayName: ''
  44. };
  45. /**
  46. * Initializes a new {@code DisplayNameForm} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props: Props) {
  52. super(props);
  53. // Bind event handlers so they are only bound once for every instance.
  54. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  55. this._onSubmit = this._onSubmit.bind(this);
  56. }
  57. /**
  58. * Implements React's {@link Component#render()}.
  59. *
  60. * @inheritdoc
  61. * @returns {ReactElement}
  62. */
  63. render() {
  64. const { t } = this.props;
  65. return (
  66. <div id = 'nickname'>
  67. <span>{ this.props.t('chat.nickname.title') }</span>
  68. <form onSubmit = { this._onSubmit }>
  69. <FieldTextStateless
  70. autoFocus = { true }
  71. id = 'nickinput'
  72. onChange = { this._onDisplayNameChange }
  73. placeholder = { t('chat.nickname.popover') }
  74. type = 'text'
  75. value = { this.state.displayName } />
  76. </form>
  77. </div>
  78. );
  79. }
  80. _onDisplayNameChange: (Object) => void;
  81. /**
  82. * Dispatches an action update the entered display name.
  83. *
  84. * @param {event} event - Keyboard event.
  85. * @private
  86. * @returns {void}
  87. */
  88. _onDisplayNameChange(event: Object) {
  89. this.setState({ displayName: event.target.value });
  90. }
  91. _onSubmit: (Object) => void;
  92. /**
  93. * Dispatches an action to hit enter to change your display name.
  94. *
  95. * @param {event} event - Keyboard event
  96. * that will check if user has pushed the enter key.
  97. * @private
  98. * @returns {void}
  99. */
  100. _onSubmit(event: Object) {
  101. event.preventDefault();
  102. this.props.dispatch(participantDisplayNameChanged(
  103. this.props._localParticipantId,
  104. this.state.displayName));
  105. }
  106. }
  107. /**
  108. * Maps (parts of) the Redux state to the associated props for the
  109. * {@code DisplayNameForm} component.
  110. *
  111. * @param {Object} state - The Redux state.
  112. * @private
  113. * @returns {{
  114. * _localParticipantId: string
  115. * }}
  116. */
  117. function _mapStateToProps(state) {
  118. return {
  119. _localParticipantId: getLocalParticipant(state).id
  120. };
  121. }
  122. export default translate(connect(_mapStateToProps)(DisplayNameForm));