Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix(custom_types): fix display custom types in select box (#737)

Guy Ben-Aharon24be28a662c4
diff --git a/src/components/select-box/select-box.tsx b/src/components/select-box/select-box.tsx
index af8774bf..839e6835 100644
--- a/src/components/select-box/select-box.tsx
+++ b/src/components/select-box/select-box.tsx
@@ -418,27 +418,22 @@ export const SelectBox = React.forwardRef<HTMLInputElement, SelectBoxProps>(
<ScrollArea>
<div className="max-h-64 w-full">
- <CommandGroup>
- <CommandList className="max-h-fit w-full">
- {hasGroups
- ? Object.entries(groups).map(
- ([
- groupName,
- groupOptions,
- ]) => (
- <CommandGroup
- key={groupName}
- heading={groupName}
- >
- {groupOptions.map(
- renderOption
- )}
- </CommandGroup>
- )
+ <CommandList className="max-h-fit w-full">
+ {hasGroups
+ ? Object.entries(groups).map(
+ ([groupName, groupOptions]) => (
+ <CommandGroup
+ key={groupName}
+ heading={groupName}
+ >
+ {groupOptions.map(
+ renderOption
+ )}
+ </CommandGroup>
)
- : options.map(renderOption)}
- </CommandList>
- </CommandGroup>
+ )
+ : options.map(renderOption)}
+ </CommandList>
</div>
</ScrollArea>
</Command>
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
index bc5a5f60..242ffc35 100644
--- a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
@@ -1,4 +1,4 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import { GripVertical, KeyRound } from 'lucide-react';
import { Input } from '@/components/input/input';
import type { DBField } from '@/lib/domain/db-field';
@@ -34,20 +34,42 @@ export const TableField: React.FC<TableFieldProps> = ({
updateField,
removeField,
}) => {
- const { databaseType } = useChartDB();
+ const { databaseType, customTypes } = useChartDB();
const { t } = useTranslation();
const { attributes, listeners, setNodeRef, transform, transition } =
useSortable({ id: field.id });
- const dataFieldOptions: SelectBoxOption[] = sortedDataTypeMap[
- databaseType
- ].map((type) => ({
- label: type.name,
- value: type.id,
- regex: type.hasCharMaxLength ? `^${type.name}\\(\\d+\\)$` : undefined,
- extractRegex: type.hasCharMaxLength ? /\((\d+)\)/ : undefined,
- }));
+ const dataFieldOptions = useMemo(() => {
+ const standardTypes: SelectBoxOption[] = sortedDataTypeMap[
+ databaseType
+ ].map((type) => ({
+ label: type.name,
+ value: type.id,
+ regex: type.hasCharMaxLength
+ ? `^${type.name}\\(\\d+\\)$`
+ : undefined,
+ extractRegex: type.hasCharMaxLength ? /\((\d+)\)/ : undefined,
+ group: customTypes?.length ? 'Standard Types' : undefined,
+ }));
+
+ if (!customTypes?.length) {
+ return standardTypes;
+ }
+
+ // Add custom types as options
+ const customTypeOptions: SelectBoxOption[] = customTypes.map(
+ (type) => ({
+ label: type.name,
+ value: type.name,
+ description:
+ type.kind === 'enum' ? `${type.values?.join(' | ')}` : '',
+ group: 'Custom Types',
+ })
+ );
+
+ return [...standardTypes, ...customTypeOptions];
+ }, [databaseType, customTypes]);
const onChangeDataType = useCallback<
NonNullable<SelectBoxProps['onChange']>