forked from carbon-design-system/carbon-components-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InlineLoadingUx.test.svelte
52 lines (43 loc) · 1.11 KB
/
InlineLoadingUx.test.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<script lang="ts">
import { Button, ButtonSet, InlineLoading } from "../types";
import { onDestroy } from "svelte";
type State = "dormant" | "active" | "finished" | "inactive";
const descriptionMap = {
active: "Submitting...",
finished: "Success",
inactive: "Cancelling...",
};
const stateMap = {
active: "finished",
inactive: "dormant",
finished: "dormant",
};
let timeout = undefined;
let state: State = "dormant";
function reset(incomingState?: State) {
if (typeof timeout === "number") {
clearTimeout(timeout);
}
if (incomingState) {
timeout = setTimeout(() => {
state = incomingState;
}, 2000);
}
}
onDestroy(reset);
$: reset(stateMap[state]);
</script>
<ButtonSet>
<Button
kind="ghost"
disabled="{state === 'dormant' || state === 'finished'}"
on:click="{() => (state = 'inactive')}"
>
Cancel
</Button>
{#if state !== "dormant"}
<InlineLoading status="{state}" description="{descriptionMap[state]}" />
{:else}
<Button on:click="{() => (state = 'active')}">Submit</Button>
{/if}
</ButtonSet>