Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DisplayNamePrompt.web.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { FieldTextStateless as TextField } from '@atlaskit/field-text';
  5. import { Dialog } from '../../base/dialog';
  6. import { translate } from '../../base/i18n';
  7. import {
  8. getLocalParticipant,
  9. participantDisplayNameChanged
  10. } from '../../base/participants';
  11. /**
  12. * Implements a React {@code Component} for displaying a dialog with an field
  13. * for setting the local participant's display name.
  14. *
  15. * @extends Component
  16. */
  17. class DisplayNamePrompt extends Component {
  18. /**
  19. * {@code DisplayNamePrompt} component's property types.
  20. *
  21. * @static
  22. */
  23. static propTypes = {
  24. /**
  25. * The current ID for the local participant. Used for setting the
  26. * display name on the associated participant.
  27. */
  28. _localParticipantID: PropTypes.string,
  29. /**
  30. * Invoked to update the local participant's display name.
  31. */
  32. dispatch: PropTypes.func,
  33. /**
  34. * Invoked to obtain translated strings.
  35. */
  36. t: PropTypes.func
  37. };
  38. /**
  39. * Initializes a new {@code DisplayNamePrompt} instance.
  40. *
  41. * @param {Object} props - The read-only properties with which the new
  42. * instance is to be initialized.
  43. */
  44. constructor(props) {
  45. super(props);
  46. this.state = {
  47. /**
  48. * The name to show in the display name text field.
  49. *
  50. * @type {string}
  51. */
  52. displayName: ''
  53. };
  54. // Bind event handlers so they are only bound once for every instance.
  55. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  56. this._onSubmit = this._onSubmit.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. return (
  66. <Dialog
  67. isModal = { true }
  68. onSubmit = { this._onSubmit }
  69. titleKey = 'dialog.displayNameRequired'
  70. width = 'small'>
  71. <TextField
  72. autoFocus = { true }
  73. compact = { true }
  74. label = { this.props.t('dialog.enterDisplayName') }
  75. name = 'displayName'
  76. onChange = { this._onDisplayNameChange }
  77. shouldFitContainer = { true }
  78. type = 'text'
  79. value = { this.state.displayName } />
  80. </Dialog>);
  81. }
  82. /**
  83. * Updates the entered display name.
  84. *
  85. * @param {Object} event - The DOM event triggered from the entered display
  86. * name value having changed.
  87. * @private
  88. * @returns {void}
  89. */
  90. _onDisplayNameChange(event) {
  91. this.setState({
  92. displayName: event.target.value
  93. });
  94. }
  95. /**
  96. * Dispatches an action to update the local participant's display name. A
  97. * name must be entered for the action to dispatch.
  98. *
  99. * @private
  100. * @returns {void}
  101. */
  102. _onSubmit() {
  103. const { displayName } = this.state;
  104. if (!displayName.trim()) {
  105. return false;
  106. }
  107. const { dispatch, _localParticipantID } = this.props;
  108. dispatch(
  109. participantDisplayNameChanged(_localParticipantID, displayName));
  110. return true;
  111. }
  112. }
  113. /**
  114. * Maps (parts of) the Redux state to the associated {@code DisplayNamePrompt}'s
  115. * props.
  116. *
  117. * @param {Object} state - The Redux state.
  118. * @private
  119. * @returns {{
  120. * _localParticipantID: string
  121. * }}
  122. */
  123. function _mapStateToProps(state) {
  124. const { id } = getLocalParticipant(state);
  125. return {
  126. /**
  127. * The current ID for the local participant.
  128. *
  129. * @type {string}
  130. */
  131. _localParticipantID: id
  132. };
  133. }
  134. export default translate(connect(_mapStateToProps)(DisplayNamePrompt));