editableText.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import React, {useCallback, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'app/actionCreators/indicator';
  4. import TextOverflow from 'app/components/textOverflow';
  5. import {IconEdit} from 'app/icons/iconEdit';
  6. import space from 'app/styles/space';
  7. import {defined} from 'app/utils';
  8. import useKeypress from 'app/utils/useKeyPress';
  9. import useOnClickOutside from 'app/utils/useOnClickOutside';
  10. import Input from 'app/views/settings/components/forms/controls/input';
  11. import Field from 'app/views/settings/components/forms/field';
  12. type Props = {
  13. value: string;
  14. onChange: (value: string) => void;
  15. name?: string;
  16. errorMessage?: React.ReactNode;
  17. successMessage?: React.ReactNode;
  18. isDisabled?: boolean;
  19. };
  20. function EditableText({
  21. value,
  22. onChange,
  23. name,
  24. errorMessage,
  25. successMessage,
  26. isDisabled = false,
  27. }: Props) {
  28. const [isEditing, setIsEditing] = useState(false);
  29. const [inputValue, setInputValue] = useState(value);
  30. const isEmpty = !inputValue.trim();
  31. const innerWrapperRef = useRef<HTMLDivElement>(null);
  32. const labelRef = useRef<HTMLDivElement>(null);
  33. const inputRef = useRef<HTMLInputElement>(null);
  34. const enter = useKeypress('Enter');
  35. const esc = useKeypress('Escape');
  36. function revertValueAndCloseEditor() {
  37. if (value !== inputValue) {
  38. setInputValue(value);
  39. }
  40. if (isEditing) {
  41. setIsEditing(false);
  42. }
  43. }
  44. // check to see if the user clicked outside of this component
  45. useOnClickOutside(innerWrapperRef, () => {
  46. if (isEditing) {
  47. if (isEmpty) {
  48. displayStatusMessage('error');
  49. return;
  50. }
  51. if (inputValue !== value) {
  52. onChange(inputValue);
  53. displayStatusMessage('success');
  54. }
  55. setIsEditing(false);
  56. }
  57. });
  58. const onEnter = useCallback(() => {
  59. if (enter) {
  60. if (isEmpty) {
  61. displayStatusMessage('error');
  62. return;
  63. }
  64. if (inputValue !== value) {
  65. onChange(inputValue);
  66. displayStatusMessage('success');
  67. }
  68. setIsEditing(false);
  69. }
  70. }, [enter, inputValue, onChange]);
  71. const onEsc = useCallback(() => {
  72. if (esc) {
  73. revertValueAndCloseEditor();
  74. }
  75. }, [esc]);
  76. useEffect(() => {
  77. revertValueAndCloseEditor();
  78. }, [isDisabled, value]);
  79. // focus the cursor in the input field on edit start
  80. useEffect(() => {
  81. if (isEditing) {
  82. const inputElement = inputRef.current;
  83. if (defined(inputElement)) {
  84. inputElement.focus();
  85. }
  86. }
  87. }, [isEditing]);
  88. useEffect(() => {
  89. if (isEditing) {
  90. // if Enter is pressed, save the value and close the editor
  91. onEnter();
  92. // if Escape is pressed, revert the value and close the editor
  93. onEsc();
  94. }
  95. }, [onEnter, onEsc, isEditing]); // watch the Enter and Escape key presses
  96. function displayStatusMessage(status: 'error' | 'success') {
  97. if (status === 'error') {
  98. if (errorMessage) {
  99. addErrorMessage(errorMessage);
  100. }
  101. return;
  102. }
  103. if (successMessage) {
  104. addSuccessMessage(successMessage);
  105. }
  106. }
  107. function handleInputChange(event: React.ChangeEvent<HTMLInputElement>) {
  108. setInputValue(event.target.value);
  109. }
  110. function handleEditClick() {
  111. setIsEditing(true);
  112. }
  113. return (
  114. <Wrapper>
  115. <InnerWrapper ref={innerWrapperRef} isDisabled={isDisabled} isEditing={isEditing}>
  116. {isEditing ? (
  117. <InputWrapper isEmpty={isEmpty} data-test-id="editable-text-input">
  118. <StyledField inline={false} flexibleControlStateSize stacked>
  119. <StyledInput
  120. name={name}
  121. ref={inputRef}
  122. value={inputValue}
  123. onChange={handleInputChange}
  124. />
  125. </StyledField>
  126. <InputLabel>{inputValue}</InputLabel>
  127. </InputWrapper>
  128. ) : (
  129. <React.Fragment>
  130. <Label
  131. onClick={isDisabled ? undefined : handleEditClick}
  132. ref={labelRef}
  133. data-test-id="editable-text-label"
  134. >
  135. <InnerLabel>{inputValue}</InnerLabel>
  136. </Label>
  137. {!isDisabled && <StyledIconEdit />}
  138. </React.Fragment>
  139. )}
  140. </InnerWrapper>
  141. </Wrapper>
  142. );
  143. }
  144. export default EditableText;
  145. const Label = styled('div')`
  146. display: inline-block;
  147. border-radius: ${p => p.theme.borderRadius};
  148. text-align: left;
  149. padding-left: 10px;
  150. height: 40px;
  151. max-width: 100%;
  152. `;
  153. const InnerLabel = styled(TextOverflow)`
  154. border-top: 1px solid transparent;
  155. border-bottom: 1px dotted ${p => p.theme.gray200};
  156. transition: border 150ms;
  157. height: 40px;
  158. line-height: 38px;
  159. `;
  160. const StyledIconEdit = styled(IconEdit)`
  161. height: 40px;
  162. position: absolute;
  163. right: 0;
  164. `;
  165. const Wrapper = styled('div')`
  166. display: flex;
  167. justify-content: flex-start;
  168. height: 40px;
  169. `;
  170. const InnerWrapper = styled('div')<{isDisabled: boolean; isEditing: boolean}>`
  171. position: relative;
  172. display: inline-flex;
  173. max-width: 100%;
  174. ${p =>
  175. p.isDisabled
  176. ? `
  177. ${StyledIconEdit} {
  178. cursor: default;
  179. }
  180. ${InnerLabel} {
  181. border-bottom-color: transparent;
  182. }
  183. `
  184. : `
  185. ${!p.isEditing && `padding-right: 25px;`}
  186. :hover {
  187. padding-right: 0;
  188. ${StyledIconEdit} {
  189. display: none;
  190. }
  191. ${Label} {
  192. background: ${p.theme.gray100};
  193. padding: 0 14px 0 10px;
  194. }
  195. ${InnerLabel} {
  196. border-bottom-color: transparent;
  197. }
  198. }
  199. `}
  200. `;
  201. const InputWrapper = styled('div')<{isEmpty: boolean}>`
  202. position: relative;
  203. min-width: ${p => (p.isEmpty ? '100px' : '50px')};
  204. overflow: hidden;
  205. `;
  206. const StyledField = styled(Field)`
  207. width: 100%;
  208. padding: 0;
  209. position: absolute;
  210. right: 0;
  211. border-color: transparent;
  212. `;
  213. const StyledInput = styled(Input)`
  214. line-height: 40px;
  215. height: 40px;
  216. border: none !important;
  217. background: ${p => p.theme.gray100};
  218. &,
  219. &:focus,
  220. &:active,
  221. &:hover {
  222. box-shadow: none;
  223. }
  224. `;
  225. const InputLabel = styled('div')`
  226. width: auto;
  227. height: 40px;
  228. padding: ${space(1.5)};
  229. position: relative;
  230. z-index: -1;
  231. `;