forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.tsx
245 lines (225 loc) · 6.76 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* 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 { styled, t } from '@superset-ui/core';
import { Tooltip } from 'src/components/Tooltip';
import CertifiedBadge from '../CertifiedBadge';
export interface EditableTitleProps {
canEdit?: boolean;
editing?: boolean;
emptyText?: string;
extraClasses?: Array<string> | string;
multiLine?: boolean;
noPermitTooltip?: string;
onSaveTitle: (arg0: string) => void;
showTooltip?: boolean;
style?: object;
title?: string;
defaultTitle?: string;
placeholder?: string;
certifiedBy?: string;
certificationDetails?: string;
}
const StyledCertifiedBadge = styled(CertifiedBadge)`
vertical-align: middle;
`;
export default function EditableTitle({
canEdit = false,
editing = false,
extraClasses,
multiLine = false,
noPermitTooltip,
onSaveTitle,
showTooltip = true,
style,
title = '',
defaultTitle = '',
placeholder = '',
certifiedBy,
certificationDetails,
// rest is related to title tooltip
...rest
}: EditableTitleProps) {
const [isEditing, setIsEditing] = useState(editing);
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]);
useEffect(() => {
if (isEditing) {
contentRef.current.focus();
// move cursor and scroll to the end
if (contentRef.current.setSelectionRange) {
const { length } = contentRef.current.value;
contentRef.current.setSelectionRange(length, length);
contentRef.current.scrollLeft = contentRef.current.scrollWidth;
contentRef.current.scrollTop = contentRef.current.scrollHeight;
}
}
}, [isEditing]);
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);
}
}
// tl;dr when a EditableTitle is being edited, typically the Tab that wraps it has been
// clicked and is focused/active. For accessibility, when the focused tab anchor intercepts
// the ' ' key (among others, including all arrows) the 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();
}
if (event.key === 'Enter') {
event.preventDefault();
handleBlur();
}
}
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;
value = currentTitle;
if (!isEditing && !currentTitle) {
value = defaultTitle || title;
}
// 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}
value={value}
className={!title ? 'text-muted' : undefined}
onKeyDown={handleKeyDown}
onChange={handleChange}
onBlur={handleBlur}
onClick={handleClick}
onKeyPress={handleKeyPress}
placeholder={placeholder}
style={editStyle}
/>
) : (
<input
data-test="editable-title-input"
ref={contentRef}
type={isEditing ? 'text' : 'button'}
value={value}
className={!title ? 'text-muted' : undefined}
onKeyDown={handleKeyDown}
onChange={handleChange}
onBlur={handleBlur}
onClick={handleClick}
onKeyPress={handleKeyPress}
placeholder={placeholder}
/>
);
if (showTooltip && !isEditing) {
titleComponent = (
<Tooltip
id="title-tooltip"
title={
canEdit
? t('Click to edit')
: noPermitTooltip ||
t("You don't have the rights to alter this title.")
}
>
{titleComponent}
</Tooltip>
);
}
if (!canEdit) {
// don't actually want an input in this case
titleComponent = <span data-test="editable-title-input">{value}</span>;
}
return (
<span
data-test="editable-title"
className={cx(
'editable-title',
extraClasses,
canEdit && 'editable-title--editable',
isEditing && 'editable-title--editing',
)}
style={style}
{...rest}
>
{certifiedBy && (
<>
<StyledCertifiedBadge
certifiedBy={certifiedBy}
details={certificationDetails}
size="xl"
/>{' '}
</>
)}
{titleComponent}
</span>
);
}