Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): ensure Form submit button only uses onClick handler when needed, as that was causing issues with password manager extensions #9572

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
permissions={permissions}
readOnly={false}
/>
<FormSubmit>{t('fields:saveChanges')}</FormSubmit>
<FormSubmit programmaticSubmit={true}>{t('fields:saveChanges')}</FormSubmit>
</>
) : null}
</Drawer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {
permissions={permissions}
readOnly={false}
/>
<FormSubmit>{t('fields:saveChanges')}</FormSubmit>
<FormSubmit programmaticSubmit={true}>{t('fields:saveChanges')}</FormSubmit>
</>
) : null}
</Drawer>
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/elements/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export type Props = {
newTab?: boolean
onClick?: (event: MouseEvent) => void
onMouseDown?: (event: MouseEvent) => void
/**
* Enables form submission via an onClick handler. This is only needed if
* type="submit" does not trigger form submission, e.g. if the button DOM
* element is not a direct child of the form element.
*
* @default false
*/
Comment on lines +24 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

programmaticSubmit?: boolean
round?: boolean
secondaryActions?: secondaryAction | secondaryAction[]
size?: 'large' | 'medium' | 'small'
Expand Down
24 changes: 17 additions & 7 deletions packages/ui/src/forms/Submit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ import './index.scss'
const baseClass = 'form-submit'

export const FormSubmit = forwardRef<HTMLButtonElement, Props>((props, ref) => {
const { type = 'submit', buttonId: id, children, disabled: disabledFromProps, onClick } = props
const {
type = 'submit',
buttonId: id,
children,
disabled: disabledFromProps,
onClick,
programmaticSubmit,
} = props
const processing = useFormProcessing()
const initializing = useFormInitializing()
const { disabled, submit } = useForm()

const canSave = !(disabledFromProps || initializing || processing || disabled)

const handleClick =
onClick ??
(programmaticSubmit
? () => {
void submit()
}
: undefined)

return (
<div className={baseClass}>
<Button
ref={ref}
{...props}
disabled={canSave ? undefined : true}
id={id}
onClick={
onClick ??
(() => {
void submit()
})
}
onClick={handleClick}
type={type}
>
{children}
Expand Down