fix: add preventDefaultSubmit prop to BaseForm to prevent unwanted page refresh on Enter key

This commit is contained in:
lyzno1 2025-09-18 12:48:26 +08:00
parent 5a6cb0d887
commit 8141f53af5
2 changed files with 14 additions and 0 deletions

View File

@ -32,6 +32,8 @@ export type BaseFormProps = {
ref?: FormRef
disabled?: boolean
formFromProps?: AnyFormApi
onSubmit?: (e: React.FormEvent<HTMLFormElement>) => void
preventDefaultSubmit?: boolean
} & Pick<BaseFieldProps, 'fieldClassName' | 'labelClassName' | 'inputContainerClassName' | 'inputClassName'>
const BaseForm = ({
@ -45,6 +47,8 @@ const BaseForm = ({
ref,
disabled,
formFromProps,
onSubmit,
preventDefaultSubmit = false,
}: BaseFormProps) => {
const initialDefaultValues = useMemo(() => {
if (defaultValues)
@ -114,9 +118,18 @@ const BaseForm = ({
if (!formSchemas?.length)
return null
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
if (preventDefaultSubmit) {
e.preventDefault()
e.stopPropagation()
}
onSubmit?.(e)
}
return (
<form
className={cn(formClassName)}
onSubmit={handleSubmit}
>
{formSchemas.map(renderFieldWrapper)}
</form>

View File

@ -218,6 +218,7 @@ export const CommonCreateModal = ({ onClose, createType }: Props) => {
formSchemas={credentialsSchema}
ref={credentialsFormRef}
labelClassName='system-sm-medium mb-2 block text-text-primary'
preventDefaultSubmit={true}
/>
</div>
)}