2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

[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 <tim@micromelon.com.au>
Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Tim Hadwen 2024-09-27 08:29:13 +10:00 committed by GitHub
parent 4f06918c36
commit 23de1e038d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 2 deletions

View File

@ -602,6 +602,15 @@ export function ApiForm({
control={form.control} control={form.control}
url={url} url={url}
setFields={setFields} setFields={setFields}
onKeyDown={(value) => {
if (
value == 'Enter' &&
!isLoading &&
(!props.fetchInitialData || isDirty)
) {
form.handleSubmit(submitForm, onFormError)();
}
}}
/> />
); );
})} })}

View File

@ -48,6 +48,7 @@ export type ApiFormAdjustFilterType = {
* @param onValueChange : Callback function to call when the field value changes * @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 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 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 = { export type ApiFormFieldType = {
label?: string; label?: string;
@ -104,7 +105,8 @@ export function ApiFormField({
control, control,
hideLabels, hideLabels,
url, url,
setFields setFields,
onKeyDown
}: Readonly<{ }: Readonly<{
fieldName: string; fieldName: string;
definition: ApiFormFieldType; definition: ApiFormFieldType;
@ -112,6 +114,7 @@ export function ApiFormField({
hideLabels?: boolean; hideLabels?: boolean;
url?: string; url?: string;
setFields?: React.Dispatch<React.SetStateAction<ApiFormFieldSet>>; setFields?: React.Dispatch<React.SetStateAction<ApiFormFieldSet>>;
onKeyDown?: (value: any) => void;
}>) { }>) {
const fieldId = useId(); const fieldId = useId();
const controller = useController({ const controller = useController({
@ -223,6 +226,9 @@ export function ApiFormField({
controller={controller} controller={controller}
fieldName={fieldName} fieldName={fieldName}
onChange={onChange} onChange={onChange}
onKeyDown={(value) => {
onKeyDown?.(value);
}}
/> />
); );
case 'icon': case 'icon':
@ -332,6 +338,7 @@ export function ApiFormField({
fieldDefinition, fieldDefinition,
numericalValue, numericalValue,
onChange, onChange,
onKeyDown,
reducedDefinition, reducedDefinition,
ref, ref,
setFields, setFields,

View File

@ -13,12 +13,14 @@ export default function TextField({
controller, controller,
fieldName, fieldName,
definition, definition,
onChange onChange,
onKeyDown
}: Readonly<{ }: Readonly<{
controller: UseControllerReturn<FieldValues, any>; controller: UseControllerReturn<FieldValues, any>;
definition: any; definition: any;
fieldName: string; fieldName: string;
onChange: (value: any) => void; onChange: (value: any) => void;
onKeyDown: (value: any) => void;
}>) { }>) {
const fieldId = useId(); const fieldId = useId();
const { const {
@ -60,6 +62,7 @@ export default function TextField({
onBlur={(event) => { onBlur={(event) => {
onChange(event.currentTarget.value); onChange(event.currentTarget.value);
}} }}
onKeyDown={(event) => onKeyDown(event.code)}
rightSection={ rightSection={
value && !definition.required ? ( value && !definition.required ? (
<IconX size="1rem" color="red" onClick={() => onTextChange('')} /> <IconX size="1rem" color="red" onClick={() => onTextChange('')} />