Functional foreign key account selector and path generator

This commit is contained in:
2025-02-02 20:12:55 +01:00
parent fd92c57eb5
commit 1e8731d78b
4 changed files with 56 additions and 22 deletions

View File

@@ -23,6 +23,21 @@ const UnionEnumField = (props: FieldProps) => {
} = props;
const enumOptions: any[] = []
if (options.length == 2 && (options[0].type == "null" || options[1].type == "null")) {
const { SchemaField: _SchemaField } = registry.fields;
let opt_schema = {...schema}
delete(opt_schema.anyOf)
if (options[0].type == "null") {
opt_schema = {...opt_schema, ...options[1]}
} else if (options[1].type == "null") {
opt_schema = {...opt_schema, ...options[0]}
}
return <_SchemaField {...props} schema={opt_schema} uiSchema={uiSchema} />
}
for (let opt of options) {
if (!opt.hasOwnProperty('enum')) {
return (<AnyOfField {...props} />)

View File

@@ -2,7 +2,7 @@ import { WidgetProps } from '@rjsf/utils';
import { Autocomplete } from "@mui/material";
import { useState, useEffect } from "react";
import TextField from "@mui/material/TextField";
import { useList, useOne } from "@refinedev/core";
import {BaseRecord, useList, useOne} from "@refinedev/core";
export const ForeignKeyWidget = (props: WidgetProps) => {
const resource = props.schema.foreign_key.reference.resource
@@ -10,7 +10,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
const valueResult = useOne({
resource: resource,
id: props.value != "" ? props.value : undefined
id: props.value != null ? props.value : undefined
});
const [inputValue, setInputValue] = useState<string>("");
@@ -30,6 +30,13 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
});
const options = listResult.data?.data || [];
if (! props.required) {
const empty_option: BaseRecord = {
id: null
}
empty_option[labelField] = "(None)"
options.unshift(empty_option);
}
const isLoading = listResult.isLoading || valueResult.isLoading;
if(! selectedValue && valueResult.data) {
@@ -40,9 +47,9 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
<Autocomplete
value={selectedValue}
onChange={(event, newValue) => {
setSelectedValue(newValue)
props.onChange(newValue ? newValue.id : "")
return true
setSelectedValue(newValue);
props.onChange(newValue ? newValue.id : null);
return true;
}}
//inputValue={inputValue}
onInputChange={(event, newInputValue) => setInputValue(newInputValue)}

View File

@@ -33,11 +33,15 @@ function buildResource(rawSchemas: RJSFSchema, resourceName: string) {
} else if (is_union(prop)) {
const union = prop.hasOwnProperty("oneOf") ? prop.oneOf : prop.anyOf;
for (let i in union) {
resolveReference(rawSchemas, resource, union[i]);
if (is_reference(union[i])) {
resolveReference(rawSchemas, resource, union[i]);
}
}
} else if (is_enum(prop)) {
for (let i in prop.allOf) {
resolveReference(rawSchemas, resource, prop.allOf[i]);
if (is_reference(prop.allOf[i])) {
resolveReference(rawSchemas, resource, prop.allOf[i]);
}
}
} else if (is_array(prop) && is_reference(prop.items)) {
resolveReference(rawSchemas, resource, prop.items);