diff --git a/src/renderer/src/components/DiagramEditor.tsx b/src/renderer/src/components/DiagramEditor.tsx index f34805480..9be78eddc 100644 --- a/src/renderer/src/components/DiagramEditor.tsx +++ b/src/renderer/src/components/DiagramEditor.tsx @@ -166,7 +166,7 @@ export const DiagramEditor: React.FC = ({ manager, editor, s - {editor !== null ? ( + {editor && ( = ({ manager, editor, s onClose={closeEventsModal} onSubmit={handleCreateEventsModal} /> - ) : ( - '' )} {isModalOpen ? ( diff --git a/src/renderer/src/components/EventsModal.tsx b/src/renderer/src/components/EventsModal.tsx index a08dbe0c4..c19a2ebd0 100644 --- a/src/renderer/src/components/EventsModal.tsx +++ b/src/renderer/src/components/EventsModal.tsx @@ -1,6 +1,4 @@ -import React, { useEffect, useState } from 'react'; - -import ReactModal, { Props } from 'react-modal'; +import React, { useLayoutEffect, useState } from 'react'; import './Modal/style.css'; import { Select, SelectOption } from '@renderer/components/UI'; @@ -10,6 +8,7 @@ import { State } from '@renderer/lib/drawable/State'; import { Action, Event } from '@renderer/types/diagram'; import { ArgumentProto } from '@renderer/types/platform'; +import { Modal } from './Modal'; import { WithHint } from './WithHint'; import { EventSelection } from '../lib/drawable/Events'; @@ -25,13 +24,11 @@ interface FormPreset { argForm: ArgForm; } -interface EventsModalProps extends Props { - editor: CanvasEditor | null; +interface EventsModalProps { + editor: CanvasEditor; manager: EditorManager; isData: { state: State; event: EventSelection; click: boolean } | undefined; isOpen: boolean; - cancelLabel?: string; - submitLabel?: string; onSubmit: (data: EventsModalResult) => void; onClose: () => void; } @@ -43,17 +40,16 @@ export interface EventsModalResult { } export const CreateEventsModal: React.FC = ({ - cancelLabel, - submitLabel, onSubmit, onClose, editor, manager, - ...props + isData, + isOpen, }) => { const componentsData = manager.useData('elements.components'); - const machine = editor!.container.machineController; - const isEditingEvent = props.isData?.event.actionIdx === null; + const machine = editor.container.machineController; + const isEditingEvent = isData?.event.actionIdx === null; const compoEntry = (idx: string) => { const proto = machine.platform.getComponent(idx); @@ -132,7 +128,7 @@ export const CreateEventsModal: React.FC = ({ const [methods, setMethods] = useState(optionsMethods[0]); - useEffect(() => { + useLayoutEffect(() => { if (!isChanged) return; if (optionsMethods.length > 0) { setMethods(optionsMethods[0]); @@ -159,7 +155,7 @@ export const CreateEventsModal: React.FC = ({ return argForm; }; - useEffect(() => { + useLayoutEffect(() => { if (!isChanged) return; setArgSet({}); if (methods) { @@ -169,9 +165,9 @@ export const CreateEventsModal: React.FC = ({ } }, [methods]); - const tryGetData: () => FormPreset | undefined = () => { - if (props.isData) { - const d = props.isData; + const tryGetData = (): FormPreset | undefined => { + if (isData) { + const d = isData; if (d.event.eventIdx >= 0) { const evs = d.state.eventBox.data[d.event.eventIdx]; if (evs) { @@ -208,21 +204,19 @@ export const CreateEventsModal: React.FC = ({ const name = entry.name; const data = argSet[entry.name] ?? ''; return ( - <> - - + ); }); - const handleInputChange = (e) => { + const handleInputChange = (e: React.ChangeEvent) => { const newSet = { ...argSet }; newSet[e.target.name] = e.target.value; setArgSet(newSet); @@ -230,8 +224,8 @@ export const CreateEventsModal: React.FC = ({ const [isChanged, setIsChanged] = useState(false); const [wasOpen, setWasOpen] = useState(false); - useEffect(() => { - if (!wasOpen && props.isOpen) { + useLayoutEffect(() => { + if (!wasOpen && isOpen) { const d: FormPreset | undefined = tryGetData(); if (d) { setComponents(d.compo); @@ -243,10 +237,10 @@ export const CreateEventsModal: React.FC = ({ } setIsChanged(false); } - setWasOpen(props.isOpen); - }, [props.isOpen]); + setWasOpen(isOpen); + }, [isOpen]); - const handleSubmit = (ev) => { + const handleSubmit = (ev: React.FormEvent) => { ev.preventDefault(); if (!methods) { @@ -255,7 +249,7 @@ export const CreateEventsModal: React.FC = ({ // FIXME: очень некорректное дублирование, его нужно снять const data = { - id: props.isData, + id: isData, trigger: { component: components.value, method: methods.value, @@ -276,42 +270,32 @@ export const CreateEventsModal: React.FC = ({ }; return ( - -
-

Выберите {isEditingEvent ? 'событие' : 'действие'}

+
+ + {parameters?.length > 0 ?
{parameters}
: ''}
-
-
- - {parameters?.length > 0 ?
{parameters}
: ''} -
-
- - -
-
- + ); }; diff --git a/src/renderer/src/components/Modal/Modal.tsx b/src/renderer/src/components/Modal/Modal.tsx index 78f62b9bc..9b15947b0 100644 --- a/src/renderer/src/components/Modal/Modal.tsx +++ b/src/renderer/src/components/Modal/Modal.tsx @@ -5,10 +5,11 @@ import ReactModal, { Props } from 'react-modal'; import { ReactComponent as Close } from '@renderer/assets/icons/close.svg'; import './style.css'; +import { twMerge } from 'tailwind-merge'; ReactModal.setAppElement('#root'); -interface ModalProps extends Props { +interface ModalProps extends Omit { title: string; cancelLabel?: string; submitLabel?: string; @@ -19,6 +20,8 @@ interface ModalProps extends Props { onSide?: React.FormEventHandler; onSubmit?: React.FormEventHandler; submitDisabled?: boolean; + className?: string; + overlayClassName?: string; } export const Modal: React.FC = ({ @@ -32,13 +35,21 @@ export const Modal: React.FC = ({ onExtra, onSide, submitDisabled, + className, + overlayClassName, ...props }) => { return (