您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DisplayName.web.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { translate } from '../../base/i18n';
  5. import { participantDisplayNameChanged } from '../../base/participants';
  6. /**
  7. * React {@code Component} for displaying and editing a participant's name.
  8. *
  9. * @extends Component
  10. */
  11. class DisplayName extends Component {
  12. /**
  13. * {@code DisplayName}'s property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * Whether or not the display name should be editable on click.
  20. */
  21. allowEditing: PropTypes.bool,
  22. /**
  23. * Invoked to update the participant's display name.
  24. */
  25. dispatch: PropTypes.func,
  26. /**
  27. * The participant's current display name.
  28. */
  29. displayName: PropTypes.string,
  30. /**
  31. * A string to append to the displayName, if provided.
  32. */
  33. displayNameSuffix: PropTypes.string,
  34. /**
  35. * The ID attribute to add to the component. Useful for global querying
  36. * for the component by legacy components and torture tests.
  37. */
  38. elementID: PropTypes.string,
  39. /**
  40. * The ID of the participant whose name is being displayed.
  41. */
  42. participantID: PropTypes.string,
  43. /**
  44. * Invoked to obtain translated strings.
  45. */
  46. t: PropTypes.func
  47. };
  48. /**
  49. * Initializes a new {@code DisplayName} instance.
  50. *
  51. * @param {Object} props - The read-only properties with which the new
  52. * instance is to be initialized.
  53. */
  54. constructor(props) {
  55. super(props);
  56. this.state = {
  57. /**
  58. * The current value of the display name in the edit field.
  59. *
  60. * @type {string}
  61. */
  62. editDisplayNameValue: '',
  63. /**
  64. * Whether or not the component should be displaying an editable
  65. * input.
  66. *
  67. * @type {boolean}
  68. */
  69. isEditing: false
  70. };
  71. /**
  72. * The internal reference to the HTML element backing the React
  73. * {@code Component} input with id {@code editDisplayName}. It is
  74. * necessary for automatically selecting the display name input field
  75. * when starting to edit the display name.
  76. *
  77. * @private
  78. * @type {HTMLInputElement}
  79. */
  80. this._nameInput = null;
  81. // Bind event handlers so they are only bound once for every instance.
  82. this._onChange = this._onChange.bind(this);
  83. this._onKeyDown = this._onKeyDown.bind(this);
  84. this._onStartEditing = this._onStartEditing.bind(this);
  85. this._onSubmit = this._onSubmit.bind(this);
  86. this._setNameInputRef = this._setNameInputRef.bind(this);
  87. }
  88. /**
  89. * Automatically selects the input field's value after starting to edit the
  90. * display name.
  91. *
  92. * @inheritdoc
  93. * @returns {void}
  94. */
  95. componentDidUpdate(previousProps, previousState) {
  96. if (!previousState.isEditing && this.state.isEditing) {
  97. this._nameInput.select();
  98. }
  99. }
  100. /**
  101. * Implements React's {@link Component#render()}.
  102. *
  103. * @inheritdoc
  104. * @returns {ReactElement}
  105. */
  106. render() {
  107. const {
  108. allowEditing,
  109. displayName,
  110. displayNameSuffix,
  111. elementID,
  112. t
  113. } = this.props;
  114. if (allowEditing && this.state.isEditing) {
  115. return (
  116. <input
  117. autoFocus = { true }
  118. className = 'editdisplayname'
  119. id = 'editDisplayName'
  120. onBlur = { this._onSubmit }
  121. onChange = { this._onChange }
  122. onKeyDown = { this._onKeyDown }
  123. placeholder = { t('defaultNickname') }
  124. ref = { this._setNameInputRef }
  125. type = 'text'
  126. value = { this.state.editDisplayNameValue } />
  127. );
  128. }
  129. const suffix
  130. = displayName && displayNameSuffix ? ` (${displayNameSuffix})` : '';
  131. return (
  132. <span
  133. className = 'displayname'
  134. id = { elementID }
  135. onClick = { this._onStartEditing }>
  136. { `${displayName || displayNameSuffix || ''}${suffix}` }
  137. </span>
  138. );
  139. }
  140. /**
  141. * Updates the internal state of the display name entered into the edit
  142. * field.
  143. *
  144. * @param {Object} event - DOM Event for value change.
  145. * @private
  146. * @returns {void}
  147. */
  148. _onChange(event) {
  149. this.setState({
  150. editDisplayNameValue: event.target.value
  151. });
  152. }
  153. /**
  154. * Submits the editted display name update if the enter key is pressed.
  155. *
  156. * @param {Event} event - Key down event object.
  157. * @private
  158. * @returns {void}
  159. */
  160. _onKeyDown(event) {
  161. if (event.key === 'Enter') {
  162. this._onSubmit();
  163. }
  164. }
  165. /**
  166. * Updates the component to display an editable input field and sets the
  167. * initial value to the current display name.
  168. *
  169. * @private
  170. * @returns {void}
  171. */
  172. _onStartEditing() {
  173. if (this.props.allowEditing) {
  174. this.setState({
  175. isEditing: true,
  176. editDisplayNameValue: this.props.displayName || ''
  177. });
  178. }
  179. }
  180. /**
  181. * Dispatches an action to update the display name if any change has
  182. * occurred after editing. Clears any temporary state used to keep track
  183. * of pending display name changes and exits editing mode.
  184. *
  185. * @param {Event} event - Key down event object.
  186. * @private
  187. * @returns {void}
  188. */
  189. _onSubmit() {
  190. const { editDisplayNameValue } = this.state;
  191. const { dispatch, participantID } = this.props;
  192. dispatch(participantDisplayNameChanged(
  193. participantID, editDisplayNameValue));
  194. this.setState({
  195. isEditing: false,
  196. editDisplayNameValue: ''
  197. });
  198. this._nameInput = null;
  199. }
  200. /**
  201. * Sets the internal reference to the HTML element backing the React
  202. * {@code Component} input with id {@code editDisplayName}.
  203. *
  204. * @param {HTMLInputElement} element - The DOM/HTML element for this
  205. * {@code Component}'s input.
  206. * @private
  207. * @returns {void}
  208. */
  209. _setNameInputRef(element) {
  210. this._nameInput = element;
  211. }
  212. }
  213. export default translate(connect()(DisplayName));