mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Handle text fields
This commit is contained in:
		| @@ -1,11 +1,11 @@ | |||||||
| import { t } from '@lingui/macro'; | import { t } from '@lingui/macro'; | ||||||
| import { Alert, Divider, Modal } from '@mantine/core'; | import { Alert, Divider, Modal, ScrollArea, TextInput } from '@mantine/core'; | ||||||
| import { Button, Center, Group, Loader, Stack, Text } from '@mantine/core'; | import { Button, Center, Group, Loader, Stack, Text } from '@mantine/core'; | ||||||
| import { useForm } from '@mantine/form'; | import { useForm } from '@mantine/form'; | ||||||
| import { IconAlertCircle } from '@tabler/icons-react'; | import { IconAlertCircle } from '@tabler/icons-react'; | ||||||
| import { useQuery } from '@tanstack/react-query'; | import { useQuery } from '@tanstack/react-query'; | ||||||
| import { AxiosResponse } from 'axios'; | import { AxiosResponse } from 'axios'; | ||||||
| import { useEffect } from 'react'; | import { ReactNode, useEffect } from 'react'; | ||||||
| import { useState } from 'react'; | import { useState } from 'react'; | ||||||
| import { useMemo } from 'react'; | import { useMemo } from 'react'; | ||||||
|  |  | ||||||
| @@ -23,10 +23,12 @@ export type ApiFormFieldType = { | |||||||
|   type?: string; |   type?: string; | ||||||
|   required?: boolean; |   required?: boolean; | ||||||
|   hidden?: boolean; |   hidden?: boolean; | ||||||
|  |   disabled?: boolean; | ||||||
|   placeholder?: string; |   placeholder?: string; | ||||||
|   help_text?: string; |   description?: string; | ||||||
|   icon?: string; |   icon?: ReactNode; | ||||||
|   errors?: string[]; |   errors?: string[]; | ||||||
|  |   error?: any; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -44,13 +46,23 @@ function ApiFormField({ | |||||||
|   const definition: ApiFormFieldType = useMemo(() => { |   const definition: ApiFormFieldType = useMemo(() => { | ||||||
|     let def = definitions.find((def) => def.name == field.name) || field; |     let def = definitions.find((def) => def.name == field.name) || field; | ||||||
|  |  | ||||||
|     return { |     def = { | ||||||
|       ...def, |       ...def, | ||||||
|       ...field |       ...field | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|  |     // Format the errors | ||||||
|  |     if (def.errors?.length == 1) { | ||||||
|  |       def.error = def.errors[0]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return def; | ||||||
|   }, [field, definitions]); |   }, [field, definitions]); | ||||||
|  |  | ||||||
|   switch (definition.type) { |   switch (definition.type) { | ||||||
|  |     case 'url': | ||||||
|  |     case 'string': | ||||||
|  |       return <TextInput {...definition} />; | ||||||
|     default: |     default: | ||||||
|       return ( |       return ( | ||||||
|         <Alert color="red" title="Error"> |         <Alert color="red" title="Error"> | ||||||
| @@ -138,6 +150,15 @@ export function ApiForm({ | |||||||
|     ); |     ); | ||||||
|   }, [definitionQuery, error]); |   }, [definitionQuery, error]); | ||||||
|  |  | ||||||
|  |   // State variable to determine if the form can be submitted | ||||||
|  |   const [canSubmit, setCanSubmit] = useState<boolean>(false); | ||||||
|  |  | ||||||
|  |   // Update the canSubmit state variable on status change | ||||||
|  |   useEffect(() => { | ||||||
|  |     setCanSubmit(canRender && true); | ||||||
|  |     // TODO: This will be updated when we have a query manager for form submission | ||||||
|  |   }, [canRender]); | ||||||
|  |  | ||||||
|   // Construct a fully-qualified URL based on the provided details |   // Construct a fully-qualified URL based on the provided details | ||||||
|   function getUrl(): string { |   function getUrl(): string { | ||||||
|     let u = url; |     let u = url; | ||||||
| @@ -171,7 +192,7 @@ export function ApiForm({ | |||||||
|       definitions.push({ |       definitions.push({ | ||||||
|         name: fieldName, |         name: fieldName, | ||||||
|         label: field.label, |         label: field.label, | ||||||
|         help_text: field.help_text, |         description: field.help_text, | ||||||
|         value: field.value, |         value: field.value, | ||||||
|         type: field.type, |         type: field.type, | ||||||
|         required: field.required, |         required: field.required, | ||||||
| @@ -209,6 +230,7 @@ export function ApiForm({ | |||||||
|           </Alert> |           </Alert> | ||||||
|         )} |         )} | ||||||
|         {canRender && ( |         {canRender && ( | ||||||
|  |           <ScrollArea> | ||||||
|             <Stack spacing="md"> |             <Stack spacing="md"> | ||||||
|               {fields.map((field) => ( |               {fields.map((field) => ( | ||||||
|                 <ApiFormField |                 <ApiFormField | ||||||
| @@ -218,6 +240,7 @@ export function ApiForm({ | |||||||
|                 /> |                 /> | ||||||
|               ))} |               ))} | ||||||
|             </Stack> |             </Stack> | ||||||
|  |           </ScrollArea> | ||||||
|         )} |         )} | ||||||
|         <Divider /> |         <Divider /> | ||||||
|         <Group position="right"> |         <Group position="right"> | ||||||
| @@ -229,8 +252,12 @@ export function ApiForm({ | |||||||
|             variant="outline" |             variant="outline" | ||||||
|             radius="sm" |             radius="sm" | ||||||
|             color="green" |             color="green" | ||||||
|  |             disabled={!canSubmit} | ||||||
|           > |           > | ||||||
|  |             <Group position="right" spacing={5} noWrap={true}> | ||||||
|  |               <Loader size="xs" /> | ||||||
|               {submitText} |               {submitText} | ||||||
|  |             </Group> | ||||||
|           </Button> |           </Button> | ||||||
|         </Group> |         </Group> | ||||||
|       </Stack> |       </Stack> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user