forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameDialog.tsx
More file actions
174 lines (155 loc) · 4.48 KB
/
RenameDialog.tsx
File metadata and controls
174 lines (155 loc) · 4.48 KB
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
import React, {
type ChangeEvent,
type FormEvent,
PureComponent,
type ReactElement,
type RefObject,
} from 'react';
import classNames from 'classnames';
import { Button, Popper } from '@deephaven/components';
interface RenameDialogProps {
isShared: boolean;
isOwner: boolean;
isShown: boolean;
itemType: string;
onSubmit: (title: string) => void;
onCancel: () => void;
value?: string | null;
}
interface RenameDialogState {
value?: string | null;
valueWasValidated: boolean;
}
export default class RenameDialog extends PureComponent<
RenameDialogProps,
RenameDialogState
> {
static defaultProps = {
isShared: false,
isOwner: true,
itemType: 'Item',
value: '',
};
constructor(props: RenameDialogProps) {
super(props);
this.handleRenameDialogOpened = this.handleRenameDialogOpened.bind(this);
this.handleRenameInputChange = this.handleRenameInputChange.bind(this);
this.handleRenameCancel = this.handleRenameCancel.bind(this);
this.handleRenameSubmit = this.handleRenameSubmit.bind(this);
this.renameInputRef = React.createRef();
const { value } = props;
this.state = {
value,
valueWasValidated: false,
};
}
componentDidUpdate(prevProps: RenameDialogProps): void {
const { isShown: prevIsShown } = prevProps;
const { isShown } = this.props;
// Reset the state on dialog shown and not on the `value` prop change
// so the input keeps the internal value while the dialog is open.
// Useful in case the `value` prop update is triggered externally
// i.e. by someone else renaming the same shared dashboard
if (isShown && !prevIsShown) {
this.resetState();
}
}
renameInputRef: RefObject<HTMLInputElement>;
resetState(): void {
const { value } = this.props;
this.setState({ value, valueWasValidated: false });
}
handleRenameDialogOpened(): void {
this.renameInputRef?.current?.focus();
}
handleRenameInputChange(event: ChangeEvent<HTMLInputElement>): void {
this.setState({ value: event.target.value });
}
handleRenameCancel(): void {
const { onCancel } = this.props;
onCancel();
}
handleRenameSubmit(event: FormEvent<HTMLFormElement>): void {
event.stopPropagation();
event.preventDefault();
const { value } = this.state;
const newTitle = value?.trim();
if (newTitle !== undefined && newTitle !== '') {
const { onSubmit } = this.props;
onSubmit(newTitle);
} else {
this.setState({ value: newTitle, valueWasValidated: true });
}
}
renderRenameDialog(): ReactElement {
const { isShared, isOwner, itemType } = this.props;
const { value, valueWasValidated } = this.state;
return (
<form
className={classNames('p-3', { 'was-validated': valueWasValidated })}
onSubmit={this.handleRenameSubmit}
noValidate
role="presentation"
onMouseDown={event => {
// block events in the NewTabScreen ItemList
event.stopPropagation();
}}
onMouseUp={event => {
event.stopPropagation();
}}
>
<div className="form-group">
<label htmlFor={`rename-dialog-${itemType}-input`}>
Rename {itemType}
</label>
<input
type="text"
className="form-control"
id={`rename-dialog-${itemType}-input`}
value={value ?? undefined}
ref={this.renameInputRef}
onChange={this.handleRenameInputChange}
required
/>
<div className="invalid-feedback">
{itemType} name cannot be empty
</div>
{(isShared || !isOwner) && (
<div className="pt-2">
Renaming this {itemType} will rename for all users.
</div>
)}
</div>
<div className="text-right">
<Button
kind="secondary"
className="mr-2"
onClick={this.handleRenameCancel}
>
Cancel
</Button>
<Button kind="primary" type="submit">
Rename
</Button>
</div>
</form>
);
}
render(): ReactElement {
const { isShown, onCancel } = this.props;
return (
<Popper
isShown={isShown}
onEntered={this.handleRenameDialogOpened}
onExited={onCancel}
options={{
placement: 'bottom',
}}
interactive
closeOnBlur
>
{this.renderRenameDialog()}
</Popper>
);
}
}