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

Prevent default when the submit button is clicked while isPending is true #6209

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/@react-spectrum/button/src/Button.tsx
Expand Up @@ -114,11 +114,24 @@ function Button<T extends ElementType = 'button'>(props: SpectrumButtonProps<T>,
if (isAppleDevice() && (!hasAriaLabel || isFirefox())) {
ariaLive = 'off';
}

let isPendingProps = isPending ? {
onClick: (e) => {
if (e.currentTarget instanceof HTMLButtonElement && e.currentTarget.type === 'submit') {
snowystinger marked this conversation as resolved.
Show resolved Hide resolved
e.preventDefault();
}
}
} : {
// no-op.
// Not sure why, but TypeScript wouldn't allow to have an empty object `{}`.
onClick: () => {}
};

return (
<FocusRing focusRingClass={classNames(styles, 'focus-ring')} autoFocus={autoFocus}>
<Element
{...styleProps}
{...mergeProps(buttonProps, hoverProps, focusProps)}
{...mergeProps(buttonProps, hoverProps, focusProps, isPendingProps)}
id={buttonId}
ref={domRef}
data-variant={variant}
Expand Down
34 changes: 34 additions & 0 deletions packages/@react-spectrum/button/stories/Button.stories.tsx
Expand Up @@ -16,6 +16,7 @@ import Bell from '@spectrum-icons/workflow/Bell';
import {Button} from '../';
import {ComponentMeta, ComponentStoryObj} from '@storybook/react';
import {Flex} from '@react-spectrum/layout';
import {Form} from '@react-spectrum/form';
import React, {ElementType, useState} from 'react';
import {SpectrumButtonProps} from '@react-types/button';
import {Text} from '@react-spectrum/text';
Expand Down Expand Up @@ -345,6 +346,14 @@ function Pending(props) {
</Button>
</PendingButtonContainerComponent>
</Flex>

<Flex wrap="wrap" alignItems={'center'}>
<PendingButtonContainerComponent {...props}>
<PendingButtonFormComponent {...props} type="submit">
Form submit
</PendingButtonFormComponent>
</PendingButtonContainerComponent>
</Flex>
</div>
);
}
Expand Down Expand Up @@ -390,3 +399,28 @@ function PendingButtonOnClickComponent(props) {
</Button>
);
}

function PendingButtonFormComponent(props) {
let [isPending, setPending] = useState(false);

let onSubmit = (e) => {
console.log('onSubmit called.');
e.preventDefault();
if (!isPending) {
setPending(true);
setTimeout(() => {
setPending(false);
}, timerValue);
}
};

return (
<Form onSubmit={onSubmit}>
<Button
{...props}
isPending={isPending}>
{props.children}
</Button>
</Form>
);
}