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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  52. /**
  53. * Implements React's {@link Component#render()}.
  54. *
  55. * @inheritdoc
  56. * @returns {ReactElement}
  57. */
  58. render() {
  59. const { t } = this.props;
  60. return (
  61. <div id = 'nickname'>
  62. <form onSubmit = { this._onSubmit }>
  63. <FieldTextStateless
  64. autoFocus = { true }
  65. compact = { true }
  66. id = 'nickinput'
  67. label = { t('chat.nickname.title') }
  68. onChange = { this._onDisplayNameChange }
  69. placeholder = { t('chat.nickname.popover') }
  70. shouldFitContainer = { true }
  71. type = 'text'
  72. value = { this.state.displayName } />
  73. </form>
  74. <div
  75. className = { `enter-chat${this.state.displayName.trim() ? '' : ' disabled'}` }
  76. onClick = { this._onSubmit }>
  77. { t('chat.enter') }
  78. </div>
  79. <KeyboardAvoider />
  80. </div>
  81. );
  82. }
  83. _onDisplayNameChange: (Object) => void;
  84. /**
  85. * Dispatches an action update the entered display name.
  86. *
  87. * @param {event} event - Keyboard event.
  88. * @private
  89. * @returns {void}
  90. */
  91. _onDisplayNameChange(event: Object) {
  92. this.setState({ displayName: event.target.value });
  93. }
  94. _onSubmit: (Object) => void;
  95. /**
  96. * Dispatches an action to hit enter to change your display name.
  97. *
  98. * @param {event} event - Keyboard event
  99. * that will check if user has pushed the enter key.
  100. * @private
  101. * @returns {void}
  102. */
  103. _onSubmit(event: Object) {
  104. event.preventDefault();
  105. // Store display name in settings
  106. this.props.dispatch(updateSettings({
  107. displayName: this.state.displayName
  108. }));
  109. }
  110. }
  111. export default translate(connect()(DisplayNameForm));