Skip to content

Commit ba2f551

Browse files
committed
feat(improve-modal): migrate edit-layer-name modal
1 parent 5bfe6ce commit ba2f551

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/keyboard/LayerPicker.tsx

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
Selection,
99
useDragAndDrop,
1010
} from "react-aria-components";
11-
import { useModalRef } from "../misc/useModalRef";
12-
import { GenericModal } from "../GenericModal";
11+
import { Modal, ModalContent } from "../components/modal/Modal.tsx";
1312

1413
interface Layer {
1514
id: number;
@@ -49,56 +48,62 @@ const EditLabelModal = ({
4948
}: {
5049
open: boolean;
5150
onClose: () => void;
52-
editLabelData: EditLabelData;
51+
editLabelData: EditLabelData | null;
5352
handleSaveNewLabel: (
5453
id: number,
5554
oldName: string,
5655
newName: string | null
5756
) => void;
5857
}) => {
59-
const ref = useModalRef(open);
60-
const [newLabelName, setNewLabelName] = useState(editLabelData.name);
58+
const [newLabelName, setNewLabelName] = useState(editLabelData?.name ?? '');
6159

62-
const handleSave = () => {
60+
const handleSave = useCallback(() => {
61+
if (!editLabelData) return
62+
6363
handleSaveNewLabel(editLabelData.id, editLabelData.name, newLabelName);
6464
onClose();
65-
};
65+
}, [editLabelData]);
6666

6767
return (
68-
<GenericModal
69-
ref={ref}
70-
onClose={onClose}
71-
className="min-w-min w-[30vw] flex flex-col"
72-
>
73-
<span className="mb-3 text-lg">New Layer Name</span>
74-
<input
75-
className="p-1 border rounded border-base-content border-solid"
76-
type="text"
77-
defaultValue={editLabelData.name}
78-
autoFocus
79-
onChange={(e) => setNewLabelName(e.target.value)}
80-
onKeyDown={(e) => {
81-
if (e.key === "Enter") {
82-
e.preventDefault();
83-
handleSave();
84-
}
85-
}}
86-
/>
87-
<div className="mt-4 flex justify-end">
88-
<button className="py-1.5 px-2" type="button" onClick={onClose}>
89-
Cancel
90-
</button>
91-
<button
92-
className="py-1.5 px-2 ml-4 rounded-md bg-gray-100 text-black hover:bg-gray-300"
93-
type="button"
94-
onClick={() => {
95-
handleSave();
96-
}}
97-
>
98-
Save
99-
</button>
100-
</div>
101-
</GenericModal>
68+
<Modal open={open} onOpenChange={onClose}>
69+
<ModalContent
70+
className="min-w-min w-[30vw] flex flex-col"
71+
>
72+
{editLabelData && (
73+
<>
74+
<span className="mb-3 text-lg">New Layer Name</span>
75+
<input
76+
className="p-1 border rounded border-base-content border-solid"
77+
type="text"
78+
defaultValue={editLabelData.name}
79+
autoFocus
80+
onChange={(e) => setNewLabelName(e.target.value)}
81+
onKeyDown={(e) => {
82+
if (e.key === "Enter") {
83+
e.preventDefault();
84+
handleSave();
85+
}
86+
}}
87+
/>
88+
<div className="mt-4 flex justify-end">
89+
<button className="py-1.5 px-2" type="button" onClick={onClose}>
90+
Cancel
91+
</button>
92+
<button
93+
className="py-1.5 px-2 ml-4 rounded-md bg-gray-100 text-black hover:bg-gray-300"
94+
type="button"
95+
disabled={!!editLabelData}
96+
onClick={() => {
97+
handleSave();
98+
}}
99+
>
100+
Save
101+
</button>
102+
</div>
103+
</>
104+
)}
105+
</ModalContent>
106+
</Modal>
102107
);
103108
};
104109

@@ -190,14 +195,14 @@ export const LayerPicker = ({
190195
</button>
191196
)}
192197
</div>
193-
{editLabelData !== null && (
194-
<EditLabelModal
195-
open={editLabelData !== null}
196-
onClose={() => setEditLabelData(null)}
197-
editLabelData={editLabelData}
198-
handleSaveNewLabel={handleSaveNewLabel}
199-
/>
200-
)}
198+
199+
<EditLabelModal
200+
open={!!editLabelData}
201+
onClose={() => setEditLabelData(null)}
202+
editLabelData={editLabelData}
203+
handleSaveNewLabel={handleSaveNewLabel}
204+
/>
205+
201206
<ListBox
202207
aria-label="Keymap Layer"
203208
selectionMode="single"

0 commit comments

Comments
 (0)