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 7.2KB

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