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

feat: add loading state for switch component #4688

Merged
merged 2 commits into from
Oct 8, 2023
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
31 changes: 27 additions & 4 deletions console/packages/components/src/components/switch/Switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const props = withDefaults(
defineProps<{
modelValue?: boolean;
disabled?: boolean;
loading?: boolean;
}>(),
{
modelValue: false,
disabled: false,
loading: false,
}
);

Expand All @@ -16,7 +18,7 @@ const emit = defineEmits<{
}>();

const handleChange = () => {
if (props.disabled) return;
if (props.disabled || props.loading) return;

emit("update:modelValue", !props.modelValue);
emit("change", !props.modelValue);
Expand All @@ -28,13 +30,13 @@ const handleChange = () => {
:class="{
'bg-gray-200': !modelValue,
'!bg-primary': modelValue,
'switch-disabled': disabled,
'switch-disabled': disabled || loading,
}"
aria-checked="false"
class="switch-inner"
role="switch"
type="button"
:disabled="disabled"
:disabled="disabled || loading"
@click="handleChange"
>
<span
Expand All @@ -45,7 +47,28 @@ const handleChange = () => {
aria-hidden="true"
class="switch-indicator"
>
<slot name="icon" />
<svg
v-if="loading"
class="animate-spin"
fill="none"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<circle
class="opacity-0"
cx="12"
cy="12"
r="10"
stroke="currentColor"
stroke-width="4"
></circle>
<path
class="opacity-30"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
fill="currentColor"
></path>
</svg>
<slot v-else name="icon" />
</span>
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { describe, expect, it } from "vitest";
import { VSwitch } from "../index";
import { mount } from "@vue/test-utils";
import { mount, shallowMount } from "@vue/test-utils";

describe("Switch", () => {
it("should render", () => {
expect(mount(VSwitch)).toBeDefined();
});

it("emits the correct events when clicked", () => {
const wrapper = shallowMount(VSwitch);
wrapper.find("button").trigger("click");
expect(wrapper.emitted("update:modelValue")).toBeTruthy();
expect(wrapper.emitted("change")).toBeTruthy();
});

it("does not emit events when disabled or loading", () => {
const wrapper = shallowMount(VSwitch, {
props: { disabled: true, loading: true },
});
wrapper.find("button").trigger("click");
expect(wrapper.emitted("update:modelValue")).toBeFalsy();
expect(wrapper.emitted("change")).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { changingStatus, changeStatus } = usePluginLifeCycle(plugin);
<div class="flex items-center">
<VSwitch
:model-value="plugin.spec.enabled"
:disabled="changingStatus"
:loading="changingStatus"
@click="changeStatus"
/>
</div>
Expand Down