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.6KB

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