Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 6beed32

Browse files
committed
control duplicates
1 parent e4b187a commit 6beed32

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/components/HeaderMenu.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
7070
const [settingsPopperElement, setSettingsPopperElement] = useState(null);
7171
const [showSettings, setShowSettings] = useState(false);
7272

73+
// Manage errors
74+
const [labelStateInvalid, setLabelStateInvalid] = useState(false);
7375
useEffect(() => {
7476
if (created) {
7577
setExpanded(true);
@@ -208,9 +210,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
208210
}
209211
);
210212

211-
function persistLabelChange() {
212-
// trim label will get a valid yaml key
213-
const newKey = dbTrim(labelState);
213+
function persistLabelChange(newKey: string) {
214214
const futureOrder = headerMenuProps.headerProps.allColumns.map(
215215
(o: Column) => (o.id === column.id ? newKey : o.id)
216216
);
@@ -239,12 +239,26 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
239239
}
240240
function handleKeyDown(e: any) {
241241
if (e.key === "Enter") {
242-
persistLabelChange();
242+
// trim label will get a valid yaml key
243+
const newKey = dbTrim(labelState);
244+
// Check if key already exists. If so, mark it as invalid
245+
if (
246+
headerMenuProps.headerProps.allColumns.find(
247+
(o: Column) => o.id === newKey
248+
)
249+
) {
250+
setLabelStateInvalid(true);
251+
return;
252+
}
253+
persistLabelChange(newKey);
243254
}
244255
}
245256

246257
function handleChange(e: any) {
247258
setLabelState(e.target.value);
259+
if (labelStateInvalid) {
260+
setLabelStateInvalid(false);
261+
}
248262
}
249263

250264
/**
@@ -292,7 +306,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
292306
<div
293307
className={`menu ${c("popper")}`}
294308
style={{
295-
width: 240
309+
width: 240,
296310
}}
297311
>
298312
{/** Edit header label section */}
@@ -305,7 +319,11 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
305319
>
306320
<div className="is-fullwidth" style={{ marginBottom: 12 }}>
307321
<input
308-
className="form-input"
322+
className={
323+
labelStateInvalid
324+
? `${c("invalid-form")}`
325+
: `${c("form-input")}`
326+
}
309327
ref={setInputRef}
310328
type="text"
311329
value={labelState}
@@ -356,7 +374,10 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
356374
>
357375
{types.map((type) => (
358376
<div key={type.label}>
359-
<div className="menu-item sort-button" onClick={type.onClick}>
377+
<div
378+
className="menu-item sort-button"
379+
onClick={type.onClick}
380+
>
360381
<span className="svg-icon svg-text icon-margin">
361382
{type.icon}
362383
</span>
@@ -439,9 +460,8 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
439460
</div>
440461
</div>
441462
</div>
442-
)
443-
}
444-
</div >
463+
)}
464+
</div>
445465
);
446466
};
447467

styles.css

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
transform: rotate(180deg);
5555
}
5656

57-
.form-input {
57+
.database-plugin__form-input {
5858
padding: 0.375rem;
5959
background-color: var(--background-primary);
6060
border: none;
@@ -63,11 +63,20 @@
6363
color: var(--text-normal);
6464
}
6565

66-
.form-input:focus {
66+
.database-plugin__form-input:focus {
6767
outline: none;
6868
box-shadow: 0 0 1px 2px var(--background-modifier-border);
6969
}
7070

71+
.database-plugin__invalid-form{
72+
padding: 0.375rem;
73+
border: none;
74+
border-radius: 4px;
75+
font-size: 0.875rem;
76+
color: var(--text-error);
77+
background-color: var(--background-modifier-error);
78+
}
79+
7180
.is-fullwidth {
7281
width: 100%;
7382
}

0 commit comments

Comments
 (0)