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.js 7.7KB

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