Override field types
This commit is contained in:
parent
5f9eb16aec
commit
12fe08145c
|
|
@ -5,6 +5,7 @@ import { useCallback, useEffect, useMemo } from 'react';
|
||||||
import { type Control, type FieldValues, useController } from 'react-hook-form';
|
import { type Control, type FieldValues, useController } from 'react-hook-form';
|
||||||
|
|
||||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
||||||
|
import { apiUrl } from '@lib/functions/Api';
|
||||||
import type { ApiFormFieldSet, ApiFormFieldType } from '@lib/types/Forms';
|
import type { ApiFormFieldSet, ApiFormFieldType } from '@lib/types/Forms';
|
||||||
import { IconFileUpload } from '@tabler/icons-react';
|
import { IconFileUpload } from '@tabler/icons-react';
|
||||||
import type { NavigateFunction } from 'react-router-dom';
|
import type { NavigateFunction } from 'react-router-dom';
|
||||||
|
|
@ -122,15 +123,62 @@ export function ApiFormField({
|
||||||
// Construct the individual field
|
// Construct the individual field
|
||||||
const fieldInstance = useMemo(() => {
|
const fieldInstance = useMemo(() => {
|
||||||
switch (fieldDefinition.field_type) {
|
switch (fieldDefinition.field_type) {
|
||||||
case 'related field':
|
case 'location':
|
||||||
return (
|
return (
|
||||||
<RelatedModelField
|
<TreeField
|
||||||
definition={fieldDefinition}
|
|
||||||
controller={controller}
|
controller={controller}
|
||||||
|
definition={fieldDefinition}
|
||||||
fieldName={fieldName}
|
fieldName={fieldName}
|
||||||
navigate={navigate}
|
endpoint={ApiEndpoints.stock_location_tree}
|
||||||
|
childIdentifier='sublocations'
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
case 'category':
|
||||||
|
return (
|
||||||
|
<TreeField
|
||||||
|
controller={controller}
|
||||||
|
definition={fieldDefinition}
|
||||||
|
fieldName={fieldName}
|
||||||
|
endpoint={ApiEndpoints.category_tree}
|
||||||
|
childIdentifier='subcategories'
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
case 'related field':
|
||||||
|
// Redirect location or category fields to the appropriate tree field
|
||||||
|
if (
|
||||||
|
fieldDefinition.api_url == apiUrl(ApiEndpoints.stock_location_list)
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
<TreeField
|
||||||
|
controller={controller}
|
||||||
|
definition={fieldDefinition}
|
||||||
|
fieldName={fieldName}
|
||||||
|
endpoint={ApiEndpoints.stock_location_tree}
|
||||||
|
childIdentifier='sublocations'
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
} else if (
|
||||||
|
fieldDefinition.api_url == apiUrl(ApiEndpoints.category_list)
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
<TreeField
|
||||||
|
controller={controller}
|
||||||
|
definition={fieldDefinition}
|
||||||
|
fieldName={fieldName}
|
||||||
|
endpoint={ApiEndpoints.category_tree}
|
||||||
|
childIdentifier='subcategories'
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<RelatedModelField
|
||||||
|
definition={fieldDefinition}
|
||||||
|
controller={controller}
|
||||||
|
fieldName={fieldName}
|
||||||
|
navigate={navigate}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
case 'email':
|
case 'email':
|
||||||
case 'url':
|
case 'url':
|
||||||
case 'string':
|
case 'string':
|
||||||
|
|
@ -256,26 +304,6 @@ export function ApiFormField({
|
||||||
return (
|
return (
|
||||||
<TagsField controller={controller} definition={fieldDefinition} />
|
<TagsField controller={controller} definition={fieldDefinition} />
|
||||||
);
|
);
|
||||||
case 'location':
|
|
||||||
return (
|
|
||||||
<TreeField
|
|
||||||
controller={controller}
|
|
||||||
definition={fieldDefinition}
|
|
||||||
fieldName={fieldName}
|
|
||||||
endpoint={ApiEndpoints.stock_location_tree}
|
|
||||||
childIdentifier='sublocations'
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
case 'category':
|
|
||||||
return (
|
|
||||||
<TreeField
|
|
||||||
controller={controller}
|
|
||||||
definition={fieldDefinition}
|
|
||||||
fieldName={fieldName}
|
|
||||||
endpoint={ApiEndpoints.category_tree}
|
|
||||||
childIdentifier='subcategories'
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
default:
|
default:
|
||||||
return (
|
return (
|
||||||
<Alert color='red' title={t`Error`}>
|
<Alert color='red' title={t`Error`}>
|
||||||
|
|
|
||||||
|
|
@ -228,13 +228,11 @@ export function partCategoryFields({
|
||||||
const fields: ApiFormFieldSet = {
|
const fields: ApiFormFieldSet = {
|
||||||
parent: {
|
parent: {
|
||||||
description: t`Parent part category`,
|
description: t`Parent part category`,
|
||||||
required: false,
|
required: false
|
||||||
field_type: 'category'
|
|
||||||
},
|
},
|
||||||
name: {},
|
name: {},
|
||||||
description: {},
|
description: {},
|
||||||
default_location: {
|
default_location: {
|
||||||
field_type: 'location',
|
|
||||||
filters: {
|
filters: {
|
||||||
structural: false
|
structural: false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue