2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-15 03:25:42 +00:00

Add callback for value changes

This commit is contained in:
Oliver Walters
2023-07-27 15:42:32 +10:00
parent fd8621fae4
commit be57bee1a8

View File

@ -43,10 +43,12 @@ export type ApiFormFieldType = {
*/ */
function ApiFormField({ function ApiFormField({
field, field,
definitions definitions,
onValueChange
}: { }: {
field: ApiFormFieldType; field: ApiFormFieldType;
definitions: ApiFormFieldType[]; definitions: ApiFormFieldType[];
onValueChange: (fieldName: string, value: any) => void;
}) { }) {
// Extract field definition from provided data // Extract field definition from provided data
// Where user has provided specific data, override the API definition // Where user has provided specific data, override the API definition
@ -66,17 +68,39 @@ function ApiFormField({
return def; return def;
}, [field, definitions]); }, [field, definitions]);
// Callback helper when form value changes
function onChange(value: any) {
onValueChange(definition.name, value);
}
switch (definition.fieldType) { switch (definition.fieldType) {
case 'url': case 'url':
case 'string': case 'string':
return <TextInput {...definition} />; return (
<TextInput
{...definition}
onChange={(event) => onChange(event.currentTarget.value)}
/>
);
case 'boolean': case 'boolean':
return <Checkbox radius="sm" {...definition} />; return (
<Checkbox
radius="sm"
{...definition}
onChange={(event) => onChange(event.currentTarget.checked)}
/>
);
case 'integer': case 'integer':
case 'decimal': case 'decimal':
case 'float': case 'float':
case 'number': case 'number':
return <NumberInput radius="sm" {...definition} />; return (
<NumberInput
radius="sm"
{...definition}
onChange={(value: number) => onChange(value)}
/>
);
default: default:
return ( return (
<Alert color="red" title="Error"> <Alert color="red" title="Error">
@ -251,6 +275,7 @@ export function ApiForm({
key={field.name} key={field.name}
field={field} field={field}
definitions={fieldDefinitions} definitions={fieldDefinitions}
onValueChange={(fieldName, value) => {}}
/> />
))} ))}
</Stack> </Stack>