From a08faec231c47f8186c5011d2507d6e12336c068 Mon Sep 17 00:00:00 2001 From: IRBorisov <8611739+IRBorisov@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:24:41 +0300 Subject: [PATCH] Add name change when creating cst --- .../src/pages/RSFormPage/DlgCreateCst.tsx | 37 +++++++++++++++---- .../src/pages/RSFormPage/DlgRenameCst.tsx | 4 +- .../frontend/src/pages/RSFormPage/RSTabs.tsx | 4 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/rsconcept/frontend/src/pages/RSFormPage/DlgCreateCst.tsx b/rsconcept/frontend/src/pages/RSFormPage/DlgCreateCst.tsx index a3a1f22c..596cfd59 100644 --- a/rsconcept/frontend/src/pages/RSFormPage/DlgCreateCst.tsx +++ b/rsconcept/frontend/src/pages/RSFormPage/DlgCreateCst.tsx @@ -1,22 +1,26 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useLayoutEffect, useState } from 'react'; import Modal, { ModalProps } from '../../components/Common/Modal'; import SelectSingle from '../../components/Common/SelectSingle'; import TextArea from '../../components/Common/TextArea'; +import TextInput from '../../components/Common/TextInput'; import RSInput from '../../components/RSInput'; -import { CstType,ICstCreateData } from '../../models/rsform'; +import { CstType,ICstCreateData, IRSForm } from '../../models/rsform'; import { labelCstType } from '../../utils/labels'; +import { createAliasFor, getCstTypePrefix } from '../../utils/misc'; import { SelectorCstType } from '../../utils/selectors'; interface DlgCreateCstProps extends Pick { initial?: ICstCreateData + schema: IRSForm onCreate: (data: ICstCreateData) => void } -function DlgCreateCst({ hideWindow, initial, onCreate }: DlgCreateCstProps) { +function DlgCreateCst({ hideWindow, initial, schema, onCreate }: DlgCreateCstProps) { const [validated, setValidated] = useState(false); const [selectedType, setSelectedType] = useState(CstType.BASE); + const [alias, setAlias] = useState(''); const [term, setTerm] = useState(''); const [textDefinition, setTextDefinition] = useState(''); @@ -27,7 +31,7 @@ function DlgCreateCst({ hideWindow, initial, onCreate }: DlgCreateCstProps) { return { cst_type: selectedType, insert_after: initial?.insert_after ?? null, - alias: '', + alias: alias, convention: convention, definition_formal: expression, definition_raw: textDefinition, @@ -37,17 +41,30 @@ function DlgCreateCst({ hideWindow, initial, onCreate }: DlgCreateCstProps) { const handleSubmit = () => onCreate(getData()); - useEffect(() => { + useLayoutEffect(() => { if (initial) { setSelectedType(initial.cst_type); setTerm(initial.term_raw); setTextDefinition(initial.definition_raw); setExpression(initial.definition_formal); setConvention(initial.convention); + setAlias(initial.alias); } }, [initial]); - useEffect(() => setValidated(selectedType !== undefined), [selectedType]); + useLayoutEffect( + () => { + setAlias(createAliasFor(selectedType, schema)); + }, [selectedType, schema]); + + useEffect( + () => { + if(alias.length < 2 || alias[0] !== getCstTypePrefix(selectedType)) { + setValidated(false); + } else { + setValidated(!schema.items.find(cst => cst.alias === alias)) + } + }, [alias, selectedType, schema]); return (
-
+
setSelectedType(data?.value ?? CstType.BASE)} /> + setAlias(event.target.value)} + />