This commit is contained in:
Miklós Márton 2026-06-30 00:40:12 +10:00 committed by GitHub
commit 3f9ea83144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 72 additions and 10 deletions

View File

@ -9,13 +9,15 @@ export function StandaloneField({
fieldName = 'field',
defaultValue,
hideLabels,
error
error,
selectAndFocus = false
}: Readonly<{
fieldDefinition: ApiFormFieldType;
fieldName?: string;
defaultValue?: any;
hideLabels?: boolean;
error?: string;
selectAndFocus?: boolean;
}>) {
// Field must have a defined name
const name = useMemo(() => fieldName ?? 'field', [fieldName]);
@ -49,6 +51,7 @@ export function StandaloneField({
control={form.control}
hideLabels={hideLabels}
setFields={undefined}
selectAndFocus={selectAndFocus}
/>
</FormProvider>
);

View File

@ -31,7 +31,8 @@ export function ApiFormField({
navigate,
url,
setFields,
onKeyDown
onKeyDown,
selectAndFocus = false
}: Readonly<{
fieldName: string;
definition: ApiFormFieldType;
@ -41,6 +42,7 @@ export function ApiFormField({
url?: string;
setFields?: React.Dispatch<React.SetStateAction<ApiFormFieldSet>>;
onKeyDown?: (value: any) => void;
selectAndFocus?: boolean;
}>) {
const fieldId = useId();
const controller = useController({
@ -196,6 +198,7 @@ export function ApiFormField({
onChange={(value: any) => {
onChange(value);
}}
selectAndFocus={selectAndFocus}
/>
);
case 'choice':

View File

@ -1,5 +1,5 @@
import { NumberInput } from '@mantine/core';
import { useId, useMemo } from 'react';
import { useEffect, useId, useMemo, useRef } from 'react';
import type { FieldValues, UseControllerReturn } from 'react-hook-form';
import AutoFillRightSection, { AutoFillWarning } from './AutoFillRightSection';
@ -14,7 +14,8 @@ export default function NumberField({
placeholderAutofill,
placeholderWarningCompare,
placeholderWarning,
onChange
onChange,
selectAndFocus = false
}: Readonly<{
controller: UseControllerReturn<FieldValues, any>;
definition: any;
@ -23,8 +24,11 @@ export default function NumberField({
placeholderWarningCompare?: number | string;
placeholderWarning?: string;
onChange: (value: any) => void;
selectAndFocus?: boolean;
}>) {
const fieldId = useId();
const inputRef = useRef<HTMLInputElement | null>(null);
const {
field,
fieldState: { error }
@ -60,6 +64,18 @@ export default function NumberField({
return val;
}, [definition.field_type, value]);
useEffect(() => {
if (!selectAndFocus) return;
if (!inputRef.current) return;
inputRef.current.focus();
requestAnimationFrame(() => {
const el = inputRef.current;
if (!el) return;
el.setSelectionRange(0, el.value.length);
});
}, [selectAndFocus]);
const rightSection = useMemo(() => {
if (
definition.placeholder &&
@ -101,7 +117,10 @@ export default function NumberField({
<NumberInput
{...definition}
radius={'sm'}
ref={field.ref}
ref={(node) => {
inputRef.current = node;
field.ref(node);
}}
id={fieldId}
aria-label={`number-field-${field.name}`}
error={definition.error ?? error?.message}

View File

@ -587,6 +587,7 @@ function StockOperationsRow({
transferMerge?: boolean;
returnStock?: boolean;
record?: any;
focusAndSelect?: boolean;
}) {
const form = useFormContext();
@ -691,6 +692,7 @@ function StockOperationsRow({
<Table.Td>
<StandaloneField
fieldName='quantity'
selectAndFocus={true}
fieldDefinition={{
field_type: 'number',
value: quantity,
@ -1302,7 +1304,8 @@ function useStockOperationModal({
title,
preFormContent,
successMessage,
modalFunc = useCreateApiFormModal
modalFunc = useCreateApiFormModal,
onOpen
}: {
items?: object;
pk?: number;
@ -1315,6 +1318,7 @@ function useStockOperationModal({
preFormContent?: JSX.Element;
successMessage?: string;
modalFunc?: apiModalFunc;
onOpen?: () => void;
}) {
const baseParams: any = {
part_detail: true,
@ -1371,7 +1375,10 @@ function useStockOperationModal({
successMessage: successMessage,
onFormSuccess: () => refresh(),
onClose: () => setOpened(false),
onOpen: () => setOpened(true)
onOpen: () => {
setOpened(true);
onOpen?.();
}
});
}
@ -1386,7 +1393,17 @@ export function useAddStockItem(props: StockOperationProps) {
<Alert color='blue'>
{t`Increase the quantity of the selected stock items by a given amount.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'#number-field-quantity, input[aria-label="number-field-quantity"]'
);
input?.focus();
input?.select();
}, 0);
}
});
}
@ -1401,7 +1418,17 @@ export function useRemoveStockItem(props: StockOperationProps) {
<Alert color='blue'>
{t`Decrease the quantity of the selected stock items by a given amount.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'input[aria-label="number-field-quantity"]'
);
input?.focus();
input?.select();
}, 0);
}
});
}
@ -1457,7 +1484,17 @@ export function useCountStockItem(props: StockOperationProps) {
<Alert color='blue'>
{t`Count the selected stock items, and adjust the quantity accordingly.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'input[aria-label="number-field-quantity"]'
);
input?.focus();
input?.select();
}, 0);
}
});
}