disable button when wating for response
This commit is contained in:
parent
62713c7e71
commit
23bb82f00c
@ -39,6 +39,33 @@ const Stack = styled.div`
|
|||||||
background-color: white;
|
background-color: white;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
export const WithDelay = (): VNode => (
|
||||||
|
<Stack>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="contained"
|
||||||
|
onClick={() =>
|
||||||
|
new Promise((resolve) => {
|
||||||
|
setTimeout(resolve, 2000);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Returns after 2 seconds
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="contained"
|
||||||
|
onClick={() =>
|
||||||
|
new Promise((_, reject) => {
|
||||||
|
setTimeout(reject, 2000);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Fails after 2 seconds
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
|
||||||
export const BasicExample = (): VNode => (
|
export const BasicExample = (): VNode => (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<Stack>
|
<Stack>
|
||||||
|
@ -19,6 +19,7 @@ import { css } from "@linaria/core";
|
|||||||
import { theme, Colors, rippleEnabled, rippleEnabledOutlined } from "./style";
|
import { theme, Colors, rippleEnabled, rippleEnabledOutlined } from "./style";
|
||||||
// eslint-disable-next-line import/extensions
|
// eslint-disable-next-line import/extensions
|
||||||
import { alpha } from "./colors/manipulation";
|
import { alpha } from "./colors/manipulation";
|
||||||
|
import { useState } from "preact/hooks";
|
||||||
|
|
||||||
export const buttonBaseStyle = css`
|
export const buttonBaseStyle = css`
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
@ -219,7 +220,7 @@ export function Button({
|
|||||||
size = "medium",
|
size = "medium",
|
||||||
style: parentStyle,
|
style: parentStyle,
|
||||||
color = "primary",
|
color = "primary",
|
||||||
onClick,
|
onClick: doClick,
|
||||||
}: Props): VNode {
|
}: Props): VNode {
|
||||||
const style = css`
|
const style = css`
|
||||||
user-select: none;
|
user-select: none;
|
||||||
@ -275,9 +276,21 @@ export function Button({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
const [running, setRunning] = useState(false);
|
||||||
|
|
||||||
|
async function onClick(): Promise<void> {
|
||||||
|
if (!doClick || disabled || running) return;
|
||||||
|
setRunning(true);
|
||||||
|
try {
|
||||||
|
await doClick();
|
||||||
|
} finally {
|
||||||
|
setRunning(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ButtonBase
|
<ButtonBase
|
||||||
disabled={disabled}
|
disabled={disabled || running}
|
||||||
class={[
|
class={[
|
||||||
theme.typography.button,
|
theme.typography.button,
|
||||||
theme.shape.roundBorder,
|
theme.shape.roundBorder,
|
||||||
|
Loading…
Reference in New Issue
Block a user