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.

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 { appendSuffix } from '../functions';
  5. import { translate } from '../../base/i18n';
  6. import { participantDisplayNameChanged } from '../../base/participants';
  7. /**
  8. * React {@code Component} for displaying and editing a participant's name.
  9. *
  10. * @extends Component
  11. */
  12. class DisplayName extends Component {
  13. /**
  14. * {@code DisplayName}'s property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. /**
  20. * Whether or not the display name should be editable on click.
  21. */
  22. allowEditing: PropTypes.bool,
  23. /**
  24. * Invoked to update the participant's display name.
  25. */
  26. dispatch: PropTypes.func,
  27. /**
  28. * The participant's current display name.
  29. */
  30. displayName: PropTypes.string,
  31. /**
  32. * A string to append to the displayName, if provided.
  33. */
  34. displayNameSuffix: PropTypes.string,
  35. /**
  36. * The ID attribute to add to the component. Useful for global querying
  37. * for the component by legacy components and torture tests.
  38. */
  39. elementID: PropTypes.string,
  40. /**
  41. * The ID of the participant whose name is being displayed.
  42. */
  43. participantID: PropTypes.string,
  44. /**
  45. * Invoked to obtain translated strings.
  46. */
  47. t: PropTypes.func
  48. };
  49. /**
  50. * Initializes a new {@code DisplayName} instance.
  51. *
  52. * @param {Object} props - The read-only properties with which the new
  53. * instance is to be initialized.
  54. */
  55. constructor(props) {
  56. super(props);
  57. this.state = {
  58. /**
  59. * The current value of the display name in the edit field.
  60. *
  61. * @type {string}
  62. */
  63. editDisplayNameValue: '',
  64. /**
  65. * Whether or not the component should be displaying an editable
  66. * input.
  67. *
  68. * @type {boolean}
  69. */
  70. isEditing: false
  71. };
  72. /**
  73. * The internal reference to the HTML element backing the React
  74. * {@code Component} input with id {@code editDisplayName}. It is
  75. * necessary for automatically selecting the display name input field
  76. * when starting to edit the display name.
  77. *
  78. * @private
  79. * @type {HTMLInputElement}
  80. */
  81. this._nameInput = null;
  82. // Bind event handlers so they are only bound once for every instance.
  83. this._onChange = this._onChange.bind(this);
  84. this._onKeyDown = this._onKeyDown.bind(this);
  85. this._onStartEditing = this._onStartEditing.bind(this);
  86. this._onSubmit = this._onSubmit.bind(this);
  87. this._setNameInputRef = this._setNameInputRef.bind(this);
  88. }
  89. /**
  90. * Automatically selects the input field's value after starting to edit the
  91. * display name.
  92. *
  93. * @inheritdoc
  94. * @returns {void}
  95. */
  96. componentDidUpdate(previousProps, previousState) {
  97. if (!previousState.isEditing && this.state.isEditing) {
  98. this._nameInput.select();
  99. }
  100. }
  101. /**
  102. * Implements React's {@link Component#render()}.
  103. *
  104. * @inheritdoc
  105. * @returns {ReactElement}
  106. */
  107. render() {
  108. const {
  109. allowEditing,
  110. displayName,
  111. displayNameSuffix,
  112. elementID,
  113. t
  114. } = this.props;
  115. if (allowEditing && this.state.isEditing) {
  116. return (
  117. <input
  118. autoFocus = { true }
  119. className = 'editdisplayname'
  120. id = 'editDisplayName'
  121. onBlur = { this._onSubmit }
  122. onChange = { this._onChange }
  123. onKeyDown = { this._onKeyDown }
  124. placeholder = { t('defaultNickname') }
  125. ref = { this._setNameInputRef }
  126. spellCheck = { 'false' }
  127. type = 'text'
  128. value = { this.state.editDisplayNameValue } />
  129. );
  130. }
  131. return (
  132. <span
  133. className = 'displayname'
  134. id = { elementID }
  135. onClick = { this._onStartEditing }>
  136. { `${appendSuffix(displayName, displayNameSuffix)}` }
  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));