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

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