Skip to content

Commit

Permalink
refactor(base-switch): migrate component to vue3
Browse files Browse the repository at this point in the history
  • Loading branch information
victorcg88 committed Aug 6, 2024
1 parent f915731 commit 0ad6743
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/_vue3-migration-test/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { default as TestTagging } from './tagging/test-tagging.vue';
export { default as TestRenderlessExtraParam } from './extra-params/test-renderless-extra-param.vue';
export { default as TestIcons } from './icons/test-icons.vue';
export { default as TestDisplayEmitter } from './test-display-emitter.vue';
export { default as TestBaseSwitch } from './test-base-switch.vue';
18 changes: 18 additions & 0 deletions packages/_vue3-migration-test/src/components/test-base-switch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<p>Using props and events:</p>
<BaseSwitch @change="value = !value" :value="value" />
{{ value }}
<br />
<p>Using the `v-model` directive:</p>
<BaseSwitch v-model="value2" />
{{ value2 }}
</template>

<script setup lang="ts">
import { ref } from 'vue';
import BaseSwitch from '../../../x-components/src/components/base-switch.vue';
const value = ref(true);
const value2 = ref(true);
</script>
8 changes: 7 additions & 1 deletion packages/_vue3-migration-test/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import {
TestRenderlessExtraParam,
TestAnimationFactory,
TestIcons,
TestDisplayEmitter
TestDisplayEmitter,
TestBaseSwitch
} from './';

const routes = [
Expand Down Expand Up @@ -302,6 +303,11 @@ const routes = [
path: '/display-emitter',
name: 'DisplayEmitter',
component: TestDisplayEmitter
},
{
path: '/base-switch',
name: 'BaseSwitch',
component: TestBaseSwitch
}
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount, Wrapper } from '@vue/test-utils';
import { mount, VueWrapper } from '@vue/test-utils';
import BaseSwitch from '../base-switch.vue';

function renderBaseSwitch({ template, value }: RenderBaseSwitchOptions): RenderBaseSwitchApi {
Expand Down Expand Up @@ -27,7 +27,7 @@ describe('testing Switch component', () => {
});

expect(wrapper.attributes('role')).toBe('switch');
expect(wrapper.attributes('aria-checked')).toBe('false');
expect(wrapper.attributes('aria-checked')).toBeFalsy();
expect(wrapper.classes('x-switch--is-selected')).toBe(false);
expect(wrapper.classes('x-selected')).toBe(false);

Expand All @@ -43,7 +43,7 @@ describe('testing Switch component', () => {
value: false
});
expect(wrapper.attributes('role')).toBe('switch');
expect(wrapper.attributes('aria-checked')).toBe('false');
expect(wrapper.attributes('aria-checked')).toBeFalsy();
expect(wrapper.classes('x-switch--is-selected')).toBe(false);
expect(wrapper.classes('x-selected')).toBe(false);

Expand All @@ -69,5 +69,5 @@ interface RenderBaseSwitchApi {
/**
* The wrapper testing component instance.
*/
wrapper: Wrapper<Vue>;
wrapper: VueWrapper;
}
21 changes: 14 additions & 7 deletions packages/x-components/src/components/base-switch.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<button
@click="toggle"
:aria-checked="value.toString()"
:aria-checked="currentValue || undefined"
:class="cssClasses"
class="x-switch"
role="switch"
Expand All @@ -11,7 +11,7 @@
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { defineComponent, ref, computed } from 'vue';
import { VueCSSClasses } from '../utils/types';
/**
Expand All @@ -32,11 +32,17 @@
props: {
value: {
type: Boolean,
required: true
default: false
},
modelValue: {
type: Boolean,
default: false
}
},
emits: ['change', 'input'],
emits: ['change', 'update:modelValue'],
setup(props, { emit }) {
const currentValue = computed(() => props.modelValue || props.value);
/**
* Dynamic CSS classes to add to the switch component
* depending on its internal state.
Expand All @@ -45,7 +51,7 @@
* @internal
*/
const cssClasses = ref<VueCSSClasses>({
'x-switch--is-selected x-selected': props.value
'x-switch--is-selected x-selected': currentValue.value
});
/**
Expand All @@ -54,17 +60,18 @@
* @internal
*/
const toggle = (): void => {
const newValue = !props.value;
const newValue = !currentValue.value;
cssClasses.value = {
'x-switch--is-selected': newValue,
'x-selected': newValue
};
emit('input', newValue);
emit('update:modelValue', newValue);
emit('change', newValue);
};
return {
currentValue,
cssClasses,
toggle
};
Expand Down

0 comments on commit 0ad6743

Please sign in to comment.