diff --git a/console/packages/components/src/components/switch/Switch.vue b/console/packages/components/src/components/switch/Switch.vue index 01fced8e91..b99f683357 100644 --- a/console/packages/components/src/components/switch/Switch.vue +++ b/console/packages/components/src/components/switch/Switch.vue @@ -3,10 +3,12 @@ const props = withDefaults( defineProps<{ modelValue?: boolean; disabled?: boolean; + loading?: boolean; }>(), { modelValue: false, disabled: false, + loading: false, } ); @@ -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); @@ -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" > { aria-hidden="true" class="switch-indicator" > - + + + + + diff --git a/console/packages/components/src/components/switch/__tests__/Switch.spec.ts b/console/packages/components/src/components/switch/__tests__/Switch.spec.ts index b777244986..76723a7bbb 100644 --- a/console/packages/components/src/components/switch/__tests__/Switch.spec.ts +++ b/console/packages/components/src/components/switch/__tests__/Switch.spec.ts @@ -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(); + }); }); diff --git a/console/src/modules/system/plugins/components/entity-fields/SwitchField.vue b/console/src/modules/system/plugins/components/entity-fields/SwitchField.vue index 73f676377e..25c848ab36 100644 --- a/console/src/modules/system/plugins/components/entity-fields/SwitchField.vue +++ b/console/src/modules/system/plugins/components/entity-fields/SwitchField.vue @@ -21,7 +21,7 @@ const { changingStatus, changeStatus } = usePluginLifeCycle(plugin);