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

Commit 4c61bdb

Browse files
authored
feat(gui): generate enum names automatically and added buttons for generating (#948)
* generate enum names automatically and added buttons for generating * swapped places of button and textfield for a more user friendly look * u * style: apply automatic fixes of linters Co-authored-by: jofaul <[email protected]>
1 parent acc08a5 commit 4c61bdb

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

api-editor/gui/src/features/annotations/forms/EnumForm.tsx

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@chakra-ui/react';
1212
import React, { useEffect } from 'react';
1313
import { useFieldArray, useForm } from 'react-hook-form';
14-
import { FaPlus, FaTrash } from 'react-icons/fa';
14+
import { FaAngleDoubleRight, FaAngleRight, FaPlus, FaTrash } from 'react-icons/fa';
1515
import { useAppDispatch, useAppSelector } from '../../../app/hooks';
1616
import { pythonIdentifierPattern } from '../../../common/validation';
1717
import { PythonDeclaration } from '../../packageData/model/PythonDeclaration';
@@ -45,6 +45,8 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
4545
handleSubmit,
4646
setFocus,
4747
reset,
48+
watch,
49+
setValue,
4850
formState: { errors },
4951
} = useForm<EnumFormState>({
5052
defaultValues: {
@@ -63,6 +65,8 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
6365
name: 'pairs',
6466
});
6567

68+
const watchPairs = watch('pairs');
69+
6670
useEffect(() => {
6771
try {
6872
setFocus('enumName');
@@ -113,6 +117,21 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
113117
dispatch(hideAnnotationForm());
114118
};
115119

120+
const onGenerate = (index: number) => () => {
121+
generateEnumName(index);
122+
};
123+
124+
const onGenerateAll = () => {
125+
for (let i = 0; i < watchPairs.length; i++) {
126+
generateEnumName(i);
127+
}
128+
};
129+
130+
const generateEnumName = (index: number) => {
131+
const instanceName = formatEnumName(watchPairs[index].stringValue.toUpperCase());
132+
setValue(`pairs.${index}.instanceName`, instanceName);
133+
};
134+
116135
// Rendering -------------------------------------------------------------------------------------------------------
117136

118137
return (
@@ -141,6 +160,12 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
141160
<Text fontSize="md" fontWeight="medium" w="100%">
142161
String value:
143162
</Text>
163+
<IconButton
164+
icon={<FaAngleDoubleRight />}
165+
aria-label="Generate all instance names"
166+
colorScheme="blue"
167+
onClick={onGenerateAll}
168+
/>
144169
<Text fontSize="md" fontWeight="medium" w="100%">
145170
Instance name:
146171
</Text>
@@ -154,12 +179,27 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
154179
{...register(`pairs.${index}.stringValue`, {
155180
required: 'This is required.',
156181
})}
182+
onBlur={() => {
183+
if (
184+
watchPairs[index].stringValue.length !== 0 &&
185+
watchPairs[index].instanceName.length === 0
186+
) {
187+
generateEnumName(index);
188+
}
189+
}}
157190
/>
158191
<FormErrorMessage>
159192
<FormErrorIcon /> {errors?.pairs?.[index]?.stringValue?.message}
160193
</FormErrorMessage>
161194
</FormControl>
162195

196+
<IconButton
197+
icon={<FaAngleRight />}
198+
aria-label="Generate instance name"
199+
colorScheme="blue"
200+
onClick={onGenerate(index)}
201+
/>
202+
163203
<FormControl isInvalid={Boolean(errors?.pairs?.[index]?.instanceName)}>
164204
<Input
165205
{...register(`pairs.${index}.instanceName`, {
@@ -189,3 +229,16 @@ export const EnumForm: React.FC<EnumFormProps> = function ({ target }) {
189229
</AnnotationForm>
190230
);
191231
};
232+
233+
const formatEnumName = (stringValue: string) => {
234+
const segments = stringValue.split(/[_\-.]/u);
235+
const formattedString = segments
236+
.map((segment) => segment.replaceAll(/\W/gu, '').toUpperCase())
237+
.filter((segment) => segment.length > 0)
238+
.join('_');
239+
240+
if (formattedString.length === 0 || formattedString.charAt(0).match(/\d/u)) {
241+
return '_' + formattedString;
242+
}
243+
return formattedString;
244+
};

0 commit comments

Comments
 (0)