Skip to content

Commit fb8f2c2

Browse files
sookmaxsnowystingeryihuiliao
authored
Prevent default when the submit button is clicked while isPending is true (#6209)
* Prevent default when the submit button is clicked while isPending is true * prevent default for all button types --------- Co-authored-by: Robert Snow <rsnow@adobe.com> Co-authored-by: Yihui Liao <44729383+yihuiliao@users.noreply.github.com>
1 parent 8d4f407 commit fb8f2c2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/@react-spectrum/button/src/Button.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,24 @@ function Button<T extends ElementType = 'button'>(props: SpectrumButtonProps<T>,
114114
if (isAppleDevice() && (!hasAriaLabel || isFirefox())) {
115115
ariaLive = 'off';
116116
}
117+
118+
let isPendingProps = isPending ? {
119+
onClick: (e) => {
120+
if (e.currentTarget instanceof HTMLButtonElement) {
121+
e.preventDefault();
122+
}
123+
}
124+
} : {
125+
// no-op.
126+
// Not sure why, but TypeScript wouldn't allow to have an empty object `{}`.
127+
onClick: () => {}
128+
};
129+
117130
return (
118131
<FocusRing focusRingClass={classNames(styles, 'focus-ring')} autoFocus={autoFocus}>
119132
<Element
120133
{...styleProps}
121-
{...mergeProps(buttonProps, hoverProps, focusProps)}
134+
{...mergeProps(buttonProps, hoverProps, focusProps, isPendingProps)}
122135
id={buttonId}
123136
ref={domRef}
124137
data-variant={variant}

packages/@react-spectrum/button/stories/Button.stories.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Bell from '@spectrum-icons/workflow/Bell';
1616
import {Button} from '../';
1717
import {ComponentMeta, ComponentStoryObj} from '@storybook/react';
1818
import {Flex} from '@react-spectrum/layout';
19+
import {Form} from '@react-spectrum/form';
1920
import React, {ElementType, useState} from 'react';
2021
import {SpectrumButtonProps} from '@react-types/button';
2122
import {Text} from '@react-spectrum/text';
@@ -345,6 +346,14 @@ function Pending(props) {
345346
</Button>
346347
</PendingButtonContainerComponent>
347348
</Flex>
349+
350+
<Flex wrap="wrap" alignItems={'center'}>
351+
<PendingButtonContainerComponent {...props}>
352+
<PendingButtonFormComponent {...props} type="submit">
353+
Form submit
354+
</PendingButtonFormComponent>
355+
</PendingButtonContainerComponent>
356+
</Flex>
348357
</div>
349358
);
350359
}
@@ -390,3 +399,28 @@ function PendingButtonOnClickComponent(props) {
390399
</Button>
391400
);
392401
}
402+
403+
function PendingButtonFormComponent(props) {
404+
let [isPending, setPending] = useState(false);
405+
406+
let onSubmit = (e) => {
407+
console.log('onSubmit called.');
408+
e.preventDefault();
409+
if (!isPending) {
410+
setPending(true);
411+
setTimeout(() => {
412+
setPending(false);
413+
}, timerValue);
414+
}
415+
};
416+
417+
return (
418+
<Form onSubmit={onSubmit}>
419+
<Button
420+
{...props}
421+
isPending={isPending}>
422+
{props.children}
423+
</Button>
424+
</Form>
425+
);
426+
}

0 commit comments

Comments
 (0)