mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 21:25:42 +00:00 
			
		
		
		
	Very basic form implementation
This commit is contained in:
		
							
								
								
									
										56
									
								
								src/frontend/src/components/forms/ApiForm.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/frontend/src/components/forms/ApiForm.tsx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | import { Modal } from '@mantine/core'; | ||||||
|  | import { useForm } from '@mantine/form'; | ||||||
|  |  | ||||||
|  | /* Definition of the ApiForm field component. | ||||||
|  |  * - The 'name' attribute *must* be provided | ||||||
|  |  * - All other attributes are optional, and may be provided by the API | ||||||
|  |  * - However, they can be overridden by the user | ||||||
|  |  */ | ||||||
|  | export type ApiFormField = { | ||||||
|  |   name: string; | ||||||
|  |   label?: string; | ||||||
|  |   value?: any; | ||||||
|  |   type?: string; | ||||||
|  |   required?: boolean; | ||||||
|  |   placeholder?: string; | ||||||
|  |   help_text?: string; | ||||||
|  |   icon?: string; | ||||||
|  |   errors?: string[]; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * An ApiForm component is a modal form which is rendered dynamically, | ||||||
|  |  * based on an API endpoint. | ||||||
|  |  * @param url : The API endpoint to fetch the form from. | ||||||
|  |  * @param fields : The fields to render in the form. | ||||||
|  |  * @param opened : Whether the form is opened or not. | ||||||
|  |  * @param onClose : A callback function to call when the form is closed. | ||||||
|  |  * @param onFormSuccess : A callback function to call when the form is submitted successfully. | ||||||
|  |  * @param onFormError : A callback function to call when the form is submitted with errors. | ||||||
|  |  */ | ||||||
|  | export function ApiForm({ | ||||||
|  |   url, | ||||||
|  |   title, | ||||||
|  |   fields, | ||||||
|  |   opened, | ||||||
|  |   onClose, | ||||||
|  |   onFormSuccess, | ||||||
|  |   onFormError | ||||||
|  | }: { | ||||||
|  |   url: string; | ||||||
|  |   title: string; | ||||||
|  |   fields: ApiFormField[]; | ||||||
|  |   opened: boolean; | ||||||
|  |   onClose?: () => void; | ||||||
|  |   onFormSuccess?: () => void; | ||||||
|  |   onFormError?: () => void; | ||||||
|  | }) { | ||||||
|  |   // Form state | ||||||
|  |   const form = useForm({}); | ||||||
|  |  | ||||||
|  |   return ( | ||||||
|  |     <Modal opened={opened} onClose={onClose} title={title}> | ||||||
|  |       Form data goes here | ||||||
|  |     </Modal> | ||||||
|  |   ); | ||||||
|  | } | ||||||
| @@ -1,10 +1,15 @@ | |||||||
| import { Trans } from '@lingui/macro'; | import { Trans } from '@lingui/macro'; | ||||||
| import { Group } from '@mantine/core'; | import { Group } from '@mantine/core'; | ||||||
|  | import { Button } from '@mantine/core'; | ||||||
|  | import { useState } from 'react'; | ||||||
|  |  | ||||||
|  | import { ApiForm } from '../../components/forms/ApiForm'; | ||||||
| import { PlaceholderPill } from '../../components/items/Placeholder'; | import { PlaceholderPill } from '../../components/items/Placeholder'; | ||||||
| import { StylishText } from '../../components/items/StylishText'; | import { StylishText } from '../../components/items/StylishText'; | ||||||
|  |  | ||||||
| export default function Home() { | export default function Home() { | ||||||
|  |   const [formOpened, setFormOpened] = useState(false); | ||||||
|  |  | ||||||
|   return ( |   return ( | ||||||
|     <> |     <> | ||||||
|       <Group> |       <Group> | ||||||
| @@ -13,6 +18,19 @@ export default function Home() { | |||||||
|         </StylishText> |         </StylishText> | ||||||
|         <PlaceholderPill /> |         <PlaceholderPill /> | ||||||
|       </Group> |       </Group> | ||||||
|  |       <ApiForm | ||||||
|  |         url="/part/1/" | ||||||
|  |         title="Edit Part" | ||||||
|  |         opened={formOpened} | ||||||
|  |         onClose={() => setFormOpened(false)} | ||||||
|  |       /> | ||||||
|  |       <Button | ||||||
|  |         onClick={() => setFormOpened(true)} | ||||||
|  |         variant="outline" | ||||||
|  |         color="blue" | ||||||
|  |       > | ||||||
|  |         Open Form | ||||||
|  |       </Button> | ||||||
|     </> |     </> | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user