-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
Copy pathEditableTitle.tsx
212 lines (194 loc) · 5.8 KB
/
EditableTitle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, { useEffect, useState, useRef } from 'react';
import cx from 'classnames';
import { t } from '@superset-ui/core';
import TooltipWrapper from './TooltipWrapper';
interface EditableTitleProps {
canEdit?: boolean;
emptyText?: string;
extraClasses?: Array<string> | string;
multiLine?: boolean;
noPermitTooltip?: string;
onSaveTitle: (arg0: string) => {};
showTooltip?: boolean;
style?: object;
title: string;
}
export default function EditableTitle({
canEdit = false,
emptyText,
extraClasses,
multiLine = false,
noPermitTooltip,
onSaveTitle,
showTooltip = true,
style,
title,
}: EditableTitleProps) {
const [isEditing, setIsEditing] = useState(false);
const [currentTitle, setCurrentTitle] = useState(title);
const [lastTitle, setLastTitle] = useState(title);
const [
contentBoundingRect,
setContentBoundingRect,
] = useState<DOMRect | null>(null);
// Used so we can access the DOM element if a user clicks on this component.
const contentRef = useRef<any | HTMLInputElement | HTMLTextAreaElement>();
useEffect(() => {
if (title !== currentTitle) {
setLastTitle(currentTitle);
setCurrentTitle(title);
}
}, [title]);
function handleClick() {
if (!canEdit || isEditing) {
return;
}
// For multi-line values, save the actual rendered size of the displayed text.
// Later, if a textarea is constructed for editing the value, we'll need this.
const contentBounding = contentRef.current
? contentRef.current.getBoundingClientRect()
: null;
setIsEditing(true);
setContentBoundingRect(contentBounding);
}
function handleBlur() {
const formattedTitle = currentTitle.trim();
if (!canEdit) {
return;
}
setIsEditing(false);
if (!formattedTitle.length) {
setCurrentTitle(lastTitle);
return;
}
if (lastTitle !== formattedTitle) {
setLastTitle(formattedTitle);
}
if (title !== formattedTitle) {
onSaveTitle(formattedTitle);
}
}
// this entire method exists to support using EditableTitle as the title of a
// react-bootstrap Tab, as a workaround for this line in react-bootstrap https://goo.gl/ZVLmv4
//
// tl;dr when a Tab EditableTitle is being edited, typically the Tab it's within has been
// clicked and is focused/active. for accessibility, when focused the Tab <a /> intercepts
// the ' ' key (among others, including all arrows) and onChange() doesn't fire. somehow
// keydown is still called so we can detect this and manually add a ' ' to the current title
function handleKeyDown(event: any) {
if (event.key === ' ') {
event.stopPropagation();
}
}
function handleChange(ev: any) {
if (!canEdit) {
return;
}
setCurrentTitle(ev.target.value);
}
function handleKeyPress(ev: any) {
if (ev.key === 'Enter') {
ev.preventDefault();
handleBlur();
}
}
let value: string | undefined;
if (currentTitle) {
value = currentTitle;
} else if (!isEditing) {
value = emptyText;
}
// Construct an inline style based on previously-saved height of the rendered label. Only
// used in multi-line contexts.
const editStyle =
isEditing && contentBoundingRect
? { height: `${contentBoundingRect.height}px` }
: undefined;
// Create a textarea when we're editing a multi-line value, otherwise create an input (which may
// be text or a button).
let titleComponent =
multiLine && isEditing ? (
<textarea
data-test="editable-title-input"
ref={contentRef}
required
value={value}
className={!title ? 'text-muted' : undefined}
onKeyDown={handleKeyDown}
onChange={handleChange}
onBlur={handleBlur}
onClick={handleClick}
onKeyPress={handleKeyPress}
style={editStyle}
/>
) : (
<input
data-test="editable-title-input"
ref={contentRef}
required
type={isEditing ? 'text' : 'button'}
value={value}
className={!title ? 'text-muted' : undefined}
onKeyDown={handleKeyDown}
onChange={handleChange}
onBlur={handleBlur}
onClick={handleClick}
onKeyPress={handleKeyPress}
/>
);
if (showTooltip && !isEditing) {
titleComponent = (
<TooltipWrapper
label="title"
tooltip={
canEdit
? t('click to edit')
: noPermitTooltip ||
t("You don't have the rights to alter this title.")
}
>
{titleComponent}
</TooltipWrapper>
);
}
if (!canEdit) {
// don't actually want an input in this case
titleComponent = (
<span data-test="editable-title-input" title={value}>
{value}
</span>
);
}
return (
<span
data-test="editable-title"
className={cx(
'editable-title',
extraClasses,
canEdit && 'editable-title--editable',
isEditing && 'editable-title--editing',
)}
style={style}
>
{titleComponent}
</span>
);
}