enumOptions and enum reader
This commit is contained in:
@@ -80,6 +80,21 @@ function parseSchemaFields(
|
||||
const relation = schemaToResourceMap.get(targetSchema);
|
||||
if (relation) {
|
||||
fields[key].relation = relation;
|
||||
|
||||
// Propagate enumOption from target resource config, or derive from target schema
|
||||
const explicitEnumOption = configuration[relation]?.enumOption;
|
||||
if (explicitEnumOption) {
|
||||
fields[key].enumOption = explicitEnumOption;
|
||||
} else {
|
||||
const targetProps = targetSchema.properties || {};
|
||||
const valueField = Object.entries(targetProps).find(
|
||||
([name, p]: [string, any]) => name !== 'id' && p.type === 'string'
|
||||
)?.[0];
|
||||
fields[key].enumOption = {
|
||||
key: 'id',
|
||||
value: valueField ?? 'id',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively parse nested objects (only if not a relation)
|
||||
|
||||
28
react-openapi/utils/options.ts
Normal file
28
react-openapi/utils/options.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ResourceField, SelectOption } from "../types/config";
|
||||
|
||||
export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] {
|
||||
if (field.type === 'enum' && field.options) {
|
||||
return field.options.map(opt => ({
|
||||
key: opt,
|
||||
value: field.enumLabels?.[opt] ?? opt,
|
||||
}));
|
||||
}
|
||||
|
||||
if (field.relation) {
|
||||
const data = relationData ?? [];
|
||||
const enumOption = field.enumOption ?? { key: 'id', value: 'name' };
|
||||
|
||||
return data.map(item => ({
|
||||
key: String(item[enumOption.key] ?? ''),
|
||||
value: Array.isArray(enumOption.value)
|
||||
? enumOption.value.map(k => item[k]).filter(v => v != null).join(' ')
|
||||
: String(item[enumOption.value] ?? ''),
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
export function toGridValueOptions(options: SelectOption[]): { value: string; label: string }[] {
|
||||
return options.map(opt => ({ value: opt.key, label: opt.value }));
|
||||
}
|
||||
Reference in New Issue
Block a user