From 23de1e038df6c8913bdab9464ece1a45e791e061 Mon Sep 17 00:00:00 2001 From: Tim Hadwen Date: Fri, 27 Sep 2024 08:29:13 +1000 Subject: [PATCH] [PUI] form enter key to submit (#8129) * Potential fix for #7014. Might be wrong way to approach * Cleanup null check on onKeyDown callback * revert pre-commit change --------- Co-authored-by: timmyhadwen Co-authored-by: Matthias Mair --- src/frontend/src/components/forms/ApiForm.tsx | 9 +++++++++ .../src/components/forms/fields/ApiFormField.tsx | 9 ++++++++- src/frontend/src/components/forms/fields/TextField.tsx | 5 ++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/frontend/src/components/forms/ApiForm.tsx b/src/frontend/src/components/forms/ApiForm.tsx index 9f1fe377f5..07ac3edfe2 100644 --- a/src/frontend/src/components/forms/ApiForm.tsx +++ b/src/frontend/src/components/forms/ApiForm.tsx @@ -602,6 +602,15 @@ export function ApiForm({ control={form.control} url={url} setFields={setFields} + onKeyDown={(value) => { + if ( + value == 'Enter' && + !isLoading && + (!props.fetchInitialData || isDirty) + ) { + form.handleSubmit(submitForm, onFormError)(); + } + }} /> ); })} diff --git a/src/frontend/src/components/forms/fields/ApiFormField.tsx b/src/frontend/src/components/forms/fields/ApiFormField.tsx index 2567b7055b..7cae0ac09f 100644 --- a/src/frontend/src/components/forms/fields/ApiFormField.tsx +++ b/src/frontend/src/components/forms/fields/ApiFormField.tsx @@ -48,6 +48,7 @@ export type ApiFormAdjustFilterType = { * @param onValueChange : Callback function to call when the field value changes * @param adjustFilters : Callback function to adjust the filters for a related field before a query is made * @param adjustValue : Callback function to adjust the value of the field before it is sent to the API + * @param onKeyDown : Callback function to get which key was pressed in the form to handle submission on enter */ export type ApiFormFieldType = { label?: string; @@ -104,7 +105,8 @@ export function ApiFormField({ control, hideLabels, url, - setFields + setFields, + onKeyDown }: Readonly<{ fieldName: string; definition: ApiFormFieldType; @@ -112,6 +114,7 @@ export function ApiFormField({ hideLabels?: boolean; url?: string; setFields?: React.Dispatch>; + onKeyDown?: (value: any) => void; }>) { const fieldId = useId(); const controller = useController({ @@ -223,6 +226,9 @@ export function ApiFormField({ controller={controller} fieldName={fieldName} onChange={onChange} + onKeyDown={(value) => { + onKeyDown?.(value); + }} /> ); case 'icon': @@ -332,6 +338,7 @@ export function ApiFormField({ fieldDefinition, numericalValue, onChange, + onKeyDown, reducedDefinition, ref, setFields, diff --git a/src/frontend/src/components/forms/fields/TextField.tsx b/src/frontend/src/components/forms/fields/TextField.tsx index b12cec7178..5e22e2290e 100644 --- a/src/frontend/src/components/forms/fields/TextField.tsx +++ b/src/frontend/src/components/forms/fields/TextField.tsx @@ -13,12 +13,14 @@ export default function TextField({ controller, fieldName, definition, - onChange + onChange, + onKeyDown }: Readonly<{ controller: UseControllerReturn; definition: any; fieldName: string; onChange: (value: any) => void; + onKeyDown: (value: any) => void; }>) { const fieldId = useId(); const { @@ -60,6 +62,7 @@ export default function TextField({ onBlur={(event) => { onChange(event.currentTarget.value); }} + onKeyDown={(event) => onKeyDown(event.code)} rightSection={ value && !definition.required ? ( onTextChange('')} />