Skip to content

Commit 44acf8c

Browse files
gnoulkpetejohanson
andauthored
feat: replace edit label prompt with modal
* replace edit label prompt with modal --------- Co-authored-by: Pete Johanson <[email protected]>
1 parent b495443 commit 44acf8c

File tree

1 file changed

+91
-12
lines changed

1 file changed

+91
-12
lines changed

src/keyboard/LayerPicker.tsx

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PencilIcon } from "@heroicons/react/24/solid";
2-
import { useCallback, useMemo } from "react";
2+
import { useCallback, useMemo, useState } from "react";
33
import {
44
DropIndicator,
55
Label,
@@ -8,6 +8,7 @@ import {
88
Selection,
99
useDragAndDrop,
1010
} from "react-aria-components";
11+
import { useModalRef } from "../misc/useModalRef";
1112

1213
interface Layer {
1314
id: number;
@@ -30,10 +31,76 @@ interface LayerPickerProps {
3031
onLayerNameChanged?: (
3132
id: number,
3233
oldName: string,
33-
newName: string
34+
newName: string,
3435
) => void | Promise<void>;
3536
}
3637

38+
interface EditLabelData {
39+
id: number;
40+
name: string;
41+
}
42+
43+
const EditLabelModal = ({
44+
open,
45+
onClose,
46+
editLabelData,
47+
handleSaveNewLabel,
48+
}: {
49+
open: boolean;
50+
onClose: () => void;
51+
editLabelData: EditLabelData;
52+
handleSaveNewLabel: (
53+
id: number,
54+
oldName: string,
55+
newName: string | null,
56+
) => void;
57+
}) => {
58+
const ref = useModalRef(open);
59+
const [newLabelName, setNewLabelName] = useState(editLabelData.name);
60+
61+
const handleSave = () => {
62+
handleSaveNewLabel(editLabelData.id, editLabelData.name, newLabelName);
63+
onClose();
64+
};
65+
66+
return (
67+
<dialog
68+
ref={ref}
69+
onClose={onClose}
70+
className="p-5 rounded-lg border-text-base border min-w-min w-[30vw] flex flex-col"
71+
>
72+
<span className="mb-3 text-lg">New Layer Name</span>
73+
<input
74+
className="p-1 border rounded border-text-base border-solid"
75+
type="text"
76+
defaultValue={editLabelData.name}
77+
autoFocus
78+
onChange={(e) => setNewLabelName(e.target.value)}
79+
onKeyDown={(e) => {
80+
if (e.key === "Enter") {
81+
e.preventDefault();
82+
handleSave();
83+
}
84+
}}
85+
/>
86+
<div className="mt-4 flex justify-end">
87+
<button className="py-1.5 px-2" type="button" onClick={onClose}>
88+
Cancel
89+
</button>
90+
<button
91+
className="py-1.5 px-2 ml-4 rounded-md bg-gray-100 text-black hover:bg-gray-300"
92+
type="button"
93+
onClick={() => {
94+
handleSave();
95+
}}
96+
>
97+
Save
98+
</button>
99+
</div>
100+
</dialog>
101+
);
102+
};
103+
37104
export const LayerPicker = ({
38105
layers,
39106
selectedLayerIndex,
@@ -46,14 +113,18 @@ export const LayerPicker = ({
46113
onLayerNameChanged,
47114
...props
48115
}: LayerPickerProps) => {
116+
const [editLabelData, setEditLabelData] = useState<EditLabelData | null>(
117+
null,
118+
);
119+
49120
const layer_items = useMemo(() => {
50121
return layers.map((l, i) => ({
51122
name: l.name || i.toLocaleString(),
52123
id: l.id,
53124
index: i,
54125
selected: i === selectedLayerIndex,
55126
}));
56-
}, [layers]);
127+
}, [layers, selectedLayerIndex]);
57128

58129
const selectionChanged = useCallback(
59130
(s: Selection) => {
@@ -63,7 +134,7 @@ export const LayerPicker = ({
63134

64135
onLayerClicked?.(layer_items.findIndex((l) => s.has(l.id)));
65136
},
66-
[onLayerClicked, layer_items]
137+
[onLayerClicked, layer_items],
67138
);
68139

69140
let { dragAndDropHooks } = useDragAndDrop({
@@ -84,15 +155,13 @@ export const LayerPicker = ({
84155
},
85156
});
86157

87-
let onEditClicked = useCallback(
88-
(id: number, name: string) => {
89-
let newName = window.prompt("Label");
90-
158+
const handleSaveNewLabel = useCallback(
159+
(id: number, oldName: string, newName: string | null) => {
91160
if (newName !== null) {
92-
onLayerNameChanged?.(id, name, newName);
161+
onLayerNameChanged?.(id, oldName, newName);
93162
}
94163
},
95-
[onLayerNameChanged]
164+
[onLayerNameChanged],
96165
);
97166

98167
return (
@@ -120,6 +189,14 @@ export const LayerPicker = ({
120189
</button>
121190
)}
122191
</div>
192+
{editLabelData !== null && (
193+
<EditLabelModal
194+
open={editLabelData !== null}
195+
onClose={() => setEditLabelData(null)}
196+
editLabelData={editLabelData}
197+
handleSaveNewLabel={handleSaveNewLabel}
198+
/>
199+
)}
123200
<ListBox
124201
aria-label="Keymap Layer"
125202
selectionMode="single"
@@ -138,8 +215,10 @@ export const LayerPicker = ({
138215
>
139216
<span>{layer_item.name}</span>
140217
<PencilIcon
141-
className="h-4 w-4 mx-1 invisible group-hover:visible"
142-
onClick={() => onEditClicked(layer_item.id, layer_item.name)}
218+
className="h-4 w-4 mx-1 invisible group-hover:visible hover:text-accent"
219+
onClick={() =>
220+
setEditLabelData({ id: layer_item.id, name: layer_item.name })
221+
}
143222
/>
144223
</ListBoxItem>
145224
)}

0 commit comments

Comments
 (0)