Skip to content

Commit

Permalink
feat: migrate base-switch to use defineComponent sintax (#1415)
Browse files Browse the repository at this point in the history
Co-authored-by: acondal <[email protected]>
  • Loading branch information
albertjcuac and annacv authored Feb 20, 2024
1 parent 3fe4ca4 commit 0592ec9
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions packages/x-components/src/components/base-switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
</template>

<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { defineComponent, ref } from 'vue';
import { VueCSSClasses } from '../utils/types';
/**
Expand All @@ -22,42 +21,55 @@
*
* @public
*/
@Component({
model: {
event: 'change'
}
})
export default class BaseSwitch extends Vue {
export default defineComponent({
name: 'BaseSwitch',
/**
* The selected value of the switch.
*
* @public
*/
@Prop({ required: true })
public value!: boolean;
props: {
value: {
type: Boolean,
required: true
}
},
emits: ['change', 'input'],
setup(props, { emit }) {
/**
* Dynamic CSS classes to add to the switch component
* depending on its internal state.
*
* @returns A boolean dictionary with dynamic CSS classes.
* @internal
*/
const cssClasses = ref<VueCSSClasses>({
'x-switch--is-selected x-selected': props.value
});
/**
* Emits a change and input event with the desired value of the switch.
*
* @internal
*/
const toggle = (): void => {
const newValue = !props.value;
cssClasses.value = {
'x-switch--is-selected': newValue,
'x-selected': newValue
};
emit('input', newValue);
emit('change', newValue);
};
/**
* Dynamic CSS classes to add to the switch component
* depending on its internal state.
*
* @returns A boolean dictionary with dynamic CSS classes.
* @internal
*/
protected get cssClasses(): VueCSSClasses {
return {
'x-switch--is-selected x-selected': this.value
cssClasses,
toggle
};
}
/**
* Emits a change event with the desired value of the switch.
*
* @internal
*/
protected toggle(): void {
this.$emit('change', !this.value);
}
}
});
</script>

<style lang="scss" scoped>
Expand Down

0 comments on commit 0592ec9

Please sign in to comment.